Class: Bitcoin::PSBT::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/psbt/output.rb

Overview

Class for PSBTs which contains per output information

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOutput

Returns a new instance of Output.



12
13
14
15
# File 'lib/bitcoin/psbt/output.rb', line 12

def initialize
  @hd_key_paths = {}
  @unknowns = {}
end

Instance Attribute Details

#hd_key_pathsObject

Returns the value of attribute hd_key_paths.



9
10
11
# File 'lib/bitcoin/psbt/output.rb', line 9

def hd_key_paths
  @hd_key_paths
end

#redeem_scriptObject

Returns the value of attribute redeem_script.



7
8
9
# File 'lib/bitcoin/psbt/output.rb', line 7

def redeem_script
  @redeem_script
end

#unknownsObject

Returns the value of attribute unknowns.



10
11
12
# File 'lib/bitcoin/psbt/output.rb', line 10

def unknowns
  @unknowns
end

#witness_scriptObject

Returns the value of attribute witness_script.



8
9
10
# File 'lib/bitcoin/psbt/output.rb', line 8

def witness_script
  @witness_script
end

Class Method Details

.parse_from_buf(buf) ⇒ Bitcoin::PSBTOutput

parse PSBT output data form buffer.

Parameters:

  • buf (StringIO)

    psbt buffer.

Returns:

  • (Bitcoin::PSBTOutput)

    psbt output.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bitcoin/psbt/output.rb', line 20

def self.parse_from_buf(buf)
  output = self.new
  until buf.eof?
    key_len = Bitcoin.unpack_var_int_from_io(buf)
    break if key_len == 0
    key_type = buf.read(1).unpack('C').first
    key = buf.read(key_len - 1)
    value = buf.read(Bitcoin.unpack_var_int_from_io(buf))
    case key_type
    when PSBT_OUT_TYPES[:redeem_script]
      raise ArgumentError, 'Duplicate Key, output redeemScript already provided' if output.redeem_script
      output.redeem_script = value
    when PSBT_OUT_TYPES[:witness_script]
      raise ArgumentError, 'Duplicate Key, output witnessScript already provided' if output.witness_script
      output.witness_script = value
    when PSBT_OUT_TYPES[:bip32_derivation]
      raise ArgumentError, 'Duplicate Key, pubkey derivation path already provided' if output.hd_key_paths[key.bth]
      output.hd_key_paths[key.bth] = Bitcoin::PSBT::HDKeyPath.parse_from_payload(key, value)
    else
      unknown_key = ([key_type].pack('C') + key).bth
      raise ArgumentError, 'Duplicate Key, key for unknown value already provided' if output.unknowns[unknown_key]
      output.unknowns[unknown_key] = value
    end
  end
  output
end

Instance Method Details

#merge(psbo) ⇒ Bitcoin::PSBT::Output

merge two PSBT outputs to create one PSBT.

Parameters:

  • psbo (Bitcoin::PSBT::Output)

    PSBT output to be combined which must have same property in PSBT Output.

Returns:

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bitcoin/psbt/output.rb', line 60

def merge(psbo)
  raise ArgumentError, 'The argument psbt must be an instance of Bitcoin::PSBT::Output.' unless psbo.is_a?(Bitcoin::PSBT::Output)
  raise ArgumentError, 'The Partially Signed Output\'s redeem_script are different.' unless redeem_script == psbo.redeem_script
  raise ArgumentError, 'The Partially Signed Output\'s witness_script are different.' unless witness_script == psbo.witness_script
  combined = Bitcoin::PSBT::Output.new
  combined.redeem_script = redeem_script
  combined.witness_script = witness_script
  combined.unknowns = unknowns.merge(psbo.unknowns)
  combined.hd_key_paths = hd_key_paths.merge(psbo.hd_key_paths)
  combined
end

#to_payloadObject



47
48
49
50
51
52
53
54
55
# File 'lib/bitcoin/psbt/output.rb', line 47

def to_payload
  payload = ''
  payload << PSBT.serialize_to_vector(PSBT_OUT_TYPES[:redeem_script], value: redeem_script) if redeem_script
  payload << PSBT.serialize_to_vector(PSBT_OUT_TYPES[:witness_script], value: witness_script) if witness_script
  payload << hd_key_paths.values.map{|v|v.to_payload(PSBT_OUT_TYPES[:bip32_derivation])}.join
  payload << unknowns.map {|k,v|Bitcoin.pack_var_int(k.htb.bytesize) << k.htb << Bitcoin.pack_var_int(v.bytesize) << v}.join
  payload << PSBT_SEPARATOR.to_even_length_hex.htb
  payload
end