Module: Nano::Hash

Extended by:
Hash
Included in:
Hash
Defined in:
lib/nano/hash.rb

Overview

The Hash module is responsible for producing a block hash given a Block object.

Constant Summary collapse

STATE_BLOCK_PREAMBLE_BITS =
Nano::Utils.bytes_to_bin(
  Array.new(32) {|i| i == 31 ? 6 : 0}
).freeze

Instance Method Summary collapse

Instance Method Details

#hash_block(block) ⇒ String

This method will produce a hash for the given block

Parameters:

  • block (Nano::Block)

    The block to derive a hash from

Returns:

  • (String)

    The hash in a hexadecimal formatted string.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nano/hash.rb', line 22

def hash_block(block)
   = Nano::Utils.hex_to_bin(
    Nano::Key.derive_public_key(block.)
  )
  previous_bits = Nano::Utils.hex_to_bin(
    block.previous
  )
  representative_bits = Nano::Utils.hex_to_bin(
    Nano::Key.derive_public_key(block.representative)
  )
  balance_bits = Nano::Utils.hex_to_bin(
    Nano::Unit.convert(block.balance, :raw, :hex)
  )
  link_bits = Nano::Utils.hex_to_bin(
    block.link
  )
  bits = STATE_BLOCK_PREAMBLE_BITS +  + previous_bits +
         representative_bits + balance_bits + link_bits;
  Blake2b.hex(bits).upcase
end