├── README ├── example.php └── lib ├── bitcoin_address.php └── botg.sh /README: -------------------------------------------------------------------------------- 1 | WARNING: not recommended for use. I don't know if the address generation is secure. 2 | 3 | 4 | Check out example.php to see how to use this in practice. 5 | 6 | This library is really just one function which will return a private key and a public address in an associated array. 7 | 8 | Example result of calling bitcoin_address_pair(): 9 | 10 | Array 11 | ( 12 | [private] => KwFR8a14jDAX5V9N6yXNv9tsbMfJHgiWFgp9L3hTRF5K2kCuVwFB 13 | [public] => 1KzWzshpyzS4Udmr2RLLTaiMNtPTKTRewh 14 | ) 15 | 16 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | \n"; 4 | print_r(bitcoin_address_pair()); -------------------------------------------------------------------------------- /lib/bitcoin_address.php: -------------------------------------------------------------------------------- 1 | $priv, 'public'=>$pub); 9 | } 10 | -------------------------------------------------------------------------------- /lib/botg.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # this was derived from here: https://bitcointalk.org/index.php?topic=23081.20 4 | # I simply slimmed it down so it could be used by a script -RobKohr 5 | 6 | base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z}) 7 | bitcoinregex="^[$(printf "%s" "${base58}")]{34}$" 8 | 9 | decodeBase58() { 10 | local s=$1 11 | for i in {0..57} 12 | do s="${s//${base58}/ $i}" 13 | done 14 | dc <<< "16o0d${s// /+58*}+f" 15 | } 16 | 17 | encodeBase58() { 18 | # 58 = 0x3A 19 | bc <<<"ibase=16; n=${1^^}; while(n>0) { n%3A ; n/=3A }" | 20 | tac | 21 | while read n 22 | do echo -n ${base58[n]} 23 | done 24 | } 25 | 26 | checksum() { 27 | xxd -p -r <<<"$1" | 28 | openssl dgst -sha256 -binary | 29 | openssl dgst -sha256 -binary | 30 | xxd -p -c 80 | 31 | head -c 8 32 | } 33 | 34 | checkBitcoinAddress() { 35 | if [[ "$1" =~ $bitcoinregex ]] 36 | then 37 | h=$(decodeBase58 "$1") 38 | checksum "00${h::${#h}-8}" | 39 | grep -qi "^${h: -8}$" 40 | else return 2 41 | fi 42 | } 43 | 44 | hash160() { 45 | openssl dgst -sha256 -binary | 46 | openssl dgst -rmd160 -binary | 47 | xxd -p -c 80 48 | } 49 | 50 | hash160ToAddress() { 51 | printf "%34s\n" "$(encodeBase58 "00$1$(checksum "00$1")")" | 52 | sed "y/ /1/" 53 | } 54 | 55 | hash256ToAddress() { 56 | #printf "80$1$(checksum "80$1")" 57 | printf "%34s\n" "$(encodeBase58 "80$1$(checksum "80$1")")" | 58 | sed "y/ /1/" 59 | } 60 | 61 | publicKeyToAddress() { 62 | hash160ToAddress $( 63 | openssl ec -pubin -pubout -outform DER | 64 | tail -c 65 | 65 | hash160 66 | ) 67 | } 68 | 69 | privateKeyToWIF() { 70 | hash256ToAddress $(openssl ec -text -noout -in data.pem | head -5 | tail -3 | fmt -120 | sed 's/[: ]//g') 71 | } 72 | 73 | openssl ecparam -genkey -name secp256k1 | tee data.pem &>/dev/null 74 | openssl ec -text -noout -in data.pem | head -5 | tail -3 | fmt -120 | sed 's/[: ]//g' 75 | echo "PrivateKey" 76 | privateKeyToWIF 77 | echo "PublicKey" 78 | openssl ec -pubout < data.pem | publicKeyToAddress 79 | rm data.pem 80 | --------------------------------------------------------------------------------