OSDN Git Service

add broadcast_transaction
authorChengcheng Zhang <943420582@qq.com>
Wed, 9 Jan 2019 11:18:11 +0000 (19:18 +0800)
committerChengcheng Zhang <943420582@qq.com>
Wed, 9 Jan 2019 11:18:11 +0000 (19:18 +0800)
app/api/__init__.py
app/api/resources.py
app/model/key.py
app/model/receiver.py
app/model/signature.py
app/model/transaction.py

index 9d7e0c8..da6fb2e 100644 (file)
@@ -18,6 +18,7 @@ from app.api.resources import Xpub_To_Child_Xpub
 from app.api.resources import Create_P2WPKH_Program
 from app.api.resources import Create_Address
 from app.api.resources import Get_Path_From_Index
+from app.api.resources import Broadcast_Transaction
 
 blueprint = Blueprint('api', __name__, url_prefix='/api/v1')
 api = Api(blueprint)
@@ -38,4 +39,5 @@ api.add_resource(Xprv_To_Child_Xprv, '/xprv_to_child_xprv')
 api.add_resource(Xpub_To_Child_Xpub, '/xpub_to_child_xpub')
 api.add_resource(Create_P2WPKH_Program, '/create_P2WPKH_program')
 api.add_resource(Create_Address, '/create_address')
-api.add_resource(Get_Path_From_Index, '/get_path_from_index')
\ No newline at end of file
+api.add_resource(Get_Path_From_Index, '/get_path_from_index')
+api.add_resource(Broadcast_Transaction, '/broadcast_transaction')
\ No newline at end of file
index 8eff581..f6301ba 100644 (file)
@@ -19,6 +19,7 @@ from app.model.key import xpub_to_child_xpub
 from app.model.receiver import create_P2WPKH_program
 from app.model.receiver import create_address
 from app.model.receiver import get_path_from_index
+from app.model.transaction import broadcast_transaction
 
 parser = reqparse.RequestParser()
 parser.add_argument('private_key_str', type=str)
@@ -36,6 +37,7 @@ parser.add_argument('address_index_int', type=int)
 parser.add_argument('change_bool', type=inputs.boolean)
 parser.add_argument('control_program_str', type=str)
 parser.add_argument('network_str', type=str)
+parser.add_argument('raw_transaction_str', type=str)
 
 class Hello(Resource):
 
@@ -181,4 +183,13 @@ class Create_Address(Resource):
         control_program = args.get('control_program_str')
         network = args.get('network_str')
         address = create_address(control_program, network)
-        return address
\ No newline at end of file
+        return address
+
+class Broadcast_Transaction(Resource):
+
+    def post(self):
+        args = parser.parse_args()
+        raw_transaction = args.get('raw_transaction_str')
+        network = args.get('network_str')
+        response = broadcast_transaction(raw_transaction, network)
+        return response
\ No newline at end of file
index 768cc96..25cfa42 100644 (file)
@@ -15,7 +15,6 @@ def create_entropy():
         # create interger in range [1,15]
         num = random.randint(0,15)
         entropy_str += hex_str[num]
-
     return entropy_str
 
 
@@ -58,7 +57,6 @@ def entropy_to_mnemonic(entropy_str):
     for i in range(12):
         mnemonic_str += mnemonic_list[i][:-1]
         mnemonic_str += " "
-
     return mnemonic_str[:-1]
 
 
@@ -78,7 +76,6 @@ def mnemonic_to_seed(mnemonic_str):
     password_str = mnemonic_str
     salt_str = "mnemonic"
     seed_str = pbkdf2.PBKDF2(password_str, salt_str, iterations=2048, digestmodule=hashlib.sha512, macmodule=hmac).hexread(64)
-
     return seed_str
 
 
@@ -91,7 +88,6 @@ def prune_root_scalar(s_str):
     s[0] = s[0] & 248
     s[31] = s[31] & 31 # clear top 3 bits
     s[31] = s[31] | 64 # set second highest bit
-
     return s
 
 
@@ -111,7 +107,6 @@ def prune_root_scalar(s_str):
 def seed_to_root_xprv(seed_str):
     hc_str = hmac.HMAC(b'Root', bytes.fromhex(seed_str), digestmod=hashlib.sha512).hexdigest()
     root_xprv_str = prune_root_scalar(hc_str[:64]).hex() + hc_str[64:]
-
     return root_xprv_str
 
 
@@ -155,7 +150,6 @@ def xprv_to_xpub(xprv_str):
 def xprv_to_expanded_private_key(xprv_str):
     hc_str = hmac.HMAC(b'Expand', bytes.fromhex(xprv_str), digestmod=hashlib.sha512).hexdigest()
     expanded_private_key_str = xprv_str[:64] + hc_str[64:]
-
     return expanded_private_key_str
 
 
@@ -173,7 +167,6 @@ def xprv_to_expanded_private_key(xprv_str):
 #   public_key_str: b435f948bd3748ede8f9d6f59728d669939e79c6c885667a5c138e05bbabde1d
 def xpub_to_public_key(xpub_str):
     public_key_str = xpub_str[:64]
-
     return public_key_str
 
 
@@ -343,7 +336,6 @@ def xprv_sign(xprv_str, message_str):
 
     signature_bytes = encoded_r + s
     signature_str = signature_bytes.hex()
-
     return signature_str
 
 
@@ -370,5 +362,4 @@ def xprv_sign(xprv_str, message_str):
 def xpub_verify(xpub_str, message_str, signature_str):
     result = False
     result = verify(xpub_to_public_key(xpub_str), signature_str, message_str)
-
     return result
\ No newline at end of file
index c41580b..4bdae05 100644 (file)
@@ -36,7 +36,6 @@ def get_path_from_index(account_index_int, address_index_int, change_bool):
     path_list.append(branch_str)
     address_index_str = (address_index_int).to_bytes(4, byteorder='little').hex()
     path_list.append(address_index_str)
-
     return path_list
 
 
@@ -80,7 +79,6 @@ def create_P2WPKH_program(account_index_int, address_index_int, change_bool, xpu
     ripemd160.update(child_public_key_byte)
     public_key_hash_str = ripemd160.hexdigest()
     control_program_str = '0014' + public_key_hash_str
-
     return control_program_str
 
 
@@ -121,5 +119,4 @@ def create_address(control_program_str, network_str):
     else:
         hrp = 'sm'
     address_str = segwit_addr.encode(hrp, 0, bytes.fromhex(public_key_hash_str))
-
     return address_str
\ No newline at end of file
index 427b3a0..4452c2a 100644 (file)
@@ -20,7 +20,6 @@ def sign(private_key_str, message_str):
     signing_key = ed25519.SigningKey(bytes.fromhex(private_key_str))
     # signature = signing_key.sign(message_str.encode(), encoding='hex')
     signature = signing_key.sign(bytes.fromhex(message_str), encoding='hex')
-
     return signature.decode()
 
 
index efd1c39..9d2912b 100644 (file)
@@ -4,21 +4,21 @@ import json
 # broadcast_transaction broadcast raw transaction
 # raw_transaction_str is signed transaction,
 # network_str is mainnet or testnet
+# test data 1:
+#   raw_transaction_str: 070100010160015e0873eddd68c4ba07c9410984799928288ae771bdccc6d974e72c95727813461fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8094ebdc030101160014052620b86a6d5e07311d5019dffa3864ccc8a6bd630240312a052f36efb9826aa1021ec91bc6f125dd07f9c4bff87014612069527e15246518806b654d57fff8b6fe91866a19d5a2fb63a5894335fce92a7b4a7fcd340720e87ca3acdebdcad9a1d0f2caecf8ce0dbfc73d060807a210c6f225488347961402013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8082eee0020116001418028ef4f8b8c278907864a1977a5ee6707b2a6b00013cffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80b8b872011600142935e4869d0317d9701c80a02ecf888143cb9dd200
+#   network_str: testnet
 def broadcast_transaction(raw_transaction_str, network_str):
     raw_transaction_dict = {
         "transaction": raw_transaction_str
     }
     raw_transaction_json = json.dumps(raw_transaction_dict)
     headers = {
-        "Content-Type": "application/json",
-        "Accept": "application/json"
+        "content-type": "application/json",
+        "accept": "application/json"
     }
     if network_str == "mainnet":
         url = "https://blockmeta.com/api/v2/broadcast-transaction"
     else:
         url = "https://blockmeta.com/api/wisdom/broadcast-transaction"
     response = requests.post(url, headers=headers, data=raw_transaction_json)
-    return response
-
-
-
+    return response.text[:-1]