def test_identity(self): xkeys = [ "xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L", "xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y", ] for data in xkeys: xkey_bytes = (HDKey.from_base58(data)).serialize() self.assertEqual((HDKey.parse(xkey_bytes)).to_base58(), data)
def test_sign(self): """Parses a PSBT, signs both inputs (1 segwit and 1 legacy), and verifies each signature is correct""" xkey = HDKey.from_base58( "tprv8ZgxMBicQKsPd9TeAdPADNnSyH9SSUUbTVeFszDE23Ki6TBB5nCefAdHkK8Fm3qMQR6sHwA56zqRmKmxnHk37JkiFzvncDqoKmPWubu7hDF" ) psbt_str = "70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000000100bb0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000010304010000000104475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae2206029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f10d90c6a4f000000800000008000000080220602dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d710d90c6a4f0000008000000080010000800001012000c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e8870103040100000001042200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903010547522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae2206023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7310d90c6a4f000000800000008003000080220603089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc10d90c6a4f00000080000000800200008000220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500008000" exp_partial_sigs = [ { "029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f": "3044022074018ad4180097b873323c0015720b3684cc8123891048e7dbcd9b55ad679c99022073d369b740e3eb53dcefa33823c8070514ca55a7dd9544f157c167913261118c01", "02dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d7": "3045022100f61038b308dc1da865a34852746f015772934208c6d24454393cd99bdf2217770220056e675a675a6d0a02b85b14e5e29074d8a25a9b5760bea2816f661910a006ea01", }, { "03089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc": "3044022062eb7a556107a7c73f45ac4ab5a1dddf6f7075fb1275969a7f383efff784bcb202200c05dbb7470dbf2f08557dd356c7325c1ed30913e996cd3840945db12228da5f01", "023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e73": "3044022065f45ba5998b59a27ffe1a7bed016af1f1f90d54b3aa8f7450aa5f56a25103bd02207f724703ad1edb96680b284b56d4ffcb88f7fb759eabbe08aa30f29b851383d201", }, ] psbt = PSBT.parse(unhexlify(psbt_str)) psbt.sign_with(xkey) for i in range(len(psbt.inputs)): inp = psbt.inputs[i] self.assertEqual(len(inp.partial_sigs), len(exp_partial_sigs[i])) for act_pub, act_sig in inp.partial_sigs.items(): act_pub_str = hexlify(act_pub.serialize()).decode("utf-8") self.assertIn(act_pub_str, exp_partial_sigs[i].keys()) self.assertEqual( hexlify(act_sig).decode("utf-8"), exp_partial_sigs[i][act_pub_str])
def dispatch_message(self, json): header = json["header"] payload = json["payload"] if header == IncomingMessageHeader.REQUEST_INIT_WALLET: network = json["payload"]["network"] self.controller.handle_init_wallet_request(network) elif header == IncomingMessageHeader.REQUEST_SIGN_TRANSACTION: psbt_bytes = binascii.unhexlify(payload["psbt"]) psbt = PSBT.parse(psbt_bytes) self.controller.handle_sign_transaction_request(psbt) elif header == IncomingMessageHeader.REQUEST_SHOW_ADDRESS: key_path = payload["key_path"] self.controller.handle_show_address_request(key_path) elif header == IncomingMessageHeader.REQUEST_LOAD_WALLET: wallet_xpub = HDKey.parse( binascii.unhexlify(payload["wallet_xpub"])) wallet_keypath = payload["wallet_keypath"] network = payload["network"] recovery_phrase_length = payload["recovery_phrase_length"] self.controller.handle_load_wallet_request(wallet_xpub, wallet_keypath, network, recovery_phrase_length)
def test_bip32_derive(self): for seed, path, exp_xprv, exp_xpub in DERIVE_VECTORS: root_xprv = HDKey.from_seed(unhexlify(seed)) act_xprv = root_xprv.derive(path) act_xpub = act_xprv.to_public() self.assertTrue(root_xprv.is_private) self.assertTrue(act_xprv.is_private) self.assertEqual(act_xprv.to_base58(), exp_xprv) self.assertEqual(act_xpub.to_base58(), exp_xpub)
def test_bip39(self): for [seed, exp_mnemonic, hex_seed, xprv] in VECTORS: act_mnemonic = mnemonic_from_bytes(unhexlify(seed)) act_xkey = HDKey.from_seed( mnemonic_to_seed(act_mnemonic, password="******")) self.assertEqual(act_mnemonic, exp_mnemonic) self.assertTrue(mnemonic_is_valid(act_mnemonic)) self.assertEqual( hexlify(mnemonic_to_bytes(act_mnemonic)).decode(), seed) self.assertEqual(act_xkey.to_base58(), xprv)
def load_wallet(self, recovery_phrase: str, password: str) -> bool: seed = mnemonic_to_seed(recovery_phrase, password) master_xpriv = HDKey.from_seed( seed, NETWORKS[self.network]["xprv"]) wallet_xpub = master_xpriv.derive(self.wallet_keypath).to_public() if wallet_xpub == self.wallet_xpub: self.master_xpriv = master_xpriv self.serial_client.write_load_wallet_success() return True return False
def save_wallet(self, recovery_phrase: str, password: str, was_recovered: bool): seed = mnemonic_to_seed(recovery_phrase, password) self.master_xpriv = HDKey.from_seed( seed, NETWORKS[self.network]["xprv"]) self.master_xpub = self.master_xpriv.to_public() wallet_xpubs = self.derive_wallet_xpubs(was_recovered) master_fingerprint = self.master_xpub.child(0).fingerprint self.serial_client.write_init_wallet_success( wallet_xpubs, master_fingerprint, len(recovery_phrase.split()) )