Skip to content

Instantly share code, notes, and snippets.

@kannapoix
Last active January 22, 2019 04:09
Show Gist options
  • Save kannapoix/3eb15442dc7ba220fd1ec3b544d02e6b to your computer and use it in GitHub Desktop.
Save kannapoix/3eb15442dc7ba220fd1ec3b544d02e6b to your computer and use it in GitHub Desktop.
Unlocking p2pkh-multisig script.
require 'pry'
require 'pp'
require 'bitcoin'
include Bitcoin::Builder
include Bitcoin::Util
Bitcoin::network = :regtest
# Prepare elements
# Previous transaction
# sender address 'cNBfQu3fAJ1QXMrLvd4PGNAd7RzjUj62kHbkL9owNF2zN2g3M4GB', 'cPJSC2SZqW3d9EWZTWtnDdGUQY7HFWcCH3cSm4kkkDpidJxp188N', 'cMujetBk2WXGwnyA1pWZHYLrGVwk1h5V4nYnCQhr9kJshWKPgNpZ'
potential_signers = [
'cNBfQu3fAJ1QXMrLvd4PGNAd7RzjUj62kHbkL9owNF2zN2g3M4GB',
'cPJSC2SZqW3d9EWZTWtnDdGUQY7HFWcCH3cSm4kkkDpidJxp188N',
'cMujetBk2WXGwnyA1pWZHYLrGVwk1h5V4nYnCQhr9kJshWKPgNpZ'
]
signers = potential_signers.sample(2).map{ |address| Bitcoin::Key.from_base58 address}
# Recipient
# recipient address muaJ6gXpMn6mUQxbAtgDUnnaNDVDfX6emo
# dumpprivkey get WIF privkey cNBfQu3fAJ1QXMrLvd4PGNAd7RzjUj62kHbkL9owNF2zN2g3M4GB
privkey = 'cTx5t224vzS8vXWpfZJwCMEvkGA89APnzy94umbscgovjLU7HitV'
recipient = Bitcoin::Key.from_base58(privkey)
tx = build_tx do |t|
t.input do |i|
i.prev_out 'da85de487c78bd312ab6806f6497f5ad1b9c480d3f71869b7472919959ba99be' #txid
i.prev_out_index 0
i.prev_out_script '522102ec35d437a41da7ab7f8b8024377ff6b532b49fb95804a73c41af6510663eaf4121039976de4fc156fe8dc5282d89eaf7ddc5e26494877eb8d995d732cd87791a45f421036647aa901188f4e784bd251e8ddb7802239b911ab66fa90165a3fe5320328ebf53ae'.htb
i.signature_key signers
end
t.output do |o|
o.value 1998000000
o.to recipient.addr
end
end
p tx.to_hash
p tx.to_payload.unpack('H*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment