-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtc_rpc.py
179 lines (132 loc) · 5.5 KB
/
btc_rpc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import subprocess
import json
import random
WALLET = "testing"
def exec(script):
return subprocess.check_output(script, shell=True).decode('utf-8').strip()
def getnewaddress(legacy=True, wallet=WALLET):
if legacy:
x = exec("bitcoin-cli -rpcwallet="+wallet+" getnewaddress '' legacy")
else:
x = exec("bitcoin-cli -rpcwallet="+wallet+" getnewaddress")
return x
def dumprivkey(addr, wallet=WALLET):
x = exec("bitcoin-cli -rpcwallet="+wallet+" dumpprivkey "+addr)
return x
def sendtoaddress(address, amount, wallet=WALLET):
# print("Sending {} btc to {}".format(str(amount),address))
x = exec("bitcoin-cli -rpcwallet="+wallet +
" sendtoaddress "+address+" "+str(amount))
return x
def sendspecific(ins, out, wallet=WALLET):
'''
assumes ins and out are lists of dicts
ins = [{"txid":str(txid),"vout": int(vout)}]
out = [{address:float(amt)}]
assumes all inputs are from the same wallet
'''
command_string = 'bitcoin-cli -rpcwallet=' + \
wallet+' createrawtransaction \"['
for i in range(len(ins)):
if i != len(ins)-1:
command_string += '{\\"txid\\":\\"' + \
ins[i]["txid"]+'\\",\\"vout\\":'+str(ins[i]["vout"])+'},'
else:
command_string += '{\\"txid\\":\\"' + \
ins[i]["txid"]+'\\",\\"vout\\":'+str(ins[i]["vout"])+'}'
command_string += ']\" \"['
for i in range(len(out)):
if i != len(out)-1:
for k, v in out[i].items():
command_string += '{\\"'+k+'\\":'+str(v)+"},"
else:
for k, v in out[i].items():
command_string += '{\\"'+k+'\\":'+str(v)+"}"
command_string += ']\"'
txrawhex = exec(command_string)
command_string_signed = 'bitcoin-cli -rpcwallet='+wallet + \
' signrawtransactionwithkey \"'+txrawhex+'\" \"['
for i in range(len(ins)):
txid_details = gettransaction(ins[i]['txid'], wallet)["details"]
vout = ins[i]['vout']
privkey = ""
for j in range(len(txid_details)):
if txid_details[j]['vout'] == vout:
privkey = dumprivkey(txid_details[j]['address'], wallet)
break
if i != len(ins)-1:
command_string_signed += '\\"' + privkey + '\\",'
else:
command_string_signed += '\\"' + privkey + '\\"'
command_string_signed += ']\"'
signedtx = eval(exec(command_string_signed).replace(
'true', "True").replace('false', "False"))
if signedtx["complete"]:
txhash = sendrawtransaction(signedtx["hex"], wallet)
return txhash
else:
print("Error: Transaction not complete")
return signedtx
# return signedtx
def generatetoaddress(nblocks, address, wallet=WALLET):
x = exec("bitcoin-cli -rpcwallet="+wallet +
" generatetoaddress "+str(nblocks)+" "+address)
return x
def sendrawtransaction(txhex, wallet=WALLET):
x = exec("bitcoin-cli -rpcwallet="+wallet +
' sendrawtransaction \"'+txhex+'\"')
return x
def getscriptfromvout(txid, vout, wallet=WALLET):
decodedtx = eval(decodetransaction(txid, wallet))
return decodedtx["vout"][vout]["scriptPubKey"]["asm"]
def getvoutfromamount(txid, amount, wallet=WALLET):
decodedtx = eval(decodetransaction(txid, wallet))
for i in range(len(decodedtx["vout"])):
if decodedtx["vout"][i]["value"] == amount:
return i
return -1
def getbalance(wallet=WALLET):
x = exec("bitcoin-cli -rpcwallet="+wallet+" getbalance")
return x
def gettransaction(txid, wallet=WALLET):
x = exec("bitcoin-cli -rpcwallet="+wallet+" gettransaction "+txid)
# print(x)
return eval(x.replace("true", "True").replace("false", "False"))
def gettransactionhex(txid, wallet=WALLET):
x = gettransaction(txid, wallet)
return x["hex"]
def decodetransaction(txid, wallet=WALLET):
txhex = gettransactionhex(txid, wallet)
x = exec("bitcoin-cli -rpcwallet="+wallet+" decoderawtransaction "+txhex)
return x
def createwallet(wallet):
x = exec("bitcoin-cli createwallet "+wallet)
return x
def importaddress(addr, wallet=WALLET):
x = exec("bitcoin-cli -rpcwallet="+wallet +
" importaddress "+addr+" '' false")
return x
if __name__ == "__main__":
x = getnewaddress()
amt = random.randint(1, 3)
print("getting a new address", x)
print("sending {} btc to {}".format(amt, x))
txhash = sendtoaddress(x, amt, wallet="aliceWallet")
# print(txhash)
generatetoaddress(2, getnewaddress())
print(json.dumps(gettransaction(txhash), indent=4))
print("decode transaction {}".format(txhash))
print(decodetransaction(txhash))
# print("this the private key of address {}".format(x))
# # print(dumprivkey(x))
# txid1 = "7cb48e0e939f5e882c12405e2f2eca705e125325ac99b01d9460c6e1a7614be9"
# txid2 = "1d1878c13e91ddc34ff4a02ce4cb09fcd8141dcff9a7cfe43fecea70628291d0"
# amt = 40.005
# x=getnewaddress()
# # ins = [{"txid":str(txid),"vout": int(vout)}]
# ins = [{'txid':str(txid1),'vout': 0},{'txid':str(txid2),'vout': 0}]
# out = [{x:str(amt)}]
# generatetoaddress(2, getnewaddress())
# print("sending {} btc to {}".format(amt,x))
# print(sendspecific(ins, out, wallet="aliceWallet"))
# print("the vout of amount {} in txid {} is {}".format(20.012, "d31c3bc60cc0eef1a5af5f3067a2a4c89d1b686d1b2f62ce842a7d32fb765083", getvoutfromamount("d31c3bc60cc0eef1a5af5f3067a2a4c89d1b686d1b2f62ce842a7d32fb765083", 20.012,wallet="aliceWallet")))