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 #613 from nohkwak/202404-update-web3js-examples
web3js: Separate each usecases
- Loading branch information
Showing
14 changed files
with
286 additions
and
165 deletions.
There are no files selected for viewing
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
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
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
30 changes: 30 additions & 0 deletions
30
web3js-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,30 @@ | ||
// AccountKeyPublic | ||
// https://docs.klaytn.foundation/docs/learn/accounts/ | ||
|
||
const { Web3 } = require("@klaytn/web3js-ext"); | ||
|
||
// Using senderPriv == senderNewPriv to execute this example repeatedly. | ||
// But you might want to register a different private key. | ||
const senderAddr = "0xfb60ded0ae96fe04eed6450aead860aa9d57128e"; | ||
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"; | ||
|
||
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net"); | ||
const web3 = new Web3(provider); | ||
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv); | ||
|
||
async function main() { | ||
const msg = "hello"; | ||
const msghex = Web3.utils.utf8ToHex(msg); | ||
const signResult = senderAccount.sign(msg); | ||
console.log({ senderAddr, msg, msghex, sig: signResult.signature }); | ||
|
||
const { v, r, s } = signResult; | ||
const addr1 = web3.eth.accounts.recover(msg, v, r, s); | ||
console.log("recoveredAddr lib", addr1, addr1.toLowerCase() === senderAccount.address.toLowerCase()); | ||
|
||
const sig = signResult.signature; | ||
const addr2 = await web3.klay.recoverFromMessage(senderAddr, msghex, sig, "latest"); | ||
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAccount.address.toLowerCase()); | ||
} | ||
|
||
main().catch(console.error); |
30 changes: 30 additions & 0 deletions
30
web3js-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 { Web3 } = require("@klaytn/web3js-ext"); | ||
|
||
const senderAddr = "0x334b4d3c775c45c59de54e9f0408cba25a1aece7"; | ||
const senderRoleTransactionPriv = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac"; | ||
const senderRoleAccountUpdatePriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda"; | ||
const senderRoleFeePayerPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"; | ||
|
||
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net"); | ||
const web3 = new Web3(provider); | ||
const txAccount = web3.eth.accounts.privateKeyToAccount(senderRoleTransactionPriv); | ||
|
||
async function main() { | ||
const msg = "hello"; | ||
const msghex = Web3.utils.utf8ToHex(msg); | ||
const signResult = txAccount.sign(msg); | ||
console.log({ senderAddr, msg, msghex, sig: signResult.signature }); | ||
|
||
const { v, r, s } = signResult; | ||
const addr1 = web3.eth.accounts.recover(msg, v, r, s); | ||
console.log("recoveredAddr lib", addr1, addr1.toLowerCase() === txAccount.address.toLowerCase()); | ||
|
||
const sig = signResult.signature; | ||
const addr2 = await web3.klay.recoverFromMessage(senderAddr, msghex, sig, "latest"); | ||
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === txAccount.address.toLowerCase()); | ||
} | ||
|
||
main().catch(console.error); |
30 changes: 30 additions & 0 deletions
30
web3js-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,30 @@ | ||
// AccountKeyWeightedMultiSig | ||
// https://docs.klaytn.foundation/docs/learn/accounts/ | ||
|
||
const { Web3, TxType, AccountKeyType, toPeb, getPublicKeyFromPrivate } = require("@klaytn/web3js-ext"); | ||
|
||
const senderAddr = "0x2bf611d14d330fd3688d10f2201321eacc8aa2ce"; | ||
const senderPriv1 = "0x31fadf868e68fd2e3f7a1c528023c9a86a45db850e9d6b82c1a82d4c75b469d1"; | ||
const senderPriv2 = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"; | ||
const senderPriv3 = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac"; | ||
|
||
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net"); | ||
const web3 = new Web3(provider); | ||
|
||
async function main() { | ||
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv1); | ||
const msg = "hello"; | ||
const msghex = Web3.utils.utf8ToHex(msg); | ||
const signResult = senderAccount.sign(msg); | ||
console.log({ senderAddr, msg, msghex, sig: signResult.signature }); | ||
|
||
const { v, r, s } = signResult; | ||
const addr1 = web3.eth.accounts.recover(msg, v, r, s); | ||
console.log("recoveredAddr lib", addr1, addr1.toLowerCase() === senderAccount.address.toLowerCase()); | ||
|
||
const sig = signResult.signature; | ||
const addr2 = await web3.klay.recoverFromMessage(senderAddr, msghex, sig, "latest"); | ||
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAccount.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,32 @@ | ||
// AccountKeyLegacy | ||
// https://docs.klaytn.foundation/docs/learn/accounts/ | ||
|
||
const { Web3, toPeb } = require("@klaytn/web3js-ext"); | ||
|
||
const senderAddr = "0xb2ba72e1f84b7b8cb15487a2bf20328f2cf40c25"; | ||
const senderPriv = "0xebceaca693ea3740231be94f38af6090d3aded336725d26a09b7d14e8e485e1e"; | ||
const receiverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9"; | ||
|
||
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net"); | ||
const web3 = new Web3(provider); | ||
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv); | ||
|
||
async function main() { | ||
let tx = { | ||
from: senderAddr, | ||
to: receiverAddr, | ||
value: toPeb("0.01", "KLAY"), | ||
}; | ||
|
||
const signResult = await senderAccount.signTransaction(tx); | ||
console.log("signedTx", signResult.transactionHash); | ||
|
||
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction); | ||
console.log("receipt", receipt); | ||
|
||
const sig = signResult.signature; | ||
const addr2 = await web3.klay.recoverFromTransaction(senderAddr, sig, "latest"); | ||
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAddr); | ||
} | ||
|
||
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,35 @@ | ||
// AccountKeyPublic | ||
// https://docs.klaytn.foundation/docs/learn/accounts/ | ||
|
||
const { Web3, TxType, toPeb } = require("@klaytn/web3js-ext"); | ||
|
||
// Using senderPriv == senderNewPriv to execute this example repeatedly. | ||
// But you might want to register a different private key. | ||
const senderAddr = "0xfb60ded0ae96fe04eed6450aead860aa9d57128e"; | ||
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"; | ||
const receiverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9"; | ||
|
||
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net"); | ||
const web3 = new Web3(provider); | ||
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv); | ||
|
||
async function main() { | ||
let tx = { | ||
type: TxType.ValueTransfer, | ||
to: receiverAddr, | ||
value: toPeb("0.01", "KLAY"), | ||
from: senderAddr, | ||
}; | ||
|
||
const signResult = await senderAccount.signTransaction(tx); | ||
console.log("signedTx", signResult.transactionHash); | ||
|
||
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction); | ||
console.log("receipt", receipt); | ||
|
||
const sig = signResult.signature; | ||
const addr2 = await web3.klay.recoverFromTransaction(senderAddr, sig, "latest"); | ||
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAddr); | ||
} | ||
|
||
main().catch(console.error); |
36 changes: 36 additions & 0 deletions
36
web3js-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,36 @@ | ||
// AccountKeyRoleBased | ||
// https://docs.klaytn.foundation/docs/learn/accounts/ | ||
|
||
const { Web3, TxType, toPeb } = require("@klaytn/web3js-ext"); | ||
|
||
const senderAddr = "0x334b4d3c775c45c59de54e9f0408cba25a1aece7"; | ||
const senderRoleTransactionPriv = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac"; | ||
const senderRoleAccountUpdatePriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda"; | ||
const senderRoleFeePayerPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"; | ||
const receiverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9"; | ||
|
||
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net"); | ||
const web3 = new Web3(provider); | ||
const txAccount = web3.eth.accounts.privateKeyToAccount(senderRoleTransactionPriv); | ||
|
||
async function main() { | ||
let tx = { | ||
type: TxType.ValueTransfer, | ||
from: senderAddr, | ||
to: receiverAddr, | ||
value: toPeb("0.01", "KLAY"), | ||
gasLimit: 100000, | ||
}; | ||
|
||
const signResult = await txAccount.signTransaction(tx); | ||
console.log("signedTx", signResult.transactionHash); | ||
|
||
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction); | ||
console.log("receipt", receipt); | ||
|
||
const sig = signResult.signature; | ||
const addr2 = await web3.klay.recoverFromTransaction(senderAddr, sig, "latest"); | ||
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAddr); | ||
} | ||
|
||
main().catch(console.error); |
50 changes: 50 additions & 0 deletions
50
web3js-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,50 @@ | ||
// AccountKeyWeightedMultiSig | ||
// https://docs.klaytn.foundation/docs/learn/accounts/ | ||
|
||
const { Web3, TxType, toPeb } = require("@klaytn/web3js-ext"); | ||
|
||
const senderAddr = "0x2bf611d14d330fd3688d10f2201321eacc8aa2ce"; | ||
const senderPriv1 = "0x31fadf868e68fd2e3f7a1c528023c9a86a45db850e9d6b82c1a82d4c75b469d1"; | ||
const senderPriv2 = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"; | ||
const senderPriv3 = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac"; | ||
const receiverAddr = "0x2bf611d14d330fd3688d10f2201321eacc8aa2ce"; | ||
|
||
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net"); | ||
const web3 = new Web3(provider); | ||
const senderAccount1 = web3.eth.accounts.privateKeyToAccount(senderPriv1); | ||
const senderAccount2 = web3.eth.accounts.privateKeyToAccount(senderPriv2); | ||
const senderAccount3 = web3.eth.accounts.privateKeyToAccount(senderPriv3); | ||
|
||
async function main() { | ||
let tx = { | ||
type: TxType.ValueTransfer, | ||
from: senderAddr, | ||
to: receiverAddr, | ||
value: toPeb("0.01", "KLAY"), | ||
gasLimit: 100000, | ||
}; | ||
|
||
// The example senderAddr actually requires only 2 signature, | ||
// but we use 3 signatures to show different ways to sign a transaction. | ||
|
||
// sign 1: First signer sign from the tx object | ||
const signResult1 = await senderAccount1.signTransaction(tx); | ||
console.log("rawTx1", signResult1.rawTransaction); | ||
|
||
// sign 2: Rest of the signers sign from the rawTx | ||
const signResult2 = await senderAccount2.signTransaction(signResult1.rawTransaction); | ||
console.log("rawTx2", signResult2.rawTransaction); | ||
|
||
// sign 3: Last signer sign from the rawTx then send it | ||
const signResult3 = await senderAccount3.signTransaction(signResult2.rawTransaction); | ||
console.log("signedTx3", signResult3.transactionHash); | ||
|
||
const receipt = await web3.eth.sendSignedTransaction(signResult3.rawTransaction); | ||
console.log("receipt", receipt); | ||
|
||
const sig = signResult3.signature; | ||
const addr2 = await web3.klay.recoverFromTransaction(senderAddr, sig, "latest"); | ||
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAddr); | ||
} | ||
|
||
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,31 @@ | ||
// AccountKeyLegacy | ||
// https://docs.klaytn.foundation/docs/learn/accounts/ | ||
|
||
const { Web3, TxType, AccountKeyType, getPublicKeyFromPrivate } = require("@klaytn/web3js-ext"); | ||
|
||
// Using legacy AccountKey to execute this example repeatedly. | ||
// But you might want to register a different Accountkey. | ||
const senderAddr = "0xecbf243ac167a3b5097fef758e07881582a89027"; | ||
const senderPriv = "0xc696ccd259792f2ffb87e0012e4a37ae3526a3224686225af679e3aaa2aeab0d"; | ||
|
||
const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net"); | ||
const web3 = new Web3(provider); | ||
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv); | ||
|
||
async function main() { | ||
const tx = { | ||
type: TxType.AccountUpdate, | ||
from: senderAddr, | ||
key: { | ||
type: AccountKeyType.Legacy, | ||
} | ||
}; | ||
|
||
const signResult = await senderAccount.signTransaction(tx); | ||
console.log("signedTx", signResult.transactionHash); | ||
|
||
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction); | ||
console.log("receipt", receipt); | ||
} | ||
|
||
main().catch(console.error); |
Oops, something went wrong.