OSDN Git Service

add path_list test data
[bytom/bytom-kit.git] / app / model / receiver.py
1 import hashlib
2 from app.model.key import *
3 from app.model import segwit_addr
4
5 # get_path_from_index create xpub path from account key index and current address index
6 # path: purpose(0x2c=44)/coin_type(btm:0x99)/account_index/change(1 or 0)/address_index
7 # You can find more details from: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
8 # You can get more test data from: https://gist.github.com/zcc0721/616eaf337673635fa5c9dd5dbb8dd114
9 # Please attention:
10 #   account_index_int >= 1
11 #   address_index_int >= 1
12 #   change_bool: true or false
13 # test data 1:
14 #   account_index_int: 1
15 #   address_index_int: 1
16 #   change_bool: true
17 #   path_list: 2c000000 99000000 01000000 01000000 01000000
18 # test data 2:
19 #   account_index_int: 1
20 #   address_index_int: 1
21 #   change_bool: false
22 #   path_list: 2c000000 99000000 01000000 00000000 01000000
23 # test data 3:
24 #   account_index_int: 3
25 #   address_index_int: 1
26 #   change_bool: false
27 #   path_list: 2c000000 99000000 03000000 00000000 01000000
28 def get_path_from_index(account_index_int, address_index_int, change_bool):
29     path_list = ['2c000000', '99000000']
30     account_index_str = (account_index_int).to_bytes(4, byteorder='little').hex()
31     path_list.append(account_index_str)
32     if change_bool:
33         branch_str = (1).to_bytes(4, byteorder='little').hex()
34     else:
35         branch_str = (0).to_bytes(4, byteorder='little').hex()
36     path_list.append(branch_str)
37     address_index_str = (address_index_int).to_bytes(4, byteorder='little').hex()
38     path_list.append(address_index_str)
39
40     return path_list
41
42
43 def create_P2WPKH_program(account_index_int, address_index_int, change_bool, xpub_str):
44     path_list = get_path_from_index(account_index_int, address_index_int, change_bool)
45     child_xpub_str = xpub_to_child_xpub(xpub_str, path_list)
46     child_public_key_str = xpub_to_public_key(child_xpub_str)
47     child_public_key_byte = bytes.fromhex(child_public_key_str)
48     
49     ripemd160 = hashlib.new('ripemd160')
50     ripemd160.update(child_public_key_byte)
51     public_key_hash_str = ripemd160.hexdigest()
52     control_program_str = '0014' + public_key_hash_str
53
54     return control_program_str
55
56
57 def create_address(control_program_str, network_str):
58     public_key_hash_str = control_program_str[4:]
59     if network_str == 'mainnet':
60         hrp = 'bm'
61     elif network_str == 'testnet':
62         hrp = 'tm'
63     else:
64         hrp = 'sm'
65     address_str = segwit_addr.encode(hrp, 0, bytes.fromhex(public_key_hash_str))
66
67     return address_str