@@ -10,14 +10,10 @@ import {
1010} from '@bitgo/utxo-core/testutil/descriptor' ;
1111import { toPlainObject } from '@bitgo/utxo-core/testutil' ;
1212import { createAddressFromDescriptor } from '@bitgo/utxo-core/descriptor' ;
13+ import { TxIntentMismatchRecipientError } from '@bitgo/sdk-core' ;
1314
1415import { ParsedOutputsBigInt , toBaseParsedTransactionOutputsFromPsbt } from '../../../src/transaction/descriptor/parse' ;
15- import {
16- AggregateValidationError ,
17- assertExpectedOutputDifference ,
18- ErrorImplicitExternalOutputs ,
19- ErrorMissingOutputs ,
20- } from '../../../src/transaction/descriptor/verifyTransaction' ;
16+ import { assertExpectedOutputDifference } from '../../../src/transaction/descriptor/verifyTransaction' ;
2117import { toAmountType } from '../../../src/transaction/descriptor/parseToAmountType' ;
2218import { BaseOutput } from '../../../src' ;
2319
@@ -38,10 +34,6 @@ function toBaseOutput<TNumber>(output: OutputWithValue, amountType: 'bigint' | '
3834 } ;
3935}
4036
41- function toBaseOutputBigInt ( output : OutputWithValue ) : BaseOutput < bigint > {
42- return toBaseOutput ( output , 'bigint' ) ;
43- }
44-
4537function toBaseOutputString ( output : OutputWithValue ) : BaseOutput < string > {
4638 return toBaseOutput ( output , 'string' ) ;
4739}
@@ -97,66 +89,69 @@ describe('parse', function () {
9789 ) ;
9890 } ) ;
9991
100- function assertEqualValidationError ( actual : unknown , expected : AggregateValidationError ) {
101- function normErrors ( e : Error [ ] ) : Error [ ] {
102- return e . map ( ( e ) => ( { ... e , stack : undefined } ) ) ;
103- }
104- if ( actual instanceof AggregateValidationError ) {
105- assert . deepStrictEqual ( normErrors ( actual . errors ) , normErrors ( expected . errors ) ) ;
106- } else {
107- throw new Error ( 'unexpected error type: ' + actual ) ;
108- }
109- }
110-
111- function assertValidationError ( f : ( ) => void , expected : AggregateValidationError ) {
112- assert . throws ( f , ( err ) => {
113- assertEqualValidationError ( err , expected ) ;
92+ function assertTxIntentMismatchError (
93+ f : ( ) => void ,
94+ expectedMessage : string ,
95+ expectedMismatchedRecipients : { address ?: string ; amount : string } [ ]
96+ ) {
97+ assert . throws ( f , ( err : unknown ) => {
98+ if ( ! ( err instanceof TxIntentMismatchRecipientError ) ) {
99+ const error = err as Error ;
100+ throw new Error (
101+ `Expected TxIntentMismatchRecipientError but got ${ error . constructor . name } : ${ error . message } `
102+ ) ;
103+ }
104+ assert . strictEqual ( err . message , expectedMessage ) ;
105+ assert . deepStrictEqual ( err . mismatchedRecipients , expectedMismatchedRecipients ) ;
114106 return true ;
115107 } ) ;
116108 }
117109
118- function implicitOutputError ( output : OutputWithValue , { external = true } = { } ) : ErrorImplicitExternalOutputs {
119- return new ErrorImplicitExternalOutputs ( [ { ...toBaseOutputBigInt ( output ) , external } ] ) ;
120- }
121-
122- function missingOutputError ( output : OutputWithValue , { external = true } = { } ) : ErrorMissingOutputs {
123- return new ErrorMissingOutputs ( [ { ...toBaseOutputBigInt ( output ) , external } ] ) ;
124- }
125-
126110 it ( 'should throw expected error: no recipient requested' , function ( ) {
127- assertValidationError (
111+ assertTxIntentMismatchError (
128112 ( ) => assertExpectedOutputDifference ( getBaseParsedTransaction ( psbt , [ ] ) ) ,
129- new AggregateValidationError ( [ implicitOutputError ( psbt . txOutputs [ 0 ] ) ] )
113+ 'unexpected implicit external outputs (count=1)' ,
114+ [ { address : psbt . txOutputs [ 0 ] . address , amount : psbt . txOutputs [ 0 ] . value . toString ( ) } ]
130115 ) ;
131116 } ) ;
132117
133118 it ( 'should throw expected error: only internal recipient requested' , function ( ) {
134- assertValidationError (
119+ assertTxIntentMismatchError (
135120 ( ) => assertExpectedOutputDifference ( getBaseParsedTransaction ( psbt , [ psbt . txOutputs [ 1 ] ] ) ) ,
136- new AggregateValidationError ( [ implicitOutputError ( psbt . txOutputs [ 0 ] ) ] )
121+ 'unexpected implicit external outputs (count=1)' ,
122+ [ { address : psbt . txOutputs [ 0 ] . address , amount : psbt . txOutputs [ 0 ] . value . toString ( ) } ]
137123 ) ;
138124 } ) ;
139125
140126 it ( 'should throw expected error: only internal max recipient requested' , function ( ) {
141- assertValidationError (
127+ assertTxIntentMismatchError (
142128 ( ) => assertExpectedOutputDifference ( getBaseParsedTransaction ( psbt , [ toMaxOutput ( psbt . txOutputs [ 1 ] ) ] ) ) ,
143- new AggregateValidationError ( [ implicitOutputError ( psbt . txOutputs [ 0 ] ) ] )
129+ 'unexpected implicit external outputs (count=1)' ,
130+ [ { address : psbt . txOutputs [ 0 ] . address , amount : psbt . txOutputs [ 0 ] . value . toString ( ) } ]
144131 ) ;
145132 } ) ;
146133
147134 it ( 'should throw expected error: swapped recipient' , function ( ) {
148135 const recipient = externalRecipient ( 99 ) ;
149- assertValidationError (
136+ assertTxIntentMismatchError (
150137 ( ) => assertExpectedOutputDifference ( getBaseParsedTransaction ( psbt , [ recipient ] ) ) ,
151- new AggregateValidationError ( [ missingOutputError ( recipient ) , implicitOutputError ( psbt . txOutputs [ 0 ] ) ] )
138+ 'missing outputs (count=1), unexpected implicit external outputs (count=1)' ,
139+ [
140+ { address : recipient . address , amount : recipient . value . toString ( ) } ,
141+ { address : psbt . txOutputs [ 0 ] . address , amount : psbt . txOutputs [ 0 ] . value . toString ( ) } ,
142+ ]
152143 ) ;
153144 } ) ;
154145
155146 it ( 'should throw expected error: missing internal recipient' , function ( ) {
156147 const recipient = internalRecipient ( 99 ) ;
157- assertValidationError (
148+ assertTxIntentMismatchError (
158149 ( ) => assertExpectedOutputDifference ( getBaseParsedTransaction ( psbt , [ recipient ] ) ) ,
159- new AggregateValidationError ( [ missingOutputError ( recipient ) , implicitOutputError ( psbt . txOutputs [ 0 ] ) ] )
150+ 'missing outputs (count=1), unexpected implicit external outputs (count=1)' ,
151+ [
152+ { address : recipient . address , amount : recipient . value . toString ( ) } ,
153+ { address : psbt . txOutputs [ 0 ] . address , amount : psbt . txOutputs [ 0 ] . value . toString ( ) } ,
154+ ]
160155 ) ;
161156 } ) ;
162157 } ) ;
0 commit comments