Pages:
Author

Topic: A full shell script implementation of bitcoin ? - page 3. (Read 14134 times)

legendary
Activity: 1232
Merit: 1076
If you manage to figure out that (recomputing the hashes through shell) then that'd be great for everybody. I'm also curious how to do this.
legendary
Activity: 1288
Merit: 1076
Here is my first attempt to recompute the hash of the Genesis block:

Code:
#{
#  "hash":"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
#  "ver":1,
#  "prev_block":"0000000000000000000000000000000000000000000000000000000000000000",
#  "mrkl_root":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
#  "time":1231006505,
#  "bits":486604799,
#  "nonce":2083236893,

ver=1
prev_block=0000000000000000000000000000000000000000000000000000000000000000
mrkl_root=4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
time=1231006505
bits=486604799
nonce=2083236893

printf "%2x%64s%64s%16x%16x%16x" $ver $prev_block $mrkl_root $time $bits $nonce |
sed 's/ /0/g' |
xxd -pr |
openssl dgst -sha256 -binary |
openssl dgst -sha256 -hex |
sed 's/^.* //'

So far it doesn't work.  Any help appreciated.
hero member
Activity: 602
Merit: 512
GLBSE Support [email protected]
What is it with you and bash scripts?  Roll Eyes
Can you do nothing else?


Well actually, no, I can't.  I'm not good at anything else indeed.

I'm not a very good programmer and bash is the only language in which I manage to do usefull stuffs.




Well then I join you, except I use Ruby, and pretty much nothing else, I'm also very slow.
legendary
Activity: 1288
Merit: 1076
What is it with you and bash scripts?  Roll Eyes
Can you do nothing else?


Well actually, no, I can't.  I'm not good at anything else indeed.

I'm not a very good programmer and bash is the only language in which I manage to do usefull stuffs.


hero member
Activity: 602
Merit: 512
GLBSE Support [email protected]
What is it with you and bash scripts?  Roll Eyes
Can you do nothing else?


I think the scripts are rather useful. They are far more portable than having to mess with wxwidgets or whatever the hell else just to get what should be a simple console application to work. They are also easy to understand.

I would love to see many perl, python, and shell implementations of bitcoin.

I'm sure they can be useful, that's not what I'm saying. I'm saying that everything Grondilu does is in bash, or at least everything he has proposed is in bash. I am sure that he's a bash wizard at this point though.
sr. member
Activity: 252
Merit: 250
What is it with you and bash scripts?  Roll Eyes
Can you do nothing else?


I think the scripts are rather useful. They are far more portable than having to mess with wxwidgets or whatever the hell else just to get what should be a simple console application to work. They are also easy to understand.

I would love to see many perl, python, and shell implementations of bitcoin.
hero member
Activity: 602
Merit: 512
GLBSE Support [email protected]
What is it with you and bash scripts?  Roll Eyes
Can you do nothing else?
legendary
Activity: 1288
Merit: 1076

I know a bit about this software and I think it's pretty cool.  I really would like to see an implementation of bitcoin using this as a database handler.


One of the reason why I think it would fit bitcoin nicely is the fact that mongodb uses JSON directly to represent data.   An other reason is that it is very easy to connect to an other mongodb on the net.  Thus, the network handling would be easy.

Also, it's very easy to handle from a bash script.


www.mongodb.org
legendary
Activity: 1288
Merit: 1076
I have modified dirtyfilthy's program so that it can export the whole wallet in an ascii format.

http://github.com/grondilu/bc_key


Just use "ALL" instead of a particular bitcoin address.

Be careful with this command, for it shows all your private keys in clear !  In particular, you should never redirect the output of this program to a file.  Unless you use an encrypted file system.

Anyway, I'll add a few bash functions that can be used to create a new address, sign a transaction, and so on...

Here is a function to create a new address already :

Code:
#!/bin/bash

. base58.sh

wallet="$HOME/.bitcoin-bash/wallet.dat"

generateNewAddress() {

    privkey="$(openssl ecparam -name secp256k1 -genkey)"

    openssl ec -pubout <<<"$privkey" 2>&- |
    publicKeyToAddress 2>&- |
    if [[ -f "$wallet" ]]
    then
        tee -a "$wallet"  # storing and showing the address
        printf "%s" "$privkey" >> "$wallet"  # storing the private key
    else
        # showing address and private key
        cat
        printf "%s" "$privkey"
    fi

}
legendary
Activity: 1288
Merit: 1076
I've added two functions : decodeBase68 and checkBitcoinAddress.

I think it might be usefull to check the validity of a bitcoin address in pure bash.

Requires "dc", the unix desk calculator.  Few people use this reverse polish notation calcultor but it's much easier to handle in scripts.


Code:
#!/bin/bash
#
# Requires bc, dc, openssl, xxd
#

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

decodeBase58() {
    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 -hex |
    sed 's/^.* //' |
    head -c 8
}

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

hash160() {
    openssl dgst -sha256 -binary |
    openssl dgst -rmd160 -hex |
    sed 's/^.* //'
}

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

publicKeyToAddress() {
    hash160ToAddress $(
    openssl ec -pubin -pubout -outform DER 2>/dev/null |
    tail -c 65 |
    hash160
    )
}
sr. member
Activity: 360
Merit: 250
I'd start off with strict sh compatibility, though, not bash, unless there's really a big advantage of requiring bash over lighter posix-compliant sh implementations (e.g. dash).

Not that I've got any skin in this game, but it would be worth considering targeting the more limited toolset offered by Busybox:

http://busybox.net/

The profusion of embedded linux devices these days means Busybox is everywhere.
legendary
Activity: 1288
Merit: 1076
I'd start off with strict sh compatibility, though, not bash, unless there's really a big advantage of requiring bash over lighter posix-compliant sh implementations (e.g. dash).

Writing in POSIX is much less fun.  To me GNU is a better standard de facto.

Also, what's the point of those excellent GNU tools if we can never use them ?
sr. member
Activity: 288
Merit: 263
Firstbits.com/1davux
I like the idea: having a series of small tools that do one task, do it well, and can work together through stdin/stdout, the filesystem, sockets, etc.

I'd start off with strict sh compatibility, though, not bash, unless there's really a big advantage of requiring bash over lighter posix-compliant sh implementations (e.g. dash).
legendary
Activity: 1288
Merit: 1076
Yes !  It works  Cheesy

Code:
#!/bin/bash
#
# 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
}

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

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
}

PubKeyToAdress() { Hash160ToAddress $(tail -c 65 |Hash160) ; }
legendary
Activity: 1288
Merit: 1076

I'm trying to implement the base58 functions.  I'm almost there but I have difficulties with the checksum.


Code:
#!/bin/bash

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
}

Hash160ToAddress() {
    ADDRESSVERSION=00
    EncodeBase58 "${ADDRESSVERSION}$1$(Checksum "$1")"
}

Checksum() {
    xxd -p -r <<<"$1" |
    openssl dgst -sha256 -binary |
    openssl dgst -sha256 -hex |
    cut -d\  -f2 |
    sed -r "s/^.*((..){4})/\1/"
}

Hash160() {
    echo -n "$1" |
    openssl dgst -sha256 -binary |
    openssl dgst -rmd160 -hex |
    cut -d\  -f2
}

H=0057b0dc5aac7c215a9a458d6c3c85cd21089af8
Hash160ToAddress $H
# I should get     112p3sLidyEptFEfx3C2RCvFoRPK89HyBT
# I actually get     2p3sLidyEptFEfx3C2RCvFoRPK7JQWdq
pj
newbie
Activity: 24
Merit: 0
Could anyone educate me about the interest of coding a bitcoin client in shell script ? (protip : "just for fun" is a valid answer:) )

Someone else asked the question, so I can just guess from what they said.

My guess is that the interest is not so much in coding the guts of the client in shell script (not exactly a high performance engine for computationally intensive tasks), but rather in exposing the data flows and stores to shell script manipulation.

In other words, as I have often done myself in other tasks, write several tools that each have specific capabilities, and which have interfaces that work well when combined in shell scripts with each other and other *nix utilities.  One shot commands are handled in that environment by forking/execing a command that is passed command line arguments, environment variables and files or file descriptors to read or write.  Higher performance for repetitive tasks is obtained by using commands in a series of filters, operating on a stream of line oriented text.  The new, individual tools would each be written in C or Python or some other such language that provided adequate performance and library support for their particular purpose.
legendary
Activity: 1372
Merit: 1007
1davout
Could anyone educate me about the interest of coding a bitcoin client in shell script ? (protip : "just for fun" is a valid answer:) )
pj
newbie
Activity: 24
Merit: 0
Shell script would be fine for a connect-work-disconnect method of working.

But it is grossly ineffective for maintaining a long-running P2P network node, where long-lasting TCP connections are preferred.
One could work around that inefficiency in at least a couple of ways:
 1) Background a task that has a file descriptor open back to the active shell, that can manipulate and communicate with sockets, or
 2) Use a shell that provides this builtin, such as the net/socket module in zsh as described at  http://www.cims.nyu.edu/cgi-systems/info2html?%28zsh%29The%2520zsh%2Fnet%2Fsocket%2520Module
legendary
Activity: 1708
Merit: 1007
Shell script would be fine for a connect-work-disconnect method of working.


There is a growing demand for a client that can function disconnected from the Internet.  This might be a step in this direction.
legendary
Activity: 1596
Merit: 1091
also, Kademlia network works fine without kickstarters and other things like this

Do you mean bootstrapping?  Kademlia network needs bootstrapping, just like other P2P networks.
Pages:
Jump to: