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.



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

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.



349
350
351
# File 'lib/bitcoin/builder.rb', line 349

def coinbase_data
  @coinbase_data
end

#keyObject (readonly)

Returns the value of attribute key.



349
350
351
# File 'lib/bitcoin/builder.rb', line 349

def key
  @key
end

#prev_out_forkidObject (readonly)

Returns the value of attribute prev_out_forkid.



349
350
351
# File 'lib/bitcoin/builder.rb', line 349

def prev_out_forkid
  @prev_out_forkid
end

#prev_out_value(value) ⇒ Object (readonly)

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



387
388
389
# File 'lib/bitcoin/builder.rb', line 387

def prev_out_value
  @prev_out_value
end

#prev_scriptObject (readonly)

Returns the value of attribute prev_script.



349
350
351
# File 'lib/bitcoin/builder.rb', line 349

def prev_script
  @prev_script
end

#prev_txObject (readonly)

Returns the value of attribute prev_tx.



349
350
351
# File 'lib/bitcoin/builder.rb', line 349

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.



397
398
399
# File 'lib/bitcoin/builder.rb', line 397

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.



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

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

Returns:

  • (Boolean)


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

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

#has_multiple_keys?Boolean

Returns:

  • (Boolean)


428
429
430
# File 'lib/bitcoin/builder.rb', line 428

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

#is_witness_v0_keyhash?Boolean

Returns:

  • (Boolean)


436
437
438
# File 'lib/bitcoin/builder.rb', line 436

def is_witness_v0_keyhash?
  @prev_out_script && Script.new(@prev_out_script).is_witness_v0_keyhash?
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.



361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/bitcoin/builder.rb', line 361

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.



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

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.



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

def prev_out_script script
  @prev_out_script = script
end

#sequence(s) ⇒ Object

Specify sequence. This is usually not needed.



402
403
404
# File 'lib/bitcoin/builder.rb', line 402

def sequence s
  @sequence = s
end

#sign(sig_hash) ⇒ Object



440
441
442
443
444
445
446
# File 'lib/bitcoin/builder.rb', line 440

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.



408
409
410
# File 'lib/bitcoin/builder.rb', line 408

def signature_key key
  @key = key
end

#txinObject

Create the txin according to specified values



421
422
423
424
425
426
# File 'lib/bitcoin/builder.rb', line 421

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

#valueObject



391
392
393
# File 'lib/bitcoin/builder.rb', line 391

def value
  @prev_out_value
end