1from bitcoin.main import *
2from bitcoin.transaction import *
3from bitcoin.bci import *
4from bitcoin.deterministic import *
5from bitcoin.blocks import *
6
7
8# Takes privkey, address, value (satoshis), fee (satoshis)
9def send(frm, to, value, fee=10000, **kwargs):
10    return sendmultitx(frm, to + ":" + str(value), fee, **kwargs)
11
12
13# Takes privkey, "address1:value1,address2:value2" (satoshis), fee (satoshis)
14def sendmultitx(frm, *args, **kwargs):
15    tv, fee = args[:-1], int(args[-1])
16    outs = []
17    outvalue = 0
18    for a in tv:
19        outs.append(a)
20        outvalue += int(a.split(":")[1])
21
22    u = unspent(privtoaddr(frm), **kwargs)
23    u2 = select(u, int(outvalue)+int(fee))
24    argz = u2 + outs + [privtoaddr(frm), fee]
25    tx = mksend(*argz)
26    tx2 = signall(tx, frm)
27    return pushtx(tx2, **kwargs)
28
29
30# Takes address, address, value (satoshis), fee(satoshis)
31def preparetx(frm, to, value, fee=10000, **kwargs):
32    tovalues = to + ":" + str(value)
33    return preparemultitx(frm, tovalues, fee, **kwargs)
34
35
36# Takes address, address:value, address:value ... (satoshis), fee(satoshis)
37def preparemultitx(frm, *args, **kwargs):
38    tv, fee = args[:-1], int(args[-1])
39    outs = []
40    outvalue = 0
41    for a in tv:
42        outs.append(a)
43        outvalue += int(a.split(":")[1])
44
45    u = unspent(frm, **kwargs)
46    u2 = select(u, int(outvalue)+int(fee))
47    argz = u2 + outs + [frm, fee]
48    return mksend(*argz)
49
50
51# BIP32 hierarchical deterministic multisig script
52def bip32_hdm_script(*args):
53    if len(args) == 3:
54        keys, req, path = args
55    else:
56        i, keys, path = 0, [], []
57        while len(args[i]) > 40:
58            keys.append(args[i])
59            i += 1
60        req = int(args[i])
61        path = map(int, args[i+1:])
62    pubs = sorted(map(lambda x: bip32_descend(x, path), keys))
63    return mk_multisig_script(pubs, req)
64
65
66# BIP32 hierarchical deterministic multisig address
67def bip32_hdm_addr(*args):
68    return scriptaddr(bip32_hdm_script(*args))
69
70
71# Setup a coinvault transaction
72def setup_coinvault_tx(tx, script):
73    txobj = deserialize(tx)
74    N = deserialize_script(script)[-2]
75    for inp in txobj["ins"]:
76        inp["script"] = serialize_script([None] * (N+1) + [script])
77    return serialize(txobj)
78
79
80# Sign a coinvault transaction
81def sign_coinvault_tx(tx, priv):
82    pub = privtopub(priv)
83    txobj = deserialize(tx)
84    subscript = deserialize_script(txobj['ins'][0]['script'])
85    oscript = deserialize_script(subscript[-1])
86    k, pubs = oscript[0], oscript[1:-2]
87    for j in range(len(txobj['ins'])):
88        scr = deserialize_script(txobj['ins'][j]['script'])
89        for i, p in enumerate(pubs):
90            if p == pub:
91                scr[i+1] = multisign(tx, j, subscript[-1], priv)
92        if len(filter(lambda x: x, scr[1:-1])) >= k:
93            scr = [None] + filter(lambda x: x, scr[1:-1])[:k] + [scr[-1]]
94        txobj['ins'][j]['script'] = serialize_script(scr)
95    return serialize(txobj)
96
97
98# Inspects a transaction
99def inspect(tx, **kwargs):
100    d = deserialize(tx)
101    isum = 0
102    ins = {}
103    for _in in d['ins']:
104        h = _in['outpoint']['hash']
105        i = _in['outpoint']['index']
106        prevout = deserialize(fetchtx(h, **kwargs))['outs'][i]
107        isum += prevout['value']
108        a = script_to_address(prevout['script'])
109        ins[a] = ins.get(a, 0) + prevout['value']
110    outs = []
111    osum = 0
112    for _out in d['outs']:
113        outs.append({'address': script_to_address(_out['script']),
114                     'value': _out['value']})
115        osum += _out['value']
116    return {
117        'fee': isum - osum,
118        'outs': outs,
119        'ins': ins
120    }
121
122
123def merkle_prove(txhash):
124    blocknum = str(get_block_height(txhash))
125    header = get_block_header_data(blocknum)
126    hashes = get_txs_in_block(blocknum)
127    i = hashes.index(txhash)
128    return mk_merkle_proof(header, hashes, i)
129