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)
def fromSeed(seed = None): if seed is None: seed = os.urandom(MIN_SEED_LEN) if len(seed) < MIN_SEED_LEN: raise ValueError("HDPublicKey seed must be at least 32 bytes long") signed64 = hmac.new(HMAC_MAGIC_KEY, seed, hashlib.sha512).digest() return HDPublicKey(pubkey = PublicKey.from_bytes(signed64[:32]), chain = signed64[32:])
def fromSeed(seed=None): if seed is None: seed = os.urandom(MIN_SEED_LEN) if len(seed) < MIN_SEED_LEN: raise ValueError("HDPublicKey seed must be at least 32 bytes long") signed64 = hmac.new(HMAC_MAGIC_KEY, seed, hashlib.sha512).digest() return HDPublicKey(pubkey=PublicKey.from_bytes(signed64[:32]), chain=signed64[32:])
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)
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)
def to_public_key(self): return PublicKey.from_private_key(self)