Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- class BlockHeader
- # Block header : 160 bytes
- # - Version : 4 bytes
- # - Previous block hash : 32 bytes
- # - Merkle root : 32 bytes
- # - Timestamp : 4 bytes
- # - Bits : 4 bytes
- # - Nonce : 4 bytes
- attr_accessor :raw,
- :previous_block,
- :nonce,
- :timestamp,
- :version,
- :merkle_root,
- :bits
- def initialize(raw)
- self.raw = raw
- end
- def raw=(str)
- # We keep only the first 80 bytes of data
- @raw = str[0, 160]
- end
- def version
- @version ||= @raw[0, 8].hex
- end
- def previous_block
- @previous_block ||= switch_endianness(@raw[8, 64])
- end
- def merkle_root
- @merkle_root ||= switch_endianness(@raw[72, 64])
- end
- def timestamp
- @timestamp ||= @raw[136, 8].hex
- end
- def bits
- @bits ||= @raw[144, 8].hex
- end
- def nonce
- @nonce ||= @raw[152, 8].hex
- end
- def switch_endianness(str)
- res = []
- while str.length > 0 do
- res << str[-8, 8]
- str = str[0, str.length - 8]
- end
- res.join
- end
- def to_s
- "== Dumping block data\n" +
- " Raw header : #{raw}\n" +
- " Version : #{version}\n" +
- " Previous block : #{previous_block}\n" +
- " Merkle root : #{merkle_root}\n" +
- " Timestamp : #{timestamp}\n" +
- " Bits : #{bits}\n" +
- " Nonce : #{nonce}\n" +
- "=="
- end
- def inspect
- puts self
- end
- end
- bh = BlockHeader.new('0000000150c549978a44e3ef671a4ed3f5b922195ebde0fbbccfdb5900e6827000000000c8a03dfb42fa04d47e9fbfd3e6afcb3d0df2b516eb86f4e64d09fe53cc397b6c4de61f4c1c069652a228e13d000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000')
- bh.inspect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement