@@ -48,7 +48,7 @@ describe('SwapDetector', () => {
4848 script : Script . encode ( [ 'RETURN' ] ) ,
4949 } ) ;
5050
51- const output = detectSwap ( redeemScript , transaction ) ! ;
51+ const output = detectSwap ( redeemScript , transaction , type ) ! ;
5252
5353 expect ( output ) . not . toBeUndefined ( ) ;
5454 expect ( output . vout ) . toEqual ( 1 ) ;
@@ -77,7 +77,7 @@ describe('SwapDetector', () => {
7777 amount : 312n ,
7878 } ) ;
7979
80- const output = detectSwap ( tweakedKeys , transaction ) ! ;
80+ const output = detectSwap ( tweakedKeys , transaction , OutputType . Taproot ) ! ;
8181
8282 expect ( output ) . not . toBeUndefined ( ) ;
8383 expect ( output . vout ) . toEqual ( 1 ) ;
@@ -86,6 +86,75 @@ describe('SwapDetector', () => {
8686 expect ( output . script ) . toEqual ( p2trOutput ( tweakedKeys ) ) ;
8787 } ) ;
8888
89+ test ( 'should restrict detection to an expected OutputType' , ( ) => {
90+ const publicKey = secp256k1 . getPublicKey ( secp256k1 . utils . randomSecretKey ( ) ) ;
91+ const redeemScript = swapScript ( sha256 ( publicKey ) , publicKey , publicKey , 1 ) ;
92+
93+ // A decoy output paying to the same redeem script under a different wrapper
94+ const decoy = outputFunctionForType ( OutputType . Bech32 ) ! ( redeemScript ) ;
95+ const advertised = outputFunctionForType ( OutputType . Compatibility ) ! (
96+ redeemScript ,
97+ ) ;
98+
99+ const transaction = new Transaction ( { allowUnknownOutputs : true } ) ;
100+ transaction . addOutput ( { amount : 42n , script : decoy } ) ;
101+ transaction . addOutput ( { amount : 21n , script : advertised } ) ;
102+
103+ // The decoy wrapper at vout 0 is ignored; only the advertised one is detected
104+ const output = detectSwap (
105+ redeemScript ,
106+ transaction ,
107+ OutputType . Compatibility ,
108+ ) ! ;
109+
110+ expect ( output . vout ) . toEqual ( 1 ) ;
111+ expect ( output . amount ) . toEqual ( 21n ) ;
112+ expect ( output . type ) . toEqual ( OutputType . Compatibility ) ;
113+ expect ( output . script ) . toEqual ( advertised ) ;
114+ } ) ;
115+
116+ test ( 'should restrict detection to an expected output script' , ( ) => {
117+ const publicKey = secp256k1 . getPublicKey ( secp256k1 . utils . randomSecretKey ( ) ) ;
118+ const redeemScript = swapScript ( sha256 ( publicKey ) , publicKey , publicKey , 1 ) ;
119+
120+ const decoy = outputFunctionForType ( OutputType . Bech32 ) ! ( redeemScript ) ;
121+ const advertised = outputFunctionForType ( OutputType . Compatibility ) ! (
122+ redeemScript ,
123+ ) ;
124+
125+ const transaction = new Transaction ( { allowUnknownOutputs : true } ) ;
126+ transaction . addOutput ( { amount : 42n , script : decoy } ) ;
127+ transaction . addOutput ( { amount : 21n , script : advertised } ) ;
128+
129+ const output = detectSwap ( redeemScript , transaction , advertised ) ! ;
130+
131+ expect ( output . vout ) . toEqual ( 1 ) ;
132+ expect ( output . amount ) . toEqual ( 21n ) ;
133+ expect ( output . type ) . toEqual ( OutputType . Compatibility ) ;
134+ expect ( output . script ) . toEqual ( advertised ) ;
135+ } ) ;
136+
137+ test ( 'should return undefined when no output matches the expectation' , ( ) => {
138+ const publicKey = secp256k1 . getPublicKey ( secp256k1 . utils . randomSecretKey ( ) ) ;
139+ const redeemScript = swapScript ( sha256 ( publicKey ) , publicKey , publicKey , 1 ) ;
140+
141+ const transaction = new Transaction ( { allowUnknownOutputs : true } ) ;
142+ transaction . addOutput ( {
143+ amount : 42n ,
144+ script : outputFunctionForType ( OutputType . Bech32 ) ! ( redeemScript ) ,
145+ } ) ;
146+
147+ // The advertised wrapper is not present in the transaction
148+ expect (
149+ detectSwap ( redeemScript , transaction , OutputType . Taproot ) ,
150+ ) . toBeUndefined ( ) ;
151+
152+ // An expected script that is not a valid wrapper of the key is rejected
153+ expect (
154+ detectSwap ( redeemScript , transaction , randomBytes ( 34 ) ) ,
155+ ) . toBeUndefined ( ) ;
156+ } ) ;
157+
89158 test ( 'should return undefined no swap can be found' , ( ) => {
90159 const publicKey = secp256k1 . getPublicKey ( secp256k1 . utils . randomSecretKey ( ) ) ;
91160 const transaction = new Transaction ( {
@@ -100,7 +169,7 @@ describe('SwapDetector', () => {
100169 amount : 312n ,
101170 } ) ;
102171
103- const output = detectSwap ( randomBytes ( 32 ) , transaction ) ;
172+ const output = detectSwap ( randomBytes ( 32 ) , transaction , OutputType . Bech32 ) ;
104173
105174 expect ( output ) . toBeUndefined ( ) ;
106175 } ) ;
0 commit comments