1
0

make xpub/xprv version information user-visible

This commit is contained in:
ThomasV
2017-09-04 11:04:59 +02:00
parent bd16e20a4d
commit 63a1db1172
5 changed files with 49 additions and 33 deletions

View File

@@ -47,14 +47,30 @@ def read_json_dict(filename):
return r
# Version numbers for BIP32 extended keys
# standard: xprv, xpub
# segwit in p2sh: yprv, ypub
# native segwit: zprv, zpub
XPRV_HEADERS = {
'standard': 0x0488ade4,
'segwit_p2sh': 0x049d7878,
'segwit': 0x4b2430c
}
XPUB_HEADERS = {
'standard': 0x0488b21e,
'segwit_p2sh': 0x049d7cb2,
'segwit': 0x4b24746
}
# Bitcoin network constants
TESTNET = False
NOLNET = False
ADDRTYPE_P2PKH = 0
ADDRTYPE_P2SH = 5
SEGWIT_HRP = "bc"
XPRV_HEADER = 0x0488ade4
XPUB_HEADER = 0x0488b21e
HEADERS_URL = "https://headers.electrum.org/blockchain_headers"
GENESIS = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
SERVERLIST = 'servers.json'
@@ -63,7 +79,6 @@ DEFAULT_SERVERS = read_json_dict('servers.json')
def set_testnet():
global ADDRTYPE_P2PKH, ADDRTYPE_P2SH
global XPRV_HEADER, XPUB_HEADER
global TESTNET, HEADERS_URL
global GENESIS
global SEGWIT_HRP
@@ -72,8 +87,6 @@ def set_testnet():
ADDRTYPE_P2PKH = 111
ADDRTYPE_P2SH = 196
SEGWIT_HRP = "tb"
XPRV_HEADER = 0x04358394
XPUB_HEADER = 0x043587cf
HEADERS_URL = "https://headers.electrum.org/testnet_headers"
GENESIS = "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"
SERVERLIST = 'servers_testnet.json'
@@ -816,11 +829,11 @@ def _CKD_pub(cK, c, s):
def xprv_header(xtype):
return bfh("%08x" % (XPRV_HEADER + xtype))
return bfh("%08x" % XPRV_HEADERS[xtype])
def xpub_header(xtype):
return bfh("%08x" % (XPUB_HEADER + xtype))
return bfh("%08x" % XPUB_HEADERS[xtype])
def serialize_xprv(xtype, c, k, depth=0, fingerprint=b'\x00'*4, child_number=b'\x00'*4):
@@ -841,10 +854,11 @@ def deserialize_xkey(xkey, prv):
fingerprint = xkey[5:9]
child_number = xkey[9:13]
c = xkey[13:13+32]
header = XPRV_HEADER if prv else XPUB_HEADER
xtype = int('0x' + bh2u(xkey[0:4]), 16) - header
if xtype not in [0, 1]:
raise BaseException('Invalid header')
header = int('0x' + bh2u(xkey[0:4]), 16)
headers = XPRV_HEADERS if prv else XPUB_HEADERS
if header not in headers.values():
raise BaseException('Invalid xpub format', hex(header))
xtype = list(headers.keys())[list(headers.values()).index(header)]
n = 33 if prv else 32
K_or_k = xkey[13+n:]
return xtype, depth, fingerprint, child_number, c, K_or_k