Bitcoin Forum
April 16, 2024, 08:42:42 AM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: BRAIN21 - A simple Brain Wallet generator in BASH  (Read 70 times)
apogio (OP)
Sr. Member
****
Offline Offline

Activity: 406
Merit: 920



View Profile WWW
March 13, 2024, 09:25:17 PM
Merited by RickDeckard (4), vapourminer (1)
 #1

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:
Code:
sudo apt install base58
sudo apt install xxd
sudo apt install qrencode

The script:
Code:
#! /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:
Code:
touch brainwallet.sh

Copy paste the code and save it. The easiest way is with nano:
Code:
nano brainwallet.sh
<paste the code>
Ctrl+o (save)
Ctrl+x (exit)

Make it executable for the current user:
Code:
chmod u+x brainwallet.sh

Run it:
Code:
./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:
Code:
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 '.

1713256962
Hero Member
*
Offline Offline

Posts: 1713256962

View Profile Personal Message (Offline)

Ignore
1713256962
Reply with quote  #2

1713256962
Report to moderator
It is a common myth that Bitcoin is ruled by a majority of miners. This is not true. Bitcoin miners "vote" on the ordering of transactions, but that's all they do. They can't vote to change the network rules.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713256962
Hero Member
*
Offline Offline

Posts: 1713256962

View Profile Personal Message (Offline)

Ignore
1713256962
Reply with quote  #2

1713256962
Report to moderator
1713256962
Hero Member
*
Offline Offline

Posts: 1713256962

View Profile Personal Message (Offline)

Ignore
1713256962
Reply with quote  #2

1713256962
Report to moderator
1713256962
Hero Member
*
Offline Offline

Posts: 1713256962

View Profile Personal Message (Offline)

Ignore
1713256962
Reply with quote  #2

1713256962
Report to moderator
NotATether
Legendary
*
Offline Offline

Activity: 1568
Merit: 6668


bitcoincleanup.com / bitmixlist.org


View Profile WWW
March 14, 2024, 06:32:32 AM
 #2

Why would anyone want to make a brain wallet in 2024? Huh

I know that there is no practical use for this, but I'm not even sure whether there is any educational value in playing with brain wallets anymore. Unless you want to check how fast brainflayers can swipe any coins you insert in one made from a particular string of text. (No seriously, people are investing a lot of resources into that. Maybe they even have GPUs for that purpose.)

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
apogio (OP)
Sr. Member
****
Offline Offline

Activity: 406
Merit: 920



View Profile WWW
March 14, 2024, 07:17:34 AM
 #3

Why would anyone want to make a brain wallet in 2024? Huh

I know that there is no practical use for this, but I'm not even sure whether there is any educational value in playing with brain wallets anymore. Unless you want to check how fast brainflayers can swipe any coins you insert in one made from a particular string of text. (No seriously, people are investing a lot of resources into that. Maybe they even have GPUs for that purpose.)

I have explained why I implemented it.
A user asked me to do it and it was very easy, so I just posted the solution.
As far as educational value is concerned, well, let's say I couldn't ignore the user who asked me to write the code for that.

Anyway... I couldn't have been more vocal about how nobody should add money to a brain wallet. I even added the warning at the top of the post.



Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!