UNPKG

@kaiachain/web3js-ext

Version:
84 lines (83 loc) 3.73 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.context_create = context_create; exports.context_privateKeyToAccount = context_privateKeyToAccount; exports.context_decrypt = context_decrypt; exports.context_decryptList = context_decryptList; const lodash_1 = __importDefault(require("lodash")); const web3_eth_accounts_1 = require("web3-eth-accounts"); const web3_utils_1 = require("web3-utils"); const index_js_1 = require("../index.js"); const sign_js_1 = require("./sign.js"); // Analogous to web3/src/accounts.ts:createWithContext function context_create(context) { return function () { const account = (0, web3_eth_accounts_1.create)(); return wrapAccount(context, account); }; } // Analogous to web3/src/accounts.ts:privateKeyToAccountWithContext function context_privateKeyToAccount(context) { return function (privateKey, address) { const account = (0, web3_eth_accounts_1.privateKeyToAccount)(privateKey); return wrapAccount(context, { ...account, address: address || account.address }); }; } // Analogous to web3/src/accounts.ts:decryptWithContext function context_decrypt(context) { return async function (keystore, password, options) { if (!lodash_1.default.isString(keystore)) { keystore = JSON.stringify(keystore); } if ((0, index_js_1.isKIP3Json)(keystore)) { const accounts = await decryptKIP3(keystore, password, options); return wrapAccount(context, accounts[0]); } else { const account = await (0, web3_eth_accounts_1.decrypt)(keystore, password, options?.nonStrict ?? true); return wrapAccount(context, account); } }; } function context_decryptList(context) { return async function (keystore, password, options) { if (!lodash_1.default.isString(keystore)) { keystore = JSON.stringify(keystore); } if ((0, index_js_1.isKIP3Json)(keystore)) { const accounts = await decryptKIP3(keystore, password, options); return lodash_1.default.map(accounts, (account) => wrapAccount(context, account)); } else { const account = await (0, web3_eth_accounts_1.decrypt)(keystore, password, options?.nonStrict ?? true); return [wrapAccount(context, account)]; } }; } async function decryptKIP3(keystore, password, options) { if (!lodash_1.default.isString(keystore)) { keystore = JSON.stringify(keystore); } const address = JSON.parse(keystore).address; const jsonList = (0, index_js_1.splitKeystoreKIP3)(keystore, false); const accounts = []; for (const json of jsonList) { const account = await (0, web3_eth_accounts_1.decrypt)(json, password, options?.nonStrict ?? true); account.address = (0, web3_utils_1.toChecksumAddress)(address); // the address may not coincide with private keys, get directly from the json. accounts.push(account); } return accounts; } // common components of create, privateKeyToAccount, decrypt. function wrapAccount(context, account) { const _signTransaction = (0, sign_js_1.context_signTransaction)(context); const _signTransactionAsFeePayer = (0, sign_js_1.context_signTransactionAsFeePayer)(context); return { ...account, signTransaction: (transaction) => _signTransaction(transaction, account.privateKey), signTransactionAsFeePayer: (transaction) => _signTransactionAsFeePayer(transaction, account.privateKey, account.address), }; }