Parsing the Bitcoin Genesis Block with J _ Analyze the Data not the Drivel. bitcoin value 2014

The genesis block is the first block on the bitcoin blockchain. Satoshi nakamoto, the mysterious entity that created bitcoin, mined the genesis block on january 3, 2009. It’s been five years since the genesis block’s birth and satoshi is still unknown, bitcoin is bigger than ever, and the blockchain is longer than 300,000 blocks and growing.

One of the most important features of the blockchain is its immutability. After the bitcoin network accepts a block and adds it to the blockchain it can never be altered. This makes bitcoin blocks rare durable binary artifacts. The cryptographic hash algorithms that underpin the bitcoin protocol enforce block immutability. If someone decides to tinker with a block, say maliciously flip a single bit, the block’s hash will change and the network will reject it.Bitcoin value 2014

this is what makes it almost impossible to counterfeit bitcoins. Bitcoins have been lost and stolen but they have never been successfully counterfeited. This sharply contrasts with funny money like the US dollar that is so routinely and brazenly counterfeited that many suspect the US government turns a blind eye.

The exceptional durability of bitcoin blocks, coupled with the mysterious origins of bitcoin, makes the genesis block one of the most intriguing and important byte runs in the world. This post was inspired by the now defunct post 285 bytes that changed the world. I would love to give you a link but this post has vanished. A secondary, but excellent reference is john ratcliff’s how to parse the bitcoin blockchain. I am adapting john’s nomenclature in what follows.

When programmers start exploring bitcoin they often cut their teeth on parsing the genesis block.Bitcoin value 2014 if you google “blockchain parsing” you’ll find examples in dozens of programming languages. The most popular are C, C++, java, PHP, C#, javascript, and the rest of the mainstream suspects. What you will not find, until now, are J examples.

So what does J bring to the table that makes yet another genesis block parser worth a look? Let’s take a look at bitcoin addresses. The following is the bitcoin address of this blog’s tip jar. Feel free to send as many satoshis and full bitcoins as you like to this address. Tip=. ’17mfyvfqsyezcy7nkmbfrstfmmvaj143fa’

There is nothing deep or mysterious about this funny string of letters; it’s just a plain old number in bitcoin base 58 clothing. So, what is this number in standard format? Here’s how it’s calculated with J. BASE58=. ‘123456789abcdefghjklmnpqrstuvwxyzabcdefghijkmnopqrstuvwxyz’

bitcoin value 2014

Dfb58=. 58x #. BASE58 i. ]

Dfb58 tip


The second line that defines dfb58, (decimal from base 58), is the complete J program! That’s it folks. You can troll the internet for days looking at base 58 to big integer converters and it’s unlikely you will find a shorter or more elegant conversion program. Not only is the J version short and sweet it’s also fast and versatile. Suppose you wanted to convert ten thousand bitcoin addresses. The following converts ten thousand copies of tip. Dfb58 10000 # ,: tip

1709618896654985460726422911112500711652231559804656492485 17096188966549854607264…

At this point fanboys of mainstream programming languages typically pipe up with something like, “changing number encodings is inherently trivial; what about something more demanding like going the other way, say converting bitcoin public keys to the base 58 address format?”

bitcoin value 2014

The public key in the genesis block is encoded in what many call the “challenge script.” here is the genesis block’s challenge script in hex. 41 04 67 8A FD B0 FE 55 48 27 19 67 F1 A6 71 30 B7 10 5C D6

A8 28 E0 39 09 A6 79 62 E0 EA 1F 61 DE B6 49 F6 BC 3F 4C EF

38 C4 F3 55 04 E5 1E C1 12 DE 5C 38 4D F7 BA 0B 8D 57 8A 4C

70 2B 6B F1 1D 5F AC

Public keys take a number of forms in the blockchain. John ratcliff’s post summarizes the many forms you will run into. The genesis block uses the 65 byte ECDSA form. Converting this form to base 58 requires taking SHA-256 and RIPEMD-160 hashes. These hashes are available in openssl which is conveniently distributed with J 8.02 JQT. Here’s how to convert the genesis block’s public key to base 58 with J. Load ‘c:/bitjd/scripts/sslhash.Ijs’

Base58frKey65=:3 : 0

bitcoin value 2014

NB.*base58frkey65 v– 65 byte public bitcoin key bytes to base 58.


NB. Monad: clb58 =. Base58frKey65 clbytes

Ekey=. (0{a.) , sr160 s256 y

Csum=. 4 {. S256 s256 ekey

Base58Check ekey,csum


Base58frKey65 }. }: challengescript


The challengescript noun holds the bytes given in hex above. The verbs sr150, s256 and base58check are available in the J scripts sslhash and parsegenesisblock that I have put in the jacks repository on github.

The following J verb parsegenesisblock reads the first full node bitcoin block file and then extracts and checks the genesis block. ParseGenesisBlock tests the various verbs, (functions), it employs. As a side effect it clearly describes the layout of the genesis block and provides test data for anyone that’s interested.

If this post peeks your curiosity about J a good place to start learning about the language is the recently released new dictionary of J.Bitcoin value 2014 you can download a version of J for windows, linux, OS/X, IOS, and android at jsoftware’s main site.

ParseGenesisBlock=:3 : 0

NB.*parsegenesisblock v– parse and check bitcoin genesis block.


NB. Monad: clmsg =. ParseGenesisBlock clblockfile


NB. File=. ‘c:/bitjd/blocks/blk00000.Dat’

NB. ParseGenesisBlock file

NB. Fetch genesis block data

Dat=. Read y

NB. First 4 bytes are sort of block delimiters

MagicID=: (i. Offset=. 4) { dat

‘magicid mismatch’ assert ‘F9BEB4D9’ -: ,hfd a. I. MagicID

NB. Next 4 bytes gives following block length

Offset=. Offset + 4 [ blocklength=: 2 ic (offset + i. 4) { dat

‘blocklength mismatch’ assert 285 = blocklength

NB. Next 4 bytes block format version – has changed

Offset=. Offset + 4 [ versionnumber=: 2 ic (offset + i. 4) { dat

NB. Next 32 bytes is previous block hash – genesis block

bitcoin value 2014

NB. Has no previous hash and all bytes are set to 0

Offset=. Offset + 32 [ previousblockhash=: (offset + i. 32) { dat

‘previousblockhash mismatch’ assert (32#0) -: a. I. PreviousBlockHash

NB. Next 32 bytes is the merkle tree root hash

Offset=. Offset + 32 [ merkleroot=: (offset + i. 32) { dat

Grh=. ‘3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A’

‘merkleroot mismatch’ assert grh -: ,hfd a. I. MerkleRoot

NB. Next 4 bytes is a unix epoch timestamp – rolls over 7th feb 2106

NB. There is no timezone information – it is interpreted as utc

Offset=. Offset + 4 [ timestamp=: 2 ic (offset + i. 4) { dat

‘timestamp mismatch’ assert 2009 1 3 18 15 5 -: ,tsfrunixsecs timestamp

NB. Next 4 bytes represents block target difficulty

Offset=. Offset + 4 [ targetdifficulty=: 2 ic (offset + i. 4) { dat

bitcoin value 2014

‘targetdifficulty mismatch’ assert 486604799 = targetdifficulty

NB. Next 4 bytes is a random number nonce

Offset=. Offset + 4 [ nonce=: (offset + i. 4) { dat

‘nonce mismatch’ assert ‘1DAC2B7C’ -: ,hfd a. I. Nonce

NB. Next 1 to 9 bytes is the transaction count stored as a variable length integer

NB. See: https://en.Bitcoin.It/wiki/protocol specification#variable length integer

Offset=. Offset + vlen [ ‘vlen transactioncount’=: vint (offset + i. 9) { dat

‘transactioncount mismatch’ assert transactioncount = 1 NB. (*)=. Vlen

NB. Next 4 bytes transaction version number

Offset=. Offset + 4 [ transactionversionnumber=: 2 ic (offset + i.4) { dat

‘transactionversionnumber mismatch’ assert 1 = transactionversionnumber

NB. Next 1 to 9 bytes is the number of transaction inputs

Offset=. Offset + vlen [ ‘vlen transactioninputnumber’=: vint (offset + i. 9) { dat

bitcoin value 2014

NB. Next 32 bytes is the hash of the input transaction

Offset=. Offset + 32 [ transactionhash=: (offset + i. 32) { dat

‘transactionhash mismatch’ assert (32#0) -: a. I. TransactionHash

NB. Next 4 bytes is the input transaction index

Offset=. Offset + 4 [ transactionindex=: 2 ic (offset + i. 4) { dat

‘transactionindex mismatch’ assert 1 = transactionindex

NB. Input script length is next

Offset=. Offset + vlen [ ‘vlen inputscriptlength’=: vint (offset + i. 9) { dat

‘inputscriptlength mismatch’ assert 77 = inputscriptlength

NB. Script data

Offset=. Offset + inputscriptlength [ inputscript=: (offset + i. InputScriptLength) { dat

NB. Sequence number 4 bytes

Offset=. Offset + 4 [ sequencenumber=: ,hfd a. I. (offset + i. 4) { dat

‘sequencenumber mismatch’ assert ‘FFFFFFFF’ -: sequencenumber

NB. Output count 1 to 9 bytes

bitcoin value 2014

Offset=. Offset + vlen [ ‘vlen outputcount’=: vint (offset + i.9) { dat

NB. Output value – number of satoshis sent

Offset=. Offset + 8 [ outputsatoshis=: (offset + i.8) { dat NB. 64 bit unsigned integer

‘outputsatoshis mismatch’ assert ’00F2052A01000000′ -: ,hfd a. I. OutputSatoshis

OutputSatoshis=: ]`( 3&ic)@.IF64 outputsatoshis

NB. Challenge script length

Offset=. Offset + vlen [ ‘vlen challengescriptlength’=: vint (offset + i.9) { dat

‘challengescriptlength mismatch’ assert 67 = challengescriptlength

NB. Challenge script – contains elliptic curve signatures

Offset=. Offset + challengescriptlength [ challengescript=: (offset + i. ChallengeScriptLength) { dat

‘challengescript mismatch’ assert genesisblockchallengescript -: ,hfd a. I. ChallengeScript

NB. Challenge script is 67 bytes drop first and last byte to

bitcoin value 2014

NB. Compute the familiar bitcoin base 58 address – compare with block explorer

NB. Http://blockexplorer.Com/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

OutputAddress=: base58frkey65 }. }: challengescript

‘genesis block address mismatch’ assert genesisblockoutputaddress -: outputaddress

NB. Last 4 bytes lock time

TransactionLockTime=: (offset + i.4) { dat

‘transactionlocktime mismatch’ assert 0 0 0 0 -: a. I. TransactionLockTime

‘genesis block parsed and checked’