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

Ethers.js EXT examples for signRecover #510

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
45 changes: 45 additions & 0 deletions ethers-ext/example/signRecover/1_tx_signRecover_legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const ethers = require("ethers");

// const { Wallet, parseKlay } = require("@klaytn/ethers-ext");
const { Wallet, parseKlay } = require("../../dist/src");

const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";

async function main() {
const provider = new ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");
const wallet = new Wallet(senderPriv, provider);

const recieverAddr = wallet.address;
JayChoi1736 marked this conversation as resolved.
Show resolved Hide resolved
const senderAddr = wallet.address;

let tx = {
from: senderAddr,
to: recieverAddr,
value: parseKlay("1"),
};

// sign
const popedTx = await wallet.populateTransaction(tx);
const txHashRLP = await wallet.signTransaction(popedTx);
let txObj = ethers.utils.parseTransaction(txHashRLP);
console.log(txHashRLP);
console.log(txObj);

// verify
const rawTx = ethers.utils.serializeTransaction(popedTx);
const msgHash = ethers.utils.keccak256(rawTx);
const msgBytes = ethers.utils.arrayify(msgHash);

const expandedSig = {
r: txObj.r,
s: txObj.s,
recoveryParam: 0, // not really sure what this does
v: txObj.v
};
const signature = ethers.utils.joinSignature(expandedSig);

const recoverAddr = ethers.utils.recoverAddress(msgBytes, signature);
console.log("\nsender", senderAddr, "\nrecovered", recoverAddr);
}

main();
31 changes: 31 additions & 0 deletions ethers-ext/example/signRecover/2_tx_signRecover_pubkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const ethers = require("ethers");

// const { Wallet, parseKlay } = require("@klaytn/ethers-ext");
const { Wallet, parseKlay, TxType } = require("../../dist/src");

const recieverAddr = "0xe15cd70a41dfb05e7214004d7d054801b2a2f06b";
const senderAddr = "0xe15cd70a41dfb05e7214004d7d054801b2a2f06b";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";

async function main() {
const provider = new ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");
const wallet = new Wallet(senderAddr, senderPriv, provider);

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

tx = await wallet.populateTransaction(tx);
const senderTxHashRLP = await wallet.signTransaction(tx);

console.log(senderTxHashRLP);
console.log(wallet.decodeTxFromRLP(senderTxHashRLP));

const recoverAddr = await provider.send("klay_recoverFromTransaction", [senderTxHashRLP, "latest"]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change it to the web3rpc function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, through another PR, we will test and update the two lines below.

const { Wallet, parseKlay, TxType } = require("../../dist/src");
->
const { Wallet, parseKlay } = require("@klaytn/ethers-ext");

const recoverAddr = await provider.send("klay_recoverFromTransaction", [senderTxHashRLP, "latest"]);
->
const recoverAddr = await provider.klay.recoverFromTransaction(senderTxHashRLP, "latest");

console.log("\nsender", senderAddr, "\nrecovered", recoverAddr);
}

main();
52 changes: 52 additions & 0 deletions ethers-ext/example/signRecover/3_tx_signRecover_multisig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const ethers = require("ethers");

// const { Wallet, parseKlay } = require("@klaytn/ethers-ext");
const { Wallet, parseKlay, TxType } = require("../../dist/src");


const recieverAddr = "0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e";
const senderAddr = "0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e";

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

let user1 = new Wallet(
"0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e",
"0xa32c30608667d43be2d652bede413f12a649dd1be93440878e7f712d51a6768a",
provider
);
let user2 = new Wallet(
"0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e",
"0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8",
provider
);
let user3 = new Wallet(
"0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e",
"0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac",
provider
);

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

tx = await user1.populateTransaction(tx);
const senderTxHashRLP = await user1.signTransaction(tx);

const tx2 = await user2.populateTransaction(user2.decodeTxFromRLP(senderTxHashRLP));
const senderTxHashRLP2 = await user2.signTransaction(tx2);

const tx3 = await user3.populateTransaction(user3.decodeTxFromRLP(senderTxHashRLP2));
const senderTxHashRLP3 = await user3.signTransaction(tx3);

console.log(senderTxHashRLP3);
console.log(user1.decodeTxFromRLP(senderTxHashRLP3));

const recoverAddr = await provider.send("klay_recoverFromTransaction", [senderTxHashRLP3, "latest"]);
console.log("\nsender", senderAddr, "\nrecovered", recoverAddr);
}

main();
37 changes: 37 additions & 0 deletions ethers-ext/example/signRecover/4_tx_signRecover_rolebased.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const ethers = require("ethers");

// const { Wallet, parseKlay } = require("@klaytn/ethers-ext");
const { Wallet, parseKlay, TxType } = require("../../dist/src");


const recieverAddr = "0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea";
const senderAddr = "0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea";


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

let user = new Wallet(
"0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea",
"0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac",
provider
);

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

tx = await user.populateTransaction(tx);
const senderTxHashRLP = await user.signTransaction(tx);

console.log(senderTxHashRLP);
console.log(user.decodeTxFromRLP(senderTxHashRLP));

const recoverAddr = await provider.send("klay_recoverFromTransaction", [senderTxHashRLP, "latest"]);
console.log("\nsender", senderAddr, "\nrecovered", recoverAddr);
}

main();
20 changes: 20 additions & 0 deletions ethers-ext/example/signRecover/5_msg_signRecover_legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const ethers = require("ethers");

// const { Wallet, parseKlay } = require("@klaytn/ethers-ext");
const { Wallet} = require("../../dist/src");

async function main() {
const provider = new ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");
const wallet = new Wallet("0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8", provider);

const address = wallet.address;
let msg = "I♥KLAYTN";
let msgHash = ethers.utils.hashMessage(msg);
let msgHashBytes = ethers.utils.arrayify(msgHash);
const signature = await wallet.signMessage(msgHashBytes);

const recoverAddr = await provider.send("klay_recoverFromMessage", [address, msgHash, signature, "latest"]);
console.log("\nsender", address, "\nrecovered", recoverAddr);
}

main();
23 changes: 23 additions & 0 deletions ethers-ext/example/signRecover/6_msg_signRecover_pubkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const ethers = require("ethers");

// const { Wallet, parseKlay } = require("@klaytn/ethers-ext");
const { Wallet} = require("../../dist/src");

async function main() {
const provider = new ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");
const wallet = new Wallet(
"0xe15cd70a41dfb05e7214004d7d054801b2a2f06b",
"0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8",
provider);

const address = wallet.address;
let msg = "I♥KLAYTN";
let msgHash = ethers.utils.hashMessage(msg);
let msgHashBytes = ethers.utils.arrayify(msgHash);
const signature = await wallet.signMessage(msgHashBytes);

const recoverAddr = await provider.send("klay_recoverFromMessage", [address, msgHash, signature, "latest"]);
console.log("\nsender", address, "\nrecovered", recoverAddr);
}

main();
25 changes: 25 additions & 0 deletions ethers-ext/example/signRecover/7_msg_signRecover_multisig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const ethers = require("ethers");

// const { Wallet, parseKlay } = require("@klaytn/ethers-ext");
const { Wallet} = require("../../dist/src");

// multisig account address
const address = "0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e";
// a member private key of multisig account
const priv = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";

async function main() {
const provider = new ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");
const wallet = new Wallet(address, priv, provider);

let msg = "I♥KLAYTN";
let msgHash = ethers.utils.hashMessage(msg);
let msgHashBytes = ethers.utils.arrayify(msgHash);
const signature = await wallet.signMessage(msgHashBytes);

const recoverAddr = await provider.send("klay_recoverFromMessage", [address, msgHash, signature, "latest"]);
console.log("\nsender", address, "\nrecovered", recoverAddr);
console.log("private key's legacy type address", await wallet.getEtherAddress());
}

main();
25 changes: 25 additions & 0 deletions ethers-ext/example/signRecover/8_msg_signRecover_rolebased.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const ethers = require("ethers");

// const { Wallet, parseKlay } = require("@klaytn/ethers-ext");
const { Wallet} = require("../../dist/src");

// role-based account address
const address = "0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea";
// transaction role key of role-based account
const priv = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";

async function main() {
const provider = new ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");
const wallet = new Wallet(address, priv, provider);

let msg = "I♥KLAYTN";
let msgHash = ethers.utils.hashMessage(msg);
let msgHashBytes = ethers.utils.arrayify(msgHash);
const signature = await wallet.signMessage(msgHashBytes);

const recoverAddr = await provider.send("klay_recoverFromMessage", [address, msgHash, signature, "latest"]);
console.log("\nsender", address, "\nrecovered", recoverAddr);
console.log("private key's legacy type address", await wallet.getEtherAddress());
}

main();