coins off the grid (simple script dual address generator)

Forum rules
Warning !
Avoid using binary softwares from untrusted users.
Prefer compiling it yourself and verify sources.
Post Reply
moa
Posts: 255
Joined: Mon May 23, 2011 6:13 am

coins off the grid (simple script dual address generator)

Post by moa »

I was playing around with namecoin/bitcoin address generation (based on botg.sh) and have ended up with this simple shell script that on each run generates a single private key (in hex and wallet import format) and both the associated namecoin and bitcoin addresses for that private key.

It is based on the botg.sh but I have made some mods also, e.g. private key is held in /dev/shm (RAM) instead of on disk. Use at your own risk. Would also be good if someone could double check that the addresses are valid ... I think they are.

Just thought it might be useful to post here for anyone that wants to use/learn with it.

Code: Select all

#!/bin/bash 

# Short script hack to generate off-the-grid EC private key and 
# BOTH the corresponding Bitcoin and Namecoin addresses.
#
# Use on offline computers to create cold, deep storage solutions. 
#
# No guarantees are given or implied. USE AT YOUR OWN RISK!
# Based on botg.sh. v0.0.1.

base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z})
coinregex="^[$(printf "%s" "${base58[@]}")]{34}$"

PRIVKEY='/dev/shm/data.pem'

decodeBase58() {
    local s=$1
    for i in {0..57}
    do s="${s//${base58[i]}/ $i}"
    done
    dc <<< "16o0d${s// /+58*}+f" 
}

encodeBase58() {
    # 58 = 0x3A
    bc <<<"ibase=16; n=${1^^}; while(n>0) { n%3A ; n/=3A }" |
    tac |
    while read 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" =~ $coinregex ]]
    then
        h=$(decodeBase58 "$1")
        checksum "00${h::${#h}-8}" |
        grep -qi "^${h: -8}$"
    else return 2
    fi
}

checkNamecoinAddress() {
    if [[ "$1" =~ $coinregex ]]
    then
        h=$(decodeBase58 "$1")
        checksum "34${h::${#h}-8}" |
        grep -qi "^${h: -8}$"
    else return 2
    fi
}

hash160() {
    openssl dgst -sha256 -binary |
    openssl dgst -rmd160 -binary |
    xxd -p -c 80
}

hash160ToBtcAddress() {
    printf "%34s\n" "$(encodeBase58 "00$1$(checksum "00$1")")" |
    sed "y/ /1/"
}

publicKeyToBtcAddress() {
    hash160ToBtcAddress $(
    openssl ec -pubin -pubout -outform DER |
    tail -c 65 |
    hash160
    )
}

hash160ToNmcAddress() {
    printf "%34s\n" "$(encodeBase58 "34$1$(checksum "34$1")")" |
    sed "y/ /1/"
}

publicKeyToNmcAddress() {
    hash160ToNmcAddress $(
    openssl ec -pubin -pubout -outform DER |
    tail -c 65 |
    hash160
    )
}

hash256ToAddress() {	
	#printf "80$1$(checksum "80$1")"
    printf "%34s\n" "$(encodeBase58 "80$1$(checksum "80$1")")" |
    sed "y/ /1/"
}

privateKeyToWIF() {
    hash256ToAddress $(openssl ec -text -noout -in ${PRIVKEY} | head -5 | tail -3 | fmt -120 | sed 's/[: ]//g')
}

echo " "
echo "COINS OFF-THE-GRID (COTG)"
echo " "
echo "For BEST results:"
echo " "
echo "-run './cotg' from a Live Linux CD"
echo "-run this script with the Internet turned off"
echo "-reboot computer when done"
echo "-never hold unsecured copies of the private key on an on-line computer"
echo "-safely record the private key so it won't get stolen, lost, altered or destroyed"
echo "-if you are not hiding the key, lock it up in a safe or safety deposit box"
echo " "
echo "***COTG's strength is that since the private key is never stored on an on-line computer"
echo "there is nothing for a virus, malware, or spyware to steal.***"
echo " "
echo "Type and/or move the mouse for about 5 minutes. This will help improve the"
echo "randomness of your key....."
echo "Pressing ENTER will continue the script!"

read random

openssl  ecparam -genkey -name secp256k1 | tee ${PRIVKEY} &>/dev/null

echo " "
echo " "
echo "The following is the private key in hex format. Record it carefully."
echo "Record the whole line after a 'read EC key'"
echo " "

hexsize=$(openssl ec -text -noout -in ${PRIVKEY} | head -5 | tail -3 | fmt -120 | sed 's/[: ]//g' ) 

while [ ${#hexsize} -ne 64 ]
do
openssl  ecparam -genkey -name secp256k1 | tee ${PRIVKEY} &>/dev/null && hexsize=$(openssl ec -text -noout -in ${PRIVKEY} | head -5 | tail -3 | fmt -120 | sed 's/[: ]//g' ) 
done

openssl ec -text -noout -in ${PRIVKEY} | head -5 | tail -3 | fmt -120 | sed 's/[: ]//g' 

echo " "
echo " "
echo "Hit ENTER to continue"
read random

echo "The following is the private key in base58, 'Wallet Import Format'." 
echo "Either this or the hex code above could be used but it's"
echo "best to record both for redundancy. These two codes should be"
echo "be kept secret, recorded carefully and stored safely."
echo "Putting them on a computer may lesson the security of these keys."
echo "The private key should begin with '5'."

privateKeyToWIF

echo " "
echo " "
echo "Hit ENTER to continue"
read random

echo "The following is the Bitcoin address for the above private key."
echo "The address should begin with '1'."

openssl ec -pubout -in ${PRIVKEY} | publicKeyToBtcAddress

checkBitcoinAddress

echo " "
echo " "
echo "The following is the Namecoin address for the above private key."
echo "The address should begin with the letter M or N."

openssl ec -pubout -in ${PRIVKEY} | publicKeyToNmcAddress

checkNamecoinAddress

# overwrite key file with a new key and remove from memory.
#

openssl ecparam -genkey -name secp256k1 | tee ${PRIVKEY} &>/dev/null && rm ${PRIVKEY}

echo " "
echo " "
echo "Hit ENTER to exit"
read random
exit 0

Post Reply