UNPKG

radix-account-tools-js

Version:

Javascript/Typescript account creation tools for Radix DLT

151 lines (150 loc) 7.07 kB
"use strict"; 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generate12WordMnemonic = generate12WordMnemonic; exports.generate24WordMnemonic = generate24WordMnemonic; exports.generateAccountFromPrivateKeyBytes = generateAccountFromPrivateKeyBytes; exports.generateAccountsFromMnemonic = generateAccountsFromMnemonic; exports.generateKeyPair = generateKeyPair; exports.deriveAccountAddressFromPublicKey = deriveAccountAddressFromPublicKey; exports.generateEd25519PrivateKey = generateEd25519PrivateKey; exports.generateNewVirtualAccount = generateNewVirtualAccount; exports.mnemonicToSeed = mnemonicToSeed; const radix_engine_toolkit_1 = require("@radixdlt/radix-engine-toolkit"); const bip39 = __importStar(require("bip39")); const ed25519_hd_key_1 = require("ed25519-hd-key"); const KEY_TYPE = { TRANSACTION_SIGNING: 1460, AUTHENTICATION_SIGNING: 1678, MESSAGE_ENCRYPTION: 1391, }; const ENTITY_TYPE = { ACCOUNT: 525, IDENTITY: 618, }; // generates a 12 word mnemonic phrase function generate12WordMnemonic() { return bip39.generateMnemonic(); } // generates a 24 word mnemonic phrase function generate24WordMnemonic() { return bip39.generateMnemonic(256); } // generates an account from a private key expressed as an array of bytes function generateAccountFromPrivateKeyBytes(privateKeyBytes, // the array of bytes networkId // the Radix network id (stokenet = 2, mainnet = 1) ) { return __awaiter(this, void 0, void 0, function* () { const privateKey = new radix_engine_toolkit_1.PrivateKey.Ed25519(privateKeyBytes); const publicKey = new radix_engine_toolkit_1.PublicKey.Ed25519(privateKey.publicKeyHex()); const address = yield deriveAccountAddressFromPublicKey(publicKey, networkId); return { privateKey, publicKey, address, }; }); } // generates several accounts form a mnemonic phrase based on the entity indices provided function generateAccountsFromMnemonic(mnemonic, // the mnemonic phrase as a string with words separated by spaces indices, // the entity indices to create account for networkId // the Radix network id (stokenet = 2, mainnet = 1) ) { return __awaiter(this, void 0, void 0, function* () { let accounts = []; for (const index of indices) { let keyPair = generateKeyPair(mnemonic, index, networkId, ENTITY_TYPE.ACCOUNT); let address = yield deriveAccountAddressFromPublicKey(keyPair.publicKey, networkId); accounts.push({ index: index, address, privateKey: keyPair.privateKey, publicKey: keyPair.publicKey, }); } return accounts; }); } // generates a Radix KeyPair for an account based on a mnemonic phrase and entity index function generateKeyPair(mnemonic, // the mnemonic phrase as a string with words separated by spaces entityIndex, // the entity index of the account (several private/public key pairs can be derived from the same mnemonic phrase) - the same mneominc and same index will always result in the same pair networkId, // the Radix network id (stokenet = 2, mainnet = 1) entityType = ENTITY_TYPE.ACCOUNT // the code for the type of entity you want to create (account = 525, identity = 618) ) { const derivationPath = `m/44'/1022'/${networkId}'/${entityType}'/${KEY_TYPE.TRANSACTION_SIGNING}'/${entityIndex}'`; const seedHex = mnemonicToSeed(mnemonic); const derivedKeyData = deriveKeyData(derivationPath, seedHex); const privateKey = new radix_engine_toolkit_1.PrivateKey.Ed25519(derivedKeyData.key.toString("hex")); const publicKey = new radix_engine_toolkit_1.PublicKey.Ed25519(privateKey.publicKeyHex()); return { privateKey, publicKey, }; } // derives an account address from a public key function deriveAccountAddressFromPublicKey(publicKey, // the public key to derive the address for networkId // the Radix network id (stokenet = 2, mainnet = 1) ) { return __awaiter(this, void 0, void 0, function* () { return radix_engine_toolkit_1.RadixEngineToolkit.Derive.virtualAccountAddressFromPublicKey(publicKey, networkId); }); } // generates a private key from an array of bytes function generateEd25519PrivateKey(bytes // array of bytes representing the private key ) { return __awaiter(this, void 0, void 0, function* () { return new radix_engine_toolkit_1.PrivateKey.Ed25519(bytes); }); } // generates a new account from a private key function generateNewVirtualAccount(privateKey, // the private key to use to generate the account networkId) { return __awaiter(this, void 0, void 0, function* () { const publicKey = privateKey.publicKey(); const address = yield radix_engine_toolkit_1.LTSRadixEngineToolkit.Derive.virtualAccountAddress(publicKey, networkId); return { privateKey, publicKey: publicKey, address, }; }); } // generates a hex seed from a mnemonic phrase function mnemonicToSeed(mnemonic) { return bip39.mnemonicToSeedSync(mnemonic).toString("hex"); } // derives private/public key data using the provided derivation path and hex seed function deriveKeyData(derivationPath, seedHex) { return (0, ed25519_hd_key_1.derivePath)(derivationPath, seedHex); }