Skip to content

trezor: mark sorted multisig keys as lexicographic - #865

Open
KHADEVS wants to merge 2 commits into
bitcoin-core:masterfrom
KHADEVS:trezor-sortedmulti-change
Open

KHADEVS wants to merge 2 commits into
bitcoin-core:masterfrom
KHADEVS:trezor-sortedmulti-change

Conversation

@KHADEVS

@KHADEVS KHADEVS commented Nov 20, 2026

Copy link
Copy Markdown

When a Trezor signs a sortedmulti() spend through HWI, the change output usually comes up on the device as one more recipient, so the user is asked to check an address that is their own.

Trezor decides whether an output is change by comparing a fingerprint of its multisig xpubs with the inputs'. Since core 2.8.7 and legacy 1.13.0 (trezor/trezor-firmware#4351, trezor/trezor-firmware#4396), that fingerprint follows the order the xpubs are given in, unless pubkeys_order is LEXICOGRAPHIC, in which case the device sorts them first. Earlier firmware always sorted them. parse_multisig() gives the xpubs in script order and never sets the field. With sortedmulti() the script order depends on the derived keys and changes from one address to the next: for a 2-of-3, an input and the change end up in the same order about one time in six, and every input has to match the others as well.

This adds pubkeys_order to the vendored MultisigRedeemScriptType (field 6, as in upstream trezorlib) and sets it to LEXICOGRAPHIC when the keys in the script are sorted. The device then sorts the derived keys itself, which gives the same script back, so what gets signed does not change. A multi() script whose keys happen to be sorted at some address gets the flag too; at worst that is today's behaviour, the change shown as a recipient. Sparrow's Lark uses the same rule (sparrowwallet/lark@2bee436).

Firmware without the field ignores it: the core decoder skips unknown fields, and so does nanopb on older legacy firmware and on the KeepKey, which goes through the same code. displayaddress is not affected, since HWI sorts the keys itself there.

Tested:

  • TestTrezorMultisig, added to the unit tests, recomputes the device's fingerprint (multisig_fingerprint in core/src/apps/bitcoin/multisig.py) for a receive and a change address of a 2-of-3 sortedmulti() whose cosigners come in different orders. It fails without the change to trezor.py.
  • test_trezor.py --model_t and --model_1 pass against the emulators Trezor publishes for core 2.13.1 (T2T1) and legacy 1.13.1 (T1B1), with bitcoind 32.1.
  • Signing a 2-of-3 P2WSH spend with change on those emulators, the device asked to confirm both outputs before this change (4 ConfirmOutput button requests on the Model T, 2 on the One) and only the payment after (2 and 1), and signed both times. On the Safe 5 emulator (T3T1, core 2.13.1), reading the screens over the debug link, the change address and amount appeared as "Recipient need to support network types for sign_message and get_pubkey_at_path #2" before and not after, and the signature verifies against the transaction.

I ran into this while running adversarial PSBT cases against signing devices: https://github.com/KHADEVS/bitcoin-signer-cases

Since core 2.8.7 and legacy 1.13.0 (trezor-firmware#4351, #4396), Trezor
only treats a multisig output as change if a fingerprint of its xpubs
matches the inputs', and that fingerprint follows the order the xpubs
are given in unless pubkeys_order is LEXICOGRAPHIC. parse_multisig()
gives them in script order and never sets the field. With sortedmulti()
the script order changes from one address to the next, so the change
usually gets a different fingerprint and is shown as a payment.

Add pubkeys_order to the vendored MultisigRedeemScriptType and set it to
LEXICOGRAPHIC when the script's keys are sorted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant