implemented BIP32/BIP44: create child private/public keys
This commit is contained in:
parent
4baffa113d
commit
08cc9d6b91
@ -5,7 +5,7 @@ from struct import pack, unpack
|
|||||||
from hashlib import pbkdf2_hmac
|
from hashlib import pbkdf2_hmac
|
||||||
from binascii import hexlify, unhexlify
|
from binascii import hexlify, unhexlify
|
||||||
from .constants import *
|
from .constants import *
|
||||||
from .tools import priv2pub
|
from .tools import priv2pub, is_valid_public_key
|
||||||
from .hash import hmac_sha512, hash160, double_sha256, sha256, double_sha256
|
from .hash import hmac_sha512, hash160, double_sha256, sha256, double_sha256
|
||||||
|
|
||||||
|
|
||||||
@ -111,25 +111,39 @@ def create_master_key_hdwallet(seed):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
# создание дочернего приватного ключа
|
# Создание дочернего приватного ключа
|
||||||
def create_child_key_hdwallet(key, child_idx):
|
def create_child_privkey(key, child_idx):
|
||||||
if not key.get('is_private') and child_idx >= FIRST_HARDENED_CHILD:
|
expanded_privkey = create_expanded_key(key, child_idx)
|
||||||
return None
|
if expanded_pubkey:
|
||||||
public_key = priv2pub(key['key'], True)
|
child_chain_code = expanded_pubkey[32:]
|
||||||
assert public_key is not None
|
child_privkey = add_private_keys(expanded_pubkey[:32], key['key'])
|
||||||
seed = public_key + bytes([key['depth'] + 1])
|
if validate_private_key(child_privkey):
|
||||||
intermediary = hmac_sha512(key['chain_code'], seed)
|
finger_print = hash160(priv2pub(key['key']))[:4]
|
||||||
chain_code = intermediary[32:]
|
return dict(version=MAINNET_PRIVATE_WALLET_VERSION,
|
||||||
child_key = add_private_keys(intermediary[:32], key['key'])
|
key=child_pubkey,
|
||||||
finger_print = hash160(child_key)[:4]
|
depth=key['depth'] + 1,
|
||||||
if validate_private_key(child_key) and validate_private_key(chain_code):
|
child=child_idx,
|
||||||
return dict(version=MAINNET_PRIVATE_WALLET_VERSION,
|
finger_print=finger_print,
|
||||||
key=child_key,
|
chain_code=child_chain_code,
|
||||||
depth=key['depth'] + 1,
|
is_private=False)
|
||||||
child=0,
|
return None
|
||||||
finger_print=finger_print,
|
|
||||||
chain_code=chain_code,
|
|
||||||
is_private=True)
|
# создание дочернего публичного ключа
|
||||||
|
def create_child_pubkey(key, child_idx):
|
||||||
|
expanded_pubkey = create_expanded_key(key, child_idx)
|
||||||
|
if expanded_pubkey:
|
||||||
|
child_chain_code = expanded_pubkey[32:]
|
||||||
|
child_pubkey = add_public_keys(priv2pub(expanded_pubkey[:32])[1:], key['key'])
|
||||||
|
if is_valid_public_key(child_pubkey):
|
||||||
|
finger_print = hash160(key['key'])[:4]
|
||||||
|
return dict(version=MAINNET_PUBLIC_WALLET_VERSION,
|
||||||
|
key=child_pubkey, # надо сделать какое то добавление (преобразование addpublickey)
|
||||||
|
depth=key['depth'] + 1,
|
||||||
|
child=child_idx,
|
||||||
|
finger_print=finger_print,
|
||||||
|
chain_code=child_chain_code,
|
||||||
|
is_private=False)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -162,6 +176,14 @@ def add_private_keys(ext_value, key):
|
|||||||
return ext_value_int.to_bytes((ext_value_int.bit_length() + 7) // 8, byteorder="big")
|
return ext_value_int.to_bytes((ext_value_int.bit_length() + 7) // 8, byteorder="big")
|
||||||
|
|
||||||
|
|
||||||
|
def add_public_keys(ext_value, key):
|
||||||
|
#ext_value_int = int.from_bytes(ext_value, byteorder="big")
|
||||||
|
#key_int = int.from_bytes(key, byteorder="big")
|
||||||
|
#ext_value_int = (ext_value_int + key_int) % MAX_INT_PRIVATE_KEY
|
||||||
|
#return ext_value_int.to_bytes((ext_value_int.bit_length() + 7) // 8, byteorder="big")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
## Надо удалить в будущем как дублирование. И добавить в реализации ООП как метод
|
## Надо удалить в будущем как дублирование. И добавить в реализации ООП как метод
|
||||||
def create_public_key_hdwallet(master_key):
|
def create_public_key_hdwallet(master_key):
|
||||||
return priv2pub(master_key, True)
|
return priv2pub(master_key, True)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user