Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #501 from nohkwak/202309-AddRLPdecoding
Browse files Browse the repository at this point in the history
Check RLP in each functions
  • Loading branch information
Nohyun Nehemiah Kwak authored Sep 8, 2023
2 parents 7782ad5 + fe61fbf commit 2ddc638
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
62 changes: 34 additions & 28 deletions ethers-ext/src/ethers/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,22 @@ export class Wallet extends EthersWallet {
return tx;
}

async populateTransaction(transaction: Deferrable<TransactionRequest>): Promise<TransactionRequest> {
let tx: TransactionRequest = await resolveProperties(transaction);
_convertTxFromRLP(transaction: Deferrable<TransactionRequest> | 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<TransactionRequest>): Promise<TransactionRequest> {
let tx: TransactionRequest = this._convertTxFromRLP(transaction);
tx = await resolveProperties(tx);

if (!KlaytnTxFactory.has(tx.type)) {
return super.populateTransaction(tx);
}
Expand Down Expand Up @@ -172,8 +185,9 @@ export class Wallet extends EthersWallet {
}

async signTransaction(transaction: Deferrable<TransactionRequest>): Promise<string> {
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);
}
Expand All @@ -194,18 +208,15 @@ export class Wallet extends EthersWallet {
}

async signTransactionAsFeePayer(transaction: Deferrable<TransactionRequest>): Promise<string> {
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
Expand All @@ -232,10 +243,12 @@ export class Wallet extends EthersWallet {

async sendTransaction(transaction: Deferrable<TransactionRequest>): Promise<TransactionResponse> {
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);
}

Expand All @@ -251,18 +264,11 @@ export class Wallet extends EthersWallet {
async sendTransactionAsFeePayer(transaction: Deferrable<TransactionRequest> | string): Promise<TransactionResponse> {
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) {
Expand Down

0 comments on commit 2ddc638

Please sign in to comment.