Ejemplo n.º 1
0
    def from_string(b58_str):
        data = utils.encoding.a2b_hashed_base58(b58_str) # TODO checksum?

        chain   = data[HDPublicKey.ChainCodeStart : HDPublicKey.ChainCodeEnd]
        depth   = int_from_bytes(data[HDPublicKey.DepthStart : HDPublicKey.DepthEnd])
        index   = int_from_bytes(data[HDPublicKey.ChildIndexStart : HDPublicKey.ChildIndexEnd])
        parent  = int_from_bytes(data[HDPublicKey.ParentFingerPrintStart : HDPublicKey.ParentFingerPrintEnd])

        # The version field is used to deduce the network:
        version = int_from_bytes(data[HDPublicKey.VersionStart:HDPublicKey.VersionEnd])
        network = Network.get_by_field('hd_public_key', version)
        pubkey  = PublicKey.from_bytes(data[HDPublicKey.PublicKeyStart : HDPublicKey.PublicKeyEnd], network)

        return HDPublicKey(pubkey, chain, depth, index, parent, network)
Ejemplo n.º 2
0
    def from_string(b58_str):
        data = utils.encoding.a2b_hashed_base58(b58_str) # TODO checksum?

        chain   = data[HDPrivateKey.ChainCodeStart : HDPrivateKey.ChainCodeEnd]
        depth   = int_from_bytes(data[HDPrivateKey.DepthStart : HDPrivateKey.DepthEnd])
        index   = int_from_bytes(data[HDPrivateKey.ChildIndexStart : HDPrivateKey.ChildIndexEnd])
        parent  = int_from_bytes(data[HDPrivateKey.ParentFingerPrintStart : HDPrivateKey.ParentFingerPrintEnd])

        # The version field is used to deduce the network:
        version = int_from_bytes(data[HDPrivateKey.VersionStart:HDPrivateKey.VersionEnd])
        network = networks.find(version, 'hd_private_key')
        privkey = PrivateKey.from_bytes(data[HDPrivateKey.PrivateKeyStart : HDPrivateKey.PrivateKeyEnd], network)

        return HDPrivateKey(privkey, chain, depth, index, parent, network)
Ejemplo n.º 3
0
    def derive(self, index, hardened = False):
        # TODO Is index Valid?

        if index < HARDENED_START and hardened:
            index += HARDENED_START

        if hardened:
            raise ValueError("Hardened derivation is not posible on HDPublicKey")
        else:
            key = self.to_public_key().to_bytes()

        signed64 = hmac.new(
            self.chain,
            key + to_bytes(self.index, length = 4),
            hashlib.sha512
        ).digest()

        x, y = self.pubkey.pair
        curve = utils.generator_secp256k1
        point = int_from_bytes(signed64[:32]) * curve + utils.Point(curve.curve(), x, y, curve.order())
        pubkey = PublicKey((point.x(), point.y()), self.network)

        chain   = signed64[32:]
        depth   = self.depth + 1

        return HDPublicKey(pubkey, chain, depth, index, self.fingerprint, self.network)
Ejemplo n.º 4
0
    def derive(self, index, hardened=False):
        # TODO Is index Valid?

        if index < HARDENED_START and hardened:
            index += HARDENED_START

        if hardened:
            raise ValueError(
                "Hardened derivation is not posible on HDPublicKey")
        else:
            key = self.to_public_key().to_bytes()

        signed64 = hmac.new(self.chain, key + to_bytes(self.index, length=4),
                            hashlib.sha512).digest()

        x, y = self.pubkey.pair
        curve = utils.generator_secp256k1
        point = int_from_bytes(signed64[:32]) * curve + utils.Point(
            curve.curve(), x, y, curve.order())
        pubkey = PublicKey((point.x(), point.y()), self.network)

        chain = signed64[32:]
        depth = self.depth + 1

        return HDPublicKey(pubkey, chain, depth, index, self.fingerprint,
                           self.network)
Ejemplo n.º 5
0
    def from_string(b58_str):
        data = utils.encoding.a2b_hashed_base58(b58_str)  # TODO checksum?

        chain = data[HDPublicKey.ChainCodeStart:HDPublicKey.ChainCodeEnd]
        depth = int_from_bytes(
            data[HDPublicKey.DepthStart:HDPublicKey.DepthEnd])
        index = int_from_bytes(
            data[HDPublicKey.ChildIndexStart:HDPublicKey.ChildIndexEnd])
        parent = int_from_bytes(
            data[HDPublicKey.ParentFingerPrintStart:HDPublicKey.
                 ParentFingerPrintEnd])

        # The version field is used to deduce the network:
        version = int_from_bytes(
            data[HDPublicKey.VersionStart:HDPublicKey.VersionEnd])
        network = Network.get_by_field('hd_public_key', version)
        pubkey = PublicKey.from_bytes(
            data[HDPublicKey.PublicKeyStart:HDPublicKey.PublicKeyEnd], network)

        return HDPublicKey(pubkey, chain, depth, index, parent, network)
Ejemplo n.º 6
0
    def __new__(cls,
                privkey,
                chain,
                depth=0,
                index=0,
                parent=ROOT_FINGERPRINT,
                network=network.default):
        assert isinstance(privkey, PrivateKey)
        fingerprint = int_from_bytes(calculate_fingerprint(privkey))

        return super(HDPrivateKey,
                     cls).__new__(cls, privkey, chain, depth, index, parent,
                                  network, fingerprint)
Ejemplo n.º 7
0
    def derive(self, index, hardened=False):
        # TODO Is index Valid?

        if index < HARDENED_START and hardened:
            index += HARDENED_START

        if hardened:
            key = '\0' + self.to_private_key().to_bytes(
            )  # a literal 0 is prepended to private keys
        else:
            key = self.to_public_key().to_bytes()

        signed64 = hmac.new(self.chain, key + to_bytes(self.index, length=4),
                            hashlib.sha512).digest()

        seed = (int_from_bytes(signed64[:32]) + self.to_private_key().seed
                ) % utils.generator_secp256k1.order()
        privkey = PrivateKey(seed, self.network)
        chain = signed64[32:]
        depth = self.depth + 1

        return HDPrivateKey(privkey, chain, depth, index, self.fingerprint,
                            self.network)
Ejemplo n.º 8
0
    def derive(self, index, hardened = False):
        # TODO Is index Valid?

        if index < HARDENED_START and hardened:
            index += HARDENED_START

        if hardened:
            key = '\0' + self.to_private_key().to_bytes() # a literal 0 is prepended to private keys
        else:
            key = self.to_public_key().to_bytes()

        signed64 = hmac.new(
            self.chain,
            key + to_bytes(self.index, length = 4),
            hashlib.sha512
        ).digest()

        seed    = (int_from_bytes(signed64[:32]) + self.to_private_key().seed) % utils.generator_secp256k1.order()
        privkey = PrivateKey(seed, self.network)
        chain   = signed64[32:]
        depth   = self.depth + 1

        return HDPrivateKey(privkey, chain, depth, index, self.fingerprint, self.network)
Ejemplo n.º 9
0
    def __new__(cls, pubkey, chain, depth = 0, index = 0, parent = ROOT_FINGERPRINT, network = network.default):
        assert isinstance(pubkey, PublicKey)
        fingerprint = int_from_bytes(calculate_fingerprint(pubkey))

        return super(HDPublicKey, cls).__new__(cls, pubkey, chain, depth, index, parent, network, fingerprint)