forked from assemblymade/githubber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitsource.py
336 lines (275 loc) · 8.77 KB
/
bitsource.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import requests
import json
import time
import leb128
import node
import cointools
#node_url='199.188.192.144'# '127.0.0.1'#'71.198.63.116'##
def getblockmeta(n):
#get hash of block at height n
blockhash=node.connect('getblockhash',[n])
blockdata=node.connect('getblock',[blockhash])
return blockdata
def getrawtx(txhash):
txdata=node.connect('getrawtransaction',[txhash])
return txdata
def tx_lookup(txhash):
print txhash
c=node.connect('getrawtransaction',[txhash,1])
return c
def tx_inputs(txhash):
txdata=tx_lookup(txhash)
global prevtxidsq
automatic=False
txins=txdata['vin']
prevtxids=[]
for x in txins:
if 'txid' in x: #is normal transaction, not automatic block reward
prevtxids.append([x['txid'],x['vout']])
else:
height=node.connect('getblock',[txdata['blockhash']])['height']
prevtxids.append(height)
automatic=True
answer={}
if automatic==False:
#who was the destination of that txid,outputn pair?
answer['inputs']=[]
for a in prevtxids:
data=tx_lookup(a[0])
address=data['vout'][a[1]]['scriptPubKey']['addresses'][0] #ONLY ONE ADDRESS PER OUTPUT!!!
amount=data['vout'][a[1]]['value']
f={}
f['address']=address
f['amount']=amount
f['txid']=a[0]
answer['inputs'].append(f)
else:
answer['block']=prevtxids[0]
return answer
def gettx(txhash):
a=tx_lookup(txhash)
b=tx_inputs(txhash)
c= dict(a.items() + b.items())
return c
def txs_in_block(n):
starttime=time.time()
a=getblockmeta(n)
t=[]
j=0
g=str(len(a['tx']))
for x in a['tx']:
j=j+1
print str(j)+" / "+g
t.append(gettx(x))
duration=time.time()-starttime
print "This took: "+str(duration)+" seconds"
return t
def script_to_coloraddress(script):
ripehash=leb128.ripehash(script)
answer=cointools.base58CheckEncode(0x05, ripehash.decode('hex'))
return answer
def color_address(publicaddress):
a=requests.get('https://blockexplorer.com/q/addresstohash/')
hashed=a.content #REPLACE THIS METHOD
def read_tx(txhash):
r=tx_lookup(txhash)
m=-1
if 'vout' in r:
v=0
for x in r['vout']:
if 'value' in x:
v=v+x['value']
if x['scriptPubKey']['hex'][0:2]=='6a': #OP RETURN, only 1 per tx
d=x['scriptPubKey']['hex']
m=d[2:len(d)]
m=m.decode('hex')
m=m[1:len(m)]
#return m
#if m=='':
#return -1
return m, v
def op_return_in_block(n):
blockmeta=getblockmeta(n)
txhashes=blockmeta['tx']
results=[]
messages=[]
for tx in txhashes:
#print tx
n=read_tx(tx)
m=n[0]
if not m==-1:
messages.append([tx,m,n[1]])
return messages
def parse_colored_tx(metadata, txhash_with_index):
global d,e,g, count,f, hexmetadata
hexmetadata=metadata.encode('hex')
opcode=metadata[0:2]
results={}
if opcode=='OA': #then OA
results['type']='OA'
results['version']=metadata[2:4].encode('hex')
results['asset_count']=int(metadata[4:5].encode('hex'))
count=0
d=[]
for x in metadata[5:len(metadata)]:
r=leb128.hexpiecetobinary(x.encode('hex'))
d.append(r)
e=[]
r=[]
for x in d:
r.append(x)
if x[0]=='0':
e.append(r)
r=[]
f=[]
n=0
for x in e:
if n<int(results['asset_count'])+1:
f.append(leb128.decode(x))
count=count+len(x)
n=n+1
results['asset_quantities']=f[0:len(f)-1]
results['metadata_length']=f[len(f)-1]
results['metadata']=metadata[5+count:len(metadata)]
r=txhash_with_index.index(":")
markerposition=int(txhash_with_index[r+1:len(txhash_with_index)])
txhash=txhash_with_index[0:r]
txdata=tx_lookup(txhash)
txoutputs=txdata['vout']
results['issued']=[]
for i in range(0,markerposition):
h={}
h['quantity']=results['asset_quantities'][i]
print "checking script for "+str(txdata['vin'][0]['txid'])
#assumes first input is correct input....??!
script=tx_lookup(txdata['vin'][0]['txid'])['vout'][txdata['vin'][0]['vout']]['scriptPubKey']['hex']
print script
h['txhash_index']=txhash+":"+str(i)
h['color_address']=script_to_coloraddress(script)
h['destination_address']=txoutputs[i]['scriptPubKey']['addresses'][0] #one dest per output
h['btc']=int(txoutputs[i]['value']*100000000)
h['previous_inputs']="source:"+str(tx_lookup(txdata['vin'][0]['txid'])['vout'][txdata['vin'][0]['vout']]['scriptPubKey']['addresses'][0])
results['issued'].append(h)
results['transferred']=[]
for i in range(markerposition+1, len(txoutputs)):
if i<=len(results['asset_quantities']):
h={}
h['out_n']=i
h['txhash_index']=txhash+":"+str(i)
h['quantity']=results['asset_quantities'][i-1]
h['color_address']="" #FIGURE THIS PART OUT
h['previous_inputs']=[]
for x in txdata['vin']:
h['previous_inputs'].append(str(x['txid'])+":"+str(x['vout']))
# txdata['vin'][i-1]['txid']+":"+str(txdata['vin'][i-1]['vout']) #ASSUMES ONE TO ONE CORRESPONDENCE, NOT ALWAYS TRUE
print txoutputs[i-1]
h['destination_address']=txoutputs[i]['scriptPubKey']['addresses'][0]
h['btc']=int(txoutputs[i]['value']*100000000)
results['transferred'].append(h)
return results
def write_metadata(asset_quantities, otherdata):
#PLAINTEXT SCRIPT TO BE ENCODED INTO OP RETURN using Transaction.make_info_script
global encoded
#work in hex
result='4f410100' #OA + version 0100
assetcount=str(len(asset_quantities))
if len(assetcount)==1:
assetcount='0'+assetcount
result=result+assetcount
for asset in asset_quantities:
encoded=leb128.encode(asset)
j=''
for x in encoded:
r=str(hex(int(x,2)))
if len(r)==3:
r='0'+r[2:3]
else:
r=r[2:len(r)]
j=j+r
result=result+j
length=hex(len(otherdata))
if len(length)==3:
length=length[2:len(length)]
length='0'+length
else:
length=length[2:len(length)]
result=result+length
result=result+otherdata.encode('hex')
return result
#result=result+"\x"+assetcount
def oa_tx(txid, inputcolors):
txdata=tx_lookup(txid)
message=read_tx(txid)
message=message[0]
isOA=False
markerposition=-1
result={}
#find marker position and ascertain whether OA
for x in txdata['vout']:
if x['scriptPubKey']['hex'][0:2]=='6a' and isOA==False and x['scriptPubKey']['hex'][4:8]=='4f41':
isOA=True
markerposition= x['n']
#INPUT COLORS IS ARRAY OF DICTIONARIES [ {'color_address':'', 'amount':''}]
#Tabulate sums of inputs of different colors
inputsums={}
for x in inputcolors:
inputsums[x['color_address']]= inputsums[x['color_address']]+ x['amount']
#If it is OA
if isOA:
#get meta data
result['meta']=parse_colored_tx(message)
result['txid']= txdata['txid']
#Describe Issuing Outputs
result['issued']=[]
for i in range(0,markerposition):
k={}
amt= result['meta']['asset_quantities'][i]
k['amount']=amt
k['color_address']='' #FIGURE THIS PART OUT
k['destination_address']= txdata['vout'][i]['scriptPubKey']['addresses'][0] #ONLY EVER ONE ADDRESS PER OUTPUT
k['output_n']=i
result['issued'].append(k)
#Describe Transfer Outputs
result['transferred']=[]
for i in range(markerposition,len(txdata['vout'])):
k={}
supposedamt= result['meta']['assetquantities'][i] #MIGHT BE WRONG i
k['color_address']=''
if supposedamt<= inputsums[k['color_address']]: #THERE IS ENOUGH TO TRANSFER
amt=supposedamt
k['amount']=amt
k['destination_address']= txdata['vout'][i]['scriptPubKey']['addresses'][0]
k['output_n']=i
result['transferred'].append(k)
return result
def oa_in_block(n):
messages=op_return_in_block(n)
global markerposition
results=[]
for x in messages:
metadata=x[1]
r={}
isOA=False
txdata=tx_lookup(x[0]) #REDUNDANT CALL
#POSITION OF MARKER OUTPUT IN ALL OUTPUTS
markerposition=-1
for x in txdata['vout']:
#MIGHT BE ISSUE HERE WITH OP_PUSHDATA
if x['scriptPubKey']['hex'][0:2]=='6a' and isOA==False and x['scriptPubKey']['hex'][4:8]=='4f41':
#IS OPRETURN and is OA
isOA=True
markerposition= x['n']
if isOA:
r['meta']=parse_colored_tx(metadata)
r['txid']= txdata['txid']
r['issued']=[]
for i in range(0,markerposition):
k={}
amt= r['meta']['asset_quantities'][i]
k['amount']=amt
k['color_address']='' #FIGURE THIS PART OUT
k['destination_address']= txdata['vout'][i]['scriptPubKey']['addresses'][0] #ONLY EVER ONE ADDRESS PER OUTPUT
r['issued'].append(k)
r['transferred']=[]
results.append(r)
return results