Files
hash-wallet/cw_bitcoin/lib/output_ordering.dart
T
b73c46ac59 Shuffle change output on hardware wallet and Bitcoin Cash sends (#3432)
* Shuffle change output on hardware wallet sends

Hardware wallet BTC/LTC sends passed outputs to the PSBT/device in the
given order (change last), a position fingerprint. The outputOrdering
param was plumbed but ignored. Add orderOutputs() and apply it in both
buildHardwareWalletTransaction paths; set the send call site to shuffle.

Refs #3376 (the software send + RBF paths were covered by #3420).

* Shuffle change output on Bitcoin Cash sends

The BCH send path built via ForkedTransactionBuilder with
outputOrdering: none, leaving change deterministically last. The builder
shuffles natively and change is found by isChange, not position, so flip
it to shuffle (matches the BTC software path from #3420).

Refs #3376.

* Update cw_bitcoin/lib/electrum_wallet.dart

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

---------

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
2026-07-26 02:30:41 +02:00

22 lines
684 B
Dart

import 'dart:math';
import 'package:bitcoin_base/bitcoin_base.dart';
/// Applies [ordering] to a transaction's [outputs] before building.
///
/// Returns a new list; the input is never mutated. Only [BitcoinOrdering.shuffle]
/// reorders (using a secure RNG by default) so the change output is no longer
/// placed deterministically last. Any other value preserves the given order,
/// matching the pre-existing hardware-wallet behavior.
List<T> orderOutputs<T>(
List<T> outputs,
BitcoinOrdering ordering, {
Random? rng,
}) {
final result = List<T>.of(outputs);
if (ordering == BitcoinOrdering.shuffle) {
result.shuffle(rng ?? Random.secure());
}
return result;
}