Skip to content

Instantly share code, notes, and snippets.

@kannapoix
Created October 21, 2018 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kannapoix/6a517be2eaecf760614966f8fbbc01bd to your computer and use it in GitHub Desktop.
Save kannapoix/6a517be2eaecf760614966f8fbbc01bd to your computer and use it in GitHub Desktop.
Build p2pkh bitcoin transaction with bitcoin-ruby.
require 'bitcoin'
require 'pry'
require 'pp'
include Bitcoin::Util
include Bitcoin::Builder
Bitcoin::network = :regtest
prev_tx = 'b295fd37caf41d9d7b954cc533bee353d0fa6d33bd365087b1f77e3efe4637e6'
out_index = 1
prev_script_sig = '76a914f5f7d0e66941b1eb0899bd64e1d95aa4409906fa88ac'
key = Bitcoin::Key.from_base58 'cRwUogvobfV3nuH9svycYfqXcwQn1cNtNGRF8cfm6fTU13XdWhd3' # n3wWicQ381ij1PYz1XgF7HbPCB7UnYMtDM
recipient = Bitcoin::Key.from_base58 'cPN7UjEySEVfHftqV8PYJeZRsBDRZEEWj4yMrTJwCTTDiDXp8EV8'
pubkey = recipient.pub
pubkey_hash = hash160 pubkey
# bin_script = Bitcoin::Script.binary_from_string "OP_DUP OP_HASH160 #{pubkey_hash} OP_EQUALVERIFY OP_CHECKSIG"
# hex_script = bin_script.unpack 'H*'
tx = build_tx do |t|
t.input do |i|
i.prev_out prev_tx
i.prev_out_index out_index
i.prev_out_script prev_script_sig.htb
i.signature_key key
end
t.output do |o|
o.value 1999000000
o.to recipient.addr
end
end
p tx.to_payload.unpack('H*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment