Bitcoin Forum
April 26, 2024, 03:30:02 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: « 1 2 [3] 4 »  All
  Print  
Author Topic: A full shell script implementation of bitcoin ?  (Read 14132 times)
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1076


View Profile
March 01, 2011, 10:04:56 PM
 #41

How about

Code:
{ bigEndianHex2littleEndianHex <<<"$a" | xxd -p -r ;
   bigEndianHex2littleEndianHex <<<"$b" | xxd -p -r ; } \
   | openssl dgst -sha256 -binary \
   | openssl dgst -sha256 -hex \
   | bigEndianHex2littleEndianHex

That would give the third hash, wouldn't it?

It does indeed!   But you have to get rid of the annoying 'std(...)= ':

I didn't know that there were actually 2 sha256 passes, just like for the hash of the block itself.

Code:
{ bigEndianHex2littleEndianHex <<<"$a" | xxd -p -r ;
   bigEndianHex2littleEndianHex <<<"$b" | xxd -p -r ; }  |
   openssl dgst -sha256 -binary |
   openssl dgst -sha256 -hex |
   sed 's/.* //' |
   bigEndianHex2littleEndianHex

(also, you can use '|' at the end of line without using '\')

Now how to deal with blocks with a number of transactions that are not a power of 2?

1714102202
Hero Member
*
Offline Offline

Posts: 1714102202

View Profile Personal Message (Offline)

Ignore
1714102202
Reply with quote  #2

1714102202
Report to moderator
1714102202
Hero Member
*
Offline Offline

Posts: 1714102202

View Profile Personal Message (Offline)

Ignore
1714102202
Reply with quote  #2

1714102202
Report to moderator
"I'm sure that in 20 years there will either be very large transaction volume or no volume." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
kseistrup
Hero Member
*****
Offline Offline

Activity: 566
Merit: 500


Unselfish actions pay back better


View Profile WWW
March 01, 2011, 10:18:33 PM
 #42

But you have to get rid of the annoying 'std(...)= ':

Huh?  I don't get you…

Quote
I didn't know that there were actually 2 sha256 passes, just like for the hash of the block itself.

Neither did I, but it was worth a try.

Quote
(also, you can use '|' at the end of line without using '\')

(I know, but I think explicit line continuations are more legible.)

Quote
Now how to deal with blocks with a number of transactions that are not a power of 2?

You tell me. Wink

Cheers,

Klaus Alexander Seistrup
TiagoTiago
Hero Member
*****
Offline Offline

Activity: 616
Merit: 500


Firstbits.com/1fg4i :)


View Profile
March 03, 2011, 08:07:08 PM
 #43

(just subscribing to the thread)

(I dont always get new reply notifications, pls send a pm when you think it has happened)

Wanna gimme some BTC/BCH for any or no reason? 1FmvtS66LFh6ycrXDwKRQTexGJw4UWiqDX Smiley

The more you believe in Bitcoin, and the more you show you do to other people, the faster the real value will soar!

Do you like mmmBananas?!
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1076


View Profile
March 03, 2011, 08:32:34 PM
 #44

But you have to get rid of the annoying 'std(...)= ':

Huh?  I don't get you…

Well, when I run openssl dgst -sha1 -hex <<<"hello, world", I get an annoying:

(stdin)= cd50d19784897085a8d0e3e413f8612b097c03f1


I wish there was an option not to display this "(...)= " before my result.  Anyway that's why I had to add a 'sed "s/^.* //"'.

kseistrup
Hero Member
*****
Offline Offline

Activity: 566
Merit: 500


Unselfish actions pay back better


View Profile WWW
March 03, 2011, 08:42:27 PM
 #45

Well, when I run openssl dgst -sha1 -hex <<<"hello, world", I get an annoying:

(stdin)= cd50d19784897085a8d0e3e413f8612b097c03f1


I wish there was an option not to display this "(...)= " before my result.  Anyway that's why I had to add a 'sed "s/^.* //"'.

Ah, this doesn't happen here — I only see that SHA256 digest:

Code:
$ openssl dgst -sha256 -hex <<< "hello, world"
853ff93762a06ddbf722c4ebe9ddd66d8f63ddaea97f521c3ecc20da7c976020
$

Cheers,

Klaus Alexander Seistrup
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1076


View Profile
March 03, 2011, 08:53:35 PM
 #46

Code:
$ openssl dgst -sha256 -hex <<< "hello, world"
853ff93762a06ddbf722c4ebe9ddd66d8f63ddaea97f521c3ecc20da7c976020
$

Cheers,

different version of openssl I guess.

$ openssl version
OpenSSL 1.0.0c 2 Dec 2010

kseistrup
Hero Member
*****
Offline Offline

Activity: 566
Merit: 500


Unselfish actions pay back better


View Profile WWW
March 03, 2011, 09:02:16 PM
 #47

different version of openssl I guess.

$ openssl version
OpenSSL 1.0.0c 2 Dec 2010

Yes, I'm running OpenSSL 0.9.8k 25 Mar 2009 (from Ubuntu Lucid).

Cheers,

Klaus Alexander Seistrup
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1076


View Profile
March 04, 2011, 07:25:33 PM
 #48

Here is the Merkle tree for block 95002, which had three transactions:

Code:
"mrkl_tree":[
"4fa598026d9be1ca0c2bff1b531e566b7ea7f90b72e75fda0c1795bc2dfa375c",
"186640daf908156e2616790d7c816235b0c43f668c3c38351b348c08ca44d457",
"ef3928700309b4deceac9a992a19a7481b4e520cbc0b1ab74e2645eee39c8da0",
"688c53517f62f7a65c0e87519c18a4de98f2ccafbf389b269d0bb867f88d166a",
"01889506f7fe9210045f588361881e2d16a034a62bc48ebd7b6b0a3edeaf5a6d",
"74f3a7df861d6a58957b84a3e425a8cf57e1e2e3a3def046dd200baeb8714f00"
]

I rewrote the two bash functions:

Code:
bigEndianHex2littleEndianHex() {
    local s=''
    while read -n 2 char
        do s=$char$s
    done
    echo $s
}

bitcoinHash() {
    bigEndianHex2littleEndianHex |
    xxd -p -r |
    openssl dgst -sha256 -binary |
    openssl dgst -sha256 -binary |
    xxd -p -c 80 |
    bigEndianHex2littleEndianHex
}


The fourth entry of the Merkle tree is easy to reconstitute.  It's obviously made out of the first two.

bitcoinHash <<<"186640daf908156e2616790d7c816235b0c43f668c3c38351b348c08ca44d457
4fa598026d9be1ca0c2bff1b531e566b7ea7f90b72e75fda0c1795bc2dfa375c"


But then, I don't know how to get the fith out of the third.

theymos
Administrator
Legendary
*
Offline Offline

Activity: 5180
Merit: 12884


View Profile
March 04, 2011, 08:38:02 PM
 #49

The last hash is doubled.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1076


View Profile
March 04, 2011, 09:02:11 PM
 #50

The last hash is doubled.

Indeed.  I thought I had tried that but I guess I did it wrong.

Thanks.

TiagoTiago
Hero Member
*****
Offline Offline

Activity: 616
Merit: 500


Firstbits.com/1fg4i :)


View Profile
March 16, 2011, 06:37:07 AM
 #51

how far are we from a total implementation?

(I dont always get new reply notifications, pls send a pm when you think it has happened)

Wanna gimme some BTC/BCH for any or no reason? 1FmvtS66LFh6ycrXDwKRQTexGJw4UWiqDX Smiley

The more you believe in Bitcoin, and the more you show you do to other people, the faster the real value will soar!

Do you like mmmBananas?!
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1076


View Profile
March 16, 2011, 06:41:48 AM
 #52

how far are we from a total implementation?

Very far.

grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1076


View Profile
June 15, 2011, 01:39:27 PM
 #53


Connecting to IRC is actually much easier than I thought.  I still have to request my IP and format my nick, though.

Code:
#!/bin/bash
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
#

################################################################################
#
#                                 BASE 58 encoding/decoding
#
# Requires bc, dc, openssl, xxd
################################################################################

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

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" =~ $bitcoinregex ]]
    then
        h=$(decodeBase58 "$1")
        checksum "00${h::${#h}-8}" |
        grep -qi "^${h: -8}$"
    else return 2
    fi
}

hash160() {
    # binary data expected on stdin
    openssl dgst -sha256 -binary |
    openssl dgst -rmd160 -binary |
    xxd -p -c 80
}

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

publicKeyToAddress() {
    hash160ToAddress $(
    openssl ec -pubin -pubout -outform DER 2>/dev/null |
    tail -c 65 |
    hash160
    )
}

bigEndianHex2littleEndianHex() {
    local s
    while read -n 2 c
    do s=$c$s
    done
    echo $s
}

bitcoinHash() {
    bigEndianHex2littleEndianHex |
    xxd -p -r |
    openssl dgst -sha256 -binary |
    openssl dgst -sha256 -binary |
    xxd -p -c 80 |
    bigEndianHex2littleEndianHex
}

################################################################################
#
#                                 IRC communication
#
# most of this comes from http://shudder.daemonette.org/source/BashNP-Guide.txt
################################################################################

host="irc.lfnet.org" port=6667 channel="#bitcoinTEST" mode="+i"

nick="$USER$$"
name="$USER's bitcoin bot script"

if
    # try to connect
    ! exec 3<> /dev/tcp/$host/$port
then
    echo "$(basename $0): unable to connect to $host:$port" 1>&2
    exit 1
else
    # duplicate standard input and output with the newly created socket
    exec 0<&3 1>&3-

    # register to the server
    echo "USER $nick ${mode:-+iw} $nick :$name"
    echo "NICK $nick"
    # join channel
    echo "JOIN $channel"

    while read
    do
set -- ${REPLY//$'\r'/}

# answer the critical ping request
# otherwise the server will disconnect us
[[ "$1" == "PING" ]] && echo "PONG $2"

echo $REPLY >&2

    done

    exec 1<&-
fi

kripz
Full Member
***
Offline Offline

Activity: 182
Merit: 100


View Profile
June 17, 2011, 12:00:30 PM
 #54

Subscribing, very interesting Wink

 Merged mining, free SMS notifications, PayPal payout and much more.
http://btcstats.net/sig/JZCODg2
macman31
Member
**
Offline Offline

Activity: 83
Merit: 10

The French Pool


View Profile WWW
June 27, 2011, 09:28:22 PM
 #55

Hi,

Warning ! "/dev/tcp/" is not available everywhere !
Disabled on some new Linux OS (Debian 6, ...) for more security.

You should use the wildly available "netcat" ("nc") to connect to the IRC server !

By the way, your script is very interesting !

(Sorry for my English, I'm french)

macman31

Feel free to donate to 13cyb1gvVcu6q8F1ysLhEDM4UwzZR9MvNx Wink
Rejoignez la 1ère pool Française : The French Pool ! https://frenchpool.fr
hamdi
Hero Member
*****
Offline Offline

Activity: 826
Merit: 500



View Profile
June 27, 2011, 11:13:47 PM
 #56

nice development. cant wait to see all the tools finished
grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1076


View Profile
June 28, 2011, 11:44:04 AM
 #57

You should use the wildly available "netcat" ("nc") to connect to the IRC server !

Thanks for the tip about netcat, I didn't know about it.  So many things to
know and only one life ;(

macman31
Member
**
Offline Offline

Activity: 83
Merit: 10

The French Pool


View Profile WWW
June 28, 2011, 11:53:02 AM
 #58

You're welcome !
You may take a look at :

http://fts.ifac.cnr.it/cgi-bin/dwww/usr/share/doc/netcat/examples/scripts/irc
http://www.soom.cz/download/data/windows/internet/im/irc/bots/ircb.bash

macman31

Feel free to donate to 13cyb1gvVcu6q8F1ysLhEDM4UwzZR9MvNx Wink
Rejoignez la 1ère pool Française : The French Pool ! https://frenchpool.fr
marcus_of_augustus
Legendary
*
Offline Offline

Activity: 3920
Merit: 2348


Eadem mutata resurgo


View Profile
June 28, 2011, 09:04:21 PM
 #59

Post #35 in this thread.

Quote
grondilu:
Quote
I also wonder if all communications could not be done using HTTP.  Blocks would be published via http GET method (giving the hash of the preceding block), and transactions could be sent via POST method.

One advantage of using http would be that it would be very hard for governments to forbid.

Where did you get to with the network connection side of this, netcat could be an option.

grondilu (OP)
Legendary
*
Offline Offline

Activity: 1288
Merit: 1076


View Profile
June 29, 2011, 11:45:22 AM
 #60


Could someone write a C/C++ program that could allow me to test this portion of code in irc.cpp ?

Code:
#pragma pack(push, 1)
struct ircaddr
{
    int ip;
    short port;
};
#pragma pack(pop)

string EncodeAddress(const CAddress& addr)
{
    struct ircaddr tmp;
tmp.ip    = addr.ip;
    tmp.port  = addr.port;

    vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp));
    return string("u") + EncodeBase58Check(vch);
}

I need to do some trial and error testing of the bash equivalent.

Pages: « 1 2 [3] 4 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!