UNPKG

@okxweb3/coin-kaia

Version:

An kaia SDK for building Web3 wallets and applications.

126 lines 5.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Wallet = void 0; const ethers6_1 = require("ethers6"); const lodash_1 = __importDefault(require("lodash")); const js_ext_core_1 = require("@kaiachain/js-ext-core"); const keystore_1 = require("./keystore"); const txutil_1 = require("./txutil"); class Wallet extends ethers6_1.Wallet { static async fromEncryptedJson(json, password, progress) { const { address, privateKey } = await (0, keystore_1.decryptKeystoreList)(json, password, progress); return new Wallet(address, privateKey); } static fromEncryptedJsonSync(json, password) { const { address, privateKey } = (0, keystore_1.decryptKeystoreListSync)(json, password); return new Wallet(address, privateKey); } static async fromEncryptedJsonList(json, password, progress) { const { address, privateKeyList } = await (0, keystore_1.decryptKeystoreList)(json, password, progress); return lodash_1.default.map(privateKeyList, (privateKey) => new Wallet(address, privateKey)); } static fromEncryptedJsonListSync(json, password) { const { address, privateKeyList } = (0, keystore_1.decryptKeystoreListSync)(json, password); return lodash_1.default.map(privateKeyList, (privateKey) => new Wallet(address, privateKey)); } constructor(addressOrPrivateKey, privateKeyOrProvider) { if (js_ext_core_1.HexStr.isHex(addressOrPrivateKey, 20)) { const _address = js_ext_core_1.HexStr.from(addressOrPrivateKey); const _privateKey = privateKeyOrProvider; super(_privateKey); this.klaytnAddr = _address; } else { const _privateKey = addressOrPrivateKey; super(_privateKey); } } getAddress(legacy) { if (legacy || !this.klaytnAddr) { return super.getAddress(); } else { return Promise.resolve(this.klaytnAddr); } } getEtherAddress() { return super.getAddress(); } decodeTxFromRLP(rlp) { return (0, js_ext_core_1.parseTransaction)(rlp); } async isDecoupled() { if (!this.klaytnAddr) { return false; } else { return (await this.getAddress(false)) == (await this.getAddress(true)); } } async populateTransaction(transaction) { return this._populateTransaction(transaction, false); } async _populateTransaction(transaction, asFeePayer) { const tx = await (0, txutil_1.getTransactionRequest)(transaction); if (!(0, js_ext_core_1.isKlaytnTxType)((0, js_ext_core_1.parseTxType)(tx.type))) { return super.populateTransaction(tx); } if (!asFeePayer) { await (0, txutil_1.populateFrom)(tx, await this.getAddress()); } await (0, txutil_1.populateTo)(tx); if (tx.nonce == undefined) { throw new Error("invalid nonce"); } if (tx.gasLimit == undefined) { throw new Error("invalid gasLimit"); } await (0, txutil_1.populateGasLimit)(tx); if (tx.gasPrice == undefined) { throw new Error("invalid gasPrice"); } await (0, txutil_1.populateGasPrice)(tx); if (tx.chainId == undefined) { throw new Error("invalid chainId"); } await (0, txutil_1.populateChainId)(tx); return tx; } async signTransaction(transaction) { const tx = await (0, txutil_1.getTransactionRequest)(transaction); if (!(0, js_ext_core_1.isKlaytnTxType)((0, js_ext_core_1.parseTxType)(tx.type))) { return super.signTransaction(tx); } await (0, txutil_1.populateChainId)(tx); const chainId = tx.chainId; const klaytnTx = js_ext_core_1.KlaytnTxFactory.fromObject(tx); const sigHash = (0, ethers6_1.keccak256)(klaytnTx.sigRLP()); const sig = (0, txutil_1.eip155sign)(this.signingKey, sigHash, Number(chainId.toString())); klaytnTx.addSenderSig(sig); if ((0, js_ext_core_1.isFeePayerSigTxType)(klaytnTx.type)) { return klaytnTx.senderTxHashRLP(); } else { return klaytnTx.txHashRLP(); } } async signTransactionAsFeePayer(transactionOrRLP) { const tx = await (0, txutil_1.getTransactionRequest)(transactionOrRLP); (0, ethers6_1.assert)((0, js_ext_core_1.isFeePayerSigTxType)((0, js_ext_core_1.parseTxType)(tx.type)), `signTransactionAsFeePayer not supported for tx type ${tx.type}`, "UNSUPPORTED_OPERATION", { operation: "signTransactionAsFeePayer", }); await (0, txutil_1.populateFeePayerAndSignatures)(tx, await this.getAddress()); await (0, txutil_1.populateChainId)(tx); const chainId = tx.chainId; const klaytnTx = js_ext_core_1.KlaytnTxFactory.fromObject(tx); const sigFeePayerHash = (0, ethers6_1.keccak256)(klaytnTx.sigFeePayerRLP()); const sig = (0, txutil_1.eip155sign)(this.signingKey, sigFeePayerHash, Number(chainId)); klaytnTx.addFeePayerSig(sig); return klaytnTx.txHashRLP(); } } exports.Wallet = Wallet; //# sourceMappingURL=signer.js.map