Class: Tapyrus::PSTT::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/tapyrus/pstt/output.rb

Overview

An output map of a PSTT.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount: nil, script_pubkey: nil, redeem_script: nil) ⇒ Output

Returns a new instance of Output.



34
35
36
37
38
39
40
41
42
# File 'lib/tapyrus/pstt/output.rb', line 34

def initialize(amount: nil, script_pubkey: nil, redeem_script: nil)
  @amount = amount
  @script_pubkey = script_pubkey
  @redeem_script = redeem_script
  @bip32_derivations = {}
  @proprietaries = []
  @unknowns = {}
  @record_order = []
end

Instance Attribute Details

#amountInteger

Returns the amount of this output. Tokens for a colored output, otherwise tapy.

Returns:

  • (Integer)

    the amount of this output. Tokens for a colored output, otherwise tapy.



7
8
9
# File 'lib/tapyrus/pstt/output.rb', line 7

def amount
  @amount
end

#bip32_derivationsObject (readonly)

Returns the value of attribute bip32_derivations.



19
20
21
# File 'lib/tapyrus/pstt/output.rb', line 19

def bip32_derivations
  @bip32_derivations
end

#proprietariesObject (readonly)

Returns the value of attribute proprietaries.



23
24
25
# File 'lib/tapyrus/pstt/output.rb', line 23

def proprietaries
  @proprietaries
end

#record_orderArray[String]

Returns the complete keys of the map this output was parsed from. See Tapyrus::PSTT.order_records.

Returns:

  • (Array[String])

    the complete keys of the map this output was parsed from. See Tapyrus::PSTT.order_records.



32
33
34
# File 'lib/tapyrus/pstt/output.rb', line 32

def record_order
  @record_order
end

#redeem_scriptTapyrus::Script

Returns the redeem script, if this output is P2SH or CP2SH.

Returns:

  • (Tapyrus::Script)

    the redeem script, if this output is P2SH or CP2SH.



15
16
17
# File 'lib/tapyrus/pstt/output.rb', line 15

def redeem_script
  @redeem_script
end

#script_pubkeyTapyrus::Script

Returns the scriptPubKey of this output.

Returns:



11
12
13
# File 'lib/tapyrus/pstt/output.rb', line 11

def script_pubkey
  @script_pubkey
end

#unknownsObject (readonly)

Returns the value of attribute unknowns.



27
28
29
# File 'lib/tapyrus/pstt/output.rb', line 27

def unknowns
  @unknowns
end

Class Method Details

.parse_from_records(records) ⇒ Tapyrus::PSTT::Output

Build an output map from the records of an output map.

Parameters:

Returns:

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tapyrus/pstt/output.rb', line 48

def self.parse_from_records(records)
  output = new
  records.each do |record|
    if OutputTypes::RESERVED.include?(record.type)
      raise Error, format("Output key type 0x%02x is reserved.", record.type)
    end
    case record.type
    when OutputTypes::REDEEM_SCRIPT
      PSTT.validate_empty_keydata!(record)
      output.redeem_script =
        PSTT.parse_field("PSTT_OUT_REDEEM_SCRIPT") { Tapyrus::Script.parse_from_payload(record.value) }
    when OutputTypes::BIP32_DERIVATION
      PSTT.validate_pubkey_keydata!(record)
      output.bip32_derivations[record.keydata.bth] = KeyOriginInfo.parse_from_payload(record.value)
    when OutputTypes::AMOUNT
      PSTT.validate_empty_keydata!(record)
      output.amount = PSTT.read_i64(record)
      PSTT.validate_amount!(output.amount)
    when OutputTypes::SCRIPT
      PSTT.validate_empty_keydata!(record)
      output.script_pubkey =
        PSTT.parse_field("PSTT_OUT_SCRIPT") { Tapyrus::Script.parse_from_payload(record.value) }
    when OutputTypes::PROPRIETARY
      output.proprietaries << Proprietary.parse_from_record(record)
    else
      output.unknowns[record.key] = record.value
    end
  end
  raise Error, "PSTT_OUT_AMOUNT is required." unless output.amount
  raise Error, "PSTT_OUT_SCRIPT is required." unless output.script_pubkey
  output.record_order = records.map(&:key)
  output
end

Instance Method Details

#==(other) ⇒ Object



124
125
126
# File 'lib/tapyrus/pstt/output.rb', line 124

def ==(other)
  other.is_a?(Output) && to_payload == other.to_payload
end

#color_idTapyrus::Color::ColorIdentifier

Returns the color of this output, or nil for TPC.

Returns:



120
121
122
# File 'lib/tapyrus/pstt/output.rb', line 120

def color_id
  script_pubkey.color_id
end

#colored?Boolean

Returns whether this output holds Colored Coins.

Returns:

  • (Boolean)

    whether this output holds Colored Coins.



115
116
117
# File 'lib/tapyrus/pstt/output.rb', line 115

def colored?
  script_pubkey.colored?
end

#to_payloadObject



103
104
105
# File 'lib/tapyrus/pstt/output.rb', line 103

def to_payload
  PSTT.serialize_map(to_records, record_order)
end

#to_recordsArray[Tapyrus::PSTT::KeyValue]

Returns the records of this output map.

Returns:

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tapyrus/pstt/output.rb', line 83

def to_records
  raise Error, "PSTT_OUT_AMOUNT is required." unless amount
  raise Error, "PSTT_OUT_SCRIPT is required." unless script_pubkey
  PSTT.validate_amount!(amount)
  records = []
  records << KeyValue.new(OutputTypes::REDEEM_SCRIPT, "".b, redeem_script.to_payload) if redeem_script
  bip32_derivations.each do |pubkey, info|
    records << KeyValue.new(OutputTypes::BIP32_DERIVATION, pubkey.htb, info.to_payload)
  end
  records << KeyValue.new(OutputTypes::AMOUNT, "".b, [amount].pack("q<"))
  records << KeyValue.new(OutputTypes::SCRIPT, "".b, script_pubkey.to_payload)
  proprietaries.each { |p| records << p.to_record(OutputTypes::PROPRIETARY) }
  unknowns.each do |key, value|
    buf = StringIO.new(key)
    type = PSTT.read_compact_size(buf)
    records << KeyValue.new(type, buf.read || "".b, value)
  end
  records
end

#to_tx_outTapyrus::TxOut

Returns the transaction output this map represents.

Returns:

Raises:



109
110
111
112
# File 'lib/tapyrus/pstt/output.rb', line 109

def to_tx_out
  PSTT.validate_amount!(amount)
  Tapyrus::TxOut.new(value: amount, script_pubkey: script_pubkey)
end