Class: Bitcoin::Taproot::ControlBlock

Inherits:
Object
  • Object
show all
Includes:
HexConverter
Defined in:
lib/bitcoin/taproot/control_block.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HexConverter

#to_hex

Constructor Details

#initialize(parity, leaf_ver, internal_key, paths = []) ⇒ ControlBlock

Returns a new instance of ControlBlock.

Parameters:

  • parity (Integer)
  • leaf_ver (Integer)
  • internal_key (String)

    public key with hex format.

  • paths (Array[String]) (defaults to: [])

    array of hash values of sibling nodes in the tree that serve as proof of inclusion



15
16
17
18
19
20
# File 'lib/bitcoin/taproot/control_block.rb', line 15

def initialize(parity, leaf_ver, internal_key, paths = [])
  @parity = parity
  @leaf_ver = leaf_ver
  @internal_key = internal_key
  @paths = paths
end

Instance Attribute Details

#internal_keyObject

Returns the value of attribute internal_key.



8
9
10
# File 'lib/bitcoin/taproot/control_block.rb', line 8

def internal_key
  @internal_key
end

#leaf_verObject

Returns the value of attribute leaf_ver.



7
8
9
# File 'lib/bitcoin/taproot/control_block.rb', line 7

def leaf_ver
  @leaf_ver
end

#parityObject

Returns the value of attribute parity.



6
7
8
# File 'lib/bitcoin/taproot/control_block.rb', line 6

def parity
  @parity
end

#pathsObject

Returns the value of attribute paths.



9
10
11
# File 'lib/bitcoin/taproot/control_block.rb', line 9

def paths
  @paths
end

Class Method Details

.parse_from_payload(payload) ⇒ Bitcoin::Taproot::ControlBlock

parse control block from payload.



25
26
27
28
29
30
31
32
33
34
# File 'lib/bitcoin/taproot/control_block.rb', line 25

def self.parse_from_payload(payload)
  raise Bitcoin::Taproot::Error, 'Invalid data length for Control Block' if payload.bytesize > TAPROOT_CONTROL_MAX_SIZE
  raise Bitcoin::Taproot::Error, 'Invalid data length for path in Control Block' unless (payload.bytesize - TAPROOT_CONTROL_BASE_SIZE) % TAPROOT_CONTROL_NODE_SIZE == 0
  control, internal_key, paths = payload.unpack('Ca32a*')
  parity = control & 1
  leaf_ver = control - parity
  raw_paths = StringIO.new(paths)
  paths = (paths.bytesize / 32).times.map { raw_paths.read(32).bth }
  ControlBlock.new(parity, leaf_ver, internal_key.bth, paths)
end

Instance Method Details

#==(other) ⇒ Object



42
43
44
# File 'lib/bitcoin/taproot/control_block.rb', line 42

def ==(other)
  to_payload == other.to_payload
end

#to_payloadString

Convert to payload.

Returns:

  • (String)

    payload with binary format.



38
39
40
# File 'lib/bitcoin/taproot/control_block.rb', line 38

def to_payload
  [parity + leaf_ver].pack("C") + internal_key.htb + paths.map(&:htb).join
end