This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #611 from nohkwak/202404-update-ethers-example
Update ethers examples
- Loading branch information
Showing
13 changed files
with
345 additions
and
182 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
ethers-ext/example/accountKey/sign_msg_AccountKeyPublic.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
ethers-ext/example/accountKey/sign_msg_AccountKeyRoleBased.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
32 changes: 32 additions & 0 deletions
32
ethers-ext/example/accountKey/sign_msg_AccountKeyWeightedMultiSig.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
ethers-ext/example/accountKey/sign_tx_AccountKeyRoleBased.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
52 changes: 52 additions & 0 deletions
52
ethers-ext/example/accountKey/sign_tx_AccountKeyWeightedMultiSig.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.