Python3 implementation of the Bytom protocol. https://pypi.org/project/pybtm/
Revision | bb8ec1166e08a0831e272b1037a4ba2a55d1d44c (tree) |
---|---|
Zeit | 2019-04-10 17:17:06 |
Autor | Chengcheng Zhang <943420582@qq.c...> |
Commiter | Chengcheng Zhang |
fix decode-raw-tx bug
@@ -444,7 +444,7 @@ decode_raw_tx decode raw transaction. | ||
444 | 444 | |
445 | 445 | Parameter: |
446 | 446 | |
447 | -- raw_tx_str: raw transaction, type is hex string. | |
447 | +- raw_transaction_str: raw transaction, type is hex string. | |
448 | 448 | - network_str: 3 types of network is available: mainnet, testnet and solonet. |
449 | 449 | |
450 | 450 | Return: |
@@ -1,2 +1,2 @@ | ||
1 | 1 | name = "pybtm" |
2 | -version = "0.1.6" | |
\ No newline at end of file | ||
2 | +version = "0.1.7" | |
\ No newline at end of file |
@@ -122,7 +122,7 @@ def get_coinbase_input_id(prepare_coinbase_input_id_hexstr): | ||
122 | 122 | ''' |
123 | 123 | decode_raw_tx decode raw transaction |
124 | 124 | testdata 1: |
125 | - raw_tx_str: 070100010161015f28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d5990100011600149335b1cbd4a77b78e33315a0ed10a95b12e7ca48630240897e2d9d24a3b5faaed0579dee7597b401491595675f897504f8945b29d836235bd2fca72a3ad0cae814628973ebcd142d9d6cc92d0b2571b69e5370a98a340c208cb7fb3086f58db9a31401b99e8c658be66134fb9034de1d5c462679270b090702013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80f9f8bc98010116001406ce4b689ba026ffd3a7ca65d1d059546d4b78a000013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f01011600147929ef91997c827bebf60fa608f876ea27523c4700 | |
125 | + raw_transaction_str: 070100010161015f28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d5990100011600149335b1cbd4a77b78e33315a0ed10a95b12e7ca48630240897e2d9d24a3b5faaed0579dee7597b401491595675f897504f8945b29d836235bd2fca72a3ad0cae814628973ebcd142d9d6cc92d0b2571b69e5370a98a340c208cb7fb3086f58db9a31401b99e8c658be66134fb9034de1d5c462679270b090702013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80f9f8bc98010116001406ce4b689ba026ffd3a7ca65d1d059546d4b78a000013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f01011600147929ef91997c827bebf60fa608f876ea27523c4700 | |
126 | 126 | network_str: solonet |
127 | 127 | transaction: |
128 | 128 | { |
@@ -171,7 +171,7 @@ testdata 1: | ||
171 | 171 | "version": 1 |
172 | 172 | } |
173 | 173 | ''' |
174 | -def decode_raw_tx(raw_tx_str, network_str): | |
174 | +def decode_raw_tx(raw_transaction_str, network_str): | |
175 | 175 | tx = { |
176 | 176 | "fee": 0, |
177 | 177 | "inputs": [], |
@@ -182,23 +182,23 @@ def decode_raw_tx(raw_tx_str, network_str): | ||
182 | 182 | "version": 0 |
183 | 183 | } |
184 | 184 | tx['fee'] = 0 |
185 | - tx['size'] = len(raw_tx_str) // 2 | |
185 | + tx['size'] = len(raw_transaction_str) // 2 | |
186 | 186 | length = 0 |
187 | 187 | 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]) | |
189 | 189 | 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]) | |
191 | 191 | 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]) | |
193 | 193 | offset = offset + 2 * length |
194 | 194 | prepare_mux_hexstr = (tx_input_amount).to_bytes((tx_input_amount.bit_length() + 7) // 8, 'little').hex() |
195 | 195 | prepare_tx_id_hexstr = (tx['version']).to_bytes(8, 'little').hex() + (tx['time_range']).to_bytes(8, 'little').hex() |
196 | 196 | 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]) | |
198 | 198 | offset = offset + 2 * length |
199 | - _, length = get_uvarint(raw_tx_str[offset:offset+18]) | |
199 | + _, length = get_uvarint(raw_transaction_str[offset:offset+18]) | |
200 | 200 | 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) | |
202 | 202 | offset += 2 |
203 | 203 | if input_type == 0: # issue |
204 | 204 | tx_input = { |
@@ -211,33 +211,33 @@ def decode_raw_tx(raw_tx_str, network_str): | ||
211 | 211 | "witness_arguments": [] |
212 | 212 | } |
213 | 213 | tx_input['type'] = "issue" |
214 | - _, length = get_uvarint(raw_tx_str[offset:offset+18]) | |
214 | + _, length = get_uvarint(raw_transaction_str[offset:offset+18]) | |
215 | 215 | offset = offset + 2 * length |
216 | - nonce = raw_tx_str[offset:offset+16] | |
216 | + nonce = raw_transaction_str[offset:offset+16] | |
217 | 217 | offset += 16 |
218 | 218 | 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] | |
220 | 220 | 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]) | |
222 | 222 | offset = offset + 2 * length |
223 | - _, length = get_uvarint(raw_tx_str[offset:offset+18]) | |
223 | + _, length = get_uvarint(raw_transaction_str[offset:offset+18]) | |
224 | 224 | 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]) | |
226 | 226 | 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() | |
228 | 228 | 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]) | |
230 | 230 | 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]) | |
232 | 232 | 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] | |
234 | 234 | 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]) | |
236 | 236 | offset = offset + 2 * length |
237 | 237 | 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]) | |
239 | 239 | offset = offset + 2 * length |
240 | - argument = raw_tx_str[offset:offset+2*argument_length] | |
240 | + argument = raw_transaction_str[offset:offset+2*argument_length] | |
241 | 241 | offset = offset + 2 * argument_length |
242 | 242 | tx_input['witness_arguments'].append(argument) |
243 | 243 | 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): | ||
259 | 259 | "witness_arguments": [] |
260 | 260 | } |
261 | 261 | tx_input['type'] = "spend" |
262 | - _, length = get_uvarint(raw_tx_str[offset:offset+18]) | |
262 | + _, length = get_uvarint(raw_transaction_str[offset:offset+18]) | |
263 | 263 | offset = offset + 2 * length |
264 | - source_id = raw_tx_str[offset:offset+64] | |
264 | + source_id = raw_transaction_str[offset:offset+64] | |
265 | 265 | offset += 64 |
266 | - tx_input['asset_id'] = raw_tx_str[offset:offset+64] | |
266 | + tx_input['asset_id'] = raw_transaction_str[offset:offset+64] | |
267 | 267 | 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]) | |
269 | 269 | offset = offset + 2 * length |
270 | 270 | 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]) | |
272 | 272 | 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]) | |
274 | 274 | 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]) | |
276 | 276 | 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] | |
278 | 278 | 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]) | |
281 | 281 | 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]) | |
283 | 283 | 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) | |
284 | 294 | 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']) |
285 | 295 | 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) | |
292 | 296 | tx['inputs'].append(tx_input) |
293 | 297 | prepare_mux_hexstr += tx_input['input_id'] + tx_input['asset_id'] + (tx_input['amount']).to_bytes(8, byteorder='little').hex() + '0000000000000000' |
294 | 298 | prepare_mux_hexstr += '0100000000000000' + '0151' |
@@ -304,17 +308,17 @@ def decode_raw_tx(raw_tx_str, network_str): | ||
304 | 308 | "witness_arguments": [] |
305 | 309 | } |
306 | 310 | 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] | |
309 | 313 | 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] | |
311 | 315 | prepare_coinbase_input_id_hexstr += tx_input['arbitrary'] |
312 | 316 | offset = offset + 2 * arbitrary_length |
313 | 317 | tx_input['input_id'] = get_coinbase_input_id(prepare_coinbase_input_id_hexstr) |
314 | 318 | offset = offset + 2 |
315 | 319 | tx['inputs'].append(tx_input) |
316 | 320 | 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]) | |
318 | 322 | offset = offset + 2 * length |
319 | 323 | prepare_tx_id_hexstr += (tx_output_amount).to_bytes((tx_output_amount.bit_length() + 7) // 8, 'little').hex() |
320 | 324 | for i in range(tx_output_amount): |
@@ -329,27 +333,27 @@ def decode_raw_tx(raw_tx_str, network_str): | ||
329 | 333 | "type": "" |
330 | 334 | } |
331 | 335 | tx_output['position'] = i |
332 | - _, length = get_uvarint(raw_tx_str[offset:offset+18]) | |
336 | + _, length = get_uvarint(raw_transaction_str[offset:offset+18]) | |
333 | 337 | offset = offset + 2 * length |
334 | - _, length = get_uvarint(raw_tx_str[offset:offset+18]) | |
338 | + _, length = get_uvarint(raw_transaction_str[offset:offset+18]) | |
335 | 339 | 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] | |
337 | 341 | 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]) | |
339 | 343 | if tx_input['type'] == "coinbase": |
340 | 344 | prepare_mux_hexstr = prepare_mux_hexstr + (tx_output['amount']).to_bytes(8, byteorder='little').hex() + '0000000000000000' + '0100000000000000' + '0151' |
341 | 345 | mux_id_hexstr = get_mux_id(prepare_mux_hexstr) |
342 | 346 | offset = offset + 2 * length |
343 | 347 | if tx_output['asset_id'] == 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff': |
344 | 348 | 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]) | |
346 | 350 | 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]) | |
348 | 352 | 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] | |
350 | 354 | 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]) | |
353 | 357 | offset = offset + 2 * length |
354 | 358 | 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'] |
355 | 359 | tx_output['id'] = get_output_id(prepare_output_id_hexstr) |
@@ -359,4 +363,4 @@ def decode_raw_tx(raw_tx_str, network_str): | ||
359 | 363 | if tx_input['type'] == "coinbase": |
360 | 364 | tx['fee'] = 0 |
361 | 365 | tx['tx_id'] = get_tx_id(prepare_tx_id_hexstr) |
362 | - return tx | |
366 | + return tx | |
\ No newline at end of file |
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh: | ||
5 | 5 | |
6 | 6 | setuptools.setup( |
7 | 7 | name="pybtm", |
8 | - version="0.1.6", | |
8 | + version="0.1.7", | |
9 | 9 | author="zcc0721", |
10 | 10 | author_email="zcc0721@foxmail.com", |
11 | 11 | description="Python3 implementation of the Bytom protocol.", |