-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement random strategy for utxo sorting
- Loading branch information
1 parent
d3a4fe5
commit e6c9977
Showing
6 changed files
with
121 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import type { TransactionInputOutputSortingStrategy } from '@trezor/utxo-lib/libDev/src'; | ||
import type { TransactionInputOutputSortingStrategy } from '@trezor/utxo-lib'; | ||
|
||
export const DEFAULT_SORTING_STRATEGY: TransactionInputOutputSortingStrategy = 'bip69'; | ||
export const DEFAULT_SORTING_STRATEGY: TransactionInputOutputSortingStrategy = 'random'; |
39 changes: 39 additions & 0 deletions
39
packages/utxo-lib/src/compose/sorting/randomSortingStrategy.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { SortingStrategy } from './sortingStrategy'; | ||
import { convertOutput } from './convertOutput'; | ||
import { arrayShuffle, getRandomInt } from '@trezor/utils'; | ||
|
||
export const randomSortingStrategy: SortingStrategy = ({ result, request, convertedInputs }) => { | ||
const nonChangeOutputPermutation: number[] = []; | ||
const changeOutputPermutation: number[] = []; | ||
|
||
const convertedOutputs = result.outputs.map((output, index) => { | ||
if (request.outputs[index]) { | ||
nonChangeOutputPermutation.push(index); | ||
|
||
return convertOutput(output, request.outputs[index]); | ||
} | ||
|
||
changeOutputPermutation.push(index); | ||
|
||
return convertOutput(output, { type: 'change', ...request.changeAddress }); | ||
}); | ||
|
||
/** | ||
* The goal here is to randomly insert change outputs into the outputs array., | ||
* so you cannot tell what is the change just by the order of the transaction. | ||
*/ | ||
const permutation = [...nonChangeOutputPermutation]; | ||
// Min (0) is inclusive, max (permutation.length + 1) is exclusive | ||
// Example: for array [0, 1, 2] the result can be: 0, 1, 2, 3 | ||
const newPositionOfChange = getRandomInt(0, permutation.length + 1); | ||
|
||
permutation.splice(newPositionOfChange, 0, ...changeOutputPermutation); | ||
const sortedOutputs = permutation.map(index => convertedOutputs[index]); | ||
|
||
return { | ||
/** Randomly shuffle inputs to make it harder to fingerprint the Trezor Suite. */ | ||
inputs: arrayShuffle(convertedInputs, { randomInt: getRandomInt }), | ||
outputs: sortedOutputs, | ||
outputsPermutation: permutation, | ||
}; | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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