test: extract PSBT helpers
What changed, and why it matters
This commit is a simple code cleanup in the test suite. It pulls out two small blocks of test code into reusable helper functions for signing and finalizing PSBTs, and for setting global xpubs in PSBTs. There is no change to production code, no security fix, and no behavior change.
No security action needed. This is a test-only refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff refactors test/test_device.py by introducing two helper methods: sign_and_finalize() wraps the device signtx command and Bitcoin Core finalizepsbt RPC with assertions, and _set_global_xpubs() replaces a PSBT’s global xpub map. Existing inline code at two call sites is replaced with calls to these helpers. The logic and assertions remain identical; only duplication is reduced.
Changed components
test/test_device.pyInspect captured patch +25 / −8
### test/test_device.py
@@ -198,6 +198,29 @@ def setup_wallets(self):
self.wrpc = self.bitcoind.get_wallet_rpc(wallet_name)
self.wpk_rpc = self.bitcoind.get_wallet_rpc("supply")
+ def sign_and_finalize(self, psbt: str, *signtx_args: str) -> Dict:
+ """Sign a PSBT with the device and assert that it finalizes."""
+ sign_res = self.do_command(
+ self.dev_args + ["signtx", *signtx_args, psbt]
+ )
+ self.assertNotIn("error", sign_res)
+ self.assertTrue(sign_res["signed"])
+
+ finalize_res = self.wrpc.finalizepsbt(sign_res["psbt"])
+ self.assertTrue(finalize_res["complete"])
+ return finalize_res
+
+ def _set_global_xpubs(
+ self,
+ psbt: str,
+ xpubs: Dict[bytes, KeyOriginInfo],
+ ) -> str:
+ """Replace a PSBT's global xpub map."""
+ psbt_obj = PSBT()
+ psbt_obj.deserialize(psbt)
+ psbt_obj.xpub = xpubs
+ return psbt_obj.serialize()
+
def setUp(self):
self.emulator.start()
@@ -372,10 +395,7 @@ def _generate_and_finalize(self, unknown_inputs, psbt):
if not unknown_inputs:
# Just do the normal signing process to test "all inputs" case
- sign_res = self.do_command(self.dev_args + ['signtx', psbt])
- finalize_res = self.wrpc.finalizepsbt(sign_res['psbt'])
- self.assertTrue(sign_res["signed"])
- self.assertTrue(finalize_res["complete"])
+ finalize_res = self.sign_and_finalize(psbt)
else:
# Sign only input one on first pass
# then rest on second pass to test ability to successfully
@@ -576,10 +596,7 @@ def _test_signtx(self, input_types, multisig_types, external, op_return: bool):
)["psbt"]
# We need to modify the psbt to include our xpubs as Core does not include xpubs
- psbt_obj = PSBT()
- psbt_obj.deserialize(psbt)
- psbt_obj.xpub = xpubs
- psbt = psbt_obj.serialize()
+ psbt = self._set_global_xpubs(psbt, xpubs)
if external:
# Sign with unknown inputs in two stepsWhy this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.