class Tapyrus::BlockHeader

Block Header

Constants

X_FILED_TYPES

Attributes

features[RW]
im_merkle_root[RW]
merkle_root[RW]
prev_hash[RW]
proof[RW]
time[RW]
x_field[RW]
x_field_type[RW]

Public Class Methods

new(features, prev_hash, merkle_root, im_merkle_root, time, x_field_type, x_field, proof = nil) click to toggle source
# File lib/tapyrus/block_header.rb, line 19
def initialize(features, prev_hash, merkle_root, im_merkle_root, time, x_field_type, x_field, proof = nil)
  @features = features
  @prev_hash = prev_hash
  @merkle_root = merkle_root
  @im_merkle_root = im_merkle_root
  @time = time
  @x_field_type = x_field_type
  @x_field = x_field
  @proof = proof
end
parse_from_payload(payload) click to toggle source
# File lib/tapyrus/block_header.rb, line 30
def self.parse_from_payload(payload)
  buf = payload.is_a?(String) ? StringIO.new(payload) : payload
  features, prev_hash, merkle_root, im_merkle_root, time, x_filed_type = buf.read(105).unpack('Va32a32a32Vc')
  x_field = buf.read(unpack_var_int_from_io(buf)) unless x_filed_type == X_FILED_TYPES[:none]
  proof = buf.read(unpack_var_int_from_io(buf))
  new(
    features,
    prev_hash.bth,
    merkle_root.bth,
    im_merkle_root.bth,
    time,
    x_filed_type,
    x_field ? x_field.bth : x_field,
    proof.bth
  )
end

Public Instance Methods

==(other) click to toggle source
# File lib/tapyrus/block_header.rb, line 113
def ==(other)
  other && other.to_payload == to_payload
end
block_hash() click to toggle source

Calculate block hash @return [String] hash of block with hex format.

# File lib/tapyrus/block_header.rb, line 62
def block_hash
  Tapyrus.double_sha256(to_payload).bth
end
block_id() click to toggle source

block hash(big endian) @return [String] block id which is reversed version of block hash.

# File lib/tapyrus/block_header.rb, line 68
def block_id
  block_hash.rhex
end
hash_for_sign() click to toggle source

Calculate hash using sign which does not contains proof. @return [String] hash of block without proof.

# File lib/tapyrus/block_header.rb, line 56
def hash_for_sign
  Tapyrus.double_sha256(to_payload(true)).bth
end
size() click to toggle source

get bytesize. @return [Integer] bytesize.

# File lib/tapyrus/block_header.rb, line 119
def size
  to_payload.bytesize
end
to_payload(skip_proof = false) click to toggle source
# File lib/tapyrus/block_header.rb, line 47
def to_payload(skip_proof = false)
  payload = [features, prev_hash.htb, merkle_root.htb, im_merkle_root.htb, time, x_field_type].pack('Va32a32a32Vc')
  payload << pack_var_string(x_field.htb) unless x_field_type == X_FILED_TYPES[:none]
  payload << pack_var_string(proof.htb) if proof && !skip_proof
  payload
end
upgrade_agg_pubkey?() click to toggle source

Check this header contains upgrade aggregated publiec key. @return [Boolean] if contains return true, otherwise false.

# File lib/tapyrus/block_header.rb, line 109
def upgrade_agg_pubkey?
  x_field_type == X_FILED_TYPES[:aggregate_pubkey]
end
valid?(agg_pubkey) click to toggle source

evaluate block header @param [String] agg_pubkey aggregated public key for signers with hex format. @return [Boolean] result.

# File lib/tapyrus/block_header.rb, line 75
def valid?(agg_pubkey)
  valid_timestamp? && valid_proof?(agg_pubkey) && valid_x_field?
end
valid_proof?(agg_pubkey) click to toggle source

Check whether proof is valid. @param [String] agg_pubkey aggregated public key for signers with hex format. @return [Boolean] Return true if proof is valid, otherwise return false.

# File lib/tapyrus/block_header.rb, line 88
def valid_proof?(agg_pubkey)
  pubkey = Tapyrus::Key.new(pubkey: agg_pubkey)
  msg = hash_for_sign.htb
  pubkey.verify(proof.htb, msg, algo: :schnorr)
end
valid_timestamp?() click to toggle source

evaluate valid timestamp. en.bitcoin.it/wiki/Block_timestamp

# File lib/tapyrus/block_header.rb, line 81
def valid_timestamp?
  time <= Time.now.to_i + Tapyrus::MAX_FUTURE_BLOCK_TIME
end
valid_x_field?() click to toggle source

Check whether x_field is valid. @return [Boolean] if valid return true, otherwise false

# File lib/tapyrus/block_header.rb, line 96
def valid_x_field?
  case x_field_type
  when X_FILED_TYPES[:none]
    x_field.nil?
  when X_FILED_TYPES[:aggregate_pubkey]
    Tapyrus::Key.new(pubkey: x_field).fully_valid_pubkey?
  else
    false
  end
end