From 3a79d2b973fc064fbd787b0bb682ee45ff7427bc Mon Sep 17 00:00:00 2001 From: Ling Date: Wed, 12 Jun 2024 17:58:26 +0800 Subject: [PATCH] fix: derive wallets from HD wallet using address_index --- 15_MultiTransfer/MultiTransfer.js | 3 +-- 15_MultiTransfer/readme.md | 3 +-- 16_MultiCollect/MultiCollect.js | 3 +-- 16_MultiCollect/readme.md | 3 +-- en/15_MultiTransfer/MultiTransfer.js | 3 +-- en/15_MultiTransfer/readme.md | 3 +-- en/16_MultiCollect/MultiCollect.js | 3 +-- en/16_MultiCollect/readme.md | 3 +-- 8 files changed, 8 insertions(+), 16 deletions(-) diff --git a/15_MultiTransfer/MultiTransfer.js b/15_MultiTransfer/MultiTransfer.js index 3f1d0f5..2b42972 100644 --- a/15_MultiTransfer/MultiTransfer.js +++ b/15_MultiTransfer/MultiTransfer.js @@ -12,10 +12,9 @@ console.log("\n2. 通过HD钱包派生20个钱包") const numWallet = 20 // 派生路径:m / purpose' / coin_type' / account' / change / address_index // 我们只需要切换最后一位address_index,就可以从hdNode派生出新钱包 -let basePath = "m/44'/60'/0'/0"; let addresses = []; for (let i = 0; i < numWallet; i++) { - let hdNodeNew = hdNode.derivePath(basePath + "/" + i); + let hdNodeNew = hdNode.derivePath(i.toString()); let walletNew = new ethers.Wallet(hdNodeNew.privateKey); addresses.push(walletNew.address); } diff --git a/15_MultiTransfer/readme.md b/15_MultiTransfer/readme.md index b9c3cce..f679127 100644 --- a/15_MultiTransfer/readme.md +++ b/15_MultiTransfer/readme.md @@ -61,10 +61,9 @@ tags: const numWallet = 20 // 派生路径:m / purpose' / coin_type' / account' / change / address_index // 我们只需要切换最后一位address_index,就可以从hdNode派生出新钱包 - let basePath = "m/44'/60'/0'/0"; let addresses = []; for (let i = 0; i < numWallet; i++) { - let hdNodeNew = hdNode.derivePath(basePath + "/" + i); + let hdNodeNew = hdNode.derivePath(i.toString()); let walletNew = new ethers.Wallet(hdNodeNew.privateKey); addresses.push(walletNew.address); } diff --git a/16_MultiCollect/MultiCollect.js b/16_MultiCollect/MultiCollect.js index 102db9c..fb57371 100644 --- a/16_MultiCollect/MultiCollect.js +++ b/16_MultiCollect/MultiCollect.js @@ -31,10 +31,9 @@ console.log("\n2. 通过HD钱包派生20个钱包") const numWallet = 20 // 派生路径:m / purpose' / coin_type' / account' / change / address_index // 我们只需要切换最后一位address_index,就可以从hdNode派生出新钱包 -let basePath = "m/44'/60'/0'/0"; let wallets = []; for (let i = 0; i < numWallet; i++) { - let hdNodeNew = hdNode.derivePath(basePath + "/" + i); + let hdNodeNew = hdNode.derivePath(i.toString()); let walletNew = new ethers.Wallet(hdNodeNew.privateKey); wallets.push(walletNew); console.log(walletNew.address) diff --git a/16_MultiCollect/readme.md b/16_MultiCollect/readme.md index 7baa3bb..732c4f9 100644 --- a/16_MultiCollect/readme.md +++ b/16_MultiCollect/readme.md @@ -67,10 +67,9 @@ tags: const numWallet = 20 // 派生路径:m / purpose' / coin_type' / account' / change / address_index // 我们只需要切换最后一位address_index,就可以从hdNode派生出新钱包 - let basePath = "m/44'/60'/0'/0"; let wallets = []; for (let i = 0; i < numWallet; i++) { - let hdNodeNew = hdNode.derivePath(basePath + "/" + i); + let hdNodeNew = hdNode.derivePath(i.toString()); let walletNew = new ethers.Wallet(hdNodeNew.privateKey); wallets.push(walletNew); console.log(walletNew.address) diff --git a/en/15_MultiTransfer/MultiTransfer.js b/en/15_MultiTransfer/MultiTransfer.js index 1ba6302..4cd6750 100644 --- a/en/15_MultiTransfer/MultiTransfer.js +++ b/en/15_MultiTransfer/MultiTransfer.js @@ -11,10 +11,9 @@ console.log("\n2. Derive 20 wallet addresses from the HD wallet"); const numWallet = 20; // Derivation path: m / purpose' / coin_type' / account' / change / address_index // We only need to change the last part, address_index, to derive new wallets from hdNode -let basePath = "m/44'/60'/0'/0"; let addresses = []; for (let i = 0; i < numWallet; i++) { - let hdNodeNew = hdNode.derivePath(basePath + "/" + i); + let hdNodeNew = hdNode.derivePath(i.toString()); let walletNew = new ethers.Wallet(hdNodeNew.privateKey); addresses.push(walletNew.address); } diff --git a/en/15_MultiTransfer/readme.md b/en/15_MultiTransfer/readme.md index a5035b7..d9630ae 100644 --- a/en/15_MultiTransfer/readme.md +++ b/en/15_MultiTransfer/readme.md @@ -60,10 +60,9 @@ Next, we will write a script that calls the `Airdrop` contract to transfer `ETH` const numWallet = 20; // Derivation path: m / purpose' / coin_type' / account' / change / address_index // We only need to change the last part, address_index, to derive new wallets from hdNode - let basePath = "m/44'/60'/0'/0"; let addresses = []; for (let i = 0; i < numWallet; i++) { - let hdNodeNew = hdNode.derivePath(basePath + "/" + i); + let hdNodeNew = hdNode.derivePath(i.toString()); let walletNew = new ethers.Wallet(hdNodeNew.privateKey); addresses.push(walletNew.address); } diff --git a/en/16_MultiCollect/MultiCollect.js b/en/16_MultiCollect/MultiCollect.js index 9c0d294..4c47bbd 100644 --- a/en/16_MultiCollect/MultiCollect.js +++ b/en/16_MultiCollect/MultiCollect.js @@ -31,10 +31,9 @@ console.log("\n2. Derive 20 wallets from HD wallet") const numWallet = 20 // Derivation path: m / purpose' / coin_type' / account' / change / address_index // We only need to switch the last address_index to derive new wallets from hdNode -let basePath = "m/44'/60'/0'/0"; let wallets = []; for (let i = 0; i < numWallet; i++) { - let hdNodeNew = hdNode.derivePath(basePath + "/" + i); + let hdNodeNew = hdNode.derivePath(i.toString()); let walletNew = new ethers.Wallet(hdNodeNew.privateKey); wallets.push(walletNew); console.log(walletNew.address) diff --git a/en/16_MultiCollect/readme.md b/en/16_MultiCollect/readme.md index 547679c..81fcc9f 100644 --- a/en/16_MultiCollect/readme.md +++ b/en/16_MultiCollect/readme.md @@ -67,10 +67,9 @@ After interacting and playing around with the chain, we need to consolidate and const numWallet = 20 // Derivation path: m / purpose' / coin_type' / account' / change / address_index // We only need to switch the last address_index to derive a new wallet from hdNode - let basePath = "m/44'/60'/0'/0"; let wallets = []; for (let i = 0; i < numWallet; i++) { - let hdNodeNew = hdNode.derivePath(basePath + "/" + i); + let hdNodeNew = hdNode.derivePath(i.toString()); let walletNew = new ethers.Wallet(hdNodeNew.privateKey); wallets.push(walletNew); console.log(walletNew.address)