6

Is there complete solution for console to generate brain wallet? Something like:

echo "sausage" | xxd -r -p | sha256sum | awk '{print $1}' | rmd160 -x | blablabla

I'm read this article: https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses

0-1) I'm skip generation public key from private, and start with it like it was a brain wallet "sausage":

2) - Perform a sha256sum from pubkey:

# echo -n 0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6 \
| xxd -r -p | sha256sum | awk '{print $1}' 
600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408

3) - Perform RIPEMD-160 hashing on the result of SHA-256:

# echo -n 600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408 \
| xxd -r -p | openssl rmd160 
010966776006953d5567439e5e39f86a0d273bee

4) - Add version byte in front of RIPEMD-160 hash (0x00 for Main Network)

# echo 010966776006953d5567439e5e39f86a0d273bee \ 
| sed -e 's/^/00/'  
00010966776006953d5567439e5e39f86a0d273bee

5) - Perform SHA-256 hash on the extended RIPEMD-160 result

# echo -n 00010966776006953D5567439E5E39F86A0D273BEE \
| xxd -r -p | sha256sum | awk '{print $1}'
445c7a8007a93d8733188288bb320a8fe2debd2ae1b47f0f50bc10bae845c094

6) - Perform SHA-256 hash on the result of the previous SHA-256 hash

# echo -n 445C7A8007A93D8733188288BB320A8FE2DEBD2AE1B47F0F50BC10BAE845C094 \
| xxd -r -p | sha256sum | awk '{print $1}'
d61967f63c7dd183914a4ae452c9f6ad5d462ce3d277798075b107615c1a8a30

7) - Take the first 4 bytes of the second SHA-256 hash. This is the address checksum

# echo d61967f63c7dd183914a4ae452c9f6ad5d462ce3d277798075b107615c1a8a30 \
| cut -b 1,2,3,4,5,6,7,8 
d61967f6

Am I correct?

8) - Add the 4 checksum bytes from stage 7 at the end of extended RIPEMD-160 hash from stage 4. This is the 25-byte binary Bitcoin Address.

concatenate

9) - Convert the result from a byte string into a base58 string using Base58Check encoding. This is the most commonly used Bitcoin Address form

I'm take base58 from here: https://github.com/grondilu/bitcoin-bash-tools/blob/master/bitcoin.sh


The result I have is:
declare -a base58=(
      1 2 3 4 5 6 7 8 9
    A B C D E F G H   J K L M N   P Q R S T U V W X Y Z
    a b c d e f g h i j k   m n o p q r s t u v w x y z
)

encodeBase58() {
    dc -e "16i ${1^^} [3A ~r d0<x]dsxx +f" |
    while read -r n; do echo -n "${base58[n]}"; done
}
stage24="$(echo -n 0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6 \
    | xxd -r -p | sha256sum | awk '{print $1}' \
    | xxd -r -p | openssl rmd160 | awk '{print $2}' \
    | sed -e 's/^/00/')"

stage57="$(echo $stage24 | xxd -r -p | sha256sum | awk '{print $1}' \
    | xxd -r -p | sha256sum | awk '{print $1}' \
    | cut -b 1,2,3,4,5,6,7,8)"

# stage 89
echo "1$(encodeBase58 "${stage24}${stage57}")"

Bingo!

16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM

According to Wiki: en.bitcoin.it/wiki/Wallet_import_format

Private Key before importing must be converted the similar way:

declare -a base58=(
      1 2 3 4 5 6 7 8 9
    A B C D E F G H   J K L M N   P Q R S T U V W X Y Z
    a b c d e f g h i j k   m n o p q r s t u v w x y z
)

encodeBase58() {
    dc -e "16i ${1^^} [3A ~r d0<x]dsxx +f" |
    while read -r n; do echo -n "${base58[n]}"; done
}

stage12="$(echo -n "18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725" | sed -e 's/^/80/')"
stage35="$(echo $stage12 | xxd -r -p | sha256sum | awk '{print $1}' | xxd -r -p | sha256sum | awk '{print $1}'  | cut -b 1,2,3,4,5,6,7,8)"

echo "$(encodeBase58 "${stage12}${stage35}")"

Now, I still have an question: How to make brain wallet like "sausage"?

dchapes
  • 1,755
  • 1
  • 12
  • 24
Anomalous Awe
  • 39
  • 1
  • 6

1 Answers1

3

Answer was here: https://en.bitcoin.it/wiki/Wallet_import_format

To make brainwallet need to take a sha256 of password:

# echo -n 'yourbrainwalletpassword' | sha256sum 
8abe468e0d5a814c644d9517ae35b36666d554b7bd682fa28c39e90d0cb5f91a

I've write a little bash script:

declare -a base58=(
      1 2 3 4 5 6 7 8 9
    A B C D E F G H   J K L M N   P Q R S T U V W X Y Z
    a b c d e f g h i j k   m n o p q r s t u v w x y z
)

encodeBase58() {
    dc -e "16i ${1^^} [3A ~r d0<x]dsxx +f" |
    while read -r n; do echo -n "${base58[n]}"; done
}

[ -z "$1" ] && echo "Usage: ./script.sh privkey" && exit 1

privkey="$1"

stage12="$(echo -n "$privkey" | sed -e 's/^/80/')"
stage35="$(echo $stage12 | xxd -r -p | sha256sum | awk '{print $1}' | xxd -r -p | sha256sum | awk '{print $1}'  | cut -b 1,2,3,4,5,6,7,8)"

echo "$(encodeBase58 "${stage12}${stage35}")"

Use this script against sha256 sum:

# bash script.sh 8abe468e0d5a814c644d9517ae35b36666d554b7bd682fa28c39e90d0cb5f91a
5JsPbHfVB7FeD6gGWoaQv6khRsasTJDK1tAiJ4TxgyWJLcjfgNa

Now, I can import privkey to the wallet.dat:

# bitcoind importprivkey 5JsPbHfVB7FeD6gGWoaQv6khRsasTJDK1tAiJ4TxgyWJLcjfgNa "yourbrainwalletpassword" 
# bitcoind getaddressesbyaccount "yourbrainwalletpassword" 
[ 
    "1Gu3HZDq6YEXfxwDC3A3EhDfEVAeCijbEt" 
] 

According to wiki, there are higher and lower borders.

Nearly every 256-bit number is a valid private key. Specifically, any 256-bit number between 0x1 and 0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4141 is a valid private key.

However, I'm successfully import priv key

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 

After a while, bitcoind tell me:

error: {"code":-1,"message":"CKey::GetPrivKey() : i2d_ECPrivateKey failed"}

Hope it would be helpfull.

Thank you !

Anomalous Awe
  • 39
  • 1
  • 6