• R/O
  • HTTP
  • SSH
  • HTTPS

pybtm: Commit

Python3 implementation of the Bytom protocol. https://pypi.org/project/pybtm/


Commit MetaInfo

Révisionbb8ec1166e08a0831e272b1037a4ba2a55d1d44c (tree)
l'heure2019-04-10 17:17:06
AuteurChengcheng Zhang <943420582@qq.c...>
CommiterChengcheng Zhang

Message de Log

fix decode-raw-tx bug

Change Summary

Modification

--- a/README.md
+++ b/README.md
@@ -444,7 +444,7 @@ decode_raw_tx decode raw transaction.
444444
445445 Parameter:
446446
447-- raw_tx_str: raw transaction, type is hex string.
447+- raw_transaction_str: raw transaction, type is hex string.
448448 - network_str: 3 types of network is available: mainnet, testnet and solonet.
449449
450450 Return:
--- a/pybtm/__init__.py
+++ b/pybtm/__init__.py
@@ -1,2 +1,2 @@
11 name = "pybtm"
2-version = "0.1.6"
\ No newline at end of file
2+version = "0.1.7"
\ No newline at end of file
--- a/pybtm/transaction.py
+++ b/pybtm/transaction.py
@@ -122,7 +122,7 @@ def get_coinbase_input_id(prepare_coinbase_input_id_hexstr):
122122 '''
123123 decode_raw_tx decode raw transaction
124124 testdata 1:
125- raw_tx_str: 070100010161015f28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d5990100011600149335b1cbd4a77b78e33315a0ed10a95b12e7ca48630240897e2d9d24a3b5faaed0579dee7597b401491595675f897504f8945b29d836235bd2fca72a3ad0cae814628973ebcd142d9d6cc92d0b2571b69e5370a98a340c208cb7fb3086f58db9a31401b99e8c658be66134fb9034de1d5c462679270b090702013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80f9f8bc98010116001406ce4b689ba026ffd3a7ca65d1d059546d4b78a000013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f01011600147929ef91997c827bebf60fa608f876ea27523c4700
125+ raw_transaction_str: 070100010161015f28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d5990100011600149335b1cbd4a77b78e33315a0ed10a95b12e7ca48630240897e2d9d24a3b5faaed0579dee7597b401491595675f897504f8945b29d836235bd2fca72a3ad0cae814628973ebcd142d9d6cc92d0b2571b69e5370a98a340c208cb7fb3086f58db9a31401b99e8c658be66134fb9034de1d5c462679270b090702013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80f9f8bc98010116001406ce4b689ba026ffd3a7ca65d1d059546d4b78a000013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f01011600147929ef91997c827bebf60fa608f876ea27523c4700
126126 network_str: solonet
127127 transaction:
128128 {
@@ -171,7 +171,7 @@ testdata 1:
171171 "version": 1
172172 }
173173 '''
174-def decode_raw_tx(raw_tx_str, network_str):
174+def decode_raw_tx(raw_transaction_str, network_str):
175175 tx = {
176176 "fee": 0,
177177 "inputs": [],
@@ -182,23 +182,23 @@ def decode_raw_tx(raw_tx_str, network_str):
182182 "version": 0
183183 }
184184 tx['fee'] = 0
185- tx['size'] = len(raw_tx_str) // 2
185+ tx['size'] = len(raw_transaction_str) // 2
186186 length = 0
187187 offset = 2
188- tx['version'], length = get_uvarint(raw_tx_str[offset:offset+18])
188+ tx['version'], length = get_uvarint(raw_transaction_str[offset:offset+18])
189189 offset = offset + 2 * length
190- tx['time_range'], length = get_uvarint(raw_tx_str[offset:offset+18])
190+ tx['time_range'], length = get_uvarint(raw_transaction_str[offset:offset+18])
191191 offset = offset + 2 * length
192- tx_input_amount, length = get_uvarint(raw_tx_str[offset:offset+8])
192+ tx_input_amount, length = get_uvarint(raw_transaction_str[offset:offset+8])
193193 offset = offset + 2 * length
194194 prepare_mux_hexstr = (tx_input_amount).to_bytes((tx_input_amount.bit_length() + 7) // 8, 'little').hex()
195195 prepare_tx_id_hexstr = (tx['version']).to_bytes(8, 'little').hex() + (tx['time_range']).to_bytes(8, 'little').hex()
196196 for _ in range(tx_input_amount):
197- _, length = get_uvarint(raw_tx_str[offset:offset+18])
197+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
198198 offset = offset + 2 * length
199- _, length = get_uvarint(raw_tx_str[offset:offset+18])
199+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
200200 offset = offset + 2 * length
201- input_type = int(raw_tx_str[offset:offset+2], 16)
201+ input_type = int(raw_transaction_str[offset:offset+2], 16)
202202 offset += 2
203203 if input_type == 0: # issue
204204 tx_input = {
@@ -211,33 +211,33 @@ def decode_raw_tx(raw_tx_str, network_str):
211211 "witness_arguments": []
212212 }
213213 tx_input['type'] = "issue"
214- _, length = get_uvarint(raw_tx_str[offset:offset+18])
214+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
215215 offset = offset + 2 * length
216- nonce = raw_tx_str[offset:offset+16]
216+ nonce = raw_transaction_str[offset:offset+16]
217217 offset += 16
218218 nonce_hash_hexstr = sha3_256(bytes.fromhex(nonce)).hexdigest()
219- tx_input['asset_id'] = raw_tx_str[offset:offset+64]
219+ tx_input['asset_id'] = raw_transaction_str[offset:offset+64]
220220 offset += 64
221- tx_input['amount'], length = get_uvarint(raw_tx_str[offset:offset+18])
221+ tx_input['amount'], length = get_uvarint(raw_transaction_str[offset:offset+18])
222222 offset = offset + 2 * length
223- _, length = get_uvarint(raw_tx_str[offset:offset+18])
223+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
224224 offset = offset + 2 * length
225- asset_definition_size, length = get_uvarint(raw_tx_str[offset:offset+18])
225+ asset_definition_size, length = get_uvarint(raw_transaction_str[offset:offset+18])
226226 offset = offset + 2 * length
227- tx_input['asset_definition'] = bytes.fromhex(raw_tx_str[offset:offset+2*asset_definition_size]).decode()
227+ tx_input['asset_definition'] = bytes.fromhex(raw_transaction_str[offset:offset+2*asset_definition_size]).decode()
228228 offset = offset + 2 * asset_definition_size
229- _, length = get_uvarint(raw_tx_str[offset:offset+18])
229+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
230230 offset = offset + 2 * length
231- issuance_program_length, length = get_uvarint(raw_tx_str[offset:offset+18])
231+ issuance_program_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
232232 offset = offset + 2 * length
233- tx_input['issuance_program'] = raw_tx_str[offset:offset+2*issuance_program_length]
233+ tx_input['issuance_program'] = raw_transaction_str[offset:offset+2*issuance_program_length]
234234 offset = offset + 2 * issuance_program_length
235- witness_arguments_amount, length = get_uvarint(raw_tx_str[offset:offset+18])
235+ witness_arguments_amount, length = get_uvarint(raw_transaction_str[offset:offset+18])
236236 offset = offset + 2 * length
237237 for _ in range(witness_arguments_amount):
238- argument_length, length = get_uvarint(raw_tx_str[offset:offset+18])
238+ argument_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
239239 offset = offset + 2 * length
240- argument = raw_tx_str[offset:offset+2*argument_length]
240+ argument = raw_transaction_str[offset:offset+2*argument_length]
241241 offset = offset + 2 * argument_length
242242 tx_input['witness_arguments'].append(argument)
243243 prepare_issue_hexstr = nonce_hash_hexstr + tx_input['asset_id'] + (tx_input['amount']).to_bytes(8, byteorder='little').hex()
@@ -259,36 +259,40 @@ def decode_raw_tx(raw_tx_str, network_str):
259259 "witness_arguments": []
260260 }
261261 tx_input['type'] = "spend"
262- _, length = get_uvarint(raw_tx_str[offset:offset+18])
262+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
263263 offset = offset + 2 * length
264- source_id = raw_tx_str[offset:offset+64]
264+ source_id = raw_transaction_str[offset:offset+64]
265265 offset += 64
266- tx_input['asset_id'] = raw_tx_str[offset:offset+64]
266+ tx_input['asset_id'] = raw_transaction_str[offset:offset+64]
267267 offset += 64
268- tx_input['amount'], length = get_uvarint(raw_tx_str[offset:offset+18])
268+ tx_input['amount'], length = get_uvarint(raw_transaction_str[offset:offset+18])
269269 offset = offset + 2 * length
270270 tx['fee'] += tx_input['amount']
271- source_positon, length = get_uvarint(raw_tx_str[offset:offset+18])
271+ source_positon, length = get_uvarint(raw_transaction_str[offset:offset+18])
272272 offset = offset + 2 * length
273- vmversion, length = get_uvarint(raw_tx_str[offset:offset+18])
273+ vmversion, length = get_uvarint(raw_transaction_str[offset:offset+18])
274274 offset = offset + 2 * length
275- control_program_length, length = get_uvarint(raw_tx_str[offset:offset+18])
275+ control_program_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
276276 offset = offset + 2 * length
277- tx_input['control_program'] = raw_tx_str[offset:offset+2*control_program_length]
277+ tx_input['control_program'] = raw_transaction_str[offset:offset+2*control_program_length]
278278 offset = offset + 2 * control_program_length
279- tx_input['address'] = get_address(tx_input['control_program'], network_str)
280- _, length = get_uvarint(raw_tx_str[offset:offset+18])
279+ tx_input['address'] = receiver.create_address(tx_input['control_program'], network_str)['address']
280+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
281281 offset = offset + 2 * length
282- witness_arguments_amount, length = get_uvarint(raw_tx_str[offset:offset+18])
282+ witness_arguments_amount, length = get_uvarint(raw_transaction_str[offset:offset+18])
283283 offset = offset + 2 * length
284+ if witness_arguments_amount == 1:
285+ offset = offset + 2
286+ tx_input['witness_arguments'] = None
287+ else:
288+ for _ in range(witness_arguments_amount):
289+ argument_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
290+ offset = offset + 2 * length
291+ argument = raw_transaction_str[offset:offset+2*argument_length]
292+ offset = offset + 2 * argument_length
293+ tx_input['witness_arguments'].append(argument)
284294 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'])
285295 tx_input['input_id'] = get_input_id(tx_input['spent_output_id'])
286- for _ in range(witness_arguments_amount):
287- argument_length, length = get_uvarint(raw_tx_str[offset:offset+18])
288- offset = offset + 2 * length
289- argument = raw_tx_str[offset:offset+2*argument_length]
290- offset = offset + 2 * argument_length
291- tx_input['witness_arguments'].append(argument)
292296 tx['inputs'].append(tx_input)
293297 prepare_mux_hexstr += tx_input['input_id'] + tx_input['asset_id'] + (tx_input['amount']).to_bytes(8, byteorder='little').hex() + '0000000000000000'
294298 prepare_mux_hexstr += '0100000000000000' + '0151'
@@ -304,17 +308,17 @@ def decode_raw_tx(raw_tx_str, network_str):
304308 "witness_arguments": []
305309 }
306310 tx_input['type'] = "coinbase"
307- arbitrary_length, length = get_uvarint(raw_tx_str[offset:offset+18])
308- prepare_coinbase_input_id_hexstr = raw_tx_str[offset:offset+2*length]
311+ arbitrary_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
312+ prepare_coinbase_input_id_hexstr = raw_transaction_str[offset:offset+2*length]
309313 offset = offset + 2 * length
310- tx_input['arbitrary'] = raw_tx_str[offset:offset+2*arbitrary_length]
314+ tx_input['arbitrary'] = raw_transaction_str[offset:offset+2*arbitrary_length]
311315 prepare_coinbase_input_id_hexstr += tx_input['arbitrary']
312316 offset = offset + 2 * arbitrary_length
313317 tx_input['input_id'] = get_coinbase_input_id(prepare_coinbase_input_id_hexstr)
314318 offset = offset + 2
315319 tx['inputs'].append(tx_input)
316320 prepare_mux_hexstr += tx_input['input_id'] + 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
317- tx_output_amount, length = get_uvarint(raw_tx_str[offset:offset+18])
321+ tx_output_amount, length = get_uvarint(raw_transaction_str[offset:offset+18])
318322 offset = offset + 2 * length
319323 prepare_tx_id_hexstr += (tx_output_amount).to_bytes((tx_output_amount.bit_length() + 7) // 8, 'little').hex()
320324 for i in range(tx_output_amount):
@@ -329,27 +333,27 @@ def decode_raw_tx(raw_tx_str, network_str):
329333 "type": ""
330334 }
331335 tx_output['position'] = i
332- _, length = get_uvarint(raw_tx_str[offset:offset+18])
336+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
333337 offset = offset + 2 * length
334- _, length = get_uvarint(raw_tx_str[offset:offset+18])
338+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
335339 offset = offset + 2 * length
336- tx_output['asset_id'] = raw_tx_str[offset:offset+64]
340+ tx_output['asset_id'] = raw_transaction_str[offset:offset+64]
337341 offset = offset + 64
338- tx_output['amount'], length = get_uvarint(raw_tx_str[offset:offset+18])
342+ tx_output['amount'], length = get_uvarint(raw_transaction_str[offset:offset+18])
339343 if tx_input['type'] == "coinbase":
340344 prepare_mux_hexstr = prepare_mux_hexstr + (tx_output['amount']).to_bytes(8, byteorder='little').hex() + '0000000000000000' + '0100000000000000' + '0151'
341345 mux_id_hexstr = get_mux_id(prepare_mux_hexstr)
342346 offset = offset + 2 * length
343347 if tx_output['asset_id'] == 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff':
344348 tx['fee'] -= tx_output['amount']
345- _, length = get_uvarint(raw_tx_str[offset:offset+18])
349+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
346350 offset = offset + 2 * length
347- control_program_length, length = get_uvarint(raw_tx_str[offset:offset+18])
351+ control_program_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
348352 offset = offset + 2 * length
349- tx_output['control_program'] = raw_tx_str[offset:offset+2*control_program_length]
353+ tx_output['control_program'] = raw_transaction_str[offset:offset+2*control_program_length]
350354 offset = offset + 2 * control_program_length
351- tx_output['address'] = get_address(tx_output['control_program'], network_str)
352- _, length = get_uvarint(raw_tx_str[offset:offset+18])
355+ tx_output['address'] = receiver.create_address(tx_output['control_program'], network_str)['address']
356+ _, length = get_uvarint(raw_transaction_str[offset:offset+18])
353357 offset = offset + 2 * length
354358 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']
355359 tx_output['id'] = get_output_id(prepare_output_id_hexstr)
@@ -359,4 +363,4 @@ def decode_raw_tx(raw_tx_str, network_str):
359363 if tx_input['type'] == "coinbase":
360364 tx['fee'] = 0
361365 tx['tx_id'] = get_tx_id(prepare_tx_id_hexstr)
362- return tx
366+ return tx
\ No newline at end of file
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
55
66 setuptools.setup(
77 name="pybtm",
8- version="0.1.6",
8+ version="0.1.7",
99 author="zcc0721",
1010 author_email="zcc0721@foxmail.com",
1111 description="Python3 implementation of the Bytom protocol.",
Afficher sur ancien navigateur de dépôt.