Skip to content

Instantly share code, notes, and snippets.

@kannapoix
Created August 19, 2018 14:21
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/dfedbda045de61f172fa39b448899ed2 to your computer and use it in GitHub Desktop.
Save kannapoix/dfedbda045de61f172fa39b448899ed2 to your computer and use it in GitHub Desktop.
bitcoin-ruby-p2wpkh
require 'bitcoin'
include Bitcoin::Util
include Bitcoin::Builder
Bitcoin::network = :regtest
prev_tx = 'a6d52bb1b20b7fad41dad9b2bf4c25b76f8b18889f308a633ac82c913ca462aa'
out_index = 1
prev_script_sig = '76a91490a3e94240813b8b01859d9f2a12d028b53c44c788ac'
key = Bitcoin::Key.from_base58 'cU2ozeV3UcUTsmgmdywKJCjexjnQ53pASqov641zu5gBhu8uphsK'
pubkey = 'pubkey'
pubkey_hash = bitcoin_hash 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.script do |s|
s.type :witness
s.recipient 0, hash160(hex_script[0])
end
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