Exemplo n.º 1
0
def serialize_xpub(k):
    b = b''
    b += HD_HEADER_PUBLIC
    b += convert.int_to_byte(k['depth'])
    b += k['fingerprint']
    b += convert.int_to_bytes(k['i'], 4)
    b += k['chaincode']
    b += ecdsa.serialize_pub(k['pub'])
    return convert.bytes_to_b58check(b)
Exemplo n.º 2
0
def derive_xpub(k, i):
    i = int(i)

    if i >= 2**31:
        raise Exception("Can't do private derivation on public key!")

    pub = k['pub']
    pub_ser = ecdsa.serialize_pub(pub)

    hmacdata = pub_ser + convert.int_to_bytes(i, 4)
    I = hmac.new(k['chaincode'], hmacdata, hashlib.sha512).digest()

    return {
        'depth': k['depth'] + 1,
        'fingerprint': hashes.hash160(pub_ser)[:4],
        'i': i,
        'chaincode': I[32:],
        'pub': ec.add(k['pub'], ecdsa.priv_to_pub(ecdsa.deserialize_priv(I[:32])))
    }
Exemplo n.º 3
0
def derive_xpriv(k, i):
    i = int(i)

    pub = ecdsa.priv_to_pub(k['priv'])
    pub_ser = ecdsa.serialize_pub(pub)
    priv_ser = ecdsa.serialize_priv(k['priv'])

    if i >= 2**31:
        hmacdata = b'\x00' + priv_ser + convert.int_to_bytes(i, 4)
    else:
        hmacdata = pub_ser + convert.int_to_bytes(i, 4)

    I = hmac.new(k['chaincode'], hmacdata, hashlib.sha512).digest()

    return {
        'depth': k['depth'] + 1,
        'fingerprint': hashes.hash160(pub_ser)[:4],
        'i': i,
        'chaincode': I[32:],
        'priv': ec.add_scalar(k['priv'], ecdsa.deserialize_priv(I[:32]))
    }
Exemplo n.º 4
0
 def test_serialize(self):
     for _ in range(10):
         priv = ecdsa.random_priv()
         pub = ecdsa.priv_to_pub(priv)
         self.assertEqual(pub, ecdsa.deserialize_pub(ecdsa.serialize_pub(pub, compressed=False)))
         self.assertEqual(pub, ecdsa.deserialize_pub(ecdsa.serialize_pub(pub, compressed=True)))