Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_orderisLEXICOGRAPHIC, 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. Withsortedmulti()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_orderto the vendoredMultisigRedeemScriptType(field 6, as in upstream trezorlib) and sets it toLEXICOGRAPHICwhen 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. Amulti()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.
displayaddressis not affected, since HWI sorts the keys itself there.Tested:
TestTrezorMultisig, added to the unit tests, recomputes the device's fingerprint (multisig_fingerprintincore/src/apps/bitcoin/multisig.py) for a receive and a change address of a 2-of-3sortedmulti()whose cosigners come in different orders. It fails without the change totrezor.py.test_trezor.py --model_tand--model_1pass against the emulators Trezor publishes for core 2.13.1 (T2T1) and legacy 1.13.1 (T1B1), with bitcoind 32.1.ConfirmOutputbutton 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