Author

Topic: Timestamping a file into bitcoin's block chain (Read 3017 times)

legendary
Activity: 1288
Merit: 1080
January 06, 2011, 01:13:35 PM
#4
Now that I have implemented base58 code in bash, I don't even need to connect to blockexplorer Smiley


Code:
#!/bin/bash
#
# Timestamping program
# Returns a valid bitcoin address made from stdin
# To timestamp, just send 0.01 BTC to this address
#
# Requires bc, openssl, xxd
#

base58=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z})

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 -hex |
    cut -d\  -f2 |
    sed -r "s/^((..){4}).*/\1/"
}

Hash160() {
    openssl dgst -sha256 -binary |
    openssl dgst -rmd160 -hex |
    cut -d\  -f2
}

Hash160ToAddress() {
    printf %34s "$(EncodeBase58 "00$1$(checksum "00$1")")" |
    sed "s/ /1/g"
}

Hash160ToAddress $(Hash160)
Hal
vip
Activity: 314
Merit: 4276
Instead of a file as input, use the hash of a domain name, and you have a quick-n-dirty system for claiming ownership permanently (the name belongs to the key making the payment). Then as suggested in the BitDNS thread, use any common DHT implementation to broadcast the signed DNS records.
administrator
Activity: 5222
Merit: 13032
I made a couple of new Real-Time Stats pages for things like this:
http://blockexplorer.com/q/getreceivedbyaddress
http://blockexplorer.com/q/addressfirstseen

If you use /q/addressfirstseen, you'll be able to make a really nice script for distributed timestamping.
legendary
Activity: 1288
Merit: 1080
Here is a script to timestamp or search for a timestamp in the block chain.

Code:
#!/bin/bash
#
# timestamp.sh : Bitcoin timestamping script
#
# This script timestamps or search timestamps into bitcoin's block chain

blockexplorer="http://blockexplorer.com"

# reads data sent on stdin and hash it
sha256=$(openssl dgst -sha256)

# converts hash into a valid bitcoin address
address=$(wget -O - $blockexplorer/q/hashtoaddress/${sha256::40})

shopt -s extglob
case "$@" in
    -s|--search)
        # just search the bitcoin address in the block chain
        w3m -dump "$blockexplorer/address/$address" ;;

    0.+([0-9])|"")
        fee=${1:-0.01}
        if bitcoind getinfo > /dev/null
        then bitcoind sendtoaddress $address $fee
        else echo "Please check bitcoind is running"
        fi ;;
    *)
        echo "
        usage :  $0 [-s]  [amount]
        options :
        -s|--search : only search for a timestamp in bitcoin's block chain
        " ;;
esac


The first data I've timestamped using this code is this code itself (or more precisely my signature of it).

You can check it by running :

$ bash ./timestamp.sh -s < ./timestamp.sh.asc

where timestamp.sh.asc is :

-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.10 (GNU/Linux)

owFtk7Fv00AUxkMLSD5RRPkLXh1LNIBjlwISroKAgQUQHdiiIJ3tS+7AuTO+c1LU
dmDrgoRUsTFkQDCgMocBBlZ2xMKKBAtsDCw8J3GTILyc7/nd9/3e87tnS4uVheU3
93//3Bye5EcOFmvhCSO6TBvaTeua31l60quueKGQXkg1J1VShdnvEMANYSIl5DQs
ZAd0lInUjNLvcaEn+2mOBpWBZjSL+GxQSKMgHAue0RAmKnoIEadCEjLasK00URnL
GjY3Jg08by5aj1TXJuiZMRpriKmhaCINKAnaxAhJZQwcCwFhiOb0wqXLDWdVpUxq
nUDc0QbccbhWyERK9lhm9ORIAUehRxMRl5BA4zhjWpPJimr9DkOVu+CCMwfnPfIK
GaMmqZ6zPbYKgov+LvpprtLCH9iW6SQqJBHVDGznmo3WBPBx9Y7rjrtWI1YVHuQI
XHaRs3+hoPgrRXimjVZ/vQtunHdTVJ7nO+SavNgbG2Rk69fPrTZ990qrtmPbaNxm
rOFsrwWuX/fXdokl2qVzDFi8kG0FV8GLWc+TeZIQCyHkNKXUBweFiMUSrJJFXIG9
mbCi5Igz5D3Mx/HJcilxrAoiqy1GUGcRZHyKWLmmHYajCI4PTVe3AJq0q3JpWsTC
ngolNQTEmmkfJiuZPC6b18ZppNNBLBr3/ym0bEAGpmlE9m4erSwvVI4fWyguSYVY
p8pr9KteGbz+8eVz/8PXg5f7z1+8v/UnH5rTg8rgKd2Phu8+ffx+/TY5/yp7u7fy
bf0v
=ecN3
-----END PGP MESSAGE-----


PS.  An other version using rmd160 instead of sha256 (so that we don't have to truncate the hash) :

Code:
#!/bin/bash
#
# timestamp.sh : Bitcoin timestamping script
#
# This script timestamps or search timestamps into bitcoin's block chain

blockexplorer="http://blockexplorer.com"

# reads data sent on stdin, hash it, and converts hash into a valid bitcoin address
address=$(wget -q -O - $blockexplorer/q/hashtoaddress/$(openssl dgst -rmd160))

shopt -s extglob
case "$@" in
    -s|--search)
        # just search the bitcoin address in the block chain
        wget -q -O - "$blockexplorer/address/$address" |
        if grep -q "First seen.*Never used in the network"
        then echo no timestamp found; exit 1
        else echo timestamp found : $blockexplorer/address/$address
        fi ;;

    0.+([0-9])|"")
        # timestamps data by sending a small amount to $address
        fee=${1:-0.01}
        if bitcoind getinfo > /dev/null
        then bitcoind sendtoaddress $address $fee
        else echo "Please check bitcoind is running"
        fi ;;
    *)
        # usage information
        echo "
        usage :  $0 [-s]  [amount]
        options :
        -s|--search : only search for a timestamp in bitcoin's block chain
        " ;;
esac
Jump to: