Class: Bitcoin::Builder::TxInBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/builder.rb

Overview

Create a Bitcoin::Protocol::TxIn used by TxBuilder#input.

Inputs need the transaction hash and the index of the output they spend. You can pass either the transaction, or just its hash (in hex form). To sign the input, builder also needs the pk_script of the previous output. If you specify a tx hash instead of the whole tx, you need to specify the output script separately.

t.input do |i|
  i.prev_out prev_tx  # previous transaction
  i.prev_out_index 0  # index of previous output
  i.signature_key key # Bitcoin::Key used to sign the input
end

t.input {|i| i.prev_out prev_tx, 0 }

If you want to spend a p2sh output, you also need to specify the redeem_script.

t.input do |i|
  i.prev_out prev_tx, 0
  i.redeem_script prev_out.redeem_script
end

If you want to spend a multisig output, just provide an array of keys to #signature_key.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTxInBuilder



323
324
325
326
327
# File 'lib/bitcoin/builder.rb', line 323

def initialize
  @txin = P::TxIn.new
  @prev_out_hash = "\x00" * 32
  @prev_out_index = 0
end

Instance Attribute Details

#coinbase_dataObject (readonly)

Returns the value of attribute coinbase_data.



321
322
323
# File 'lib/bitcoin/builder.rb', line 321

def coinbase_data
  @coinbase_data
end

#keyObject (readonly)

Returns the value of attribute key.



321
322
323
# File 'lib/bitcoin/builder.rb', line 321

def key
  @key
end

#prev_scriptObject (readonly)

Returns the value of attribute prev_script.



321
322
323
# File 'lib/bitcoin/builder.rb', line 321

def prev_script
  @prev_script
end

#prev_txObject (readonly)

Returns the value of attribute prev_tx.



321
322
323
# File 'lib/bitcoin/builder.rb', line 321

def prev_tx
  @prev_tx
end

#redeem_script(script) ⇒ Object (readonly)

Redeem script for P2SH output. To spend from a P2SH output, you need to provide the script with a hash matching the P2SH address.



358
359
360
# File 'lib/bitcoin/builder.rb', line 358

def redeem_script
  @redeem_script
end

Instance Method Details

#coinbase(data = nil) ⇒ Object

Specify that this is a coinbase input. Optionally set data. If this is set, no other options need to be given.



375
376
377
378
379
# File 'lib/bitcoin/builder.rb', line 375

def coinbase data = nil
  @coinbase_data = data || OpenSSL::Random.random_bytes(32)
  @prev_out_hash = "\x00" * 32
  @prev_out_index = 4294967295
end

#has_keys?Boolean



393
394
395
# File 'lib/bitcoin/builder.rb', line 393

def has_keys?
  @key && (has_multiple_keys? ? @key.all?(&:priv) : @key.priv)
end

#has_multiple_keys?Boolean



389
390
391
# File 'lib/bitcoin/builder.rb', line 389

def has_multiple_keys?
  @key.is_a?(Array)
end

#prev_out(tx, idx = nil, script = nil) ⇒ Object

Previous transaction that contains the output we want to use. You can either pass the transaction, or just the tx hash. If you pass only the hash, you need to pass the previous outputs script separately if you want the txin to be signed.



333
334
335
336
337
338
339
340
341
342
343
# File 'lib/bitcoin/builder.rb', line 333

def prev_out tx, idx = nil, script = nil
  if tx.is_a?(Bitcoin::P::Tx)
    @prev_tx = tx
    @prev_out_hash = tx.binary_hash
    @prev_out_script = tx.out[idx].pk_script  if idx
  else
    @prev_out_hash = tx.htb.reverse
  end
  @prev_out_script = script  if script
  @prev_out_index = idx  if idx
end

#prev_out_index(i) ⇒ Object

Index of the output in the #prev_out transaction.



346
347
348
349
# File 'lib/bitcoin/builder.rb', line 346

def prev_out_index i
  @prev_out_index = i
  @prev_out_script = @prev_tx.out[i].pk_script  if @prev_tx
end

#prev_out_script(script) ⇒ Object

Previous output’s pk_script. Needed when only the tx hash is specified as #prev_out.



352
353
354
# File 'lib/bitcoin/builder.rb', line 352

def prev_out_script script
  @prev_out_script = script
end

#sequence(s) ⇒ Object

Specify sequence. This is usually not needed.



363
364
365
# File 'lib/bitcoin/builder.rb', line 363

def sequence s
  @sequence = s
end

#sign(sig_hash) ⇒ Object



397
398
399
400
401
402
403
# File 'lib/bitcoin/builder.rb', line 397

def sign(sig_hash)
  if has_multiple_keys?
    @key.map {|k| k.sign(sig_hash) }
  else
    @key.sign(sig_hash)
  end
end

#signature_key(key) ⇒ Object

Bitcoin::Key used to sign the signature_hash for the input. see Bitcoin::Script.signature_hash_for_input and Bitcoin::Key.sign.



369
370
371
# File 'lib/bitcoin/builder.rb', line 369

def signature_key key
  @key = key
end

#txinObject

Create the txin according to specified values



382
383
384
385
386
387
# File 'lib/bitcoin/builder.rb', line 382

def txin
  @txin.prev_out = @prev_out_hash
  @txin.prev_out_index = @prev_out_index
  @txin.sequence = @sequence || "\xff\xff\xff\xff"
  @txin
end