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 #613 from nohkwak/202404-update-web3js-examples
Browse files Browse the repository at this point in the history
web3js: Separate each usecases
  • Loading branch information
Nehemiah Nohyun Kwak authored May 8, 2024
2 parents 02b75f8 + 41aee82 commit f934b11
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 165 deletions.
3 changes: 0 additions & 3 deletions ethers-ext/example/accountKey/sign_tx_AccountKeyPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ async function main() {
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);
15 changes: 5 additions & 10 deletions ethers-ext/example/accountKey/update_AccountKeyLegacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,20 @@ const { ethers } = require("ethers");

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

// Using senderPriv == senderNewPriv to execute this example repeatedly.
// But you might want to register a different private key.
const senderAddr = "0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7";
const senderPriv = "0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49";
const senderNewPriv = "0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49";
// 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 ethers.providers.JsonRpcProvider("https://public-en-baobab.klaytn.net");
const wallet = new Wallet(senderPriv, provider);

async function main() {
const pub = ethers.utils.computePublicKey(senderNewPriv, true);
console.log("pub", pub);

const tx = {
type: TxType.AccountUpdate,
from: senderAddr,
key: {
type: AccountKeyType.Public,
key: pub,
type: AccountKeyType.Legacy,
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
// AccountKeyLegacy
// https://docs.klaytn.foundation/docs/learn/accounts/

const { Web3, toPeb } = require("@klaytn/web3js-ext");
const { Web3 } = require("@klaytn/web3js-ext");

const senderAddr = "0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7";
const senderPriv = "0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49";
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 sendTx() {
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);
}

async function recoverMsg() {
async function main() {
const msg = "hello";
const msghex = Web3.utils.utf8ToHex(msg);
const signResult = senderAccount.sign(msg);
Expand All @@ -40,8 +25,4 @@ async function recoverMsg() {
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAddr);
}

async function main() {
await sendTx();
await recoverMsg();
}
main().catch(console.error);
30 changes: 30 additions & 0 deletions web3js-ext/example/accountKey/sign_msg_AccountKeyPublic.js
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 web3js-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 { 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);
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);
32 changes: 32 additions & 0 deletions web3js-ext/example/accountKey/sign_tx_AccountKeyLegacy.js
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);
35 changes: 35 additions & 0 deletions web3js-ext/example/accountKey/sign_tx_AccountKeyPublic.js
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 web3js-ext/example/accountKey/sign_tx_AccountKeyRoleBased.js
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);
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);
31 changes: 31 additions & 0 deletions web3js-ext/example/accountKey/update_AccountKeyLegacy.js
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);
Loading

0 comments on commit f934b11

Please sign in to comment.