def xpriv_to_xpub(k): return { 'depth': k['depth'], 'fingerprint': k['fingerprint'], 'i': k['i'], 'chaincode': k['chaincode'], 'pub': ecdsa.priv_to_pub(k['priv']) }
def test_all(self): for _ in range(10): msghash = convert.random_bytes(32) priv = ecdsa.random_priv() pub = ecdsa.priv_to_pub(priv) sig = ecdsa.sign(msghash, priv) self.assertTrue( ecdsa.verify(msghash, sig, pub), "Verification error" ) self.assertEqual( pub, ecdsa.recover(msghash, sig), "Recovery failed" )
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]))) }
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])) }
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)))