UNPKG

@funded-labs/plug-controller

Version:

Internet Computer Plug wallet's controller

89 lines (88 loc) 4.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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAccountFromMnemonic = exports.createAccount = exports.getAccountId = void 0; const bip39 = __importStar(require("bip39")); const crypto_js_1 = __importDefault(require("crypto-js")); const errors_1 = require("../../errors"); const identity_1 = __importDefault(require("../identity/secpk256k1/identity")); const constants_1 = require("./constants"); const binary_1 = require("../crypto/binary"); const keys_1 = require("../crypto/keys"); /* Used dfinity/keysmith/account/account.go as a base for the ID generation */ const getAccountId = (principal, subaccount, cryptoAdapter = crypto_js_1.default) => { const sha = cryptoAdapter.algo.SHA224.create(); sha.update(constants_1.ACCOUNT_DOMAIN_SEPERATOR); // Internally parsed with UTF-8, like go does sha.update((0, binary_1.byteArrayToWordArray)(principal.toUint8Array())); const subBuffer = Buffer.from(constants_1.SUB_ACCOUNT_ZERO); if (subaccount) { subBuffer.writeUInt32BE(subaccount); } sha.update((0, binary_1.byteArrayToWordArray)(subBuffer)); const hash = sha.finalize(); /// While this is backed by an array of length 28, it's canonical representation /// is a hex string of length 64. The first 8 characters are the CRC-32 encoded /// hash of the following 56 characters of hex. Both, upper and lower case /// characters are valid in the input string and can even be mixed. /// [ic/rs/rosetta-api/ledger_canister/src/account_identifier.rs] const byteArray = (0, binary_1.wordArrayToByteArray)(hash, 28); const checksum = (0, binary_1.generateChecksum)(byteArray); const val = checksum + hash.toString(); return val; }; exports.getAccountId = getAccountId; const getAccountCredentials = (mnemonic, subaccount) => { const keyPair = (0, keys_1.createSecp256K1KeyPair)(mnemonic, subaccount || 0); // Identity has boths keys via getKeyPair and PID via getPrincipal const identity = identity_1.default.fromKeyPair(keyPair.publicKey.toRaw(), keyPair.secretKey); const accountId = (0, exports.getAccountId)(identity.getPrincipal()); return { mnemonic, identity, accountId, }; }; const createAccount = (entropy) => { const mnemonic = entropy ? bip39.entropyToMnemonic(entropy) : bip39.generateMnemonic(); return getAccountCredentials(mnemonic, 0); }; exports.createAccount = createAccount; const createAccountFromMnemonic = (mnemonic, accountId) => { if (!mnemonic || !bip39.validateMnemonic(mnemonic)) { throw new Error(errors_1.ERRORS.INVALID_MNEMONIC); } if (typeof accountId !== 'number' || accountId < 0) { throw new Error(errors_1.ERRORS.INVALID_ACC_ID); } return getAccountCredentials(mnemonic, accountId); }; exports.createAccountFromMnemonic = createAccountFromMnemonic;