Exemplo n.º 1
0
    def test_encode_decode(self):
        for exp_bin, exp_base58 in load_test_vectors('base58_encode_decode.json'):
            exp_bin = unhexlify(exp_bin.encode('utf8'))

            act_base58 = encode(exp_bin)
            act_bin = decode(exp_base58)

            self.assertEqual(act_base58, exp_base58)
            self.assertEqual(act_bin, exp_bin)
Exemplo n.º 2
0
def bip32_deserialize(data):
    dbin = base58.decode(data)
    if Hash(dbin[:-4])[:4] != dbin[-4:]:
        raise Exception("Invalid checksum")
    vbytes = dbin[0:4]
    depth = dbin[4]
    fingerprint = dbin[5:9]
    i = struct.unpack(b'>L',dbin[9:13])[0]
    chaincode = dbin[13:45]
    key = dbin[46:78] + b'\x01' if vbytes in PRIVATE else dbin[45:78]
    return (vbytes, depth, fingerprint, i, chaincode, key)
Exemplo n.º 3
0
def bip32_deserialize(data):
    dbin = base58.decode(data)
    if Hash(dbin[:-4])[:4] != dbin[-4:]:
        raise Exception("Invalid checksum")
    vbytes = dbin[0:4]
    if vbytes not in PRIVATE and vbytes not in PUBLIC:
        raise Exception("Invalid vbytes {}".format(vbytes))
    depth = dbin[4]
    fingerprint = dbin[5:9]
    child_num = struct.unpack(b'>L', dbin[9:13])[0]
    if depth == 0 and (fingerprint != b'\x00' * 4 or child_num != 0):
        raise Exception(
            "Invalid master key, depth = {}, fingerprint = {}, child_num = {}".
            format(depth, fingerprint, child_num))
    chaincode = dbin[13:45]
    key = dbin[46:78] + b'\x01' if vbytes in PRIVATE else dbin[45:78]
    if vbytes in PUBLIC and not is_valid_pubkey(key):
        raise Exception("Invalid public key")
    if vbytes in PRIVATE:
        if dbin[45] != 0:
            raise Exception("Invalid private key")
        # check for validity, this will raise exception
        privkey_to_pubkey(key)
    return (vbytes, depth, fingerprint, child_num, chaincode, key)
def b58check_to_bin(s):
    data = base58.decode(s)
    assert Hash(data[:-4])[:4] == data[-4:]
    return struct.pack(b"B", data[0]), data[1:-4]