class Tapyrus::BlockHeader
Block
Header
Constants
- X_FILED_TYPES
Attributes
Public Class Methods
# 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
# 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
# File lib/tapyrus/block_header.rb, line 113 def ==(other) other && other.to_payload == to_payload end
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 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
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
get bytesize. @return [Integer] bytesize.
# File lib/tapyrus/block_header.rb, line 119 def size to_payload.bytesize end
# 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
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
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
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
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
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