fixed conflicts when merging with master branch

This commit is contained in:
Alexey Karyabkin 2018-06-20 17:26:02 +04:00
parent e8a4f79d1f
commit 385f761af2
3 changed files with 28 additions and 16 deletions

View File

@ -4,5 +4,7 @@ from .consensus import *
from .transaction import * from .transaction import *
from .block import * from .block import *
from .address import * from .address import *
from .hdwallet import *
from .hash import *
version = "2.0.1" version = "2.0.1"

View File

@ -6,7 +6,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 private_to_public_key, is_valid_public_key, encode_base58, decode_base58, private_key_to_wif from .tools import private_to_public_key, is_public_key_valid, encode_base58, decode_base58, private_key_to_wif
from .hash import hmac_sha512, hash160, double_sha256, sha256, double_sha256 from .hash import hmac_sha512, hash160, double_sha256, sha256, double_sha256
@ -104,7 +104,7 @@ def create_xmaster_key(seed, testnet=False):
intermediary = hmac_sha512(key, seed) intermediary = hmac_sha512(key, seed)
mkey = intermediary[:32] mkey = intermediary[:32]
chain_code = intermediary[32:] chain_code = intermediary[32:]
if is_valid_private_key(mkey) and is_valid_private_key(chain_code): if is_xprivate_key_valid(mkey) and is_xprivate_key_valid(chain_code):
return dict(version=version, return dict(version=version,
key=mkey, key=mkey,
depth=0, depth=0,
@ -122,7 +122,7 @@ def create_xpublic_key(key):
version = TESTNET_PUBLIC_WALLET_VERSION version = TESTNET_PUBLIC_WALLET_VERSION
else: else:
version = MAINNET_PUBLIC_WALLET_VERSION version = MAINNET_PUBLIC_WALLET_VERSION
pubkey = private_to_public_key(key['key'], True) pubkey = private_to_public_key(key['key'], hex=False)
return dict(version=version, return dict(version=version,
key=pubkey, key=pubkey,
depth=key['depth'], depth=key['depth'],
@ -168,7 +168,7 @@ def derive_xkey(seed, *path_level, bip44=True, testnet=True, wif=True):
def xprivate_to_xpublic_key(xprv, encode_b58=True): def xprivate_to_xpublic_key(xprv, encode_b58=True):
if is_valid_private_key(xprv): if is_xprivate_key_valid(xprv):
xprivkey = deserialize_xkey(xprv) xprivkey = deserialize_xkey(xprv)
xpubkey = create_xpublic_key(xprivkey) xpubkey = create_xpublic_key(xprivkey)
if encode_b58: if encode_b58:
@ -181,7 +181,7 @@ def xprivate_to_xpublic_key(xprv, encode_b58=True):
# получение из расширенного приватного ключа обычный приватный ключ # получение из расширенного приватного ключа обычный приватный ключ
def xkey_to_private_key(xkey, wif=True, hex=False): def xkey_to_private_key(xkey, wif=True, hex=False):
if is_valid_private_key(xkey): if is_xprivate_key_valid(xkey):
xprivkey = deserialize_xkey(xkey) xprivkey = deserialize_xkey(xkey)
privkey = xprivkey['key'] privkey = xprivkey['key']
if xprivkey['version'] in TESTNET_PRIVATE_WALLET_VERSION: if xprivkey['version'] in TESTNET_PRIVATE_WALLET_VERSION:
@ -200,9 +200,9 @@ def xkey_to_private_key(xkey, wif=True, hex=False):
# получение из расширенного приватного/публичного ключа обычный публичный ключ # получение из расширенного приватного/публичного ключа обычный публичный ключ
def xkey_to_public_key(xkey, hex=False): def xkey_to_public_key(xkey, hex=False):
if is_valid_private_key(xkey): if is_xprivate_key_valid(xkey):
xkey = xprivate_to_xpublic_key(xkey) xkey = xprivate_to_xpublic_key(xkey)
if is_valid_public_key(xkey): if is_xpublic_key_valid(xkey):
xpubkey = deserialize_xkey(xkey) xpubkey = deserialize_xkey(xkey)
pubkey = xpubkey['key'] pubkey = xpubkey['key']
if xpubkey['version'] in TESTNET_PUBLIC_WALLET_VERSION: if xpubkey['version'] in TESTNET_PUBLIC_WALLET_VERSION:
@ -227,8 +227,8 @@ def create_child_privkey(key, child_idx):
if expanded_privkey: if expanded_privkey:
child_chain_code = expanded_privkey[32:] child_chain_code = expanded_privkey[32:]
child_privkey = add_private_keys(expanded_privkey[:32], key['key']) child_privkey = add_private_keys(expanded_privkey[:32], key['key'])
if is_valid_private_key(child_privkey): if is_xprivate_key_valid(child_privkey):
finger_print = hash160(private_to_public_key(key['key']))[:4] finger_print = hash160(private_to_public_key(key['key'], hex=False))[:4]
return dict(version=key['version'], return dict(version=key['version'],
key=child_privkey, key=child_privkey,
depth=key['depth'] + 1, depth=key['depth'] + 1,
@ -245,9 +245,9 @@ 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:]
ext_value = private_to_public_key(expanded_pubkey[:32]) ext_value = private_to_public_key(expanded_pubkey[:32], hex=False)
child_pubkey = add_public_keys(ext_value, key['key']) child_pubkey = add_public_keys(ext_value, key['key'])
if is_valid_public_key(child_pubkey): if is_xpublic_key_valid(child_pubkey):
finger_print = hash160(key['key'])[:4] finger_print = hash160(key['key'])[:4]
return dict(version=key['version'], return dict(version=key['version'],
key=child_pubkey, key=child_pubkey,
@ -266,7 +266,7 @@ def create_expanded_key(key, child_idx):
seed = key['key'] + pack('I', child_idx) seed = key['key'] + pack('I', child_idx)
return hmac_sha512(key['chain_code'], seed) return hmac_sha512(key['chain_code'], seed)
elif key.get('is_private') and child_idx < FIRST_HARDENED_CHILD: elif key.get('is_private') and child_idx < FIRST_HARDENED_CHILD:
public_key = private_to_public_key(key['key']) public_key = private_to_public_key(key['key'], hex=False)
seed = public_key + pack('I', child_idx) seed = public_key + pack('I', child_idx)
return hmac_sha512(key['chain_code'], seed) return hmac_sha512(key['chain_code'], seed)
return None return None
@ -300,7 +300,20 @@ def add_public_keys(ext_value, key):
return None return None
def is_valid_private_key(key): def is_xpublic_key_valid(key):
"""
Check extended public key is valid.
:param key: extended public key in BASE58 or bytes string format.
:return: boolean.
"""
if isinstance(key, str):
if not key[:4] in ['xpub', 'tpub']:
return False
return True
def is_xprivate_key_valid(key):
if isinstance(key, bytes): if isinstance(key, bytes):
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:

View File

@ -163,9 +163,6 @@ def is_public_key_valid(key):
elif key[0] == 0x02 or key[0] == 0x03: elif key[0] == 0x02 or key[0] == 0x03:
if len(key) != 33: if len(key) != 33:
return False return False
elif isinstance(key, str):
if not key[:4] in ['xpub', 'tpub']:
return False
return True return True