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

Check RLP in each functions #501

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { Wallet, TxType, parseKlay } = require("@klaytn/ethers-ext");
blukat29 marked this conversation as resolved.
Show resolved Hide resolved
const ethers = require("ethers");

//
// AccountKeyWeightedMultiSig Step 02 - value transfer
// https://docs.klaytn.foundation/content/klaytn/design/accounts#accountkeyweightedmultisig
//
// gasLimit: Must be large enough
//

const provider = new ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");

// the same address of sender in AccountKeyWeightedMultiSig_01_accountUpdate.js
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const senderAddr = "0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e";
const senderNewPriv1 = "0xa32c30608667d43be2d652bede413f12a649dd1be93440878e7f712d51a6768a";
const senderNewPriv2 = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const senderNewPriv3 = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";

async function main() {
let tx = {
type: TxType.ValueTransfer,
gasLimit: 100000,
to: recieverAddr,
value: parseKlay("1"),
from: senderAddr,
};

// sign 1
const wallet = new Wallet(senderAddr, senderNewPriv1, provider);
let ptx = await wallet.populateTransaction(tx);
const txHashRLP = await wallet.signTransaction(ptx);
console.log("TxHashRLP", txHashRLP);

// sign 2
const wallet2 = new Wallet(senderAddr, senderNewPriv2, provider);
let ptx2 = await wallet2.populateTransaction(txHashRLP);
const txHashRLP2 = await wallet2.signTransaction(ptx2);
console.log("TxHashRLP2", txHashRLP2);

// sign 3 & send
const wallet3 = new Wallet(senderAddr, senderNewPriv3, provider);
const res = await wallet3.sendTransaction(txHashRLP2);
console.log("transaction", res);

const rc = await provider.waitForTransaction(res.hash);
console.log("receipt", rc);
}

main();
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 {
blukat29 marked this conversation as resolved.
Show resolved Hide resolved
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