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.



334
335
336
337
338
# File 'lib/bitcoin/builder.rb', line 334

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.



332
333
334
# File 'lib/bitcoin/builder.rb', line 332

def coinbase_data
  @coinbase_data
end

#keyObject (readonly)

Returns the value of attribute key.



332
333
334
# File 'lib/bitcoin/builder.rb', line 332

def key
  @key
end

#prev_out_value(value) ⇒ Object (readonly)

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



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

def prev_out_value
  @prev_out_value
end

#prev_scriptObject (readonly)

Returns the value of attribute prev_script.



332
333
334
# File 'lib/bitcoin/builder.rb', line 332

def prev_script
  @prev_script
end

#prev_txObject (readonly)

Returns the value of attribute prev_tx.



332
333
334
# File 'lib/bitcoin/builder.rb', line 332

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.



379
380
381
# File 'lib/bitcoin/builder.rb', line 379

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.



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

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)


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

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

#has_multiple_keys?Boolean

Returns:

  • (Boolean)


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

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

#is_witness_v0_keyhash?Boolean

Returns:

  • (Boolean)


418
419
420
# File 'lib/bitcoin/builder.rb', line 418

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) ⇒ 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.



344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/bitcoin/builder.rb', line 344

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

#prev_out_index(i) ⇒ Object

Index of the output in the #prev_out transaction.



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

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.



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

def prev_out_script script
  @prev_out_script = script
end

#sequence(s) ⇒ Object

Specify sequence. This is usually not needed.



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

def sequence s
  @sequence = s
end

#sign(sig_hash) ⇒ Object



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

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.



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

def signature_key key
  @key = key
end

#txinObject

Create the txin according to specified values



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

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



373
374
375
# File 'lib/bitcoin/builder.rb', line 373

def value
  @prev_out_value
end