def test_invalid_psbts(self): for psbt_str in INVALID_VECTORS: psbt_bytes = unhexlify(psbt_str) try: PSBT.parse(psbt_bytes) except: continue # succeed if exception was thrown self.fail( "Invalid psbt was parsed successfully: {}".format(psbt_str))
def create_psbts(self, base64_psbt, wallet): psbts = super().create_psbts(base64_psbt, wallet) # remove non-witness utxo if they are there to reduce QR code size updated_psbt = wallet.fill_psbt(base64_psbt, non_witness=False, xpubs=False) qr_psbt = PSBT.parse(a2b_base64(updated_psbt)) # find my key fgp = None derivation = None for k in wallet.keys: if k in self.keys and k.fingerprint and k.derivation: fgp = bytes.fromhex(k.fingerprint) derivation = bip32.parse_path(k.derivation) break # remove unnecessary derivations from inputs and outputs for inp in qr_psbt.inputs + qr_psbt.outputs: # keep only my derivation for k in list(inp.bip32_derivations.keys()): if inp.bip32_derivations[k].fingerprint != fgp: inp.bip32_derivations.pop(k, None) # remove scripts from outputs (DIY should know about the wallet) for out in qr_psbt.outputs: out.witness_script = None out.redeem_script = None # remove partial sigs from inputs for inp in qr_psbt.inputs: inp.partial_sigs = {} psbts["qrcode"] = b2a_base64(qr_psbt.serialize()).strip().decode() # we can add xpubs to SD card, but non_witness can be too large for MCU psbts["sdcard"] = wallet.fill_psbt(psbts["qrcode"], non_witness=False, xpubs=True) return psbts
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 replace_derivations(self, wallet, psbts): # cc wants everyone to use the same derivation fgp = None derivation = None for k in wallet.keys: if k in self.keys and k.fingerprint and k.derivation: fgp = k.fingerprint derivation = k.derivation break if not fgp: return path = bip32.parse_path(derivation) for kk in list(psbts.keys()): psbt = PSBT.parse(a2b_base64(psbts[kk])) for xpub in psbt.xpubs: psbt.xpubs[xpub].derivation = list(path) # remove partial signatures from device psbt for scope in psbt.inputs: scope.partial_sigs = OrderedDict() for scope in psbt.inputs + psbt.outputs: for k in list(scope.bip32_derivations.keys()): original = scope.bip32_derivations[k].derivation scope.bip32_derivations[ k].derivation = path + original[-2:] psbts[kk] = b2a_base64(psbt.serialize()).decode().strip()
def test_valid_psbts(self): for psbt_str in VALID_VECTORS: psbt_bytes = unhexlify(psbt_str) psbt_act = PSBT.parse(psbt_bytes) msg = "Valid psbt changed after being parsed & serialized: {}".format( psbt_str) self.assertEqual(psbt_act.serialize(), psbt_bytes, msg)