forked from trezor/trezor-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce sortingStrategy into utxo-lib so we can have more str…
…ategies (bip69, none, and later the random) feat: refactor utxo-lib so it impements multiple sorting strategies (one strategy per file)
- Loading branch information
1 parent
49f7737
commit 3dace57
Showing
7 changed files
with
272 additions
and
66 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
packages/utxo-lib/src/compose/sorting/bip69SortingStrategy.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 { CoinSelectOutputFinal, ComposeInput } from '../../types'; | ||
import { convertOutput } from './convertOutput'; | ||
|
||
function inputComparator(a: ComposeInput, b: ComposeInput) { | ||
return Buffer.from(a.txid, 'hex').compare(Buffer.from(b.txid, 'hex')) || a.vout - b.vout; | ||
} | ||
|
||
function outputComparator(a: CoinSelectOutputFinal, b: CoinSelectOutputFinal) { | ||
return ( | ||
a.value.cmp(b.value) || | ||
(Buffer.isBuffer(a.script) && Buffer.isBuffer(b.script) | ||
? a.script.compare(b.script) | ||
: a.script.length - b.script.length) | ||
); | ||
} | ||
|
||
export const bip69SortingStrategy: SortingStrategy = ({ result, request, convertedInputs }) => { | ||
const defaultPermutation: number[] = []; | ||
const convertedOutputs = result.outputs.map((output, index) => { | ||
defaultPermutation.push(index); | ||
if (request.outputs[index]) { | ||
return convertOutput(output, request.outputs[index]); | ||
} | ||
|
||
return convertOutput(output, { type: 'change', ...request.changeAddress }); | ||
}); | ||
|
||
const permutation = defaultPermutation.sort((a, b) => | ||
outputComparator(result.outputs[a], result.outputs[b]), | ||
); | ||
const sortedOutputs = permutation.map(index => convertedOutputs[index]); | ||
|
||
return { | ||
inputs: convertedInputs.sort(inputComparator), | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { CoinSelectOutputFinal, ComposeChangeAddress, ComposeFinalOutput } from '../../types'; | ||
|
||
export const convertOutput = ( | ||
selectedOutput: CoinSelectOutputFinal, | ||
composeOutput: ComposeFinalOutput | ({ type: 'change' } & ComposeChangeAddress), | ||
) => { | ||
if (composeOutput.type === 'change') { | ||
return { | ||
...composeOutput, | ||
amount: selectedOutput.value.toString(), | ||
}; | ||
} | ||
|
||
if (composeOutput.type === 'opreturn') { | ||
return composeOutput; | ||
} | ||
|
||
return { | ||
...composeOutput, | ||
type: 'payment' as const, | ||
amount: selectedOutput.value.toString(), | ||
}; | ||
}; |
18 changes: 18 additions & 0 deletions
18
packages/utxo-lib/src/compose/sorting/noneSortingStrategy.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,18 @@ | ||
import { SortingStrategy } from './sortingStrategy'; | ||
import { convertOutput } from './convertOutput'; | ||
|
||
export const noneSortingStrategy: SortingStrategy = ({ result, request, convertedInputs }) => { | ||
const convertedOutputs = result.outputs.map((output, index) => { | ||
if (request.outputs[index]) { | ||
return convertOutput(output, request.outputs[index]); | ||
} | ||
|
||
return convertOutput(output, { type: 'change', ...request.changeAddress }); | ||
}); | ||
|
||
return { | ||
inputs: convertedInputs, | ||
outputs: convertedOutputs, | ||
outputsPermutation: Array.from(convertedOutputs.keys()), | ||
}; | ||
}; |
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,18 @@ | ||
import { | ||
CoinSelectSuccess, | ||
ComposeChangeAddress, | ||
ComposedTransaction, | ||
ComposeFinalOutput, | ||
ComposeInput, | ||
ComposeRequest, | ||
} from '../../types'; | ||
|
||
type SortingStrategyParams<Input extends ComposeInput, Change extends ComposeChangeAddress> = { | ||
request: ComposeRequest<Input, ComposeFinalOutput, Change>; | ||
result: CoinSelectSuccess; | ||
convertedInputs: Input[]; | ||
}; | ||
|
||
export type SortingStrategy = <Input extends ComposeInput, Change extends ComposeChangeAddress>( | ||
params: SortingStrategyParams<Input, Change>, | ||
) => ComposedTransaction<Input, ComposeFinalOutput, Change>; |
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