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

Returns a new instance of TxInBuilder.



372
373
374
375
376
377
378
# File 'lib/bitcoin/builder.rb', line 372

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

Instance Attribute Details

#coinbase_dataObject (readonly)

Returns the value of attribute coinbase_data.



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

def coinbase_data
  @coinbase_data
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#prev_out_forkidObject (readonly)

Returns the value of attribute prev_out_forkid.



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

def prev_out_forkid
  @prev_out_forkid
end

#prev_out_script(script) ⇒ Object

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



405
406
407
# File 'lib/bitcoin/builder.rb', line 405

def prev_out_script(script) # rubocop:disable Style/TrivialAccessors
  @prev_out_script = script
end

#prev_out_value(value) ⇒ Object

Previous output’s value. Needed when only spend segwit utxo.



410
411
412
# File 'lib/bitcoin/builder.rb', line 410

def prev_out_value(value) # rubocop:disable Style/TrivialAccessors
  @prev_out_value = value
end

#prev_scriptObject (readonly)

Returns the value of attribute prev_script.



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

def prev_script
  @prev_script
end

#prev_txObject (readonly)

Returns the value of attribute prev_tx.



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

def prev_tx
  @prev_tx
end

#redeem_script(script) ⇒ Object

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



420
421
422
# File 'lib/bitcoin/builder.rb', line 420

def redeem_script(script) # rubocop:disable Style/TrivialAccessors
  @redeem_script = script
end

#sequence(s) ⇒ Object

Specify sequence. This is usually not needed.



425
426
427
# File 'lib/bitcoin/builder.rb', line 425

def sequence(s) # rubocop:disable Style/TrivialAccessors
  @sequence = s
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.



437
438
439
440
441
# File 'lib/bitcoin/builder.rb', line 437

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

#has_keys?Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


466
467
468
469
# File 'lib/bitcoin/builder.rb', line 466

def has_keys? # rubocop:disable Naming/PredicateName
  warn '[DEPRECATION] `TxInBuilder.has_keys?` is deprecated. Use `keys?` instead.'
  keys?
end

#has_multiple_keys?Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


456
457
458
459
460
# File 'lib/bitcoin/builder.rb', line 456

def has_multiple_keys? # rubocop:disable Naming/PredicateName
  warn '[DEPRECATION] `TxInBuilder.has_multiple_keys?` is deprecated. ' \
       'Use `multiple_keys?` instaed.'
  multiple_keys?
end

#is_witness_v0_keyhash?Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


475
476
477
478
479
# File 'lib/bitcoin/builder.rb', line 475

def is_witness_v0_keyhash? # rubocop:disable Naming/PredicateName
  warn '[DEPRECATION] `TxInBuilder.is_witness_v0_keyhash?` is deprecated. ' \
       'Use `witness_v0_keyhash?` instead.'
  witness_v0_keyhash?
end

#keys?Boolean

Returns:

  • (Boolean)


462
463
464
# File 'lib/bitcoin/builder.rb', line 462

def keys?
  @key && (multiple_keys? ? @key.all?(&:priv) : @key.priv)
end

#multiple_keys?Boolean

Returns:

  • (Boolean)


452
453
454
# File 'lib/bitcoin/builder.rb', line 452

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

#prev_out(tx, idx = nil, script = nil, prev_value = nil, prev_forkid = 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.



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/bitcoin/builder.rb', line 384

def prev_out(tx, idx = nil, script = nil, prev_value = nil, prev_forkid = nil)
  @prev_out_forkid = prev_forkid
  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
  @prev_out_value = prev_value if prev_value
end

#prev_out_index(i) ⇒ Object

Index of the output in the #prev_out transaction.



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

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

#sign(sig_hash) ⇒ Object



481
482
483
484
485
486
487
# File 'lib/bitcoin/builder.rb', line 481

def sign(sig_hash)
  if 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.



431
432
433
# File 'lib/bitcoin/builder.rb', line 431

def signature_key(key)
  @key = key
end

#txinObject

Create the txin according to specified values



444
445
446
447
448
449
450
# File 'lib/bitcoin/builder.rb', line 444

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

#valueObject



414
415
416
# File 'lib/bitcoin/builder.rb', line 414

def value
  @prev_out_value
end

#witness_v0_keyhash?Boolean

Returns:

  • (Boolean)


471
472
473
# File 'lib/bitcoin/builder.rb', line 471

def witness_v0_keyhash?
  @prev_out_script && Script.new(@prev_out_script).is_witness_v0_keyhash?
end