1- import { Descriptor } from '@bitgo/wasm-miniscript' ;
2-
3- import { DescriptorMap } from './DescriptorMap' ;
4- import { createScriptPubKeyFromDescriptor } from './address' ;
5-
6- export type Output < TValue = bigint > = {
7- script : Buffer ;
8- value : TValue ;
9- } ;
10-
11- export type MaxOutput = Output < 'max' > ;
12-
13- type ValueBigInt = { value : bigint } ;
14- type ValueMax = { value : 'max' } ;
15-
16- /**
17- * @return true if the output is a max output
18- */
19- export function isMaxOutput < A extends ValueBigInt , B extends ValueMax > ( output : A | B ) : output is B {
20- return output . value === 'max' ;
21- }
22-
23- /**
24- * @return the max output if there is one
25- * @throws if there are multiple max outputs
26- */
27- export function getMaxOutput < A extends ValueBigInt , B extends ValueMax > ( outputs : ( A | B ) [ ] ) : B | undefined {
28- const max = outputs . filter ( isMaxOutput < A , B > ) ;
29- if ( max . length === 0 ) {
30- return undefined ;
31- }
32- if ( max . length > 1 ) {
33- throw new Error ( 'Multiple max outputs' ) ;
34- }
35- return max [ 0 ] ;
36- }
1+ import assert from 'assert' ;
372
38- /**
39- * @return the sum of the outputs
40- */
41- export function getOutputSum ( outputs : ValueBigInt [ ] ) : bigint {
42- return outputs . reduce ( ( sum , output ) => sum + output . value , 0n ) ;
43- }
3+ import { Descriptor } from '@bitgo/wasm-miniscript' ;
444
45- /**
46- * @return the sum of the outputs that are not 'max'
47- */
48- export function getFixedOutputSum ( outputs : ( ValueBigInt | ValueMax ) [ ] ) : bigint {
49- return getOutputSum ( outputs . filter ( ( o ) : o is Output => ! isMaxOutput ( o ) ) ) ;
50- }
5+ import { getFixedOutputSum , MaxOutput , Output , PrevOutput } from '../Output' ;
516
52- /**
53- * @param outputs
54- * @param params
55- * @return the outputs with the 'max' output replaced with the max amount
56- */
57- export function toFixedOutputs < A extends ValueBigInt , B extends ValueMax > (
58- outputs : ( A | B ) [ ] ,
59- params : { maxAmount : bigint }
60- ) : A [ ] {
61- // assert that there is at most one max output
62- const maxOutput = getMaxOutput < A , B > ( outputs ) ;
63- return outputs . map ( ( output ) : A => {
64- if ( isMaxOutput ( output ) ) {
65- if ( output !== maxOutput ) {
66- throw new Error ( 'illegal state' ) ;
67- }
68- return { ...output , value : params . maxAmount } ;
69- } else {
70- return output ;
71- }
72- } ) ;
73- }
7+ import { DescriptorMap } from './DescriptorMap' ;
8+ import { getDescriptorAtIndexCheckScript } from './derive' ;
749
7510export type WithDescriptor < T > = T & {
7611 descriptor : Descriptor ;
@@ -96,15 +31,9 @@ export function getExternalFixedAmount(outputs: WithOptDescriptor<Output | MaxOu
9631 return getFixedOutputSum ( outputs . filter ( isExternalOutput ) ) ;
9732}
9833
99- export type PrevOutput = {
100- hash : string ;
101- index : number ;
102- witnessUtxo : Output ;
103- } ;
104-
10534export type DescriptorWalletOutput = PrevOutput & {
10635 descriptorName : string ;
107- descriptorIndex : number ;
36+ descriptorIndex : number | undefined ;
10837} ;
10938
11039export type DerivedDescriptorWalletOutput = WithDescriptor < PrevOutput > ;
@@ -117,17 +46,17 @@ export function toDerivedDescriptorWalletOutput(
11746 if ( ! descriptor ) {
11847 throw new Error ( `Descriptor not found: ${ output . descriptorName } ` ) ;
11948 }
120- const derivedDescriptor = descriptor . atDerivationIndex ( output . descriptorIndex ) ;
121- const script = createScriptPubKeyFromDescriptor ( derivedDescriptor ) ;
122- if ( ! script . equals ( output . witnessUtxo . script ) ) {
123- throw new Error (
124- `Script mismatch: descriptor ${ output . descriptorName } ${ descriptor . toString ( ) } script= ${ script . toString ( 'hex' ) } `
125- ) ;
126- }
49+ assert ( descriptor instanceof Descriptor ) ;
50+ const descriptorAtIndex = getDescriptorAtIndexCheckScript (
51+ descriptor ,
52+ output . descriptorIndex ,
53+ output . witnessUtxo . script ,
54+ output . descriptorName
55+ ) ;
12756 return {
12857 hash : output . hash ,
12958 index : output . index ,
13059 witnessUtxo : output . witnessUtxo ,
131- descriptor : descriptor . atDerivationIndex ( output . descriptorIndex ) ,
60+ descriptor : descriptorAtIndex ,
13261 } ;
13362}
0 commit comments