OSDN Git Service

add get_gm_xpub
authorChengcheng Zhang <943420582@qq.com>
Tue, 29 Jan 2019 08:11:32 +0000 (16:11 +0800)
committerChengcheng Zhang <943420582@qq.com>
Tue, 29 Jan 2019 08:11:32 +0000 (16:11 +0800)
app/api/__init__.py
app/api/resources.py
app/model/key_gm.py

index 705157c..1f161fd 100644 (file)
@@ -24,6 +24,7 @@ from app.api.resources import Create_New_Key
 from app.api.resources import Create_New_Address
 from app.api.resources import Decode_Raw_Transaction
 from app.api.resources import Get_Gm_Root_Xprv
+from app.api.resources import Get_Gm_Xpub
 
 blueprint = Blueprint('api', __name__, url_prefix='/api/v1')
 api = Api(blueprint)
@@ -50,4 +51,5 @@ api.add_resource(Create_QRcode_Base64, '/create_qrcode_base64')
 api.add_resource(Create_New_Key, '/create_new_key')
 api.add_resource(Create_New_Address, '/create_new_address')
 api.add_resource(Decode_Raw_Transaction, '/decode_raw_transaction')
-api.add_resource(Get_Gm_Root_Xprv, '/get_gm_root_xprv')
\ No newline at end of file
+api.add_resource(Get_Gm_Root_Xprv, '/get_gm_root_xprv')
+api.add_resource(Get_Gm_Xpub, '/get_gm_xpub')
\ No newline at end of file
index ed2120e..9ee2db1 100644 (file)
@@ -24,6 +24,7 @@ from app.model.key import create_new_key
 from app.model.receiver import create_new_address
 from app.model.transaction import decode_raw_transaction
 from app.model.key_gm import get_gm_root_xprv
+from app.model.key_gm import get_gm_xpub
 
 
 parser = reqparse.RequestParser()
@@ -241,4 +242,12 @@ class Get_Gm_Root_Xprv(Resource):
         args = parser.parse_args()
         seed = args.get('seed_str')
         root_xprv = get_gm_root_xprv(seed)
-        return root_xprv
\ No newline at end of file
+        return root_xprv
+
+class Get_Gm_Xpub(Resource):
+
+    def post(self):
+        args = parser.parse_args()
+        xprv = args.get('xprv_str')
+        xpub = get_gm_xpub(xprv)
+        return xpub
\ No newline at end of file
index f3fbf08..45094d0 100644 (file)
@@ -1,5 +1,6 @@
 import hmac
 import hashlib
+from gmssl import sm2, func
 
 # get_gm_root_xprv create rootxprv from seed
 # seed_str length is 512 bits.
@@ -19,4 +20,27 @@ def get_gm_root_xprv(seed_str):
     root_xprv_str = hc_str
     return {
         "root_xprv": root_xprv_str
+    }
+
+# get_gm_xpub derives new xpub from xprv
+# xprv length is 64 bytes.
+# xpub length is 65 bytes.
+# You can get more test data from: https://gist.github.com/zcc0721/9e5761e6a924cce3aa7cf7f72721218a
+# test data 1:
+#   xprv_str: c003f4bcccf9ad6f05ad2c84fa5ff98430eb8e73de5de232bc29334c7d074759d513bc370335cac51d77f0be5dfe84de024cfee562530b4d873b5f5e2ff4f57c
+#   xpub_str: 02476044353971ae0ed41cba76f27d0bd2e09d09db5c238bb74f69569bf343f742d513bc370335cac51d77f0be5dfe84de024cfee562530b4d873b5f5e2ff4f57c
+# test data 2:
+#   xprv_str: 36667587de27eec684fc4b222276f22a24d9a82e947ee0119148bedd4dec461dd4e1b1d95dfb0f78896677ea1026af7510b41fabd3bd5771311c0cb6968337b2
+#   xpub_str: 0396a36cd902db56eca016c213a8ac25de35a7afd78061351f1898529f0956c22ed4e1b1d95dfb0f78896677ea1026af7510b41fabd3bd5771311c0cb6968337b2
+# test data 3:
+#   xprv_str: 74a49c698dbd3c12e36b0b287447d833f74f3937ff132ebff7054baa18623c35a705bb18b82e2ac0384b5127db97016e63609f712bc90e3506cfbea97599f46f
+#   xpub_str: 03cafbdedea4a639d31fe4c257f1bb58303359be1a00b9f90b5c605f57e4308ed1a705bb18b82e2ac0384b5127db97016e63609f712bc90e3506cfbea97599f46f
+def get_gm_xpub(xprv_str):
+    private_key_int = int(xprv_str[:64], 16)
+    sm2_crypt = sm2.CryptSM2(private_key=xprv_str[:64], public_key="")
+    public_key_str = sm2_crypt._kg(private_key_int, sm2.default_ecc_table['g'])
+    pc = '0' + str(int(public_key_str[-1], 16) % 2 + 2)
+    xpub_str = pc + public_key_str[:64] + xprv_str[64:]
+    return {
+        "xpub": xpub_str
     }
\ No newline at end of file