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. 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. 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.
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.
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?”
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.
c=.' 41 04 67 8A FD B0 FE 55 48 27 19 67 F1 A6 71 30 B7 10 5C D6'c=.c,' A8 28 E0 39 09 A6 79 62 E0 EA 1F 61 DE B6 49 F6 BC 3F 4C EF'c=.c,' 38 C4 F3 55 04 E5 1E C1 12 DE 5C 38 4D F7 BA 0B 8D 57 8A 4C'c=.c,' 70 2B 6B F1 1D 5F AC'Challenge=:];._1cs
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 : 0NB.*Base58frKey65 v-- 65 byte public Bitcoin key bytes to base 58.NB.NB. monad: clB58 =. Base58frKey65 clBytesekey=.(0{a.),sr160s256ycsum=.4{.s256s256ekeyBase58Checkekey,csum)Base58frKey65}.}:ChallengeScript1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
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. You can download a version of J for Windows, Linux, OS/X, IOS, and Android at Jsoftware’s main site.
ParseGenesisBlock=:3 : 0NB.*ParseGenesisBlock v-- parse and check Bitcoin genesis block.NB.NB. monad: clMsg =. ParseGenesisBlock clBlockFileNB.NB. file=. 'c:/bitjd/blocks/blk00000.dat'NB. ParseGenesisBlock fileNB. fetch genesis block datadat=.readyNB. first 4 bytes are "sort of" block delimitersMagicID=:(i.offset=.4){dat'MagicID mismatch'assert'F9BEB4D9'-:,hfda.i.MagicIDNB. next 4 bytes gives following block lengthoffset=.offset+4[BlockLength=:_2ic(offset+i.4){dat'BlockLength mismatch'assert285=BlockLengthNB. next 4 bytes block format version - has changedoffset=.offset+4[VersionNumber=:_2ic(offset+i.4){datNB. next 32 bytes is previous block hash - genesis blockNB. has no previous hash and all bytes are set to 0offset=.offset+32[PreviousBlockHash=:(offset+i.32){dat'PreviousBlockHash mismatch'assert(32#0)-:a.i.PreviousBlockHashNB. next 32 bytes is the Merkle tree root hashoffset=.offset+32[MerkleRoot=:(offset+i.32){datgrh=.'3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A''MerkleRoot mismatch'assertgrh-:,hfda.i.MerkleRootNB. next 4 bytes is a unix epoch timestamp - rolls over 7th feb 2106NB. there is no timezone information - it is interpreted as utcoffset=.offset+4[TimeStamp=:_2ic(offset+i.4){dat'TimeStamp mismatch'assert20091318155-:,tsfrunixsecsTimeStampNB. next 4 bytes represents block target difficultyoffset=.offset+4[TargetDifficulty=:_2ic(offset+i.4){dat'TargetDifficulty mismatch'assert486604799=TargetDifficultyNB. next 4 bytes is a random number nonceoffset=.offset+4[Nonce=:(offset+i.4){dat'Nonce mismatch'assert'1DAC2B7C'-:,hfda.i.NonceNB. next 1 to 9 bytes is the transaction count stored as a variable length integerNB. see: https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integeroffset=.offset+vlen['vlen TransactionCount'=:vint(offset+i.9){dat'TransactionCount mismatch'assertTransactionCount=1NB. (*)=. vlenNB. next 4 bytes transaction version numberoffset=.offset+4[TransactionVersionNumber=:_2ic(offset+i.4){dat'TransactionVersionNumber mismatch'assert1=TransactionVersionNumberNB. next 1 to 9 bytes is the number of transaction inputsoffset=.offset+vlen['vlen TransactionInputNumber'=:vint(offset+i.9){datNB. next 32 bytes is the hash of the input transactionoffset=.offset+32[TransactionHash=:(offset+i.32){dat'TransactionHash mismatch'assert(32#0)-:a.i.TransactionHashNB. next 4 bytes is the input transaction indexoffset=.offset+4[TransactionIndex=:_2ic(offset+i.4){dat'TransactionIndex mismatch'assert_1=TransactionIndexNB. input script length is nextoffset=.offset+vlen['vlen InputScriptLength'=:vint(offset+i.9){dat'InputScriptLength mismatch'assert77=InputScriptLengthNB. script dataInputScript=:(offset+i.InputScriptLength){datoffset=.offset+InputScriptLengthNB. sequence number 4 bytesoffset=.offset+4[SequenceNumber=:,hfda.i.(offset+i.4){dat'SequenceNumber mismatch'assert'FFFFFFFF'-:SequenceNumberNB. output count 1 to 9 bytesoffset=.offset+vlen['vlen OutputCount'=:vint(offset+i.9){datNB. output value - number of satoshis sentoffset=.offset+8[OutputSatoshis=:(offset+i.8){datNB. 64 bit unsigned integer'OutputSatoshis mismatch'assert'00F2052A01000000'-:,hfda.i.OutputSatoshisOutputSatoshis=:]`(_3&ic)@.IF64OutputSatoshisNB. challenge script lengthoffset=.offset+vlen['vlen ChallengeScriptLength'=:vint(offset+i.9){dat'ChallengeScriptLength mismatch'assert67=ChallengeScriptLengthNB. challenge script - contains elliptic curve signaturesChallengeScript=:(offset+i.ChallengeScriptLength){datoffset=.offset+ChallengeScriptLengthcscript=.,hfda.i.ChallengeScript'ChallengeScript mismatch'assertGenesisBlockChallengeScript-:cscriptNB. challenge script is 67 bytes drop first and last byte toNB. compute the familiar Bitcoin base 58 address - compare with block explorerNB. http://blockexplorer.com/block/NB. 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26fOutputAddress=:Base58frKey65}.}:ChallengeScript'Genesis Block address mismatch'assertGenesisBlockOutputAddress-:OutputAddressNB. last 4 bytes lock timeTransactionLockTime=:(offset+i.4){dat'TransactionLockTime mismatch'assert0000-:a.i.TransactionLockTime'Genesis Block Parsed and Checked')
04ffff001d0104455468652054696d657320303
What does this one say?
https://blockexplorer.com/tx/4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b#inputs