Class: BTC::AssetTransactionInput

Inherits:
Object
  • Object
show all
Defined in:
lib/btcruby/open_assets/asset_processor.rb,
lib/btcruby/open_assets/asset_transaction_input.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transaction_input: nil, asset_id: nil, value: nil, verified: false) ⇒ AssetTransactionInput

Returns a new instance of AssetTransactionInput.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 14

def initialize(transaction_input: nil, asset_id: nil, value: nil, verified: false)
  raise ArgumentError, "No transaction_input provided" if !transaction_input
  @transaction_input = transaction_input
  @index = transaction_input ? transaction_input.index : nil
  @asset_id = asset_id
  @value = value
  @verified = !!verified
end

Instance Attribute Details

#asset_idObject

Verified inputs with ‘asset_id == nil` are uncolored. Non-verified inputs are not known to have assets associated with them.



10
11
12
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 10

def asset_id
  @asset_id
end

#indexObject

Returns the value of attribute index.



5
6
7
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 5

def index
  @index
end

#previous_asset_transactionObject

Returns the value of attribute previous_asset_transaction.



355
356
357
# File 'lib/btcruby/open_assets/asset_processor.rb', line 355

def previous_asset_transaction
  @previous_asset_transaction
end

#transaction_inputObject

BTC::TransactionInput



6
7
8
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 6

def transaction_input
  @transaction_input
end

#valueObject

Returns the value of attribute value.



11
12
13
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 11

def value
  @value
end

#verifiedObject

true if asset_id and value are verified and can be used.



12
13
14
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 12

def verified
  @verified
end

Instance Method Details

#==(other) ⇒ Object



60
61
62
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 60

def ==(other)
  super(other) || self.transaction_input == other.transaction_input
end

#assets_dataObject



38
39
40
41
42
43
44
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 38

def assets_data
  data = "".b
  data << WireFormat.encode_uint8(@verified ? 1 : 0)
  data << WireFormat.encode_string(@asset_id ? @asset_id.hash : "".b)
  data << WireFormat.encode_int64le(@value.to_i)
  data
end

#colored?Boolean

Input is colored when it has AssetID and a positive value.

Returns:

  • (Boolean)


30
31
32
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 30

def colored?
  !!@asset_id && @value && @value > 0
end

#parse_assets_data(data, offset: 0) ⇒ Object

Returns total length read

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 47

def parse_assets_data(data, offset: 0)
  v, len = WireFormat.read_uint8(data: data, offset: offset)
  raise ArgumentError, "Invalid data: verified flag" if !v
  asset_hash, len = WireFormat.read_string(data: data, offset: len) # empty or 20 bytes
  raise ArgumentError, "Invalid data: asset hash" if !asset_hash
  value, len = WireFormat.read_int64le(data: data, offset: len)
  raise ArgumentError, "Invalid data: value" if !value
  @verified = (v == 1)
  @asset_id = asset_hash.bytesize > 0 ? AssetID.new(hash: asset_hash) : nil
  @value = value
  len
end

#verified?Boolean

Input is verified when we know for sure if it’s colored or not and if it is, what asset and how much is associated with it.

Returns:

  • (Boolean)


25
26
27
# File 'lib/btcruby/open_assets/asset_transaction_input.rb', line 25

def verified?
  !!@verified
end