cngn-typescript-library
Version:
A lightweight Typescript library to give you the best experience with managing your cNGN merchant account
121 lines (120 loc) • 5.6 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CryptoWallet = void 0;
const secp256k1_1 = require("@noble/curves/secp256k1");
const keccak256_1 = __importDefault(require("keccak256"));
const bip39 = __importStar(require("bip39"));
const ethereumjs_wallet_1 = require("ethereumjs-wallet");
const types_1 = require("./types");
const tronweb_1 = require("tronweb");
const tweetnacl_1 = __importDefault(require("tweetnacl"));
const multicoin_address_validator_1 = __importDefault(require("multicoin-address-validator"));
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const ed25519 = __importStar(require("ed25519-hd-key"));
class CryptoWallet {
static generateWalletWithMnemonicDetails(network) {
const mnemonic = bip39.generateMnemonic(this.MNEMONIC_ENTROPY_BYTES);
return this.generateWalletFromMnemonic(mnemonic, network);
}
static generateWalletFromMnemonic(mnemonic, network) {
if (network === types_1.Network.xbn) {
return this.generateXbnWallet(mnemonic);
}
const privateKey = this.getPrivateKeyFromMnemonic(mnemonic, network);
const publicKey = this.getPublicKey(privateKey, network);
const address = this.getAddressFromPublicKey(publicKey, network);
if ([types_1.Network.trx, types_1.Network.eth, types_1.Network.matic].includes(network) && !multicoin_address_validator_1.default.validate(address, network)) {
return this.generateWalletWithMnemonicDetails(network);
}
return { mnemonic, privateKey, address, network };
}
static getPublicKey(privateKey, network) {
if (network === types_1.Network.xbn) {
const keyPair = tweetnacl_1.default.sign.keyPair.fromSeed(Buffer.from(privateKey, 'hex'));
return Buffer.from(keyPair.publicKey).toString('hex');
}
return secp256k1_1.secp256k1.getPublicKey(privateKey, false).slice(1).toString();
}
static getPrivateKeyFromMnemonic(mnemonic, network) {
const seed = bip39.mnemonicToSeedSync(mnemonic);
const hdWallet = ethereumjs_wallet_1.hdkey.fromMasterSeed(seed);
const derivationPath = this.getDerivationPath(network);
if (network === types_1.Network.xbn) {
return hdWallet.derivePath(derivationPath).privateExtendedKey().toString('hex').slice(0, 64);
}
return hdWallet.derivePath(derivationPath).getWallet().getPrivateKey().toString('hex');
}
static getAddressFromPublicKey(publicKey, network) {
switch (network) {
case types_1.Network.eth:
case types_1.Network.bsc:
case types_1.Network.atc:
case types_1.Network.matic:
return this.getEthereumStyleAddress(publicKey);
case types_1.Network.trx:
return tronweb_1.TronWeb.address.fromHex(this.getEthereumStyleAddress(publicKey));
default:
throw new Error(`Unsupported network: ${network}`);
}
}
static getEthereumStyleAddress(publicKey) {
const cleanPublicKey = publicKey.startsWith('04') ? publicKey.slice(2) : publicKey;
const hash = (0, keccak256_1.default)(Buffer.from(cleanPublicKey, 'hex'));
return '0x' + hash.subarray(-20).toString('hex');
}
static getDerivationPath(network) {
const path = this.DERIVATION_PATHS[network];
if (!path) {
throw new Error(`Unsupported network: ${network}`);
}
return path;
}
static generateXbnWallet(mnemonic) {
const seed = bip39.mnemonicToSeedSync(mnemonic);
const derivationPath = this.getDerivationPath(types_1.Network.xbn);
const { key } = ed25519.derivePath(derivationPath, seed.toString('hex'));
const keypair = stellar_sdk_1.Keypair.fromRawEd25519Seed(key);
return {
mnemonic,
privateKey: keypair.secret(),
address: keypair.publicKey(),
network: types_1.Network.xbn,
};
}
}
exports.CryptoWallet = CryptoWallet;
CryptoWallet.MNEMONIC_ENTROPY_BYTES = 128;
CryptoWallet.DERIVATION_PATHS = {
[types_1.Network.eth]: `m/44'/60'/0'/0/0`,
[types_1.Network.bsc]: `m/44'/60'/0'/0/0`,
[types_1.Network.atc]: `m/44'/60'/0'/0/0`,
[types_1.Network.matic]: `m/44'/60'/0'/0/0`,
[types_1.Network.trx]: `m/44'/195'/0'/0/0`,
[types_1.Network.xbn]: `m/44'/703'/0'`
};