final implementation of function of creating child public key (improved add_public_keys)
This commit is contained in:
parent
c6dea92425
commit
353abb28b6
@ -1,11 +1,12 @@
|
|||||||
import os
|
import os
|
||||||
import hmac
|
import hmac
|
||||||
|
|
||||||
|
from secp256k1 import ffi
|
||||||
from struct import pack, unpack
|
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, is_valid_public_key
|
from .tools import priv2pub, is_valid_pub
|
||||||
from .hash import hmac_sha512, hash160, double_sha256, sha256, double_sha256
|
from .hash import hmac_sha512, hash160, double_sha256, sha256, double_sha256
|
||||||
|
|
||||||
|
|
||||||
@ -134,11 +135,12 @@ def create_child_pubkey(key, child_idx):
|
|||||||
expanded_pubkey = create_expanded_key(key, child_idx)
|
expanded_pubkey = create_expanded_key(key, child_idx)
|
||||||
if expanded_pubkey:
|
if expanded_pubkey:
|
||||||
child_chain_code = expanded_pubkey[32:]
|
child_chain_code = expanded_pubkey[32:]
|
||||||
child_pubkey = add_public_keys(priv2pub(expanded_pubkey[:32])[1:], key['key'])
|
ext_value = priv2pub(expanded_pubkey[:32])
|
||||||
if is_valid_public_key(child_pubkey):
|
child_pubkey = add_public_keys(ext_value, key['key'])
|
||||||
|
if is_valid_pub(child_pubkey):
|
||||||
finger_print = hash160(key['key'])[:4]
|
finger_print = hash160(key['key'])[:4]
|
||||||
return dict(version=MAINNET_PUBLIC_WALLET_VERSION,
|
return dict(version=MAINNET_PUBLIC_WALLET_VERSION,
|
||||||
key=child_pubkey, # надо сделать какое то добавление (преобразование addpublickey)
|
key=child_pubkey,
|
||||||
depth=key['depth'] + 1,
|
depth=key['depth'] + 1,
|
||||||
child=child_idx,
|
child=child_idx,
|
||||||
finger_print=finger_print,
|
finger_print=finger_print,
|
||||||
@ -177,18 +179,17 @@ def add_private_keys(ext_value, key):
|
|||||||
|
|
||||||
|
|
||||||
def add_public_keys(ext_value, key):
|
def add_public_keys(ext_value, key):
|
||||||
#ext_value_int = int.from_bytes(ext_value, byteorder="big")
|
pubkey_ptr = ffi.new('secp256k1_pubkey *')
|
||||||
#key_int = int.from_bytes(key, byteorder="big")
|
if not secp256k1.secp256k1_ec_pubkey_parse(ECDSA_CONTEXT_VERIFY, pubkey_ptr, ext_value, len(ext_value)):
|
||||||
#ext_value_int = (ext_value_int + key_int) % MAX_INT_PRIVATE_KEY
|
raise TypeError("public key format error")
|
||||||
#return ext_value_int.to_bytes((ext_value_int.bit_length() + 7) // 8, byteorder="big")
|
if secp256k1.secp256k1_ec_pubkey_tweak_add(ECDSA_CONTEXT_ALL, pubkey_ptr, key):
|
||||||
|
pubkey = ffi.new('char [%d]' % 33)
|
||||||
|
outlen = ffi.new('size_t *', 33)
|
||||||
|
if secp256k1.secp256k1_ec_pubkey_serialize(ECDSA_CONTEXT_VERIFY, pubkey, outlen, pubkey_ptr, EC_COMPRESSED):
|
||||||
|
return bytes(ffi.buffer(pubkey, 33))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
## Надо удалить в будущем как дублирование. И добавить в реализации ООП как метод
|
|
||||||
def create_public_key_hdwallet(master_key):
|
|
||||||
return priv2pub(master_key, True)
|
|
||||||
|
|
||||||
|
|
||||||
def validate_private_key(key):
|
def validate_private_key(key):
|
||||||
key_int = int.from_bytes(key, byteorder="big")
|
key_int = int.from_bytes(key, byteorder="big")
|
||||||
if key_int > 0 and key_int < MAX_INT_PRIVATE_KEY and len(key) == 32:
|
if key_int > 0 and key_int < MAX_INT_PRIVATE_KEY and len(key) == 32:
|
||||||
@ -196,10 +197,6 @@ def validate_private_key(key):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def validate_child_public_key(key):
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def serialize_key_hdwallet(key):
|
def serialize_key_hdwallet(key):
|
||||||
try:
|
try:
|
||||||
key_bytes = key['key']
|
key_bytes = key['key']
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user