Skip to content

Instantly share code, notes, and snippets.

@JBaczuk
Created December 15, 2018 20:00
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save JBaczuk/dab51daf6280f6a2d4b58897d6b36e9b to your computer and use it in GitHub Desktop.
Save JBaczuk/dab51daf6280f6a2d4b58897d6b36e9b to your computer and use it in GitHub Desktop.
addrgen
#!/bin/bash
echo
echo "Welcome to the Bitcoin address generator!"
echo "input private key (32 bytes, hex format)"
read priv
echo ""
echo "#####################################"
# priv=0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D # Testing only
## Calculate WIFs
# 0x80 for mainnet, 0xef for testnet
pub_suffix=01 # append 01 if the corresponding public key is compressed
testnet_prefix=ef
mainnet_prefix=80
ext_priv_testnet=$testnet_prefix$priv
ext_priv_mainnet=$mainnet_prefix$priv
wif_enc_testnet=$(printf "$ext_priv_testnet" | xxd -r -p | base58 -c)
wif_enc_mainnet=$(printf "$ext_priv_mainnet" | xxd -r -p | base58 -c)
wif_enc_testnet_compressed=$(printf "$ext_priv_testnet$pub_suffix" | xxd -r -p | base58 -c)
wif_enc_mainnet_compressed=$(printf "$ext_priv_mainnet$pub_suffix" | xxd -r -p | base58 -c)
#echo "WIF Testnet: "$wif_enc_testnet
#echo "WIF Mainnet: "$wif_enc_mainnet
echo "WIF Testnet (Compressed): "$wif_enc_testnet_compressed
echo "WIF Mainnet (Compressed): "$wif_enc_mainnet_compressed
# note the test priv should return 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ
## Calculate Public Keys
public_key=$(openssl ec -inform DER -text -noout -in <(cat <(echo -n "302e0201010420") <(echo -n $priv) <(echo -n "a00706052b8104000a") | xxd -r -p) 2>/dev/null | tail -6 | head -5 | sed 's/[ :]//g' | tr -d '\n' && echo)
echo "Public Key: "$public_key
x_coord=$(printf $public_key | cut -c -66 | cut -c 3-)
last_byte=$(printf $public_key | cut -c 129-)
last_int=$(printf "%d" 0x$last_byte)
is_odd=$(expr $last_int % 2)
if [ "$is_odd" == 1 ]; then
compressed_public_key=03$x_coord
else
compressed_public_key=02$x_coord
fi
echo "Compressed Public Key: "$compressed_public_key
## Create P2PKH address from uncompressed public key
prefix_bitcoin_mainnet=00
prefix_bitcoin_testnet=6f
hash=$(printf $public_key | xxd -r -p | openssl sha256 | cut -c 10-)
hash2_uncompressed=$(printf $hash | xxd -r -p | openssl ripemd160 | cut -c 10-)
p2pkh_mainnet=$(printf $prefix_bitcoin_mainnet$hash2_uncompressed | xxd -r -p | base58 -c)
p2pkh_testnet=$(printf $prefix_bitcoin_testnet$hash2_uncompressed | xxd -r -p | base58 -c)
OP_DUP=76
OP_HASH160=a9
bytes_to_push=14
OP_EQUALVERIFY=88
OP_CHECKSIG=ac
scriptPubKey=$OP_DUP$OP_HASH160$bytes_to_push$hash2$OP_EQUALVERIFY$OP_CHECKSIG
#echo "P2PKH Main Net Address: "$p2pkh_mainnet
#echo "P2PKH Test Net Address: "$p2pkh_testnet
#echo "scriptPubKey: "$scriptPubKey
## Create P2PKH address from compressed public key
hash=$(printf $compressed_public_key | xxd -r -p | openssl sha256 | cut -c 10-)
hash2=$(printf $hash | xxd -r -p | openssl ripemd160 | cut -c 10-)
p2pkh_mainnet=$(printf $prefix_bitcoin_mainnet$hash2 | xxd -r -p | base58 -c)
p2pkh_testnet=$(printf $prefix_bitcoin_testnet$hash2 | xxd -r -p | base58 -c)
scriptPubKey=$OP_DUP$OP_HASH160$bytes_to_push$hash2$OP_EQUALVERIFY$OP_CHECKSIG
echo "P2PKH Main Net Address (Compressed): "$p2pkh_mainnet
echo "P2PKH Test Net Address (Compressed): "$p2pkh_testnet
echo "scriptPubKey (Compressed): "$scriptPubKey
## Create P2SH-P2WPKH from uncompressed key
prefix_bitcoin_mainnet=05
prefix_bitcoin_testnet=c4
hash_uncompressed=$(printf 0014$hash2_uncompressed | xxd -r -p | openssl sha256 | cut -c 10-)
hash2_uncompressed=$(printf $hash_uncompressed | xxd -r -p | openssl ripemd160 | cut -c 10-)
p2sh_p2pkh_mainnet=$(printf $prefix_bitcoin_mainnet$hash2_uncompressed | xxd -r -p | base58 -c)
p2sh_p2pkh_testnet=$(printf $prefix_bitcoin_testnet$hash2_uncompressed | xxd -r -p | base58 -c)
OP_EQUAL=87
scriptPubKey=$OP_HASH160$bytes_to_push$hash2$OP_EQUAL
#echo "P2SH-P2PKH Main Net Address: "$p2sh_p2pkh_mainnet
#echo "P2SH-P2PKH Test Net Address: "$p2sh_p2pkh_testnet
#echo "scriptPubKey: "$scriptPubKey
## Create P2SH-P2WPKH from compressed key
hash=$(printf 0014$hash2 | xxd -r -p | openssl sha256 | cut -c 10-)
hash2=$(printf $hash | xxd -r -p | openssl ripemd160 | cut -c 10-)
p2sh_p2pkh_mainnet=$(printf $prefix_bitcoin_mainnet$hash2 | xxd -r -p | base58 -c)
p2sh_p2pkh_testnet=$(printf $prefix_bitcoin_testnet$hash2 | xxd -r -p | base58 -c)
scriptPubKey=$OP_HASH160$bytes_to_push$hash2$OP_EQUAL
echo "P2SH-P2PKH Main Net Address (Compressed): "$p2sh_p2pkh_mainnet
echo "P2SH-P2PKH Test Net Address (Compressed): "$p2sh_p2pkh_testnet
#echo "scriptPubKey (Compressed): "$scriptPubKey
@AutocorrectAll
Copy link

"01000000000103971b0728993059975efa8058b0a335df0a680b9b5fb69d4586aee35b4d453c5b0100000000fffffffffae3973f06bf1486e0826c74e4f9ae4489938af3cdb553024dae1ffbf97d1a270100000000ffffffffa5dbdb823ecf5f0babba1c5c940aa0f3c3c29393f7bb512f578ac9ea7d6225bf0100000000ffffffff020003164e0200000017a914afa4b2e2fcc433c168328d9d1491cf67620c696787bfd1816401000000160014d958c2cf8e4e58d20da0667d66c67fb246609441024730440220383b1ec22b80f7f864a2e455bcb239e8059a05e57171afbc5ae6105cec9aabe90220661387fe511ab39cb637482900fde448ba261d0b9eb0fda77973d8228bb2442501210295e9714c0091ec49edaad625d039f1481ee7f85dd8ebb4d5d528e0f689353aae02483045022100a9f95854dd2baea15825cc628eb4ef3ba803e85e5f661ea0774586d011831d0d02202dc3f74deb7936b8d573f84838088fb7495eaea0826568ba75f389e82e6803e001210295e9714c0091ec49edaad625d039f1481ee7f85dd8ebb4d5d528e0f689353aae024730440220533e5d2d29160dd7e5f8187a51d91b1efb3d3f98e0c96e6866691b8bc1f1202a022039a03d95b5d65e4327ca18bf977b04f07eb5324a1fa2260ebac9d107c5da4d9301210295e9714c0091ec49edaad625d039f1481ee7f85dd8ebb4d5d528e0f689353aae00000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment