Class: Coloredcoins::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/coloredcoins/transaction.rb

Direct Known Subclasses

MultisigTx

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex) ⇒ Transaction

Returns a new instance of Transaction.



18
19
20
# File 'lib/coloredcoins/transaction.rb', line 18

def initialize(hex)
  @tx = Bitcoin::P::Tx.new([hex].pack('H*'))
end

Instance Attribute Details

#txObject (readonly)

Returns the value of attribute tx.



3
4
5
# File 'lib/coloredcoins/transaction.rb', line 3

def tx
  @tx
end

Class Method Details

.build_key(key) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/coloredcoins/transaction.rb', line 5

def self.build_key(key)
  return key if key.is_a?(Array)
  key = Bitcoin::Key.from_base58(key) unless key.is_a?(Bitcoin::Key)
  key
rescue RuntimeError => e
  raise InvalidKeyError, 'Invalid key' if e.message == 'Invalid version'
end

.build_sigs(key, sig_hash) ⇒ Object



13
14
15
16
# File 'lib/coloredcoins/transaction.rb', line 13

def self.build_sigs(key, sig_hash)
  key = [key] unless key.is_a?(Array)
  key.map { |k| k.sign(sig_hash) }
end

Instance Method Details

#broadcastObject



36
37
38
39
40
41
# File 'lib/coloredcoins/transaction.rb', line 36

def broadcast
  response = Coloredcoins.broadcast(to_hex)
  return response unless response[:txid]
  txid = response[:txid]
  txid.is_a?(Array) ? txid.first[:txid] : txid
end

#new_txObject



27
28
29
30
31
32
33
34
# File 'lib/coloredcoins/transaction.rb', line 27

def new_tx
  @new_tx = Bitcoin::P::Tx.new
  @new_tx.ver = tx.ver
  @new_tx.lock_time = tx.lock_time
  tx.in.each  { |input|   @new_tx.add_in(input)   }
  tx.out.each { |output|  @new_tx.add_out(output) }
  @new_tx
end

#to_hexObject



22
23
24
25
# File 'lib/coloredcoins/transaction.rb', line 22

def to_hex
  raw_tx = Bitcoin::P::Tx.binary_from_json(new_tx.to_json)
  raw_tx.unpack('H*').first
end