Skip to content

Instantly share code, notes, and snippets.

@9876691
Last active July 26, 2018 09:12
Show Gist options
  • Save 9876691/ba04e46a383b6193e7e84a119a932df5 to your computer and use it in GitHub Desktop.
Save 9876691/ba04e46a383b6193e7e84a119a932df5 to your computer and use it in GitHub Desktop.
hash_signature_for_input
def hash_signature_for_input(input_idx : Int32) : String
buffer = IO::Memory.new
# 1. Version
buffer.write_bytes(ver, IO::ByteFormat::LittleEndian)
# 2. Inputs
Transaction.write_var_int(buffer, inputs.size.to_u64)
inputs[input_idx].to_buffer(buffer)
# 3. Outputs
Transaction.write_var_int(buffer, outputs.size.to_u64)
outputs.each do |output|
output.to_buffer(buffer)
end
# 4. Lock time
buffer.write_bytes(lock_time, IO::ByteFormat::LittleEndian)
# 5. Hash type = 1 for SIG_HASH_ALL
buffer.write_bytes(1.to_u32, IO::ByteFormat::LittleEndian)
hash = OpenSSL::Digest.new("SHA256")
hash.update(buffer.to_slice)
hash1 = hash.digest
hash = OpenSSL::Digest.new("SHA256")
hash.update(hash1)
hash2 = hash.digest
return OnChain.to_hex(hash2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment