Warning:I am placing it here, at the top, to make sure you will see it. Use this script only for fun. The human brain is by orders of magnitude inferior to the dumbest computer when generating entropy (randomness).
Background:I was
asked to develop a brain wallet generator in Bash. So, I used most of the code I wrote in
BASH21 and I slightly changed it to take a phrase as command line argument, to use it to generate the wallet.
Prerequisites:sudo apt install base58
sudo apt install xxd
sudo apt install qrencode
The script:#! /bin/bash
###############################################
################# FUNCTIONS ###################
###############################################
calculate_checksum(){
prefix=$1
value=$2
suffix=$3
s1=$(echo -n "${prefix}${value}${suffix}" | xxd -r -p | openssl sha256 | awk '{print $2}')
s2=$(echo -n ${s1} | xxd -r -p | openssl sha256 | awk '{print $2}')
checksum=$(echo ${s2} | head -c 8)
echo ${checksum}
}
hash_160(){
input=$1
sha=$(echo -n ${input} | xxd -r -p | openssl sha256 | awk '{print $2}')
echo -n ${sha} | xxd -r -p | openssl ripemd160 | awk '{print $2}'
}
generate_p2pkh(){
hash160=$1
checksum=$(calculate_checksum "00" ${hash160} "")
echo -n "00${hash160}${checksum}" | xxd -r -p | base58
}
generate_p2sh(){
input=$1
hash160=$(hash_160 "0014${input}")
checksum=$(calculate_checksum "05" ${hash160} "")
echo -n "05${hash160}${checksum}" | xxd -r -p | base58
}
print_keys(){
echo "Entropy: "$1
echo "PK: "$2
echo "WIF: "$3
echo "Public Key: "$4
echo "Compressed Public Key: "$5
echo "HASH160: "$6
echo "Legacy Address: "$7
echo "Segwit Address: "$8
}
print_qr_codes(){
qrencode -s 6 -l M -o legacy_address.png $1
qrencode -s 6 -l M -o segwit_address.png $2
}
###############################################
################# MAIN ########################
###############################################
# CONVERT ENTROPY TO WIF KEY
entropy=$1
pk=$(echo -n ${entropy} | openssl sha256 | awk '{print $2}')
checksum=$(calculate_checksum "80" ${pk} "01")
wif=$(echo -n "80${pk}01${checksum}" | xxd -r -p | base58)
# CONVERT PRIVATE KEY TO COMPRESSED PUBLIC KEY USING OPENSSL SECP256K1
public_key=$(openssl ec -inform DER -text -noout -in <(cat <(echo -n "302e0201010420") <(echo -n ${pk}) <(echo -n "a00706052b8104000a") | xxd -r -p) 2>/dev/null | tail -6 | head -5 | sed 's/[ :]//g' | tr -d '\n' && echo)
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
# CONVERTING PUBLIC KEY TO COMPRESSED LEGACY ADDRESS
hash160=$(hash_160 ${compressed_public_key})
legacy_address=$(generate_p2pkh ${hash160})
segwit_address=$(generate_p2sh ${hash160})
# PRINT DATA
print_keys "${entropy}" ${pk} ${wif} ${public_key} ${compressed_public_key} ${hash160} ${legacy_address} ${segwit_address} > data.txt
print_qr_codes ${legacy_address} ${segwit_address}
Usage:Create a .sh script file anywhere on your computer:
touch brainwallet.sh
Copy paste the code and save it. The easiest way is with nano:
nano brainwallet.sh
Ctrl+o (save)
Ctrl+x (exit)
Make it executable for the current user:
chmod u+x brainwallet.sh
Run it:
./brainwallet.sh 'apogio created a brainwallet generator using bash'
Execution results:1. A file
data.txt which includes the sensitive data (keys etc.) of the wallet.
2. A file
legacy_address.png which displays a QR code for the wallet's legacy (P2PKH) address.
3. A file
segwit_address.png which displays a QR code for the wallet's segwit (P2WPKH-P2SH) address.
data.txt file format:Entropy: apogio created a brainwallet generator using bash
PK: 913fc1abf77ae447c662cbd14a0803e519df65f8c40b3bcb20a911f0f31091dc
WIF: L264Cp6WU73fzmQCvJ8Te2EazXTr3A17yAC13NQDQBwQvyUAaiG3
Public Key: 04582ed090da2d4e4fda943923910a0720391a9903fa5259aa9d50cf3710ed40bbc6ce378a86ab86f2b2d6635e8797e9c4fa2021eff4f57942c22395d7ad1afe83
Compressed Public Key: 03582ed090da2d4e4fda943923910a0720391a9903fa5259aa9d50cf3710ed40bb
HASH160: 8ef81d4f19a7f284e68b32dd58931c6817ceb275
Legacy Address: 1E2xBY8kVhGgNZuRK8RwbvimpeW1E6DPat
Segwit Address: 3MpZWJr5ct3Y4zEeSmbA1R17vj2RrRhfNw
Some notes:1. I don't encrypt the sensitive data, like I did in BASH21. It's one more way, from my side, to convince you that this script should be used only for fun.
2. Make sure to use single quotes to include the phrase. Otherwise Bash will think that each word is a separate command line argument and the results will be totally unexpected.
3. Make sure to remember that in brain wallets, every character matters. Thus, 'I am the best' is different from 'i am the best', or from 'I am the best '.