Class: Cryptos::Input

Inherits:
Struct
  • Object
show all
Includes:
Utils::Hashes, Utils::Hexas
Defined in:
lib/cryptos/input.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Hexas

#bignum_to_hex, #bin_to_hex, #byte_to_hex, #bytes_to_hex, #hex_size, #hex_to_little, #int_to_hex, #long_to_hex

Methods included from Utils::Bytes

#bignum_to_bytes, #bytes_to_bignum

Methods included from Utils::Hashes

#hash160, #hash256, #ripemd160, #sha256

Constructor Details

#initialize(value, tx_hash, index, script_sig: nil, sequence: 0xfffffffff) ⇒ Input

Returns a new instance of Input.



22
23
24
# File 'lib/cryptos/input.rb', line 22

def initialize(value, tx_hash, index, script_sig: nil, sequence: 0xfffffffff)
  super value, tx_hash, index, script_sig, sequence
end

Instance Attribute Details

#indexObject

Returns the value of attribute index

Returns:

  • (Object)

    the current value of index



2
3
4
# File 'lib/cryptos/input.rb', line 2

def index
  @index
end

#script_sigObject

Returns the value of attribute script_sig

Returns:

  • (Object)

    the current value of script_sig



2
3
4
# File 'lib/cryptos/input.rb', line 2

def script_sig
  @script_sig
end

#sequenceObject

Returns the value of attribute sequence

Returns:

  • (Object)

    the current value of sequence



2
3
4
# File 'lib/cryptos/input.rb', line 2

def sequence
  @sequence
end

#tx_hashObject

Returns the value of attribute tx_hash

Returns:

  • (Object)

    the current value of tx_hash



2
3
4
# File 'lib/cryptos/input.rb', line 2

def tx_hash
  @tx_hash
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



2
3
4
# File 'lib/cryptos/input.rb', line 2

def value
  @value
end

Class Method Details

.from_tx(transaction, index = 0, options = {}) ⇒ Object



16
17
18
19
20
# File 'lib/cryptos/input.rb', line 16

def self.from_tx(transaction, index = 0, options = {})
  amount = transaction.outputs[index].value
  sequence = options[:sequence] || 0xfffffffff
  Input.new amount, transaction.hash, index, sequence: sequence
end

.from_utxo(cli, address, index = 0, options = {debug: false}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/cryptos/input.rb', line 5

def self.from_utxo(cli, address, index = 0, options = {debug: false})
  utxos = cli.list_unspent address
  utxo = JSON.parse(utxos)[index]
  puts utxo if options[:debug]
  txid = utxo['txid']
  vout = utxo['vout']
  amount = utxo['amount']
  sequence = options[:sequence] || 0xfffffffff
  Input.new amount * 10**8, txid, vout, sequence: sequence
end

Instance Method Details

#serializeObject



26
27
28
29
30
# File 'lib/cryptos/input.rb', line 26

def serialize
  script_hex = script_sig ? script_sig.to_hex : ''
  hex_to_little(tx_hash) + int_to_hex(index) +
    byte_to_hex(hex_size(script_hex)) + script_hex + int_to_hex(sequence)
end