Skip to content

Instantly share code, notes, and snippets.

@colindean
Last active October 12, 2023 23:45
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save colindean/5239812 to your computer and use it in GitHub Desktop.
Save colindean/5239812 to your computer and use it in GitHub Desktop.
Bitcoin address generator in bash
#!/bin/bash
#
# This is free and unencumbered software released into the public domain.
#
# Requires bc, dc, openssl, xxd
#
# by grondilu from https://bitcointalk.org/index.php?topic=10970.msg156708#msg156708
base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z})
bitcoinregex="^[$(printf "%s" "${base58[@]}")]{34}$"
if [ `uname -s` = 'Darwin' ]; then
TAC="tail -r "
else
TAC="tac"
fi
decodeBase58() {
local s=$1
for i in {0..57}
do s="${s//${base58[i]}/ $i}"
done
dc <<< "16o0d${s// /+58*}+f"
}
encodeBase58() {
# 58 = 0x3A
echo -n "$1" | sed -e's/^\(\(00\)*\).*/\1/' -e's/00/1/g' | tr -d '\n'
dc -e "16i ${1^^} [3A ~r d0<x]dsxx +f" |
while read -r n; do echo -n "${base58[n]}"; done
}
checksum() {
xxd -p -r <<<"$1" |
openssl dgst -sha256 -binary |
openssl dgst -sha256 -binary |
xxd -p -c 80 |
head -c 8
}
checkBitcoinAddress() {
if [[ "$1" =~ $bitcoinregex ]]
then
h=$(decodeBase58 "$1")
checksum "00${h::${#h}-8}" |
grep -qi "^${h: -8}$"
else return 2
fi
}
hash160() {
openssl dgst -sha256 -binary |
openssl dgst -rmd160 -binary |
xxd -p -c 80
}
hash160ToAddress() {
printf "%34s\n" "$(encodeBase58 "00$1$(checksum "00$1")")" |
sed "y/ /1/"
}
publicKeyToAddress() {
hash160ToAddress $(
openssl ec -pubin -pubout -outform DER |
tail -c 65 |
hash160
)
}
echo -n "Public key: "
openssl ecparam -name secp256k1 -genkey | tee priv.pem | openssl ec -pubout | publicKeyToAddress
echo ""
echo -n "Private key: "
cat priv.pem
echo ""
@colindean
Copy link
Author

@b4zz4 Can you clarify what you mean? The Bitcoin address is the public address but not the public key. I think you're wanting to see it output a public key in addition to the Base58 address. That may be possible but I don't really have the bandwidth available to do it.

@b4zz4
Copy link

b4zz4 commented Sep 7, 2021 via email

@colindean
Copy link
Author

Negative.

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