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 #611 from nohkwak/202404-update-ethers-example
Browse files Browse the repository at this point in the history
Update ethers examples
  • Loading branch information
Nehemiah Nohyun Kwak authored Apr 22, 2024
2 parents cfaa8d9 + a8d5afc commit 95f7704
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 182 deletions.
72 changes: 0 additions & 72 deletions ethers-ext/example/accountKey/AccountKeyPublic.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,11 @@ const { Wallet } = require("@klaytn/ethers-ext");

const senderAddr = "0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7";
const senderPriv = "0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";

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

async function sendTx() {
const tx = {
from: senderAddr,
to: recieverAddr,
value: 0,
};

const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);

const receipt = await sentTx.wait();
console.log("receipt", receipt);
}

async function recoverMsg() {
async function main() {
const msg = "hello";
const msghex = ethers.utils.hexlify(ethers.utils.toUtf8Bytes(msg));
const sig = await wallet.signMessage(msg);
Expand All @@ -39,8 +24,4 @@ async function recoverMsg() {
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAddr);
}

async function main() {
await sendTx();
await recoverMsg();
}
main().catch(console.error);
27 changes: 27 additions & 0 deletions ethers-ext/example/accountKey/sign_msg_AccountKeyPublic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// AccountKeyPublic
// https://docs.klaytn.foundation/docs/learn/accounts/

const { ethers } = require("ethers");

const { Wallet } = require("@klaytn/ethers-ext");

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

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

async function main() {
const msg = "hello";
const msghex = ethers.utils.hexlify(ethers.utils.toUtf8Bytes(msg));
const sig = await wallet.signMessage(msg);
console.log({ senderAddr, msg, msghex, sig });

const addr1 = ethers.utils.verifyMessage(msg, sig);
console.log("recoveredAddr lib", addr1, addr1.toLowerCase() === wallet.address.toLowerCase());

const addr2 = await provider.send("klay_recoverFromMessage", [senderAddr, msghex, sig, "latest"]);
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === wallet.address.toLowerCase());
}

main().catch(console.error);
30 changes: 30 additions & 0 deletions ethers-ext/example/accountKey/sign_msg_AccountKeyRoleBased.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// AccountKeyRoleBased
// https://docs.klaytn.foundation/docs/learn/accounts/

const { ethers } = require("ethers");

const { Wallet } = require("@klaytn/ethers-ext");

const senderAddr = "0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea";
const senderPriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda";
const senderRoleTransactionPriv = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";
const senderRoleAccountUpdatePriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda";
const senderRoleFeePayerPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";

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

async function main() {
const msg = "hello";
const msghex = ethers.utils.hexlify(ethers.utils.toUtf8Bytes(msg));
const sig = await txWallet.signMessage(msg);
console.log({ senderAddr, msg, msghex, sig });

const addr1 = ethers.utils.verifyMessage(msg, sig);
console.log("recoveredAddr lib", addr1, addr1.toLowerCase() === senderAddr.toLowerCase());

const addr2 = await provider.send("klay_recoverFromMessage", [senderAddr, msghex, sig, "latest"]);
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAddr.toLowerCase());
}

main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// AccountKeyWeightedMultiSig
// https://docs.klaytn.foundation/docs/learn/accounts/

const { ethers } = require("ethers");

const { Wallet } = require("@klaytn/ethers-ext");

const senderAddr = "0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e";
const senderPriv = "0xa32c30608667d43be2d652bede413f12a649dd1be93440878e7f712d51a6768a";
const senderNewPriv1 = "0xa32c30608667d43be2d652bede413f12a649dd1be93440878e7f712d51a6768a";
const senderNewPriv2 = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const senderNewPriv3 = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";

const provider = new ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");
const wallet1 = new Wallet(senderAddr, senderNewPriv1, provider);
const wallet2 = new Wallet(senderAddr, senderNewPriv2, provider);
const wallet3 = new Wallet(senderAddr, senderNewPriv3, provider);

async function main() {
const msg = "hello";
const msghex = ethers.utils.hexlify(ethers.utils.toUtf8Bytes(msg));
const sig = await wallet3.signMessage(msg);
console.log({ senderAddr, msg, msghex, sig });

const addr1 = ethers.utils.verifyMessage(msg, sig);
console.log("recoveredAddr lib", addr1, addr1.toLowerCase() === wallet3.address.toLowerCase());

const addr2 = await provider.send("klay_recoverFromMessage", [senderAddr, msghex, sig, "latest"]);
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === wallet3.address.toLowerCase());
}

main().catch(console.error);
38 changes: 38 additions & 0 deletions ethers-ext/example/accountKey/sign_tx_AccountKeyLegacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// AccountKeyLegacy
// https://docs.klaytn.foundation/docs/learn/accounts/

const { ethers } = require("ethers");

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

const senderAddr = "0xb2ba72e1f84b7b8cb15487a2bf20328f2cf40c25";
const senderPriv = "0xebceaca693ea3740231be94f38af6090d3aded336725d26a09b7d14e8e485e1e";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";

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

async function main() {
const tx = {
// for should not be called by a legacy transaction for calling klay_recoverFromTransaction
type: TxType.ValueTransfer,
from: senderAddr,
to: recieverAddr,
value: 0,
};

const populatedTx = await wallet.populateTransaction(tx);
const rawTx = await wallet.signTransaction(populatedTx);
console.log("rawTx", rawTx);

const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);

const receipt = await sentTx.wait();
console.log("receipt", receipt);

const addr = await provider.send("klay_recoverFromTransaction", [rawTx, "latest"]);
console.log("recoveredAddr rpc", addr, addr.toLowerCase() === senderAddr.toLowerCase());
}

main().catch(console.error);
41 changes: 41 additions & 0 deletions ethers-ext/example/accountKey/sign_tx_AccountKeyPublic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// AccountKeyPublic
// https://docs.klaytn.foundation/docs/learn/accounts/

const { ethers } = require("ethers");

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

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

const provider = new ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");
const newWallet = new Wallet(senderAddr, senderNewPriv, provider); // decoupled account

async function main() {
let tx = { // use Klaytn TxType to send transaction from Klaytn typed account
type: TxType.ValueTransfer,
from: senderAddr,
to: recieverAddr,
value: parseKlay("0.01"),
};

const populatedTx = await newWallet.populateTransaction(tx);
const rawTx = await newWallet.signTransaction(populatedTx);
console.log("rawTx", rawTx);

const sentTx = await newWallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);

const receipt = await sentTx.wait();
console.log("receipt", receipt);

console.log(newWallet.address);

const addr = await provider.send("klay_recoverFromTransaction", [rawTx, "latest"]);
console.log("recoveredAddr rpc", addr, addr.toLowerCase() === senderAddr.toLowerCase());
// console.log("recoveredAddr rpc", addr, addr.toLowerCase() === newWallet.address.toLowerCase());
}

main().catch(console.error);
41 changes: 41 additions & 0 deletions ethers-ext/example/accountKey/sign_tx_AccountKeyRoleBased.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// AccountKeyRoleBased
// https://docs.klaytn.foundation/docs/learn/accounts/

const { ethers } = require("ethers");

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

const senderAddr = "0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea";
const senderPriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda";
const senderRoleTransactionPriv = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";
const senderRoleAccountUpdatePriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda";
const senderRoleFeePayerPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";

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

async function main() {
let tx = { // use Klaytn TxType to send transaction from Klaytn typed account
type: TxType.ValueTransfer,
from: senderAddr,
to: recieverAddr,
value: parseKlay("0.01"),
gasLimit: 100000,
};

const populatedTx = await txWallet.populateTransaction(tx);
const rawTx = await txWallet.signTransaction(populatedTx);
console.log("rawTx", rawTx);

const sentTx = await txWallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);

const receipt = await sentTx.wait();
console.log("receipt", receipt);

const addr = await provider.send("klay_recoverFromTransaction", [rawTx, "latest"]);
console.log("recoveredAddr rpc", addr, addr.toLowerCase() === senderAddr.toLowerCase());
}

main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// AccountKeyWeightedMultiSig
// https://docs.klaytn.foundation/docs/learn/accounts/

const { ethers } = require("ethers");

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

const senderAddr = "0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e";
const senderPriv = "0xa32c30608667d43be2d652bede413f12a649dd1be93440878e7f712d51a6768a";
const senderNewPriv1 = "0xa32c30608667d43be2d652bede413f12a649dd1be93440878e7f712d51a6768a";
const senderNewPriv2 = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const senderNewPriv3 = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";

const provider = new ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");
const wallet1 = new Wallet(senderAddr, senderNewPriv1, provider);
const wallet2 = new Wallet(senderAddr, senderNewPriv2, provider);
const wallet3 = new Wallet(senderAddr, senderNewPriv3, provider);

async function main() {
let tx = { // use Klaytn TxType to send transaction from Klaytn typed account
type: TxType.ValueTransfer,
from: senderAddr,
to: recieverAddr,
value: parseKlay("0.01"),
gasLimit: 100000,
};

// The example senderAddr actually requires only 2 signature (threshold = 2),
// but we use 3 signatures to show different ways to sign a transaction.

// sign 1: First signer sign from the tx object
const populatedTx = await wallet1.populateTransaction(tx);
const rawTx1 = await wallet1.signTransaction(populatedTx);
console.log("rawTx1", rawTx1);

// sign 2: Middle signer sign from the rawTx
const rawTx2 = await wallet2.signTransaction(rawTx1);
console.log("rawTx2", rawTx2);

// sign 3: Last signer sign and send from the rawTx
const sentTx3 = await wallet3.sendTransaction(rawTx2);
console.log("sentTx3", sentTx3.hash);

const receipt = await sentTx3.wait();
console.log("receipt", receipt);

const addr = await provider.send("klay_recoverFromTransaction", [rawTx2, "latest"]);
console.log("recoveredAddr rpc", addr, addr.toLowerCase() === senderAddr.toLowerCase());
}

main().catch(console.error);
Loading

0 comments on commit 95f7704

Please sign in to comment.