Class: Bitcoin::SigHashGenerator::LegacySigHashGenerator

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

Overview

Legacy SigHash Generator

Instance Method Summary collapse

Instance Method Details

#generate(tx, input_index, hash_type, opts) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bitcoin/sighash_generator.rb', line 21

def generate(tx, input_index, hash_type, opts)
  output_script = opts[:script_code]
  ins = tx.inputs.map.with_index do |i, idx|
    if idx == input_index
      i.to_payload(output_script.delete_opcode(Bitcoin::Opcodes::OP_CODESEPARATOR))
    else
      case hash_type & 0x1f
      when SIGHASH_TYPE[:none], SIGHASH_TYPE[:single]
        i.to_payload(Bitcoin::Script.new, 0)
      else
        i.to_payload(Bitcoin::Script.new)
      end
    end
  end

  outs = tx.outputs.map(&:to_payload)
  out_size = Bitcoin.pack_var_int(tx.outputs.size)

  case hash_type & 0x1f
  when SIGHASH_TYPE[:none]
    outs = ''
    out_size = Bitcoin.pack_var_int(0)
  when SIGHASH_TYPE[:single]
    return "\x01".ljust(32, "\x00") if input_index >= tx.outputs.size
    outs = tx.outputs[0...(input_index + 1)].map.with_index { |o, idx| (idx == input_index) ? o.to_payload : o.to_empty_payload }.join
    out_size = Bitcoin.pack_var_int(input_index + 1)
  end

  ins = [ins[input_index]] unless hash_type & SIGHASH_TYPE[:anyonecanpay] == 0

  buf = [[tx.version].pack('V'), Bitcoin.pack_var_int(ins.size),
         ins, out_size, outs, [tx.lock_time, hash_type].pack('VV')].join

  Bitcoin.double_sha256(buf)
end