Class: OpenAssets::Protocol::TransactionOutput

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/openassets/protocol/transaction_output.rb

Overview

Represents a transaction output and its asset ID and asset quantity.

Constant Summary

Constants included from Util

Util::OA_NAMESPACE, Util::OA_VERSION_BYTE, Util::OA_VERSION_BYTE_TESTNET

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#address_to_oa_address, #coin_to_satoshi, #decode_leb128, #encode_leb128, #generate_asset_id, #hash_to_asset_id, #oa_address_to_address, #pubkey_hash_to_asset_id, #read_leb128, #read_var_integer, #satoshi_to_coin, #script_to_address, #script_to_asset_id, #to_bytes, #valid_asset_id?, #validate_address

Constructor Details

#initialize(value, script, asset_id = nil, asset_quantity = 0, output_type = OutputType::UNCOLORED, metadata = '') ⇒ TransactionOutput

Returns a new instance of TransactionOutput.

Parameters:

  • value (Integer)

    The satoshi value of the output.

  • script (Bitcoin::Script)

    The script controlling redemption of the output.

  • asset_id (String) (defaults to: nil)

    The asset ID of the output.

  • asset_quantity (Integer) (defaults to: 0)

    The asset quantity of the output.

  • output_type (OpenAssets::Transaction::OutPutType) (defaults to: OutputType::UNCOLORED)

    The type of the output.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openassets/protocol/transaction_output.rb', line 26

def initialize(value, script, asset_id = nil, asset_quantity = 0, output_type = OutputType::UNCOLORED,  = '')
  raise ArgumentError, "invalid output_type : #{output_type}" unless OutputType.all.include?(output_type)
  raise ArgumentError, "invalid asset_quantity. asset_quantity should be unsignd integer. " unless asset_quantity.between?(0, MarkerOutput::MAX_ASSET_QUANTITY)
  @value = value
  @script = script
  @asset_id = asset_id
  @asset_quantity = asset_quantity
  @output_type = output_type
  @metadata = 
  load_asset_definition_url
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



15
16
17
# File 'lib/openassets/protocol/transaction_output.rb', line 15

def 
  @account
end

#asset_definitionObject

Returns the value of attribute asset_definition.



18
19
20
# File 'lib/openassets/protocol/transaction_output.rb', line 18

def asset_definition
  @asset_definition
end

#asset_definition_urlObject

Returns the value of attribute asset_definition_url.



17
18
19
# File 'lib/openassets/protocol/transaction_output.rb', line 17

def asset_definition_url
  @asset_definition_url
end

#asset_idObject

Returns the value of attribute asset_id.



11
12
13
# File 'lib/openassets/protocol/transaction_output.rb', line 11

def asset_id
  @asset_id
end

#asset_quantityObject

Returns the value of attribute asset_quantity.



12
13
14
# File 'lib/openassets/protocol/transaction_output.rb', line 12

def asset_quantity
  @asset_quantity
end

#metadataObject

Returns the value of attribute metadata.



16
17
18
# File 'lib/openassets/protocol/transaction_output.rb', line 16

def 
  @metadata
end

#output_typeObject

Returns the value of attribute output_type.



13
14
15
# File 'lib/openassets/protocol/transaction_output.rb', line 13

def output_type
  @output_type
end

#scriptObject

Returns the value of attribute script.



10
11
12
# File 'lib/openassets/protocol/transaction_output.rb', line 10

def script
  @script
end

#valueObject

Returns the value of attribute value.



9
10
11
# File 'lib/openassets/protocol/transaction_output.rb', line 9

def value
  @value
end

Instance Method Details

#addressObject



74
75
76
77
78
79
80
81
# File 'lib/openassets/protocol/transaction_output.rb', line 74

def address
  if @script.is_multisig?
    @script.get_multisig_addresses.each do |address|
      return nil if address.nil?
    end
  end
  script_to_address(@script)
end

#asset_amountObject

calculate asset amount. asset amount is the value obtained by converting the asset quantity to the unit of divisibility that are defined in the Asset definition file.



40
41
42
43
# File 'lib/openassets/protocol/transaction_output.rb', line 40

def asset_amount
  d = divisibility
  d > 0 ? (@asset_quantity.to_f / (10 ** d)).to_f : @asset_quantity
end

#divisibilityObject

get divisibility defined by asset definition file.



46
47
48
49
# File 'lib/openassets/protocol/transaction_output.rb', line 46

def divisibility
  return 0 if !valid_asset_definition? || @asset_definition.divisibility.nil?
  @asset_definition.divisibility
end

#oa_addressObject



83
84
85
86
87
88
89
90
91
# File 'lib/openassets/protocol/transaction_output.rb', line 83

def oa_address
  a = address
  return nil if a.nil?
  if a.is_a?(Array)
    a.map{|btc_address| address_to_oa_address(btc_address)}
  else
    address_to_oa_address(a)
  end
end

#proof_of_authenticityObject

Verify proof of authenticity.



52
53
54
# File 'lib/openassets/protocol/transaction_output.rb', line 52

def proof_of_authenticity
  valid_asset_definition? ? @asset_definition.proof_of_authenticity : false
end

#script_typeObject

get pubkey script type



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/openassets/protocol/transaction_output.rb', line 94

def script_type
  case @script.type
    when :hash160
      'pubkeyhash'
    when :pubkey
      'pubkey'
    when :multisig
      'multisig'
    when :p2sh
      'scripthash'
    when :op_return
      'nulldata'
    when :witness_v0_keyhash
      'witness_v0_keyhash'
    when :witness_v0_scripthash
      'witness_v0_scripthash'
    when :unknown
      'nonstandard'
    else
      'nonstandard'
  end
end

#to_hashObject

convert to hash object.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/openassets/protocol/transaction_output.rb', line 57

def to_hash
  {
      'address' =>  address,
      'oa_address' => oa_address,
      'script' => @script.to_payload.bth,
      'script_type' => script_type,
      'amount' => satoshi_to_coin(@value),
      'asset_id' => @asset_id,
      'asset_quantity' => @asset_quantity.to_s,
      'asset_amount' => asset_amount.to_s,
      'account' => @account,
      'asset_definition_url' => @asset_definition_url,
      'proof_of_authenticity' => proof_of_authenticity,
      'output_type' => OpenAssets::Protocol::OutputType.output_type_label(@output_type)
  }
end