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.



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

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.



319
320
321
# File 'lib/bitcoin/builder.rb', line 319

def coinbase_data
  @coinbase_data
end

#keyObject (readonly)

Returns the value of attribute key.



319
320
321
# File 'lib/bitcoin/builder.rb', line 319

def key
  @key
end

#prev_scriptObject (readonly)

Returns the value of attribute prev_script.



319
320
321
# File 'lib/bitcoin/builder.rb', line 319

def prev_script
  @prev_script
end

#prev_txObject (readonly)

Returns the value of attribute prev_tx.



319
320
321
# File 'lib/bitcoin/builder.rb', line 319

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.



356
357
358
# File 'lib/bitcoin/builder.rb', line 356

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.



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

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)


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

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

#has_multiple_keys?Boolean

Returns:

  • (Boolean)


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

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.



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

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.



344
345
346
347
# File 'lib/bitcoin/builder.rb', line 344

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.



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

def prev_out_script script
  @prev_out_script = script
end

#sequence(s) ⇒ Object

Specify sequence. This is usually not needed.



361
362
363
# File 'lib/bitcoin/builder.rb', line 361

def sequence s
  @sequence = s
end

#sign(sig_hash) ⇒ Object



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

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.



367
368
369
# File 'lib/bitcoin/builder.rb', line 367

def signature_key key
  @key = key
end

#txinObject

Create the txin according to specified values



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

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