Skip to content
andrewschaaf edited this page Mar 27, 2011 · 16 revisions

Bitcoin address

hash160 = (data) ->
  ripemd160(sha256(data))

bitcoin_address = (public_key) ->
  x = "\x00" + hash160(public_key)
  addr_bytes = x + sha256(sha256(x))[0:4]
  base58_encode(addr_bytes)

base58

Like base64, but without these characters: +/0O1l

Notation for the rest of this page

all integers are little-endian unless otherwise stated

{i,u}32       {signed,unsigned} 4-byte integer
(N)-byte      arbitrary bytes

var_str       (var_int(N)) + (N arbitrary bytes)

var_int = (N) ->
  uint8(N)          if N < 0xFD
  FD + uint16(N)    if N <= 0xFFFF
  FE + uint32(N)    if N <= 0xFFFFFFFF
  FF + uint64(N)    if N <= 0xFFFFFFFFFFFFFFFF

TX

BLOCK

Mining

boolean isValidBlock(bytes[] blockHeader, uint256 difficulty):
  return sha256(sha256(blockHeader)) <= difficulty

Overview of messages

Connection Messages

version:    version, start_height, ...
verack      // "ok"

ping:       // for testing the TCP connection -- no reply needed

P2P Messages

getaddr     // "please send me addr message(s)"
addr:       (timestamp, IP)-list

alert:      message, signature

Data messages

inv:        (type, 4-byte hash)-list
getdata:    (type, 4-byte hash)-list
getblocks:  (hash_start, hash_stop, ...)
tx:         (TX, ...)
block:      (BLOCK...)

...where type \in {TX, BLOCK, ERROR}

Clone this wiki locally