fix(trezor): use PAYTOADDRESS script_type for external outputs (#3311)

* fix(trezor): use PAYTOADDRESS script_type for external outputs * Apply suggestion from @konstantinullrich --------- Co-authored-by: Konstantin Ullrich <konstantinullrich12@gmail.com> Co-authored-by: Konstantin Ullrich <konstantin@cakewallet.com>

Anipy committed Jun 18, 2026 at 12:33 UTC 57a3f6875590d409dd743b5b358af7b95c47f7a6
1 file changed +17 -6
cw_bitcoin/lib/hardware/trezor_service.dart
+17 -6
@@ -69,12 +69,22 @@ class BitcoinTrezorService extends HardwareWalletService with BitcoinHardwareWal
69 final outputCount = psbt.getGlobalOutputCount();
70 for (var i = 0; i < outputCount; i++) {
71 final script = Script.fromRaw(byteData: psbt.getOutputScript(i));
72 + // Trezor's protocol expects script_type: PAYTOADDRESS whenever the
73 + // output is identified by `address` (vs. own-change `addressPath`).
74 + // Suite parses the address string itself to determine the actual
75 + // on-chain script type (P2WPKH, P2TR, P2SH, etc.); script_type is
76 + // only semantically meaningful when addressPath is set. Passing
77 + // PAYTOWITNESS / PAYTOP2SHWITNESS / PAYTOTAPROOT here together with
78 + // `address` causes Suite to reject the deeplink as "Invalid
79 + // parameters from calling app" because those values specifically
80 + // mean "own change paid to that wallet type."
81 outputs.add(TrezorTxOutput(
73 - amount: psbt.getOutputAmount(i),
74 - address: script.toAddress(),
75 - scriptType: _getScriptType(script.getAddressType()!)
76 - // ToDo: addressPath: psbt.getOutputBip32Derivation(i, pubkey).$2, // To highlight change outputs
77 - ));
82 + amount: psbt.getOutputAmount(i),
83 + address: script.toAddress(),
84 + scriptType: "PAYTOADDRESS",
85 + // ToDo: when change-output detection lands, set addressPath + _getScriptType(...) for own-change outputs.
86 + // ToDo: addressPath: psbt.getOutputBip32Derivation(i, pubkey).$2, // To highlight change outputs
87 + ));
88 }
89
90 final signedTx = await connect.signTransaction(coin: 'btc', inputs: inputs, outputs: outputs);
@@ -90,7 +100,8 @@ class BitcoinTrezorService extends HardwareWalletService with BitcoinHardwareWal
100 }
101 }
102
93 -class LitecoinTrezorService extends HardwareWalletService with BitcoinHardwareWalletService, LitecoinHardwareWalletService {
103 +class LitecoinTrezorService extends HardwareWalletService
104 + with BitcoinHardwareWalletService, LitecoinHardwareWalletService {
105 LitecoinTrezorService(this.connect);
106
107 final TrezorConnect connect;