2 from app.model.key import *
3 from app.model import segwit_addr
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
10 # account_index_int >= 1
11 # address_index_int >= 1
12 # change_bool: true or false
14 # account_index_int: 1
15 # address_index_int: 1
17 # path_list: 2c000000 99000000 01000000 01000000 01000000
19 # account_index_int: 1
20 # address_index_int: 1
22 # path_list: 2c000000 99000000 01000000 00000000 01000000
24 # account_index_int: 3
25 # address_index_int: 1
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)
33 branch_str = (1).to_bytes(4, byteorder='little').hex()
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)
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)
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
54 return control_program_str
57 def create_address(control_program_str, network_str):
58 public_key_hash_str = control_program_str[4:]
59 if network_str == 'mainnet':
61 elif network_str == 'testnet':
65 address_str = segwit_addr.encode(hrp, 0, bytes.fromhex(public_key_hash_str))