Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to export Xpub Key from Ledger Nano S Ethereum Wallet

I need to give payment ETH address to my customers for deposit ETH their accounts. I want to use a HD ETH wallet for this and I am using Ledger Nano S now. But Ledger showing me only 1 receive address so I need ETH wallet's XPub for generate many address from it for distribute to users.

If Ledger support HD, How can I export XPub? If ledger does not support, Which wallet can usable for this purpose.

Any suggestions.

like image 696
Savas Adar Avatar asked Oct 16 '22 21:10

Savas Adar


1 Answers

You can't export the xpub directly from a ledger nano s with the default bitcoin or ethereum apps.

But you can construct an xpub using the data you can extract from the ledger.

What you need is the public key and chaincode for the bip32 path you're interested in and the public key of the parent of that path.

In the ethereum app you would use getaddress (currently there is a restriction to m/44/60', m/44'/61', and m/44'/1' and below for this app (I have an issue here to hopefully remove that restriction). In the bitcoin app you would use getwalletpublickey. There is no bip32 path restrictions in the bitcoin app.

Once you have the two public keys and the chain code, you can construct the xpub. Here is a chunk of python that can accomplish this. It uses the un-maintained pybitcointools, so user-beware.

#!/usr/bin/env python

import sys
import binascii
from bitcoin import bin_hash160
from bitcoin import bip32_serialize
from bitcoin import compress

def main(parent_pubkey, pubkey, chaincode, depth, index, network):
    if (network.lower() == "main"):
        network_bytes = b'\x04\x88\xb2\x1e'
    elif (network.lower() == "test"):
        network_bytes = b'\x04\x35\x87\xcf'
    else:
        sys.exit("network must be either main or test")

    compressed_pubkey_bytes = binascii.unhexlify(compress(pubkey))
    fingerprint_bytes =  bin_hash160(binascii.unhexlify(compress(parent_pubkey)))[:4]
    chaincode_bytes = binascii.unhexlify(chaincode)

    deserialized = (network_bytes, int(depth), fingerprint_bytes, int(index), chaincode_bytes, compressed_pubkey_bytes)
    xpub = bip32_serialize(deserialized)
    print(xpub)

if __name__ == '__main__':
    if (len(sys.argv) < 7):
        sys.exit("USAGE: generate_xpub PARENT_PUBLICKEY PUBLICKEY CHAINCODE DEPTH INDEX (MAIN|TEST)")
    main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6])
like image 189
Destry Avatar answered Nov 02 '22 04:11

Destry