OSDN Git Service

fix decode bug
authorChengcheng Zhang <943420582@qq.com>
Wed, 10 Apr 2019 08:04:25 +0000 (16:04 +0800)
committerChengcheng Zhang <943420582@qq.com>
Wed, 10 Apr 2019 08:04:25 +0000 (16:04 +0800)
app/model/transaction.py
requirements.txt

index 779a2f9..b44b5de 100644 (file)
@@ -126,7 +126,7 @@ def get_coinbase_input_id(prepare_coinbase_input_id_hexstr):
 '''
 decode_raw_tx decode raw transaction
 testdata 1:
-    raw_tx_str: 070100010161015f28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d5990100011600149335b1cbd4a77b78e33315a0ed10a95b12e7ca48630240897e2d9d24a3b5faaed0579dee7597b401491595675f897504f8945b29d836235bd2fca72a3ad0cae814628973ebcd142d9d6cc92d0b2571b69e5370a98a340c208cb7fb3086f58db9a31401b99e8c658be66134fb9034de1d5c462679270b090702013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80f9f8bc98010116001406ce4b689ba026ffd3a7ca65d1d059546d4b78a000013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f01011600147929ef91997c827bebf60fa608f876ea27523c4700
+    raw_transaction_str: 070100010161015f28b7b53d8dc90006bf97e0a4eaae2a72ec3d869873188698b694beaf20789f21ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d5990100011600149335b1cbd4a77b78e33315a0ed10a95b12e7ca48630240897e2d9d24a3b5faaed0579dee7597b401491595675f897504f8945b29d836235bd2fca72a3ad0cae814628973ebcd142d9d6cc92d0b2571b69e5370a98a340c208cb7fb3086f58db9a31401b99e8c658be66134fb9034de1d5c462679270b090702013effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80f9f8bc98010116001406ce4b689ba026ffd3a7ca65d1d059546d4b78a000013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f01011600147929ef91997c827bebf60fa608f876ea27523c4700
     network_str: solonet
     transaction: 
 {
@@ -175,7 +175,7 @@ testdata 1:
   "version": 1
 }
 '''
-def decode_raw_tx(raw_tx_str, network_str):
+def decode_raw_tx(raw_transaction_str, network_str):
     tx = {
         "fee": 0,
         "inputs": [],
@@ -186,23 +186,23 @@ def decode_raw_tx(raw_tx_str, network_str):
         "version": 0
     }
     tx['fee'] = 0
-    tx['size'] = len(raw_tx_str) // 2
+    tx['size'] = len(raw_transaction_str) // 2
     length = 0
     offset = 2
-    tx['version'], length = get_uvarint(raw_tx_str[offset:offset+18])
+    tx['version'], length = get_uvarint(raw_transaction_str[offset:offset+18])
     offset = offset + 2 * length
-    tx['time_range'], length = get_uvarint(raw_tx_str[offset:offset+18])
+    tx['time_range'], length = get_uvarint(raw_transaction_str[offset:offset+18])
     offset = offset + 2 * length
-    tx_input_amount, length = get_uvarint(raw_tx_str[offset:offset+8])
+    tx_input_amount, length = get_uvarint(raw_transaction_str[offset:offset+8])
     offset = offset + 2 * length
     prepare_mux_hexstr = (tx_input_amount).to_bytes((tx_input_amount.bit_length() + 7) // 8, 'little').hex()
     prepare_tx_id_hexstr = (tx['version']).to_bytes(8, 'little').hex() + (tx['time_range']).to_bytes(8, 'little').hex()
     for _ in range(tx_input_amount):
-        _, length = get_uvarint(raw_tx_str[offset:offset+18])
+        _, length = get_uvarint(raw_transaction_str[offset:offset+18])
         offset = offset + 2 * length
-        _, length = get_uvarint(raw_tx_str[offset:offset+18])
+        _, length = get_uvarint(raw_transaction_str[offset:offset+18])
         offset = offset + 2 * length
-        input_type = int(raw_tx_str[offset:offset+2], 16)
+        input_type = int(raw_transaction_str[offset:offset+2], 16)
         offset += 2
         if input_type == 0: # issue
             tx_input = {
@@ -215,33 +215,33 @@ def decode_raw_tx(raw_tx_str, network_str):
                 "witness_arguments": []
             }
             tx_input['type'] = "issue"
-            _, length = get_uvarint(raw_tx_str[offset:offset+18])
+            _, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            nonce = raw_tx_str[offset:offset+16]
+            nonce = raw_transaction_str[offset:offset+16]
             offset += 16
             nonce_hash_hexstr = sha3_256(bytes.fromhex(nonce)).hexdigest()
-            tx_input['asset_id'] = raw_tx_str[offset:offset+64]
+            tx_input['asset_id'] = raw_transaction_str[offset:offset+64]
             offset += 64
-            tx_input['amount'], length = get_uvarint(raw_tx_str[offset:offset+18])
+            tx_input['amount'], length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            _, length = get_uvarint(raw_tx_str[offset:offset+18])
+            _, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            asset_definition_size, length = get_uvarint(raw_tx_str[offset:offset+18])
+            asset_definition_size, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            tx_input['asset_definition'] = bytes.fromhex(raw_tx_str[offset:offset+2*asset_definition_size]).decode()
+            tx_input['asset_definition'] = bytes.fromhex(raw_transaction_str[offset:offset+2*asset_definition_size]).decode()
             offset = offset + 2 * asset_definition_size
-            _, length = get_uvarint(raw_tx_str[offset:offset+18])
+            _, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            issuance_program_length, length = get_uvarint(raw_tx_str[offset:offset+18])
+            issuance_program_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            tx_input['issuance_program'] = raw_tx_str[offset:offset+2*issuance_program_length]
+            tx_input['issuance_program'] = raw_transaction_str[offset:offset+2*issuance_program_length]
             offset = offset + 2 * issuance_program_length
-            witness_arguments_amount, length = get_uvarint(raw_tx_str[offset:offset+18])
+            witness_arguments_amount, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
             for _ in range(witness_arguments_amount):
-                argument_length, length = get_uvarint(raw_tx_str[offset:offset+18])
+                argument_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
                 offset = offset + 2 * length
-                argument = raw_tx_str[offset:offset+2*argument_length]
+                argument = raw_transaction_str[offset:offset+2*argument_length]
                 offset = offset + 2 * argument_length
                 tx_input['witness_arguments'].append(argument)
             prepare_issue_hexstr = nonce_hash_hexstr + tx_input['asset_id'] + (tx_input['amount']).to_bytes(8, byteorder='little').hex()
@@ -263,36 +263,40 @@ def decode_raw_tx(raw_tx_str, network_str):
                 "witness_arguments": []
             }
             tx_input['type'] = "spend"
-            _, length = get_uvarint(raw_tx_str[offset:offset+18])
+            _, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            source_id = raw_tx_str[offset:offset+64]
+            source_id = raw_transaction_str[offset:offset+64]
             offset += 64
-            tx_input['asset_id'] = raw_tx_str[offset:offset+64]
+            tx_input['asset_id'] = raw_transaction_str[offset:offset+64]
             offset += 64
-            tx_input['amount'], length = get_uvarint(raw_tx_str[offset:offset+18])
+            tx_input['amount'], length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
             tx['fee'] += tx_input['amount']
-            source_positon, length = get_uvarint(raw_tx_str[offset:offset+18])
+            source_positon, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            vmversion, length = get_uvarint(raw_tx_str[offset:offset+18])
+            vmversion, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            control_program_length, length = get_uvarint(raw_tx_str[offset:offset+18])
+            control_program_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            tx_input['control_program'] = raw_tx_str[offset:offset+2*control_program_length]
+            tx_input['control_program'] = raw_transaction_str[offset:offset+2*control_program_length]
             offset = offset + 2 * control_program_length
             tx_input['address'] = receiver.create_address(tx_input['control_program'], network_str)['address']
-            _, length = get_uvarint(raw_tx_str[offset:offset+18])
+            _, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
-            witness_arguments_amount, length = get_uvarint(raw_tx_str[offset:offset+18])
+            witness_arguments_amount, length = get_uvarint(raw_transaction_str[offset:offset+18])
             offset = offset + 2 * length
+            if witness_arguments_amount == 1:
+                offset = offset + 2
+                tx_input['witness_arguments'] = None
+            else: 
+                for _ in range(witness_arguments_amount):
+                    argument_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
+                    offset = offset + 2 * length
+                    argument = raw_transaction_str[offset:offset+2*argument_length]
+                    offset = offset + 2 * argument_length
+                    tx_input['witness_arguments'].append(argument)
             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'])
             tx_input['input_id'] = get_input_id(tx_input['spent_output_id'])
-            for _ in range(witness_arguments_amount):
-                argument_length, length = get_uvarint(raw_tx_str[offset:offset+18])
-                offset = offset + 2 * length
-                argument = raw_tx_str[offset:offset+2*argument_length]
-                offset = offset + 2 * argument_length
-                tx_input['witness_arguments'].append(argument)
             tx['inputs'].append(tx_input)
             prepare_mux_hexstr += tx_input['input_id'] + tx_input['asset_id'] + (tx_input['amount']).to_bytes(8, byteorder='little').hex() + '0000000000000000'
             prepare_mux_hexstr += '0100000000000000' + '0151'
@@ -308,17 +312,17 @@ def decode_raw_tx(raw_tx_str, network_str):
                 "witness_arguments": []
             }
             tx_input['type'] = "coinbase"
-            arbitrary_length, length = get_uvarint(raw_tx_str[offset:offset+18])
-            prepare_coinbase_input_id_hexstr = raw_tx_str[offset:offset+2*length]
+            arbitrary_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
+            prepare_coinbase_input_id_hexstr = raw_transaction_str[offset:offset+2*length]
             offset = offset + 2 * length
-            tx_input['arbitrary'] = raw_tx_str[offset:offset+2*arbitrary_length]
+            tx_input['arbitrary'] = raw_transaction_str[offset:offset+2*arbitrary_length]
             prepare_coinbase_input_id_hexstr += tx_input['arbitrary']
             offset = offset + 2 * arbitrary_length
             tx_input['input_id'] = get_coinbase_input_id(prepare_coinbase_input_id_hexstr)
             offset = offset + 2
             tx['inputs'].append(tx_input)
             prepare_mux_hexstr += tx_input['input_id'] + 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
-    tx_output_amount, length = get_uvarint(raw_tx_str[offset:offset+18])
+    tx_output_amount, length = get_uvarint(raw_transaction_str[offset:offset+18])
     offset = offset + 2 * length
     prepare_tx_id_hexstr += (tx_output_amount).to_bytes((tx_output_amount.bit_length() + 7) // 8, 'little').hex()
     for i in range(tx_output_amount):
@@ -333,27 +337,27 @@ def decode_raw_tx(raw_tx_str, network_str):
             "type": ""
         }
         tx_output['position'] = i
-        _, length = get_uvarint(raw_tx_str[offset:offset+18])
+        _, length = get_uvarint(raw_transaction_str[offset:offset+18])
         offset = offset + 2 * length
-        _, length = get_uvarint(raw_tx_str[offset:offset+18])
+        _, length = get_uvarint(raw_transaction_str[offset:offset+18])
         offset = offset + 2 * length
-        tx_output['asset_id'] = raw_tx_str[offset:offset+64]
+        tx_output['asset_id'] = raw_transaction_str[offset:offset+64]
         offset = offset + 64
-        tx_output['amount'], length = get_uvarint(raw_tx_str[offset:offset+18])
+        tx_output['amount'], length = get_uvarint(raw_transaction_str[offset:offset+18])
         if tx_input['type'] == "coinbase":
             prepare_mux_hexstr = prepare_mux_hexstr + (tx_output['amount']).to_bytes(8, byteorder='little').hex() + '0000000000000000' + '0100000000000000' + '0151'
             mux_id_hexstr = get_mux_id(prepare_mux_hexstr)
         offset = offset + 2 * length
         if tx_output['asset_id'] == 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff':
             tx['fee'] -= tx_output['amount']
-        _, length = get_uvarint(raw_tx_str[offset:offset+18])
+        _, length = get_uvarint(raw_transaction_str[offset:offset+18])
         offset = offset + 2 * length
-        control_program_length, length = get_uvarint(raw_tx_str[offset:offset+18])
+        control_program_length, length = get_uvarint(raw_transaction_str[offset:offset+18])
         offset = offset + 2 * length
-        tx_output['control_program'] = raw_tx_str[offset:offset+2*control_program_length]
+        tx_output['control_program'] = raw_transaction_str[offset:offset+2*control_program_length]
         offset = offset + 2 * control_program_length
         tx_output['address'] = receiver.create_address(tx_output['control_program'], network_str)['address']
-        _, length = get_uvarint(raw_tx_str[offset:offset+18])
+        _, length = get_uvarint(raw_transaction_str[offset:offset+18])
         offset = offset + 2 * length
         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']
         tx_output['id'] = get_output_id(prepare_output_id_hexstr)
index bd6df99..f2df4d7 100644 (file)
@@ -2,10 +2,20 @@ aniso8601==4.0.1
 astroid==2.1.0
 atomicwrites==1.2.1
 attrs==18.2.0
+base58==1.0.3
+bip32utils==0.3.post4
+bitcoin==1.1.42
+bitmerchant==0.1.8
+bleach==3.1.0
+cachetools==3.1.0
 certifi==2018.11.29
+cffi==1.11.5
 chardet==3.0.4
 Click==7.0
+commontools==0.1.0
 Django==2.1.5
+docutils==0.14
+ecdsa==0.13
 ed25519==1.4
 Flask==1.0.2
 Flask-Cors==3.0.7
@@ -21,26 +31,44 @@ isort==4.3.4
 itsdangerous==1.1.0
 Jinja2==2.10
 jsonschema==2.6.0
+keychain==0.14.2.0
+keylib==0.1.1
 lazy-object-proxy==1.3.1
 MarkupSafe==1.1.0
 mccabe==0.6.1
 more-itertools==4.3.0
 pbkdf2==1.3
 Pillow==5.4.1
+pkginfo==1.5.0.1
 pluggy==0.8.0
 py==1.7.0
-pybase64==0.4.0
+pybase64==0.5.0
+pybitcoin==0.9.9
+pybtm==0.1.2
+pybytom==0.0.1
+pycparser==2.19
+pycryptodome==3.7.3
+Pygments==2.3.1
+pykeccak==0.2.1
 pylint==2.2.2
 pysha3==1.0.2
 pytest==4.0.1
+python-bitcoinrpc==0.1
 python-dotenv==0.10.0
 pytz==2018.7
-qrcode==6.0
+qrcode==6.1
+readme-renderer==24.0
 requests==2.21.0
+requests-toolbelt==0.9.1
+secp256k1==0.13.2
 sha3==0.2.1
 simplejson==3.16.0
 six==1.12.0
+tqdm==4.31.1
+twine==1.13.0
 urllib3==1.24.1
+utilitybelt==0.2.6
 virtualenv==16.1.0
+webencodings==0.5.1
 Werkzeug==0.14.1
 wrapt==1.10.11