diff --git a/ethers-ext/example/accountKey/AccountKeyWeightedMultiSig_02_valueTransfer.js b/ethers-ext/example/accountKey/AccountKeyWeightedMultiSig_02_valueTransfer.js index 94a384a6e..fe8c053da 100644 --- a/ethers-ext/example/accountKey/AccountKeyWeightedMultiSig_02_valueTransfer.js +++ b/ethers-ext/example/accountKey/AccountKeyWeightedMultiSig_02_valueTransfer.js @@ -34,28 +34,16 @@ async function main() { // sign 2 const wallet2 = new Wallet(senderAddr, senderNewPriv2, provider); - let decodedTx = wallet2.decodeTxFromRLP(txHashRLP); - console.log(decodedTx); - let ptx2 = await wallet2.populateTransaction(decodedTx); + let ptx2 = await wallet2.populateTransaction(txHashRLP); const txHashRLP2 = await wallet2.signTransaction(ptx2); console.log("TxHashRLP2", txHashRLP2); - // sign 3 + // sign 3 & send const wallet3 = new Wallet(senderAddr, senderNewPriv3, provider); - let decodedTx2 = wallet3.decodeTxFromRLP(txHashRLP2); - console.log(decodedTx2); - let ptx3 = await wallet3.populateTransaction(decodedTx2); - const txHashRLP3 = await wallet3.signTransaction(ptx3); - console.log("TxHashRLP3", txHashRLP3); + const res = await wallet3.sendTransaction(txHashRLP2); + console.log("transaction", res); - let decodedTx3 = wallet3.decodeTxFromRLP(txHashRLP3); - console.log(decodedTx3); - - // send - const txhash = await provider.send("klay_sendRawTransaction", [txHashRLP3]); - console.log("txhash", txhash); - - const rc = await provider.waitForTransaction(txhash); + const rc = await res.wait(); console.log("receipt", rc); } diff --git a/ethers-ext/src/ethers/signer.ts b/ethers-ext/src/ethers/signer.ts index 35fa8bf31..a79f785c8 100644 --- a/ethers-ext/src/ethers/signer.ts +++ b/ethers-ext/src/ethers/signer.ts @@ -102,9 +102,22 @@ export class Wallet extends EthersWallet { return tx; } - async populateTransaction(transaction: Deferrable): Promise { - let tx: TransactionRequest = await resolveProperties(transaction); + _convertTxFromRLP(transaction: Deferrable | string): any { + if (typeof transaction === "string") { + if (HexStr.isHex(transaction)) { + return this.decodeTxFromRLP(transaction); + } else { + throw new Error("String type input has to be RLP encoded Hex string."); + } + } else { + return transaction; + } + } + async populateTransaction(transaction: Deferrable): Promise { + let tx: TransactionRequest = this._convertTxFromRLP(transaction); + tx = await resolveProperties(tx); + if (!KlaytnTxFactory.has(tx.type)) { return super.populateTransaction(tx); } @@ -172,8 +185,9 @@ export class Wallet extends EthersWallet { } async signTransaction(transaction: Deferrable): Promise { - const tx: TransactionRequest = await resolveProperties(transaction); - + let tx: TransactionRequest = this._convertTxFromRLP(transaction); + tx = await resolveProperties(tx); + if (!KlaytnTxFactory.has(tx.type)) { return super.signTransaction(tx); } @@ -194,18 +208,15 @@ export class Wallet extends EthersWallet { } async signTransactionAsFeePayer(transaction: Deferrable): Promise { - let tx = transaction; - if (typeof transaction === "string") { - if (HexStr.isHex(transaction)) { - tx = this.decodeTxFromRLP(transaction); - // @ts-ignore : we have to add feePayer property - tx.chainId = Math.floor((tx.txSignatures[0][0] - 35) / 2); - } else { - throw new Error("Input parameter has to be RLP encoded Hex string."); - } + let tx: TransactionRequest = this._convertTxFromRLP(transaction); + + // @ts-ignore : chainId can be omitted from RLP encoded format + if (!tx.chainId) { + // @ts-ignore + tx.chainId = this.getChainId(); } - const rtx: TransactionRequest = await resolveProperties(tx); + // @ts-ignore : we have to add feePayer property if (!rtx.feePayer) { // @ts-ignore : we have to add feePayer property @@ -232,10 +243,12 @@ export class Wallet extends EthersWallet { async sendTransaction(transaction: Deferrable): Promise { this._checkProvider("sendTransaction"); - const tx = await this.populateTransaction(transaction); - const signedTx = await this.signTransaction(tx); - if (!KlaytnTxFactory.has(tx.type)) { + let tx: TransactionRequest = this._convertTxFromRLP(transaction); + let ptx = await this.populateTransaction(tx); + const signedTx = await this.signTransaction(ptx); + + if (!KlaytnTxFactory.has(ptx.type)) { return await this.provider.sendTransaction(signedTx); } @@ -251,18 +264,11 @@ export class Wallet extends EthersWallet { async sendTransactionAsFeePayer(transaction: Deferrable | string): Promise { this._checkProvider("sendTransactionAsFeePayer"); - let tx, ptx; - if (typeof transaction === "string") { - if (HexStr.isHex(transaction)) { - tx = this.decodeTxFromRLP(transaction); - ptx = await this.populateTransaction(tx); - } else { - throw new Error("Input parameter has to be RLP encoded Hex string."); - } - } else { - ptx = await this.populateTransaction(transaction); - } + let tx: TransactionRequest = this._convertTxFromRLP(transaction); + let ptx = await this.populateTransaction(tx); + // @ts-ignore : we have to add feePayer property + ptx.feePayer = await this.getAddress(); const signedTx = await this.signTransactionAsFeePayer(ptx); if (this.provider instanceof EthersJsonRpcProvider) {