Skip to content

Instantly share code, notes, and snippets.

@kannapoix
Created August 25, 2018 14:35
Show Gist options
  • Save kannapoix/7cc5510013ed3ca3c9599bf8905bffe0 to your computer and use it in GitHub Desktop.
Save kannapoix/7cc5510013ed3ca3c9599bf8905bffe0 to your computer and use it in GitHub Desktop.
p2sh transaction with bitcoin-ruby
# [WIP] sample of p2sh using bitcoin-ruby
# https://github.com/lian/bitcoin-ruby
require 'bitcoin'
include Bitcoin::Builder
include Bitcoin::Util
Bitcoin.network = :regtest
# Prepare elements
# Previous transaction
sender = Bitcoin::Key.from_base58 'cUMiRA75ze1kjUdsLfoNR4XXQ3iGoLhPXmrap88LKu1ybjX2bG78' #WIF
# Recipient
recipient = Bitcoin::Key.from_base58 'cVjhviGmo8rATiekEES2CMAYsNPxeagQeP5fbXkjHpjxTBdaN41K' #WIF
pubkey_hash = hash160 recipient.pub
redeem_script = Bitcoin::Script.to_hash160_script(pubkey_hash).unpack('H*').first
p2sh_address = encode_base58 redeem_script
tx = build_tx do |t|
t.input do |i|
i.prev_out 'ec4197176d9d9b32b983dcff861065c1f03d13bc6f43cebe169e679479863251' #Previous transaction id
i.prev_out_index 0
i.prev_out_script '76a914c7599dceb053650cdf7d4e184fc496cb542583c988ac'.htb
i.signature_key sender
end
t.output do |o|
o.value 199900000
o.to p2sh_address, :p2sh
end
end
# Transaction in hex
puts tx.to_payload.unpack('H*').first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment