OSDN Git Service

fix fee bug
[bytom/bytom-kit.git] / app / model / transaction.py
1 import requests
2 import json
3 from _pysha3 import sha3_256
4 from app.model import receiver
5
6 # submit_transaction broadcast raw transaction
7 # raw_transaction_str is signed transaction,
8 # network_str is mainnet or testnet
9 # test data 1:
10 #   raw_transaction_str: 070100010160015e0873eddd68c4ba07c9410984799928288ae771bdccc6d974e72c95727813461fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8094ebdc030101160014052620b86a6d5e07311d5019dffa3864ccc8a6bd630240312a052f36efb9826aa1021ec91bc6f125dd07f9c4bff87014612069527e15246518806b654d57fff8b6fe91866a19d5a2fb63a5894335fce92a7b4a7fcd340720e87ca3acdebdcad9a1d0f2caecf8ce0dbfc73d060807a210c6f225488347961402013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8082eee0020116001418028ef4f8b8c278907864a1977a5ee6707b2a6b00013cffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80b8b872011600142935e4869d0317d9701c80a02ecf888143cb9dd200
11 #   network_str: testnet
12 def submit_transaction(raw_transaction_str, network_str):
13     raw_transaction_dict = {
14         "transaction": raw_transaction_str
15     }
16     raw_transaction_json = json.dumps(raw_transaction_dict)
17     headers = {
18         "content-type": "application/json",
19         "accept": "application/json"
20     }
21     if network_str == "mainnet":
22         url = "https://blockmeta.com/api/v2/broadcast-transaction"
23     else:
24         url = "https://blockmeta.com/api/wisdom/broadcast-transaction"
25     response = requests.post(url, headers=headers, data=raw_transaction_json)
26     return {
27         "response": response.text[:-1]
28     }
29
30
31 def decode_raw_transaction(raw_transaction_str):
32     raw_transaction_dict = {
33         "raw_transaction": raw_transaction_str
34     }
35     raw_transaction_json = json.dumps(raw_transaction_dict)
36     headers = {
37         "content-type": "application/json",
38         "accept": "application/json"
39     }
40     url = 'http://127.0.0.1:9888/decode-raw-transaction'
41     response = requests.post(url, headers=headers, data=raw_transaction_json)
42     return {
43         "response": response.text[:-1]
44     }
45
46
47 def get_uvarint(uvarint_str):
48     uvarint_bytes = bytes.fromhex(uvarint_str)
49     x, s, i = 0, 0, 0
50     while True:
51         b = uvarint_bytes[i]
52         if b < 0x80:
53             if i > 9 or i == 9 and b > 1:
54                 return "overflow"
55             return x | int(b) << s, i + 1
56         x |= int(b & 0x7f) << s
57         s += 7
58         i += 1
59
60
61 '''
62 get_spend_output_id create tx_input spend output id
63 test data 1:
64   source_id_hexstr: 28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21
65   asset_id_hexstr: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
66   amount_int: 41250000000
67   source_position_int: 0
68   vmversion_int: 1
69   control_program_hexstr: 00149335b1cbd4a77b78e33315a0ed10a95b12e7ca48
70   spend_output_id_hexstr: f229ec6f403d586dc87aa2546bbe64c5f7b5f46eb13c6ee4823d03bc88a7cf17
71 '''
72 def get_spend_output_id(source_id_hexstr, asset_id_hexstr, amount_int, source_position_int, vmversion_int, control_program_hexstr):
73     amount_hexstr = amount_int.to_bytes(8, byteorder='little').hex()
74     source_position_hexstr = source_position_int.to_bytes(8, byteorder='little').hex()
75     vmversion_hexstr = vmversion_int.to_bytes(8, byteorder='little').hex()
76     cp_length_int = len(control_program_hexstr) // 2
77     cp_length_hexstr = cp_length_int.to_bytes((cp_length_int.bit_length() + 7) // 8, byteorder='little').hex()
78     sc_hexstr = source_id_hexstr + asset_id_hexstr + amount_hexstr + source_position_hexstr + vmversion_hexstr + cp_length_hexstr +  control_program_hexstr
79     innerhash_bytes = sha3_256(bytes.fromhex(sc_hexstr)).digest()
80     spend_bytes = b'entryid:output1:' + innerhash_bytes
81     spend_output_id_hexstr = sha3_256(spend_bytes).hexdigest()
82     return spend_output_id_hexstr
83
84 '''
85 get_input_id create tx input_id
86 test data 1:
87     spend_output_id_hexstr: f229ec6f403d586dc87aa2546bbe64c5f7b5f46eb13c6ee4823d03bc88a7cf17
88     input_id_hexstr: 6e3f378ed844b143a335e306f4ba26746157589c87e8fc8cba6463c566c56768
89 '''
90 def get_input_id(spend_output_id_hexstr):
91     innerhash_bytes = sha3_256(bytes.fromhex(spend_output_id_hexstr)).digest()
92     input_id_hexstr = sha3_256(b'entryid:spend1:' + innerhash_bytes).hexdigest()
93     return input_id_hexstr
94
95
96 def get_mux_id(prepare_mux_hexstr):
97     innerhash_bytes = sha3_256(bytes.fromhex(prepare_mux_hexstr)).digest()
98     mux_id_hexstr = sha3_256(b'entryid:mux1:' + innerhash_bytes).hexdigest()
99     return mux_id_hexstr
100
101
102 def get_output_id(prepare_output_id_hexstr):
103     innerhash_bytes = sha3_256(bytes.fromhex(prepare_output_id_hexstr)).digest()
104     output_id_hexstr = sha3_256(b'entryid:output1:' + innerhash_bytes).hexdigest()
105     return output_id_hexstr
106
107
108 def get_tx_id(prepare_tx_id_hexstr):
109     innerhash_bytes = sha3_256(bytes.fromhex(prepare_tx_id_hexstr)).digest()
110     tx_id_hexstr = sha3_256(b'entryid:txheader:' + innerhash_bytes).hexdigest()
111     return tx_id_hexstr
112
113
114 def get_issue_input_id(prepare_issue_hexstr):
115     innerhash_bytes = sha3_256(bytes.fromhex(prepare_issue_hexstr)).digest()
116     tx_id_hexstr = sha3_256(b'entryid:issuance1:' + innerhash_bytes).hexdigest()
117     return tx_id_hexstr
118
119
120 '''
121 decode_raw_tx decode raw transaction
122 testdata 1:
123     raw_tx_str: 070100010161015f28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d5990100011600149335b1cbd4a77b78e33315a0ed10a95b12e7ca48630240897e2d9d24a3b5faaed0579dee7597b401491595675f897504f8945b29d836235bd2fca72a3ad0cae814628973ebcd142d9d6cc92d0b2571b69e5370a98a340c208cb7fb3086f58db9a31401b99e8c658be66134fb9034de1d5c462679270b090702013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80f9f8bc98010116001406ce4b689ba026ffd3a7ca65d1d059546d4b78a000013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f01011600147929ef91997c827bebf60fa608f876ea27523c4700
124     network_str: solotnet
125     transaction: 
126 {
127   "fee": 20000000,
128   "inputs": [
129     {
130       "address": "sm1qjv6mrj755aah3cenzksw6y9ftvfw0jjgk0l2mw",
131       "amount": 41250000000,
132       "asset_definition": {},
133       "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
134       "control_program": "00149335b1cbd4a77b78e33315a0ed10a95b12e7ca48",
135       "input_id": "6e3f378ed844b143a335e306f4ba26746157589c87e8fc8cba6463c566c56768",
136       "spent_output_id": "f229ec6f403d586dc87aa2546bbe64c5f7b5f46eb13c6ee4823d03bc88a7cf17",
137       "type": "spend",
138       "witness_arguments": [
139         "897e2d9d24a3b5faaed0579dee7597b401491595675f897504f8945b29d836235bd2fca72a3ad0cae814628973ebcd142d9d6cc92d0b2571b69e5370a98a340c",
140         "8cb7fb3086f58db9a31401b99e8c658be66134fb9034de1d5c462679270b0907"
141       ]
142     }
143   ],
144   "outputs": [
145     {
146       "address": "sm1qqm8yk6ym5qn0l5a8efjar5ze23k5k79qnvtslj",
147       "amount": 40930000000,
148       "asset_definition": {},
149       "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
150       "control_program": "001406ce4b689ba026ffd3a7ca65d1d059546d4b78a0",
151       "id": "74c73266730d3c6ea32e8667ef9b867068736b84be240fe9fef205fa68bb7b95",
152       "position": 0,
153       "type": "control"
154     },
155     {
156       "address": "sm1q0y57lyve0jp8h6lkp7nq37rkagn4y0z8hvh6kq",
157       "amount": 300000000,
158       "asset_definition": {},
159       "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
160       "control_program": "00147929ef91997c827bebf60fa608f876ea27523c47",
161       "id": "f115a833d0c302a5006032858a7ed3987f0feb2daf2a9f849384950e4766af51",
162       "position": 1,
163       "type": "control"
164     }
165   ],
166   "size": 333,
167   "time_range": 0,
168   "tx_id": "814a73dd57bae67c604f9cbc696cbc42035577423408cb9267136ed971e2bf63",
169   "version": 1
170 }
171 '''
172 def decode_raw_tx(raw_tx_str, network_str):
173     tx = {
174         "fee": 0,
175         "inputs": [],
176         "outputs": [],
177         "size": 0,
178         "time_range": 0,
179         "tx_id": "",
180         "version": 0
181     }
182     tx['fee'] = 0
183     tx['size'] = len(raw_tx_str) // 2
184     length = 0
185     offset = 2
186     tx['version'], length = get_uvarint(raw_tx_str[offset:offset+16])
187     offset = offset + 2 * length
188     tx['time_range'], length = get_uvarint(raw_tx_str[offset:offset+16])
189     offset = offset + 2 * length
190     tx_input_amount, length = get_uvarint(raw_tx_str[offset:offset+8])
191     offset = offset + 2 * length
192     prepare_mux_hexstr = (tx_input_amount).to_bytes((tx_input_amount.bit_length() + 7) // 8, 'little').hex()
193     prepare_tx_id_hexstr = (tx['version']).to_bytes(8, 'little').hex() + (tx['time_range']).to_bytes(8, 'little').hex()
194     for _ in range(tx_input_amount):
195         _, length = get_uvarint(raw_tx_str[offset:offset+16])
196         offset = offset + 2 * length
197         _, length = get_uvarint(raw_tx_str[offset:offset+16])
198         offset = offset + 2 * length
199         input_type = int(raw_tx_str[offset:offset+2], 16)
200         offset += 2
201         if input_type == 0: # issue
202             tx_input = {
203                 "amount": 0,
204                 "asset_definition": "", # TODO:fix it!!
205                 "asset_id": "",
206                 "input_id": "",
207                 "issuance_program": "",
208                 "type": "",
209                 "witness_arguments": []
210             }
211             tx_input['type'] = "issue"
212             _, length = get_uvarint(raw_tx_str[offset:offset+16])
213             offset = offset + 2 * length
214             nonce = raw_tx_str[offset:offset+16]
215             offset += 16
216             nonce_hash_hexstr = sha3_256(bytes.fromhex(nonce)).hexdigest()
217             tx_input['asset_id'] = raw_tx_str[offset:offset+64]
218             offset += 64
219             tx_input['amount'], length = get_uvarint(raw_tx_str[offset:offset+16])
220             offset = offset + 2 * length
221             _, length = get_uvarint(raw_tx_str[offset:offset+16])
222             offset = offset + 2 * length
223             asset_definition_size, length = get_uvarint(raw_tx_str[offset:offset+16])
224             offset = offset + 2 * length
225             tx_input['asset_definition'] = bytes.fromhex(raw_tx_str[offset:offset+2*asset_definition_size]).decode()
226             offset = offset + 2 * asset_definition_size
227             _, length = get_uvarint(raw_tx_str[offset:offset+16])
228             offset = offset + 2 * length
229             issuance_program_length, length = get_uvarint(raw_tx_str[offset:offset+16])
230             offset = offset + 2 * length
231             tx_input['issuance_program'] = raw_tx_str[offset:offset+2*issuance_program_length]
232             offset = offset + 2 * issuance_program_length
233             witness_arguments_amount, length = get_uvarint(raw_tx_str[offset:offset+16])
234             offset = offset + 2 * length
235             for _ in range(witness_arguments_amount):
236                 argument_length, length = get_uvarint(raw_tx_str[offset:offset+16])
237                 offset = offset + 2 * length
238                 argument = raw_tx_str[offset:offset+2*argument_length]
239                 offset = offset + 2 * argument_length
240                 tx_input['witness_arguments'].append(argument)
241             prepare_issue_hexstr = nonce_hash_hexstr + tx_input['asset_id'] + (tx_input['amount']).to_bytes(8, byteorder='little').hex()
242             tx_input['input_id'] = get_issue_input_id(prepare_issue_hexstr)
243             tx['inputs'].append(tx_input)
244             prepare_mux_hexstr += tx_input['input_id'] + tx_input['asset_id'] + (tx_input['amount']).to_bytes(8, byteorder='little').hex() + '0000000000000000'
245         elif input_type == 1: # spend
246             tx_input = {
247                 "address": "",
248                 "amount": 0,
249                 "asset_definition": {},
250                 "asset_id": "",
251                 "control_program": "",
252                 "input_id": "",
253                 "spent_output_id": "",
254                 "type": "",
255                 "witness_arguments": []
256             }
257             tx_input['type'] = "spend"
258             _, length = get_uvarint(raw_tx_str[offset:offset+16])
259             offset = offset + 2 * length
260             source_id = raw_tx_str[offset:offset+64]
261             offset += 64
262             tx_input['asset_id'] = raw_tx_str[offset:offset+64]
263             offset += 64
264             tx_input['amount'], length = get_uvarint(raw_tx_str[offset:offset+16])
265             offset = offset + 2 * length
266             tx['fee'] += tx_input['amount']
267             source_positon, length = get_uvarint(raw_tx_str[offset:offset+16])
268             offset = offset + 2 * length
269             vmversion, length = get_uvarint(raw_tx_str[offset:offset+16])
270             offset = offset + 2 * length
271             control_program_length, length = get_uvarint(raw_tx_str[offset:offset+16])
272             offset = offset + 2 * length
273             tx_input['control_program'] = raw_tx_str[offset:offset+2*control_program_length]
274             offset = offset + 2 * control_program_length
275             tx_input['address'] = receiver.create_address(tx_input['control_program'], network_str)['address']
276             _, length = get_uvarint(raw_tx_str[offset:offset+16])
277             offset = offset + 2 * length
278             witness_arguments_amount, length = get_uvarint(raw_tx_str[offset:offset+16])
279             offset = offset + 2 * length
280             tx_input['spent_output_id'] = get_spend_output_id(source_id, tx_input['asset_id'], tx_input['amount'], source_positon, vmversion, tx_input['control_program'])
281             tx_input['input_id'] = get_input_id(tx_input['spent_output_id'])
282             for _ in range(witness_arguments_amount):
283                 argument_length, length = get_uvarint(raw_tx_str[offset:offset+16])
284                 offset = offset + 2 * length
285                 argument = raw_tx_str[offset:offset+2*argument_length]
286                 offset = offset + 2 * argument_length
287                 tx_input['witness_arguments'].append(argument)
288             tx['inputs'].append(tx_input)
289             prepare_mux_hexstr += tx_input['input_id'] + tx_input['asset_id'] + (tx_input['amount']).to_bytes(8, byteorder='little').hex() + '0000000000000000'
290         elif input_type == 2: # coinbase
291             pass
292     tx_output_amount, length = get_uvarint(raw_tx_str[offset:offset+16])
293     offset = offset + 2 * length
294     prepare_mux_hexstr += '0100000000000000' + '0151'
295     mux_id_hexstr = get_mux_id(prepare_mux_hexstr)
296     prepare_tx_id_hexstr += (tx_output_amount).to_bytes((tx_output_amount.bit_length() + 7) // 8, 'little').hex()
297     for i in range(tx_output_amount):
298         tx_output = {
299             "address": "",
300             "amount": 0,
301             "asset_definition": {},
302             "asset_id": "",
303             "control_program": "",
304             "id": "",
305             "position": 0,
306             "type": ""
307         }
308         tx_output['position'] = i
309         _, length = get_uvarint(raw_tx_str[offset:offset+16])
310         offset = offset + 2 * length
311         _, length = get_uvarint(raw_tx_str[offset:offset+16])
312         offset = offset + 2 * length
313         tx_output['asset_id'] = raw_tx_str[offset:offset+64]
314         offset = offset + 64
315         tx_output['amount'], length = get_uvarint(raw_tx_str[offset:offset+16])
316         offset = offset + 2 * length
317         if tx_output['asset_id'] == 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff':
318             tx['fee'] -= tx_output['amount']
319         _, length = get_uvarint(raw_tx_str[offset:offset+16])
320         offset = offset + 2 * length
321         control_program_length, length = get_uvarint(raw_tx_str[offset:offset+16])
322         offset = offset + 2 * length
323         tx_output['control_program'] = raw_tx_str[offset:offset+2*control_program_length]
324         offset = offset + 2 * control_program_length
325         tx_output['address'] = receiver.create_address(tx_output['control_program'], network_str)['address']
326         _, length = get_uvarint(raw_tx_str[offset:offset+16])
327         offset = offset + 2 * length
328         prepare_output_id_hexstr = mux_id_hexstr + tx_output['asset_id'] + (tx_output['amount']).to_bytes(8, byteorder='little').hex() + (i).to_bytes(8, byteorder='little').hex() + '0100000000000000' + (control_program_length).to_bytes((control_program_length.bit_length() + 7) // 8, 'little').hex() + tx_output['control_program']
329         tx_output['id'] = get_output_id(prepare_output_id_hexstr)
330         prepare_tx_id_hexstr += tx_output['id']
331         tx_output['type'] = 'control'
332         tx['outputs'].append(tx_output)
333     tx['tx_id'] = get_tx_id(prepare_tx_id_hexstr)
334     return tx