3

I am now implementing HD Wallet using ethers.js library.

And I noticed something strange.

async function getWalletFromMnemonic(mnemonic: string): Promise<Wallet> {
    return ethers.Wallet.fromMnemonic(mnemonic);
}

async function getHDNodeFromMnemonic(mnemonic: string): Promise<HDNode> {
    return ethers.utils.HDNode.fromMnemonic(mnemonic);
}

The above two function returns different address and privateKey from the same mnemonic.

How is this possible? Can you tell me exactly how your wallet and HD Node are conceptually different?

0

1 Answer 1

5

this ethers.Wallet.fromMnemonic(mnemonic) generate only one account

You can read from hd wallet Bip32

The Bitcoin reference client uses randomly generated keys. In order to avoid the necessity for a backup after every transaction, (by default) 100 keys are cached in a pool of reserve keys. Still, these wallets are not intended to be shared and used on several systems simultaneously. They support hiding their private keys by using the wallet encrypt feature and not sharing the password, but such "neutered" wallets lose the power to generate public keys as well.

I think this is the most important advantage:

Deterministic wallets do not require such frequent backups, and elliptic curve mathematics permit schemes where one can calculate the public keys without revealing the private keys. This permits for example a webshop business to let its webserver generate fresh addresses (public key hashes) for each order or for each customer, without giving the webserver access to the corresponding private keys (which are required for spending the received funds).

However, deterministic wallets typically consist of a single "chain" of keypairs. The fact that there is only one chain means that sharing a wallet happens on an all-or-nothing basis. However, in some cases one only wants some (public) keys to be shared and recoverable. In the example of a webshop, the webserver does not need access to all public keys of the merchant's wallet; only to those addresses which are used to receive customer's payments, and not for example the change addresses that are generated when the merchant spends money. Hierarchical deterministic wallets allow such selective sharing by supporting multiple keypair chains, derived from a single root.

There are further improvements BIP44,BIP49, BIP84. you can read

enter image description here

3
  • The wallet created by ethers.Wallet.fromMnemonic is a neutered wallet, and ethers.utils.HDNode.fromMnemonic(mnemonic) is a deterministic wallet. A neutered wallet can hide the private key, but it also loses the power to generate a public key. A deterministic wallet can generate a public key while hiding the private key, thus it can generate a child wallet. Is it correct that I understand? @Yilmaz
    – Primrose
    Dec 18, 2022 at 6:15
  • 1
    correct. from child wallet you can grandchild wallets. I should put an image to be more descriptive
    – Yilmaz
    Dec 18, 2022 at 6:20
  • 1
    You're so kind! Thanks to you, I completely understand.
    – Primrose
    Dec 18, 2022 at 6:31

Your Answer

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

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