The question is basically in the title. Can I (or it is possible to) extract the chain code from a ledger nano s device? It doesn't have to be the Master Chain Code. Any of them will do.
2 Answers
Yes. You can get the public key, chaincode, and address for any derivation path.
If you use the btchip-python
library, you can use the getWalletPublicKey()
method to retrieve a dictionary containing all three of those things.
-
Hey, thanks for the response. How to I get python to interact with the ledger device? Feb 12, 2019 at 19:34
-
You need to use
getDongle
from github.com/LedgerHQ/btchip-python/blob/master/btchip/… to get the device object to do the calls with. Alternatively you can use the HWI project (github.com/bitcoin-core/HWI) to get an xpub from your Ledger and extract the chaincode from that. HWI is a python library and command line tool to do stuff with hardware wallets.– Andrew Chow ♦Feb 12, 2019 at 21:11
Extracting BIP 32/39/44 chain code is agnostic as to whether a Ledger device or Trezor device is used. Remember the chain code is based upon open standards, specifally see https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format.
Here is how chain code can be found for the Bitcoin path m/44'/0'/0'/0 for the example at https://github.com/libbitcoin/libbitcoin-system/wiki/Altcoin-Version-Mappings#7-bitcoin-btc-bip-3944-technology-examples:
This is the level the chain code was determined for below:
% echo "radar blur cabbage chef fix engine embark joy scheme fiction master release" | bx mnemonic-to-seed | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 0 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx base58-decode | cut -c 9-10
04
This is the index the chain code was determined for below:
% echo "radar blur cabbage chef fix engine embark joy scheme fiction master release" | bx mnemonic-to-seed | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 0 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx base58-decode | cut -c 19-26
00000000
This is the chain code for the particular level and index:
% echo "radar blur cabbage chef fix engine embark joy scheme fiction master release" | bx mnemonic-to-seed | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 0 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx base58-decode | cut -c 27-90
6fbe96de86322b27a540572e2777562f55fb9810327917f9dd1163592952272a
This is the associated secp256k1 private key:
% echo "radar blur cabbage chef fix engine embark joy scheme fiction master release" | bx mnemonic-to-seed | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 0 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx base58-decode | cut -c 93-156
c227b3ba34eecfee2830b06bef3b35d5a88d4f6a9c5f34bd3d4e465e6c3ead1f