Example #1
0
    def fromSeed(seed = None):
        if seed is None:
            seed = os.urandom(MIN_SEED_LEN)

        if len(seed) < MIN_SEED_LEN:
            raise ValueError("HDPrivateKey seed must be at least 32 bytes long")

        signed64 = hmac.new(HMAC_MAGIC_KEY, seed, hashlib.sha512).digest()

        return HDPrivateKey(privkey = PrivateKey.from_bytes(signed64[:32]), chain = signed64[32:])
Example #2
0
    def fromSeed(seed=None):
        if seed is None:
            seed = os.urandom(MIN_SEED_LEN)

        if len(seed) < MIN_SEED_LEN:
            raise ValueError(
                "HDPrivateKey seed must be at least 32 bytes long")

        signed64 = hmac.new(HMAC_MAGIC_KEY, seed, hashlib.sha512).digest()

        return HDPrivateKey(privkey=PrivateKey.from_bytes(signed64[:32]),
                            chain=signed64[32:])
Example #3
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)
Example #4
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 = Network.get_by_field('hd_private_key', version)
        privkey = PrivateKey.from_bytes(
            data[HDPrivateKey.PrivateKeyStart:HDPrivateKey.PrivateKeyEnd],
            network)

        return HDPrivateKey(privkey, chain, depth, index, parent, network)
Example #5
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)