-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathoutputDifference.ts
More file actions
99 lines (91 loc) · 3.51 KB
/
outputDifference.ts
File metadata and controls
99 lines (91 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
export type ComparableOutput<TValue> = {
script: Buffer;
value: TValue;
};
/** Actual outputs have fixed values. */
export type ActualOutput = ComparableOutput<bigint>;
/** Expected outputs can have a fixed value or 'max'. */
export type ExpectedOutput = ComparableOutput<bigint | 'max'> & {
/** When true, the output is not required to be present in the transaction. */
optional?: boolean;
};
/**
* @param a
* @param b
* @returns whether the two outputs are equal. Outputs with value `max` are considered equal to any other output with the same script.
*/
export function matchingOutput<TValue>(a: ComparableOutput<TValue>, b: ComparableOutput<TValue>): boolean {
if (a.value === 'max' || b.value === 'max') {
return a.script.equals(b.script);
}
return a.script.equals(b.script) && a.value === b.value;
}
/**
* @returns all outputs in the first array that are not in the second array.
* Outputs can occur more than once in each array.
* An output with value `max` is considered equal to any other output with the same script.
*/
export function outputDifference<A extends ActualOutput | ExpectedOutput, B extends ActualOutput | ExpectedOutput>(
first: A[],
second: B[]
): A[] {
first = first.slice();
for (const output of second) {
const index = first.findIndex((o) => matchingOutput(o, output));
if (index !== -1) {
first.splice(index, 1);
}
}
return first;
}
export function getMissingOutputs<A extends ActualOutput, B extends ExpectedOutput>(
actualOutputs: A[],
expectedOutputs: B[]
): B[] {
return outputDifference(expectedOutputs, actualOutputs).filter((o) => !o.optional);
}
/**
* Returns true if the given output was not explicitly requested by the user (i.e., it is not in
* the list of expected outputs). This handles both external and internal (change/self-payment)
* outputs — an output is implicit regardless of whether it is going to a wallet address or not.
*
* Used to identify surprise outputs such as PayGo fees that were added by the server without
* being part of the original send intent.
*/
export function isImplicitOutput<A extends ActualOutput>(
output: A,
expectedOutputs: ExpectedOutput[]
): boolean {
return !expectedOutputs.some((expected) => matchingOutput(output, expected));
}
export type OutputDifferenceWithExpected<TActual extends ActualOutput, TExpected extends ExpectedOutput> = {
/** These are the external outputs that were expected and found in the transaction. */
explicitOutputs: TActual[];
/**
* These are the surprise external outputs that were not explicitly specified in the transaction.
* They can be PayGo fees.
*/
implicitOutputs: TActual[];
/**
* These are the outputs that were expected to be in the transaction but were not found.
*/
missingOutputs: TExpected[];
};
/**
* @param actualOutputs - external outputs in the transaction
* @param expectedOutputs - external outputs that were expected to be in the transaction
* @returns the difference between the actual and expected external outputs
*/
export function outputDifferencesWithExpected<TActual extends ActualOutput, TExpected extends ExpectedOutput>(
actualOutputs: TActual[],
expectedOutputs: TExpected[]
): OutputDifferenceWithExpected<TActual, TExpected> {
const implicitOutputs = outputDifference(actualOutputs, expectedOutputs);
const explicitOutputs = outputDifference(actualOutputs, implicitOutputs);
const missingOutputs = getMissingOutputs(actualOutputs, expectedOutputs);
return {
explicitOutputs,
implicitOutputs,
missingOutputs,
};
}