UNPKG

@kaiachain/web3js-ext

Version:
118 lines (114 loc) 5.12 kB
"use strict"; /* This file is part of web3.js. web3.js is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. web3.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see <http://www.gnu.org/licenses/>. */ // Taken from https://github.com/web3/web3.js/blob/v4.3.0/packages/web3-eth/src/rpc_method_wrappers.ts // Modified to support Klaytn TxTypes Object.defineProperty(exports, "__esModule", { value: true }); exports.signTransaction = signTransaction; const js_ext_core_1 = require("@kaiachain/js-ext-core"); const web3_eth_1 = require("web3-eth"); const web3_types_1 = require("web3-types"); const web3_utils_1 = require("web3-utils"); const web3_validator_1 = require("web3-validator"); const sign_js_1 = require("../accounts/sign.js"); const transaction_builder_js_1 = require("./utils/transaction_builder.js"); async function signTransaction(web3Context, transaction, returnFormat) { // If not Klaytn TxType, fall back to web3-eth's original implementation if (!(0, js_ext_core_1.isKlaytnTxType)((0, sign_js_1._parseTxType)(transaction.type))) { return (0, web3_eth_1.signTransaction)(web3Context, transaction, returnFormat); } // Resolve 'from' and 'to' field, like in the original web3-eth source const tx = { ...transaction, from: (0, transaction_builder_js_1.getTransactionFromOrToAttr)("from", web3Context, transaction), to: (0, transaction_builder_js_1.getTransactionFromOrToAttr)("to", web3Context, transaction), }; // Fill 'gasLimit' field. Kaikas (window.klaytn) requires 'gas' field nonempty. // Fill 'tx.gasLimit' here, then rename to 'gas' in getRpcTxObject() below. if ((0, web3_validator_1.isNullish)(tx.gasLimit)) { if (!(0, web3_validator_1.isNullish)(tx.gas)) { tx.gasLimit = tx.gas; } else { const gasLimitHex = await (0, web3_eth_1.estimateGas)(web3Context, tx, "latest", web3_types_1.ETH_DATA_FORMAT); const gasLimitNum = Number(gasLimitHex); const bufferedNum = (0, sign_js_1.bufferedGasLimit)(gasLimitNum); tx.gasLimit = (0, web3_utils_1.format)({ format: "uint" }, bufferedNum, web3_types_1.ETH_DATA_FORMAT); } } // transactionFormatted contains all fields including Klaytn-specific fields. This is the transaction to be signed. const transactionFormatted = { ...tx, // first copy all fields from tx (including Klaytn-specific fields) ...(0, js_ext_core_1.getRpcTxObject)(tx), // then overwrite with formatted fields (only Ethereum fields) }; // Translate to string 'type' field that Kaikas understands. if (isKaikas(web3Context.provider)) { transactionFormatted.type = (0, js_ext_core_1.getKaikasTxType)(transactionFormatted.type); } // The result may be: // - a string // - an object { raw: string, tx: Transaction } (e.g. Klaytn node) // - an object { rawTranasction: string, ... } (e.g. Kaikas) const response = await web3Context.requestManager.send({ method: "klay_signTransaction", params: [transactionFormatted], }); let rawTransaction; if (typeof response === "string") { rawTransaction = response; } else if (typeof response.rawTransaction === "string") { rawTransaction = response.rawTransaction; } else if (typeof response.raw === "string") { rawTransaction = response.raw; } else { throw new Error(`Unabled to parse signTransaction response: ${JSON.stringify(response)}`); } try { return { raw: rawTransaction, tx: getTransactionSignedAPI(rawTransaction), }; } catch (e) { return { raw: rawTransaction, tx: {}, }; } } function getTransactionSignedAPI(rawTransaction) { const tx = (0, js_ext_core_1.parseTransaction)(rawTransaction); return { type: js_ext_core_1.HexStr.fromNumber(tx.type || 0), to: tx.to || "0x", gasPrice: js_ext_core_1.HexStr.fromNumber(tx.gasPrice || 0), gas: js_ext_core_1.HexStr.fromNumber(tx.gasLimit), nonce: js_ext_core_1.HexStr.fromNumber(tx.nonce), value: js_ext_core_1.HexStr.fromNumber(tx.value), input: tx.data || "0x", data: tx.data || "0x", chainId: js_ext_core_1.HexStr.fromNumber(tx.chainId || 0), // Becuase Klaytn Tx may contain multiple signatures, // we do not return v,r,s to avoid confusion. v: "", r: "", s: "", }; } function isKaikas(provider) { return !(0, web3_validator_1.isNullish)(provider) && (provider.isKaikas == true); }