@@ -8,7 +8,7 @@ import { increase } from "@nomicfoundation/hardhat-network-helpers/dist/src/help
88
99import { Depositor } from "@ethers-v6" ;
1010
11- import { Reverter , poseidonHash , getPoseidon } from "@utils" ;
11+ import { Reverter , getPoseidon } from "@utils" ;
1212import { impersonateAccount , setBalance , time } from "@nomicfoundation/hardhat-network-helpers" ;
1313
1414describe ( "Taprootized Atomic Swaps" , ( ) => {
@@ -38,16 +38,19 @@ describe("Taprootized Atomic Swaps", () => {
3838
3939 afterEach ( reverter . revert ) ;
4040
41- function generateSecret ( ) : [ string [ ] , string ] {
42- const part1 = ethers . hexlify ( ethers . randomBytes ( 8 ) ) ;
43- const part2 = ethers . hexlify ( ethers . randomBytes ( 8 ) ) ;
44- const part3 = ethers . hexlify ( ethers . randomBytes ( 8 ) ) ;
45- const part4 = ethers . hexlify ( ethers . randomBytes ( 8 ) ) ;
41+ function generateSecret ( ) : [ string , string ] {
42+ const parts = [ 1 , 2 , 3 , 4 ] . map ( ( ) => ethers . hexlify ( ethers . randomBytes ( 8 ) ) ) ;
43+ const wholeSecret =
44+ "0x" +
45+ parts
46+ . reverse ( )
47+ . map ( ( hexString : string ) => hexString . replace ( "0x" , "" ) )
48+ . join ( "" ) ;
4649
47- const inputs = [ part1 , part2 , part3 , part4 ] . map ( ( v ) => BigInt ( v ) ) ;
50+ const inputs = parts . reverse ( ) . map ( ( v ) => BigInt ( v ) ) ;
4851 const secretHash = ethers . toBeHex ( Poseidon . hash ( inputs ) , 32 ) ;
4952
50- return [ inputs . map ( ( v ) => ethers . toBeHex ( v , 32 ) ) , secretHash ] ;
53+ return [ wholeSecret , secretHash ] ;
5154 }
5255
5356 it ( "should deposit ETH with correct details" , async ( ) => {
@@ -80,7 +83,7 @@ describe("Taprootized Atomic Swaps", () => {
8083 } ) ;
8184
8285 it ( "should revert if trying to deposit with same secret hash" , async ( ) => {
83- const [ secret , secretHash ] = generateSecret ( ) ;
86+ const [ , secretHash ] = generateSecret ( ) ;
8487
8588 await depositor . deposit ( USER2 . address , secretHash , LOCK_TIME , { value : DEPOSIT_AMOUNT } ) ;
8689
@@ -90,15 +93,15 @@ describe("Taprootized Atomic Swaps", () => {
9093 } ) ;
9194
9295 it ( "should reject deposit to zero address" , async ( ) => {
93- const [ secret , secretHash ] = generateSecret ( ) ;
96+ const [ , secretHash ] = generateSecret ( ) ;
9497
9598 await expect (
9699 depositor . deposit ( ethers . ZeroAddress , secretHash , LOCK_TIME , { value : DEPOSIT_AMOUNT } )
97100 ) . to . be . revertedWithCustomError ( depositor , "ZeroAddressNotAllowed" ) ;
98101 } ) ;
99102
100103 it ( "should reject deposit with insufficient amount" , async ( ) => {
101- const [ secret , secretHash ] = generateSecret ( ) ;
104+ const [ , secretHash ] = generateSecret ( ) ;
102105
103106 await expect ( depositor . deposit ( USER2 . address , secretHash , LOCK_TIME , { value : 0 } ) ) . to . be . revertedWithCustomError (
104107 depositor ,
@@ -112,7 +115,7 @@ describe("Taprootized Atomic Swaps", () => {
112115
113116 await depositor . deposit ( USER2 . address , secretHash , LOCK_TIME , { value : DEPOSIT_AMOUNT } ) ;
114117
115- await expect ( depositor . withdraw ( [ incorrectSecret [ 0 ] , incorrectSecret [ 1 ] , incorrectSecret [ 2 ] , incorrectSecret [ 3 ] ] ) )
118+ await expect ( depositor . withdraw ( incorrectSecret ) )
116119 . to . be . revertedWithCustomError ( depositor , "DepositDoesNotExist" )
117120 . withArgs ( incorrectSecretHash ) ;
118121 } ) ;
@@ -122,7 +125,7 @@ describe("Taprootized Atomic Swaps", () => {
122125
123126 await depositor . deposit ( USER2 . address , secretHash , LOCK_TIME , { value : DEPOSIT_AMOUNT } ) ;
124127
125- await expect ( depositor . withdraw ( [ secret [ 0 ] , secret [ 1 ] , secret [ 2 ] , secret [ 3 ] ] ) )
128+ await expect ( depositor . withdraw ( secret ) )
126129 . to . emit ( depositor , "Withdrawn" )
127130 . withArgs ( USER2 . address , DEPOSIT_AMOUNT , secret , secretHash ) ;
128131 } ) ;
@@ -132,9 +135,9 @@ describe("Taprootized Atomic Swaps", () => {
132135
133136 await depositor . deposit ( USER2 . address , secretHash , LOCK_TIME , { value : DEPOSIT_AMOUNT } ) ;
134137
135- await depositor . withdraw ( [ secret [ 0 ] , secret [ 1 ] , secret [ 2 ] , secret [ 3 ] ] ) ;
138+ await depositor . withdraw ( secret ) ;
136139
137- await expect ( depositor . withdraw ( [ secret [ 0 ] , secret [ 1 ] , secret [ 2 ] , secret [ 3 ] ] ) )
140+ await expect ( depositor . withdraw ( secret ) )
138141 . to . be . revertedWithCustomError ( depositor , "DepositAlreadyWithdrawn" )
139142 . withArgs ( secretHash ) ;
140143 } ) ;
@@ -144,14 +147,11 @@ describe("Taprootized Atomic Swaps", () => {
144147
145148 await depositor . deposit ( await depositor . getAddress ( ) , secretHash , LOCK_TIME , { value : DEPOSIT_AMOUNT } ) ;
146149
147- await expect ( depositor . withdraw ( [ secret [ 0 ] , secret [ 1 ] , secret [ 2 ] , secret [ 3 ] ] ) ) . to . be . revertedWithCustomError (
148- depositor ,
149- "FailedInnerCall"
150- ) ;
150+ await expect ( depositor . withdraw ( secret ) ) . to . be . revertedWithCustomError ( depositor , "FailedInnerCall" ) ;
151151 } ) ;
152152
153153 it ( "should reject restoring before lock time expires" , async ( ) => {
154- const [ secret , secretHash ] = generateSecret ( ) ;
154+ const [ , secretHash ] = generateSecret ( ) ;
155155
156156 const nextBlockTimestamp = ( await time . latest ( ) ) + 1 ;
157157 await time . setNextBlockTimestamp ( nextBlockTimestamp ) ;
@@ -172,7 +172,7 @@ describe("Taprootized Atomic Swaps", () => {
172172 } ) ;
173173
174174 it ( "should reject restoring if the ETH transfer fails" , async ( ) => {
175- const [ secret , secretHash ] = generateSecret ( ) ;
175+ const [ , secretHash ] = generateSecret ( ) ;
176176
177177 await impersonateAccount ( await depositor . getAddress ( ) ) ;
178178 const depositorAsSigner = await ethers . getSigner ( await depositor . getAddress ( ) ) ;
@@ -190,7 +190,7 @@ describe("Taprootized Atomic Swaps", () => {
190190
191191 await depositor . deposit ( USER2 . address , secretHash , LOCK_TIME , { value : DEPOSIT_AMOUNT } ) ;
192192
193- await depositor . withdraw ( [ secret [ 0 ] , secret [ 1 ] , secret [ 2 ] , secret [ 3 ] ] ) ;
193+ await depositor . withdraw ( secret ) ;
194194
195195 await increase ( LOCK_TIME ) ;
196196
0 commit comments