Skip to content

Commit 23e1cfd

Browse files
committed
Updated tests
1 parent dbba134 commit 23e1cfd

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

contracts/contracts/Depositor.sol

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ contract Depositor {
7272
* @param secret The secret used to withdraw the deposit.
7373
* @param secretHash The Poseidon hash of the secret used to create the deposit.
7474
*/
75-
event Withdrawn(address indexed recipient, uint256 amount, bytes32[4] secret, bytes32 secretHash);
75+
event Withdrawn(
76+
address indexed recipient,
77+
uint256 amount,
78+
bytes32[4] secret,
79+
bytes32 secretHash
80+
);
7681

7782
/**
7883
* @notice Emitted when deposited funds are restored to the sender after the lock time has expired.

contracts/test/Depositor.test.ts

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { expect } from "chai";
22
import { ethers } from "hardhat";
33

4+
import { Poseidon } from "@iden3/js-crypto";
5+
46
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
57
import { increase } from "@nomicfoundation/hardhat-network-helpers/dist/src/helpers/time";
68

@@ -26,7 +28,7 @@ describe("Taprootized Atomic Swaps", () => {
2628

2729
const DepositorFactory = await ethers.getContractFactory("Depositor", {
2830
libraries: {
29-
PoseidonUnit1L: await (await getPoseidon(1)).getAddress(),
31+
PoseidonUnit4L: await (await getPoseidon(4)).getAddress(),
3032
},
3133
});
3234
depositor = await DepositorFactory.connect(DEPLOYER).deploy();
@@ -36,9 +38,20 @@ describe("Taprootized Atomic Swaps", () => {
3638

3739
afterEach(reverter.revert);
3840

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));
46+
47+
const inputs = [part1, part2, part3, part4].map((v) => BigInt(v));
48+
const secretHash = ethers.toBeHex(Poseidon.hash(inputs), 32);
49+
50+
return [inputs.map((v) => ethers.toBeHex(v, 32)), secretHash];
51+
}
52+
3953
it("should deposit ETH with correct details", async () => {
40-
const secret = ethers.hexlify(ethers.randomBytes(32));
41-
const secretHash = poseidonHash(secret);
54+
const [, secretHash] = generateSecret();
4255

4356
const nextBlockTimestamp = (await time.latest()) + 1;
4457
await time.setNextBlockTimestamp(nextBlockTimestamp);
@@ -67,8 +80,7 @@ describe("Taprootized Atomic Swaps", () => {
6780
});
6881

6982
it("should revert if trying to deposit with same secret hash", async () => {
70-
const secret = ethers.hexlify(ethers.randomBytes(32));
71-
const secretHash = poseidonHash(secret);
83+
const [secret, secretHash] = generateSecret();
7284

7385
await depositor.deposit(USER2.address, secretHash, LOCK_TIME, { value: DEPOSIT_AMOUNT });
7486

@@ -78,17 +90,15 @@ describe("Taprootized Atomic Swaps", () => {
7890
});
7991

8092
it("should reject deposit to zero address", async () => {
81-
const secret = ethers.hexlify(ethers.randomBytes(32));
82-
const secretHash = poseidonHash(secret);
93+
const [secret, secretHash] = generateSecret();
8394

8495
await expect(
8596
depositor.deposit(ethers.ZeroAddress, secretHash, LOCK_TIME, { value: DEPOSIT_AMOUNT })
8697
).to.be.revertedWithCustomError(depositor, "ZeroAddressNotAllowed");
8798
});
8899

89100
it("should reject deposit with insufficient amount", async () => {
90-
const secret = ethers.hexlify(ethers.randomBytes(32));
91-
const secretHash = poseidonHash(secret);
101+
const [secret, secretHash] = generateSecret();
92102

93103
await expect(depositor.deposit(USER2.address, secretHash, LOCK_TIME, { value: 0 })).to.be.revertedWithCustomError(
94104
depositor,
@@ -97,53 +107,51 @@ describe("Taprootized Atomic Swaps", () => {
97107
});
98108

99109
it("should reject withdrawal with incorrect secret", async () => {
100-
const secret = ethers.hexlify(ethers.randomBytes(32));
101-
const incorrectSecret = ethers.hexlify(ethers.randomBytes(32));
102-
const secretHash = poseidonHash(secret);
110+
const [, secretHash] = generateSecret();
111+
const [incorrectSecret, incorrectSecretHash] = generateSecret();
103112

104113
await depositor.deposit(USER2.address, secretHash, LOCK_TIME, { value: DEPOSIT_AMOUNT });
105114

106-
await expect(depositor.withdraw(incorrectSecret))
115+
await expect(depositor.withdraw([incorrectSecret[0], incorrectSecret[1], incorrectSecret[2], incorrectSecret[3]]))
107116
.to.be.revertedWithCustomError(depositor, "DepositDoesNotExist")
108-
.withArgs(poseidonHash(incorrectSecret));
117+
.withArgs(incorrectSecretHash);
109118
});
110119

111120
it("should allow withdrawal with correct secret", async () => {
112-
const secret = ethers.hexlify(ethers.randomBytes(32));
113-
const secretHash = poseidonHash(secret);
121+
const [secret, secretHash] = generateSecret();
114122

115123
await depositor.deposit(USER2.address, secretHash, LOCK_TIME, { value: DEPOSIT_AMOUNT });
116124

117-
await expect(depositor.withdraw(secret))
125+
await expect(depositor.withdraw([secret[0], secret[1], secret[2], secret[3]]))
118126
.to.emit(depositor, "Withdrawn")
119127
.withArgs(USER2.address, DEPOSIT_AMOUNT, secret, secretHash);
120128
});
121129

122130
it("should prevent double withdrawal with same secret", async () => {
123-
const secret = ethers.hexlify(ethers.randomBytes(32));
124-
const secretHash = poseidonHash(secret);
131+
const [secret, secretHash] = generateSecret();
125132

126133
await depositor.deposit(USER2.address, secretHash, LOCK_TIME, { value: DEPOSIT_AMOUNT });
127134

128-
await depositor.withdraw(secret);
135+
await depositor.withdraw([secret[0], secret[1], secret[2], secret[3]]);
129136

130-
await expect(depositor.withdraw(secret))
137+
await expect(depositor.withdraw([secret[0], secret[1], secret[2], secret[3]]))
131138
.to.be.revertedWithCustomError(depositor, "DepositAlreadyWithdrawn")
132139
.withArgs(secretHash);
133140
});
134141

135142
it("should reject withdrawal if the ETH transfer fails", async () => {
136-
const secret = ethers.hexlify(ethers.randomBytes(32));
137-
const secretHash = poseidonHash(secret);
143+
const [secret, secretHash] = generateSecret();
138144

139145
await depositor.deposit(await depositor.getAddress(), secretHash, LOCK_TIME, { value: DEPOSIT_AMOUNT });
140146

141-
await expect(depositor.withdraw(secret)).to.be.revertedWithCustomError(depositor, "FailedInnerCall");
147+
await expect(depositor.withdraw([secret[0], secret[1], secret[2], secret[3]])).to.be.revertedWithCustomError(
148+
depositor,
149+
"FailedInnerCall"
150+
);
142151
});
143152

144153
it("should reject restoring before lock time expires", async () => {
145-
const secret = ethers.hexlify(ethers.randomBytes(32));
146-
const secretHash = poseidonHash(secret);
154+
const [secret, secretHash] = generateSecret();
147155

148156
const nextBlockTimestamp = (await time.latest()) + 1;
149157
await time.setNextBlockTimestamp(nextBlockTimestamp);
@@ -164,8 +172,7 @@ describe("Taprootized Atomic Swaps", () => {
164172
});
165173

166174
it("should reject restoring if the ETH transfer fails", async () => {
167-
const secret = ethers.hexlify(ethers.randomBytes(32));
168-
const secretHash = poseidonHash(secret);
175+
const [secret, secretHash] = generateSecret();
169176

170177
await impersonateAccount(await depositor.getAddress());
171178
const depositorAsSigner = await ethers.getSigner(await depositor.getAddress());
@@ -179,12 +186,11 @@ describe("Taprootized Atomic Swaps", () => {
179186
});
180187

181188
it("should reject restoring if the deposit is already withdrawn", async () => {
182-
const secret = ethers.hexlify(ethers.randomBytes(32));
183-
const secretHash = poseidonHash(secret);
189+
const [secret, secretHash] = generateSecret();
184190

185191
await depositor.deposit(USER2.address, secretHash, LOCK_TIME, { value: DEPOSIT_AMOUNT });
186192

187-
await depositor.withdraw(secret);
193+
await depositor.withdraw([secret[0], secret[1], secret[2], secret[3]]);
188194

189195
await increase(LOCK_TIME);
190196

@@ -194,8 +200,7 @@ describe("Taprootized Atomic Swaps", () => {
194200
});
195201

196202
it("should allow restoring after lock time", async () => {
197-
const secret = ethers.hexlify(ethers.randomBytes(32));
198-
const secretHash = poseidonHash(secret);
203+
const [, secretHash] = generateSecret();
199204

200205
await depositor.connect(USER1).deposit(USER2.address, secretHash, LOCK_TIME, { value: DEPOSIT_AMOUNT });
201206

0 commit comments

Comments
 (0)