Skip to content

Instantly share code, notes, and snippets.

@gabridome
Last active December 29, 2021 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabridome/3a4f421336a22250d28b94fc66efa08b to your computer and use it in GitHub Desktop.
Save gabridome/3a4f421336a22250d28b94fc66efa08b to your computer and use it in GitHub Desktop.
Estract first 5 addresses from a Ledger according to BIP49 and 84. Requires HWI and pycoin
#!/bin/sh
##
## This script will extract the first 5 normal addresses and the first 5 change addresses
## from a ledger nano S (not tested but should work with any supported hardware wallets)
## To have more addresses it is sufficient to change the
## `ku -j -s0/0-5`part as desired. So if someone wants 100 addresses after the 20th,
## that part will be `ku -j -s0/20-119`.
## Use at your own risk and check few addresses with other applications.
##
ENUMERATE=$(./hwi.py enumerate|jq '.[0]')
FINGERPRINT=$(echo $ENUMERATE|jq -r '.fingerprint')
echo
echo Fingerprint: $FINGERPRINT
DEVICE_PATH=$(echo $ENUMERATE|jq '.path')
echo
echo path: $DEVICE_PATH
TYPE=$(echo $ENUMERATE|jq '.type')
echo
echo type: $TYPE
## BIP49
echo
echo BIP49
echo =====
XPUB49_JSON=$(echo -d $DEVICE_PATH -t $TYPE getxpub m/49H/0H/0H|./hwi.py --stdin)
XPUB49=$(echo $XPUB49_JSON|jq -r '.xpub')
FINGERPRINT49=$(ku -j $XPUB49|jq -r '.fingerprint')
## First 5 addresses according to BIP49
echo
echo xpub according to BIP49: $XPUB49
echo
echo fingerprint of the BIP49 subkey: $FINGERPRINT49
echo
echo Output descriptor normal:
echo
echo "sh(wpkh([$FINGERPRINT/49'/0'/0']$XPUB49/0/*))"
echo
echo first 5 addresses according to BIP49:
echo
ku -j -s0/0-5 $(echo $XPUB49_JSON|jq -r '.xpub')|jq -r '.p2sh_segwit'
echo
echo Output descriptor change:
echo
echo "sh(wpkh([$FINGERPRINT/49'/0'/0']$XPUB49/1/*))"
echo
echo first 5 change addresses according to BIP49:
echo
ku -j -s1/0-5 $(echo $XPUB49_JSON|jq -r '.xpub')|jq -r '.p2sh_segwit'
## BIP84
echo
echo BIP84
echo =====
XPUB84_JSON=$(echo -d $DEVICE_PATH -t $TYPE getxpub m/84H/0H/0H|./hwi.py --stdin)
XPUB84=$(echo $XPUB84_JSON|jq -r '.xpub')
FINGERPRINT84=$(ku -j $XPUB84|jq -r '.fingerprint')
echo
echo xpub according to BIP84: $XPUB84
echo
echo fingerprint of the BIP84 subkey: $FINGERPRINT84
echo
echo Output descriptor normal:
echo
echo "wpkh([$FINGERPRINT/84'/0'/0']$XPUB84/0/*)"
echo
echo first 5 addresses according to BIP84:
echo
ku -j -s0/0-5 $(echo $XPUB84_JSON|jq -r '.xpub')|jq -r '.address_segwit'
echo
echo Output descriptor change:
echo
echo "wpkh([$FINGERPRINT/84'/0'/0']$XPUB84/0/*))"
echo
echo first 5 change addresses according to BIP84:
echo
ku -j -s1/0-5 $(echo $XPUB84_JSON|jq -r '.xpub')|jq -r '.address_segwit'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment