UNPKG

@kaiachain/ethers-ext

Version:
62 lines (61 loc) 2.38 kB
import { decryptKeystoreJson, decryptKeystoreJsonSync, getAddress, } from "ethers"; import _ from "lodash"; import { isKIP3Json, splitKeystoreKIP3 } from "@kaiachain/js-ext-core"; function decryptKIP3(json, password, progressCallback) { const obj = JSON.parse(json); const jsonList = splitKeystoreKIP3(json); const count = jsonList.length; const accounts = _.map(jsonList, (json, index) => { const result = decryptKeystoreJsonSync(json, password); if (progressCallback) { progressCallback((index + 1) / count); } return result; }); return Promise.resolve({ address: getAddress(obj.address), // the address may not coincide with private keys, get directly from the json. privateKey: accounts[0].privateKey, privateKeyList: _.map(accounts, (account) => account.privateKey), }); } function decryptKIP3Sync(json, password) { const obj = JSON.parse(json); const jsonList = splitKeystoreKIP3(json); const accounts = _.map(jsonList, (json) => { return decryptKeystoreJsonSync(json, password); }); return { address: getAddress(obj.address), // the address may not coincide with private keys, get directly from the json. privateKey: accounts[0].privateKey, privateKeyList: _.map(accounts, (account) => account.privateKey), }; } export function decryptKeystoreList(json, password, progressCallback) { if (isKIP3Json(json)) { return decryptKIP3(json, password, progressCallback); } else { // Because ethers decryptJsonWallet is not an async function, // decryptJsonWalletList is not an async function either, hence using 'then'. return decryptKeystoreJson(json, password, progressCallback).then((account) => { return { address: account.address, privateKey: account.privateKey, privateKeyList: [account.privateKey], }; }); } } export function decryptKeystoreListSync(json, password) { if (isKIP3Json(json)) { return decryptKIP3Sync(json, password); } else { const account = decryptKeystoreJsonSync(json, password); return { address: account.address, privateKey: account.privateKey, privateKeyList: [account.privateKey], }; } }