0

I am trying to change the default address used by the wallet to send transactions.

let wallet = Wallet.fromMnemonic('hurdle cloud ...').connect(provider);

wallet.address // 0xfB98c...

How do I change the wallet's address to another address from the same private key? I feel like this should be obvious but digging through the documentation it doesn't appear possible.

Edit

Given that a single private key only has a single public key (and public address), the question needs adjusting. How would it be possible to generate new private keys (and therefore public addresses) from this mnemonic using ethers.js?

4
  • 1
    A given private key has only a single public key and a single public address mapped to it. Commented Mar 4, 2020 at 13:40
  • There are theoretical collisions of course, since the domain of private keys is larger than the domain of public addresses. But finding such a collision is supposed to be computationally impossible. Commented Mar 4, 2020 at 13:41
  • Thanks. Would it then be possible to create new a private key from this same mnemonic to generate a new public address?
    – Nick
    Commented Mar 4, 2020 at 13:46
  • I'm not sure how mnemonics work, but my common sense is that they too are 1-to-1 correspondent with private keys (i.e., each mnemonic yields a single private key). I think that the only idea about them is that they are easier to remember or something like that. But again - I'm honestly not sure about their role in the system. With web3 scripting (and even more-so with smart contract developing), they have no use AFAIK. Commented Mar 4, 2020 at 13:52

1 Answer 1

4

You can use an HDNode which is defined as:

A Hierarchical Deterministic Wallet represents a large tree of private keys which can reliably be reproduced from an initial seed. Each node in the tree is represented by an HDNode which can be descended into.

When you use this HDNode, you can change the path variable you give it in order to get different private/public key pairs that are derived from this HDNode.

You can learn more about the path variable here. For Ethereum wallets, the default address / private key used is at path: m/44'/60'/0'/0/0. To follow the outlined protocol, for ethereum addresses, you can change the account and/or index fields: m/44'/60'/1'/0/0 m/44'/60'/0'/0/1

let HDNode = require('ethers').utils.HDNode;

let mnemonic = "radar blur cabbage chef fix engine embark joy scheme fiction master release";

let masterNode = HDNode.fromMnemonic(mnemonic);

let standardEthereum = masterNode.derivePath("m/44'/60'/0'/0/0");

console.log(standardEthereum.publicKey);
console.log(standardEthereum.privateKey);

//account 0 index 1
let accountZeroIndexOne = masterNode.derivePath("m/44'/60'/0'/0/1");
console.log(accountZeroIndexOne.publicKey);
console.log(accountZeroIndexOne.privateKey);
1
  • 1
    Nice, thanks Steven. Trying to ween myself from web3+HDWalletProvider. This did the trick for me.
    – Cliff Hall
    Commented Apr 8, 2020 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.