OSDN Git Service

add receiver.py
authorChengcheng Zhang <943420582@qq.com>
Fri, 4 Jan 2019 14:16:07 +0000 (22:16 +0800)
committerChengcheng Zhang <943420582@qq.com>
Fri, 4 Jan 2019 14:16:07 +0000 (22:16 +0800)
app/api/__init__.py
app/api/resources.py
app/model/receiver.py [new file with mode: 0644]

index deaf67b..9f74c94 100644 (file)
@@ -15,6 +15,7 @@ from app.api.resources import Xprv_Sign
 from app.api.resources import Xprv_To_Xpub
 from app.api.resources import Xprv_To_Child_Xprv
 from app.api.resources import Xpub_To_Child_Xpub
+from app.api.resources import Create_P2WPKH_Program
 
 blueprint = Blueprint('api', __name__, url_prefix='/api/v1')
 api = Api(blueprint)
@@ -32,4 +33,5 @@ api.add_resource(Xpub_Verify, '/xpub_verify')
 api.add_resource(Xprv_Sign, '/xprv_sign')
 api.add_resource(Xprv_To_Xpub, '/xprv_to_xpub')
 api.add_resource(Xprv_To_Child_Xprv, '/xprv_to_child_xprv')
-api.add_resource(Xpub_To_Child_Xpub, '/xpub_to_child_xpub')
\ No newline at end of file
+api.add_resource(Xpub_To_Child_Xpub, '/xpub_to_child_xpub')
+api.add_resource(Create_P2WPKH_Program, '/create_P2WPKH_program')
\ No newline at end of file
index 3ed30f5..f18df99 100644 (file)
@@ -15,6 +15,7 @@ from app.model.key import xprv_to_xpub
 from app.model.key import xprv_sign
 from app.model.key import xprv_to_child_xprv
 from app.model.key import xpub_to_child_xpub
+from app.model.receiver import create_P2WPKH_program
 
 parser = reqparse.RequestParser()
 parser.add_argument('private_key_str', type=str)
@@ -27,6 +28,9 @@ parser.add_argument('seed_str', type=str)
 parser.add_argument('xprv_str', type=str)
 parser.add_argument('xpub_str', type=str)
 parser.add_argument('path_list', type=str, action='append')
+parser.add_argument('account_index_int', type=int)
+parser.add_argument('address_index_int', type=int)
+parser.add_argument('change_bool', type=bool)
 
 class Hello(Resource):
 
@@ -141,4 +145,15 @@ class Xpub_To_Child_Xpub(Resource):
         xpub = args.get('xpub_str')
         path = args.get('path_list')
         child_xpub = xpub_to_child_xpub(xpub, path)
-        return child_xpub
\ No newline at end of file
+        return child_xpub
+
+class Create_P2WPKH_Program(Resource):
+
+    def post(self):
+        args = parser.parse_args()
+        account_index = args.get('account_index_int')
+        address_index = args.get('address_index_int')
+        change = args.get('change_bool')
+        xpub = args.get('xpub_str')
+        P2WPKH_program = create_P2WPKH_program(account_index, address_index, change, xpub)
+        return P2WPKH_program
\ No newline at end of file
diff --git a/app/model/receiver.py b/app/model/receiver.py
new file mode 100644 (file)
index 0000000..03d2904
--- /dev/null
@@ -0,0 +1,40 @@
+import hashlib
+from app.model.key import *
+
+# get_path_from_index create xpub path from account key index and current address index
+# Please attention:
+#   account_index_int >= 1
+#   address_index_int >= 1
+# test data 1:
+#   account_index_int: 
+#   address_index_int: 
+#   path_list: 
+def get_path_from_index(account_index_int, address_index_int, change_bool):
+    path_list = ['2c000000', '99000000']
+    account_index_str = (account_index_int).to_bytes(4, byteorder='little').hex()
+    path_list.append(account_index_str)
+    if change_bool == True:
+        branch_str = (1).to_bytes(4, byteorder='little').hex()
+        print("true")
+    elif change_bool == False:
+        branch_str = (0).to_bytes(4, byteorder='little').hex()
+        print("false")
+    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
+
+
+def create_P2WPKH_program(account_index_int, address_index_int, change_bool, xpub_str):
+    path_list = get_path_from_index(account_index_int, address_index_int, change_bool)
+    child_xpub_str = xpub_to_child_xpub(xpub_str, path_list)
+    child_public_key_str = xpub_to_public_key(child_xpub_str)
+    child_public_key_byte = bytes.fromhex(child_public_key_str)
+    
+    ripemd160 = hashlib.new('ripemd160')
+    ripemd160.update(child_public_key_byte)
+    public_key_hash_str = ripemd160.hexdigest()
+    P2WPKH_program_str = '0014' + public_key_hash_str
+
+    return P2WPKH_program_str
\ No newline at end of file