def child(self, index, hardened=False): if index > 0xFFFFFFFF: raise ValueError("Index should be less then 2^32") if hardened and index < 0x80000000: index += 0x80000000 if index >= 0x80000000: hardened = True if hardened and not self.is_private: raise ValueError("Can't do hardened with public key") # we need pubkey for fingerprint anyways sec = self.sec() fingerprint = hashes.hash160(sec)[:4] if hardened: data = b'\x00' + self.key.serialize() + index.to_bytes(4, 'big') else: data = sec + index.to_bytes(4, 'big') raw = hashlib.hmac_sha512(self.chain_code, data) secret = raw[:32] chain_code = raw[32:] if self.is_private: secp256k1.ec_privkey_tweak_add(secret, self.key.serialize()) key = ec.PrivateKey(secret) else: # copy of internal secp256k1 point structure point = self.key._point[:] secp256k1.ec_pubkey_tweak_add(point, secret) key = ec.PublicKey(point) return HDKey(key, chain_code, version=self.version[:], depth=self.depth + 1, fingerprint=fingerprint, child_number=index)
def save_secret(cb_on_error): Secret.hmac = hmac_sha512(Key.key, Secret.secret) try: with open(secret_fname, "w") as f: f.write('{"secret":"%s", "hmac":"%s"}' % \ (hexlify(Secret.secret).decode('utf-8'), \ hexlify(Secret.hmac).decode('utf-8'))) except: cb_on_error("Error", "Secret could not be saved!", sys.exit)
def load_key(): global entropy with open(reckless_fname, "r") as f: d = json.loads(f.read()) entropy = unhexlify(d["entropy"]) if "hmac" in d: hmac_calc = hmac_sha512(Key.key, entropy) if unhexlify(d["hmac"]) != hmac_calc: raise ValueError('Hmac does not match!') Key.iv = unhexlify(d["iv"]) entropy = entropy_decrypt(entropy) if entropy is not None: ask_for_password() else: gui.error("Failed to load your recovery phrase.")
def load_key(): global entropy try: with open(reckless_fname, "r") as f: d = json.loads(f.read()) entropy = unhexlify(d["entropy"]) if "hmac" in d: hmac_calc = hmac_sha512(Key.key, entropy) if unhexlify(d["hmac"]) != hmac_calc: raise ValueError('Hmac does not match!') Key.iv = unhexlify(d["iv"]) entropy = entropy_decrypt(entropy) ask_for_password() except: gui.error("Something went wrong, sorry")
def save_entropy_encrypted(): try: Key.iv = get_random_bytes(16) entropy_encrypted = entropy_encrypt(entropy) hmac_entropy_encrypted = hmac_sha512(Key.key, entropy_encrypted) obj = { "entropy": hexlify(entropy_encrypted).decode('utf-8'), "iv": hexlify(Key.iv).decode('utf-8'), "hmac": hexlify(hmac_entropy_encrypted).decode('utf-8') } with open(reckless_fname, "w") as f: f.write(json.dumps(obj)) with open(reckless_fname, "r") as f: d = json.loads(f.read()) if "entropy" in d and d["entropy"] == hexlify(entropy_encrypted).decode('utf-8') and \ unhexlify(d["hmac"]) == hmac_entropy_encrypted and entropy == entropy_decrypt(entropy_encrypted): gui.alert("Success!", "Your encrypted key is saved in the memory now") else: gui.error("Something went wrong") except Exception as e: gui.error("Fail: %r" % e)
def from_seed(cls, seed, version=NETWORKS["main"]["xprv"]): raw = hashlib.hmac_sha512(b'Bitcoin seed', seed) private_key = ec.PrivateKey(raw[:32]) chain_code = raw[32:] return cls(private_key, chain_code, version=version)
def from_seed(cls, seed: bytes, version=NETWORKS["liquidv1"]["xprv"]): raw = hashlib.hmac_sha512(b"Elements blinding seed", seed) private_key = ec.PrivateKey(raw[:32]) chain_code = raw[32:] return cls(private_key, chain_code, version=version)
def from_seed(cls, seed: bytes, version=NETWORKS["main"]["xprv"]): """Creates a root private key from 64-byte seed""" raw = hashlib.hmac_sha512(b"Bitcoin seed", seed) private_key = ec.PrivateKey(raw[:32]) chain_code = raw[32:] return cls(private_key, chain_code, version=version)
def is_pin_valid(): assert (Secret.hmac != None) hmac_calc = hmac_sha512(Key.key, Secret.secret) if hmac_calc != Secret.hmac: return False return True
def generate_key(user_pin): Key.key = hmac_sha512(Secret.secret, user_pin)[0:32]
def antiphishing_word(pin_digit): _hmac = hmac_sha512(Secret.secret, pin_digit) hmac_num = unpack('<H', bytearray(_hmac[0:8]))[0] idx = hmac_num % len(bip39.WORDLIST) word = (bip39.WORDLIST[idx]) return word