dev
dart 21 lines 684 Bytes
Raw
1 import 'dart:math';
2
3 import 'package:bitcoin_base/bitcoin_base.dart';
4
5 /// Applies [ordering] to a transaction's [outputs] before building.
6 ///
7 /// Returns a new list; the input is never mutated. Only [BitcoinOrdering.shuffle]
8 /// reorders (using a secure RNG by default) so the change output is no longer
9 /// placed deterministically last. Any other value preserves the given order,
10 /// matching the pre-existing hardware-wallet behavior.
11 List<T> orderOutputs<T>(
12 List<T> outputs,
13 BitcoinOrdering ordering, {
14 Random? rng,
15 }) {
16 final result = List<T>.of(outputs);
17 if (ordering == BitcoinOrdering.shuffle) {
18 result.shuffle(rng ?? Random.secure());
19 }
20 return result;
21 }