def private_child(self, i): if (self.depth >= 255): raise Exception("Cannot go further than 255 levels") # Hardened keys have i >= 2^31. Non-hardened have i < 2^31 hardened = (i >= (2**31)) if (hardened): hmac_input = self.private_key.serialize() else: hmac_input = self.private_key.get_public_key().serialize() hmac_input += i.to_bytes(4, "big") i_left = hmac256(hmac_input + bytes([0]), self.chain_code) i_right = hmac256(hmac_input + bytes([1]), self.chain_code) sk_int = ((int.from_bytes(i_left, "big") + self.private_key.value) % default_ec.n) sk = PrivateKey.from_bytes( sk_int.to_bytes(PrivateKey.PRIVATE_KEY_SIZE, "big")) return ExtendedPrivateKey( ExtendedPrivateKey.version, self.depth + 1, self.private_key.get_public_key().get_fingerprint(), i, i_right, sk)
def public_child(self, i): if self.depth >= 255: raise Exception("Cannot go further than 255 levels") # Hardened keys have i >= 2^31. Non-hardened have i < 2^31 if i >= (2**31): raise Exception("Cannot derive hardened children from public key") hmac_input = self.public_key.serialize() + i.to_bytes(4, "big") i_left = hmac256(hmac_input + bytes([0]), self.chain_code) i_right = hmac256(hmac_input + bytes([1]), self.chain_code) sk_left_int = int.from_bytes(i_left, "big") % default_ec.n sk_left = PrivateKey.from_bytes( sk_left_int.to_bytes(PrivateKey.PRIVATE_KEY_SIZE, "big")) new_pk = PublicKey.from_g1(sk_left.get_public_key().value + self.public_key.value) return ExtendedPublicKey( self.version, self.depth + 1, self.public_key.get_fingerprint(), i, i_right, new_pk, )
def from_seed(seed): i_left = hmac256(seed + bytes([0]), b"BLS HD seed") i_right = hmac256(seed + bytes([1]), b"BLS HD seed") sk_int = int.from_bytes(i_left, "big") % default_ec.n sk = PrivateKey.from_bytes( sk_int.to_bytes(PrivateKey.PRIVATE_KEY_SIZE, "big")) return ExtendedPrivateKey(ExtendedPrivateKey.version, 0, 0, 0, i_right, sk)
def from_seed(seed): hashed = hmac256(seed, b"BLS private key seed") return PrivateKey(int.from_bytes(hashed, "big") % default_ec.n)