Created
September 8, 2016 18:44
-
-
Save kumrzz/111f8ea12af3528f496cbe678d153fc0 to your computer and use it in GitHub Desktop.
sending from 2x p2sh addresses in one(single) push to the bitcoin testnet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
OUTPUT: | |
private key#2: | |
9950aad11fc33c2d4a1f206e043c9755b377cf0bd7d6c93713143a3d23b0671d | |
public key#1: | |
0320869668e199a9499a41636983dfe337ca4878ee4170fcccd599b5674a02d055 | |
p2sh wallet: | |
2NAFVo3PXKoND1JvLkGug3FFmyzs9TxEy8X | |
... | |
private key#2: | |
d0ffdbc9ca80fb48970560d7069e6e47683c6de35bc559c64c1d6bdae72fad39 | |
public key#1: | |
038021bf19ef5effcdfa08e151e5b4a17aad7b84e10ffbc56c61bc1a486562c641 | |
p2sh wallet: | |
2NBRmugrQnJxzrSXFJob1tngrvfj4HNnePK | |
... | |
[Spendable<8E+2 mbtc "be5cffdd940f5d99dda7e5a61ba02cf7cca8ce0fe0e559006ae30dadedcdc53b:0" 0/0/0>, Spendable<7.9E+2 mbtc "3d09fc567bf6549ba1d474527b8d23c9c0be39ee92ce4935c48327940f486589:1" 0/0/0>] | |
here is the signed output transaction (XTN): | |
Tx [2a659aa9b8d254842cb06c06a8bf4eab7d85a327c4e3a83bcb61d88b67f2a151] | |
sending using pybitcointools: | |
{"status":"success","data":"2a659aa9b8d254842cb06c06a8bf4eab7d85a327c4e3a83bcb61d88b67f2a151","code":200,"message":""} | |
""" | |
from electrum import bitcoin, mnemonic | |
import json, pybitcointools | |
from pycoin.tx.pay_to import address_for_pay_to_script | |
from pycoin.tx.pay_to.ScriptMultisig import ScriptMultisig | |
from pycoin.convention import btc_to_satoshi | |
import hashlib, base58, io | |
from urllib2 import Request, urlopen, URLError | |
from pycoin.tx.pay_to import build_p2sh_lookup | |
from pycoin.serialize import h2b, h2b_rev | |
from pycoin.tx import Spendable | |
from pycoin.tx.tx_utils import create_signed_tx | |
def spendablez(address, netcode='XTN'): | |
#returns spendables, usable in txns | |
URL = "http://tbtc.blockr.io/api/v1/address/unspent/%s" % address | |
r = json.loads(urlopen(URL).read().decode("utf8")) | |
#spendablez = [] | |
for u in r.get("data", {}).get("unspent", []): | |
coin_value = btc_to_satoshi(u.get("amount")) | |
script = h2b(u.get("script")) | |
previous_hash = h2b_rev(u.get("tx")) | |
previous_index = u.get("n") | |
return Spendable(coin_value, script, previous_hash, previous_index) | |
def privkeytoWIF(self): | |
b58prefix = '\6F' | |
privprefix = 'ef' | |
magicbyte = 111 | |
electrum_pubkey = pybitcointools.privtopub(self) | |
privkey_compressed = self + "01" #https://en.bitcoin.it/wiki/Wallet_import_format | |
ExtendedPrivkeyComp = privprefix + privkey_compressed | |
key1sthash = hashlib.sha256(ExtendedPrivkeyComp.decode('hex')).digest() | |
chksum1 = hashlib.sha256(key1sthash).digest()[:4].encode('hex') | |
bytestyleWIF = ExtendedPrivkeyComp + chksum1 | |
return base58.b58encode(bytestyleWIF.decode('hex')) | |
wifs = [] | |
p2shlkup1 = [] | |
for x in range(0, 2): | |
if x == 0: | |
mswallet = '2NAFVo3PXKoND1JvLkGug3FFmyzs9TxEy8X' | |
k2 = '57b5c330a63bb4dd2536c6a851d8e4a1c71e4467f5d8bbdb28ed1cbaecc687aa' | |
prv1 = '9950aad11fc33c2d4a1f206e043c9755b377cf0bd7d6c93713143a3d23b0671d' | |
if x == 1: | |
mswallet = '2NBRmugrQnJxzrSXFJob1tngrvfj4HNnePK' | |
k2 = '1315e30b78152f96fd0aafd38d1d506a15264a0feca352c3ccec74049fbe4844' | |
prv1 = 'd0ffdbc9ca80fb48970560d7069e6e47683c6de35bc559c64c1d6bdae72fad39' | |
print 'private key#2:' | |
print k2 | |
pub1 = pybitcointools.privtopub(prv1) | |
print 'public key#1:' | |
print pybitcointools.encode_pubkey(pub1, 'hex_compressed') | |
# create sec_keys (these are public keys, streamed using the SEC format) | |
p1 = pybitcointools.encode_pubkey(pub1, 'bin') | |
p2 = pybitcointools.encode_pubkey(pybitcointools.privtopub(k2), 'bin') | |
public_key_sec_list = [p1, p2] | |
# create the 1-of-2 multisig script (any 1 signature can release the funds) | |
pay_to_multisig_script = ScriptMultisig(1, public_key_sec_list).script() | |
address = address_for_pay_to_script(pay_to_multisig_script, netcode='XTN')#BTC or XTN | |
print 'p2sh wallet:' | |
print address | |
wifs.append(privkeytoWIF(k2)) | |
p2shlkup1.append(pay_to_multisig_script) | |
if x == 0: | |
spendables = [spendablez(mswallet)] | |
if x == 1: | |
spendables.append(spendablez(mswallet)) | |
print '...' | |
print spendables | |
if spendables != []: | |
payables=['mt8kNQiejsjuZWAn5hse3nESfWb4hoadRU'] | |
p2sh_lookup = build_p2sh_lookup(p2shlkup1) | |
tx = create_signed_tx(spendables, payables=payables, wifs=(wifs), netcode='XTN', p2sh_lookup=p2sh_lookup) | |
print 'here is the signed output transaction (' + 'XTN' + '): ' | |
print(tx) | |
print 'sending using pybitcointools: ' | |
pbtctNet = 'testnet' | |
print pybitcointools.blockr_pushtx(tx.as_hex(), network=pbtctNet) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment