Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: apogio on March 13, 2024, 09:25:17 PM



Title: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: apogio on March 13, 2024, 09:25:17 PM
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 (https://bitcointalk.org/index.php?topic=5488210.msg63800502#msg63800502) to develop a brain wallet generator in Bash. So, I used most of the code I wrote in BASH21 (https://bitcointalk.org/index.php?topic=5483173) 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 '.


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: NotATether on March 14, 2024, 06:32:32 AM
Why would anyone want to make a brain wallet in 2024? ???

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.)


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: apogio on March 14, 2024, 07:17:34 AM
Why would anyone want to make a brain wallet in 2024? ???

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.




Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: seoincorporation on May 01, 2024, 11:36:40 PM
I was the user who requested this tool, i was curious about how the code would be for bash Linux, and i really love the result. As you already mentioned, there is a big risk on using this kind of tool to generate the wallet, even the fact that the command we use to generate the wallet stay on the command history is a vulnerability. But for teaching proposes is a great tool.

I remember in 2014 when bitcoin price was really low, in the casinos we used to have fun making giveaways with brainwallets, we used to post the phrase on the chat and the users had to get the private key from that passphrase, it used to be fun.

So, thanks again for this tool apogio :D


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: apogio on May 02, 2024, 06:20:53 AM
So, thanks again for this tool apogio :D

You are welcome! Enjoy! Using the tool is fun, indeed!


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: larry_vw_1955 on May 11, 2024, 03:21:08 AM


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.




can that script work in windows? it seems like everything is written for linux. ??? not everybody is using linux. but alot of people use windows.


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: apogio on May 11, 2024, 07:29:06 AM
not everybody is using linux. but alot of people use windows.

I don't consider this an argument to be honest.
Most people use a lot of things that they shouldn't.
If this was an argument, then people shouldn't use Bitcoin because most people use Visa and Mastercard.

I want to show a different path to the world and I believe we should all move to Linux!
I am paid by nobody, which means I develop tools that I like and if people want to use them, they can definitely follow the steps I provide.

1. Unix based operating systems are generally better hardened than Windows. Most linux distros support a lot of free open-source tools to help maintain privacy and security online.
2. Programming is a lot easier on Linux. I am not talking about Bash scripts only, but also about software written in C, C++, Python, Ruby, Java etc.
3. Linux kernel is much more robust than Windows'. It crashes significantly less than Windows.
4. Linux is open-source, which, I support by default!
5. Linux is free! Yeah, I know many people use Windows illegally, but don't forget that Windows is a paid Operating System.
6. Linux is much more lightweight! I think it requires half of the specs that Windows require.

can that script work in windows? it seems like everything is written for linux. ???

But yeah!
It can run on Windows using WSL (https://learn.microsoft.com/en-us/windows/wsl/install).
Imagine how much better Linux is, that Windows added a linux kernel (WSL) inside the OS.


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: larry_vw_1955 on May 12, 2024, 02:29:55 AM

1. Unix based operating systems are generally better hardened than Windows. Most linux distros support a lot of free open-source tools to help maintain privacy and security online.
2. Programming is a lot easier on Linux. I am not talking about Bash scripts only, but also about software written in C, C++, Python, Ruby, Java etc.
3. Linux kernel is much more robust than Windows'. It crashes significantly less than Windows.
4. Linux is open-source, which, I support by default!
5. Linux is free! Yeah, I know many people use Windows illegally, but don't forget that Windows is a paid Operating System.
6. Linux is much more lightweight! I think it requires half of the specs that Windows require.


the problem is that windows is still the defacto standard in the real world and computer hardware is often only compatible with windows. even a programmer who prefers to program using a linux based system probably is required to test it on their windows computer  :o

Quote
But yeah!
It can run on Windows using WSL (https://learn.microsoft.com/en-us/windows/wsl/install).
Imagine how much better Linux is, that Windows added a linux kernel (WSL) inside the OS.


i can't use WSL since it requires windows 10 and i only have windows 7. plus, maybe i'm wrong, but if it's like a 500 MB install, that's like overkill just to be able to run a simple bash script don't you think?


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: ABCbits on May 12, 2024, 09:42:23 AM
I wouldn't use it when warpwallet exist, but overall it's neat tool and also convenient (since i happen to use Linux).



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

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.)

When someone ask this question, i'd quote this webpage.

Brainwallets are not recommended to be used in general because of fallible human memory. But in special situations they could be very useful, for example when fleeing a country as a refugee with only the clothes on your back.

Although i'd also point they better use https://github.com/keybase/warpwallet (https://github.com/keybase/warpwallet) instead.



i can't use WSL since it requires windows 10 and i only have windows 7. plus, maybe i'm wrong, but if it's like a 500 MB install, that's like overkill just to be able to run a simple bash script don't you think?

Yeah, it's definitely overkill for those who don't use Linux. Anyway, VirtualBox is good alternative if you don't mind spare few GB.


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: larry_vw_1955 on May 14, 2024, 01:59:15 AM

Yeah, it's definitely overkill for those who don't use Linux. Anyway, VirtualBox is good alternative if you don't mind spare few GB.

I think I tested VirtualBox once and yes, it was cool but I don't like how it wants to take some of my system RAM. its a RAM hog. and I don't like it. we are talking about simple bash scripts and someone shouldn't have to use gigabytes of disk space and reserve RAM just to run a simple bash script. even if they are on windows. >:(


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: thecodebear on May 19, 2024, 01:22:45 PM

1. Unix based operating systems are generally better hardened than Windows. Most linux distros support a lot of free open-source tools to help maintain privacy and security online.
2. Programming is a lot easier on Linux. I am not talking about Bash scripts only, but also about software written in C, C++, Python, Ruby, Java etc.
3. Linux kernel is much more robust than Windows'. It crashes significantly less than Windows.
4. Linux is open-source, which, I support by default!
5. Linux is free! Yeah, I know many people use Windows illegally, but don't forget that Windows is a paid Operating System.
6. Linux is much more lightweight! I think it requires half of the specs that Windows require.


the problem is that windows is still the defacto standard in the real world and computer hardware is often only compatible with windows. even a programmer who prefers to program using a linux based system probably is required to test it on their windows computer  :o

Quote
But yeah!
It can run on Windows using WSL (https://learn.microsoft.com/en-us/windows/wsl/install).
Imagine how much better Linux is, that Windows added a linux kernel (WSL) inside the OS.


i can't use WSL since it requires windows 10 and i only have windows 7. plus, maybe i'm wrong, but if it's like a 500 MB install, that's like overkill just to be able to run a simple bash script don't you think?


What the guy said about why he wrote it for Linux makes sense. Linux is the defacto standard for programmers. Developers work in Linux unless they are specifically building a Windows product, which he is obviously not doing. And he isn't building a product to sell, he just built this because someone asked for it. So Linux was the obvious choice. If he was building this as a product to sell or as a product to try to get the world to use, obviously also building a Windows compatible version would make sense. But just doing this as a project for someone, it wouldn't really make any sense to choose Windows over Linux unless he happens to specifically be a Windows developer and so felt more comfortable building in Windows. Commercial desktop applications make sense to have a Windows build, but any programming project outside of that narrow scope is likely going to be done in Linux.

Basically, unless you are talking to someone who is specifically a Windows application developer or a Windows build is a requirement of the project, you can assume a programmer is going to build in Linux.


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: Cricktor on May 19, 2024, 08:16:44 PM
Don't waste your time as larry_vw_1955 likely doesn't want to move his ass from Windows and therefore plays the crybaby card. Yes, WSL is a thing, but it also eats some RAM, well, because it's a whole subsystem to run a Linux environment in parallel to Windows.

You can't get all the cherries on the cake for free...

If it's already too much to just boot a Live Linux on larry's computer (which would be a somewhat safe temporary environment for the Brain wallet generator script btw) then why waste your time any further, seriously!


Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: larry_vw_1955 on May 20, 2024, 02:14:15 AM

If it's already too much to just boot a Live Linux on larry's computer (which would be a somewhat safe temporary environment for the Brain wallet generator script btw) then why waste your time any further, seriously!

that would be the absolute last resort. reboot the entire computer just so i can run a single bash script. i'm sure you know of a way i could do it from within windows and NOT using wsl...



Title: Re: BRAIN21 - A simple Brain Wallet generator in BASH
Post by: apogio on May 20, 2024, 03:37:00 PM
Basically, unless you are talking to someone who is specifically a Windows application developer or a Windows build is a requirement of the project, you can assume a programmer is going to build in Linux.

In fact, there is nothing wrong with developing in any environment.

Personally, apart from the coding tutorials, I also try to impart an alternative approach:
1. Linux instead of Windows.
2. Open source, instead of closed-source.
3. Verification instead of trust.

Obviously, I am also learning! The road is full of new adventures!

that would be the absolute last resort. reboot the entire computer just so i can run a single bash script. i'm sure you know of a way i could do it from within windows and NOT using wsl...

If I knew, I would tell you. I don't like keeping secrets. What I said is that I don't really want to spend time trying to find a solution to make it work on windows!