Esempio n. 1
0
    def deserialize(self, ser_key: str):
        """Deserialize a serialized key into self"""
        ser_key = base58_decode(ser_key, 82)

        # Checksum
        if sha256(sha256(ser_key[:78]))[:4] != ser_key[78:]:
            raise ValueError(
                "Wrong checksum of the extended key: {:s}".format(ser_key.hex())
            )

        self.level = ser_key[4]
        self.fingerprint = ser_key[5:9]
        self.index = bytes2int(ser_key[9:13])
        self.chain_code = ser_key[13:45]

        if ser_key[:4] == MAINNET_PRIVATE:
            # Miss 00 and get the private key
            self.key = ser_key[46:78]
        elif ser_key[:4] == MAINNET_PUBLIC:
            # Get x coordinate of the public point
            x = bytes2int(ser_key[46:78])
            # Calculate y coordinate of the public point
            y = ECPoint.get_secp256k1_y(x)
            # Choice even y if prefix = 02, else choice odd y
            if ser_key[45] == 2:
                y = ECPoint.get_secp256k1_p() - y if y % 2 != 0 else y
            else:
                y = ECPoint.get_secp256k1_p() - y if y % 2 == 0 else y

            self.key = ECPoint(x, y)
        else:
            raise ValueError(
                "Invalid serialized extended key: {:s}".format(ser_key.hex())
            )
Esempio n. 2
0
# --- Usage and testing ---
if __name__ == "__main__":

    from btc.ecpoint import ECPoint

    # Find the nearest point with x coordinate >= start_x
    start_x = 10**33

    while True:
        try:
            p = ECPoint(start_x, ECPoint.get_secp256k1_y(start_x))
            break
        except AssertionError:
            start_x += 1
            continue

    print(p)