-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSendMoney.py
203 lines (180 loc) · 5.58 KB
/
SendMoney.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
##########################################################################
#
# Demonstrate how to create a transaction.
#
# We will need a running bitcoind installation to manage our wallet
# and to retrieve UTXOs
#
# MIT license
#
# Copyright (c) 2018 christianb93
# Permission is hereby granted, free of charge, to
# any person obtaining a copy of this software and
# associated documentation files (the "Software"),
# to deal in the Software without restriction,
# including without limitation the rights to use,
# copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY
# OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
##########################################################################
import argparse
import binascii
import random
import btc.txn
import btc.script
import btc.keys
import btc.utils
####################################################
# Parse arguments
####################################################
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--amount",
type=float,
default="1.0",
help="Amount in BTC to be transferred"
)
parser.add_argument("--target",
default="mpV4bFDWN8NrWX9u3V47UgzxD9wSLQivwj",
help="Target address"
)
args=parser.parse_args()
return args
####################################################
# Main
####################################################
args = get_args()
#
# First we make an RPC call to retrieve unspent transaction output and
# select the outputs that we are going to spend
#
listunspent = btc.utils.rpcCall("listunspent")
#
# Now extract transaction IDs and store that in a list of
# dictionaries
#
# We split this list into one list of entries that are greater
# than the amount we want to transfer and one list of entries
# that are smaller
#
#
smaller = []
greater = []
amount_to_spend = float(args.amount)
for _ in listunspent:
if _['spendable']:
txid = _['txid']
vout = _['vout']
amount = float(_['amount'])
address = _['address']
coin = {'txid': txid,
'vout': vout,
'amount': amount,
'address' : address}
if amount > amount_to_spend:
greater.append(coin)
else:
smaller.append(coin)
#
# Next we sort the lists.
#
greater.sort(key=lambda entry: entry['amount'])
smaller.sort(key=lambda entry: entry['amount'], reverse=True)
#
# If greater is not emtpy, take the smallest (i.e. now first)
# element
#
if len(greater) > 0:
amount_funded = greater[0]['amount']
to_be_spent = [greater[0]]
else:
#
# We need to combine more than one transaction output
#
to_be_spent = []
amount_funded = 0
for _ in smaller:
if amount_funded < amount_to_spend:
to_be_spent.append(_)
amount_funded += _['amount']
if (amount_funded < amount_to_spend):
# Failed, clean up list
to_be_spent = []
if 0 == len(to_be_spent):
print("Could not fund transaction")
exit(1)
else:
print("Here is the list of transaction outputs that I will use: ")
print(to_be_spent)
#
# Now go through the resulting list and build a list of private
# keys. At the same time, we build a list of transaction outputs
#
txos = []
privateKeys = []
for _ in to_be_spent:
tx = btc.txn.txn()
raw = btc.utils.rpcCall("getrawtransaction", [_['txid']])
tx.deserialize(raw)
#
# Get private key using again an RPC call
#
privKey = btc.utils.rpcCall("dumpprivkey", [_['address']])
privKey = btc.keys.wifToPayloadBytes(privKey)
privKey = int.from_bytes(privKey, "big")
privateKeys.append(privKey)
txos.append(tx.getOutputs()[_['vout']])
#
# Next we create our transaction. First we create the transaction
# inputs. We leave the signature scripts empty for the time
# being
#
txn = btc.txn.txn()
for _ in to_be_spent:
txin = btc.txn.txin(prevTxid = _['txid'], vout = _['vout'])
txn.addInput(txin)
#
# Next we do the outputs. For the time being, we use only one output
# So we need to convert the address to a public key hash
#
publicKeyHash = btc.keys.ecAddressToPKH(args.target)
publicKeyHash = binascii.hexlify(publicKeyHash).decode('ascii')
#
# Create locking script
#
lockingScript = btc.script.scriptPubKey(scriptType = btc.script.SCRIPTTYPE_P2PKH,
pubKeyHash = publicKeyHash)
#
# and output
#
txout = btc.txn.txout(value = int(amount_to_spend * 100000000),
scriptPubKey = lockingScript)
txn.addOutput(txout)
#
# Sign it
#
txn = btc.script.signTransaction(txn, txos, privateKeys)
#
# and send it
#
print("Sending transaction")
raw = txn.serialize()
s = btc.utils.rpcCall("sendrawtransaction", [raw, True])
print("Done, resulting transaction ID: ")
print(s)