Skip to content

Commit c27ffed

Browse files
committed
Refactored 'withdraw' function: replaced array argument with a single uint256
1 parent 23e1cfd commit c27ffed

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

contracts/contracts/Depositor.sol

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pragma solidity 0.8.23;
44
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
55

66
library PoseidonUnit4L {
7-
function poseidon(bytes32[4] calldata) public pure returns (bytes32) {}
7+
function poseidon(uint256[4] calldata) public pure returns (uint256) {}
88
}
99

1010
/**
@@ -72,12 +72,7 @@ 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(
76-
address indexed recipient,
77-
uint256 amount,
78-
bytes32[4] secret,
79-
bytes32 secretHash
80-
);
75+
event Withdrawn(address indexed recipient, uint256 amount, uint256 secret, bytes32 secretHash);
8176

8277
/**
8378
* @notice Emitted when deposited funds are restored to the sender after the lock time has expired.
@@ -160,8 +155,8 @@ contract Depositor {
160155
* Uses the PoseidonUnit1L library to hash the provided secret.
161156
* @param secret_ The prototype of the `secretHash` used in the deposit function.
162157
*/
163-
function withdraw(bytes32[4] calldata secret_) external {
164-
bytes32 secretHash_ = PoseidonUnit4L.poseidon(secret_);
158+
function withdraw(uint256 secret_) external {
159+
bytes32 secretHash_ = _getSecretHash(secret_);
165160

166161
Deposit storage userDeposit = deposits[secretHash_];
167162

@@ -206,4 +201,13 @@ contract Depositor {
206201

207202
emit Restored(userDeposit.sender, depositAmount_, secretHash_);
208203
}
204+
205+
function _getSecretHash(uint256 secret_) private pure returns (bytes32) {
206+
uint256 part1 = secret_ >> 192;
207+
uint256 part2 = ((secret_ >> 128) & 0xFFFFFFFFFFFFFFFF);
208+
uint256 part3 = ((secret_ >> 64) & 0xFFFFFFFFFFFFFFFF);
209+
uint256 part4 = (secret_ & 0xFFFFFFFFFFFFFFFF);
210+
211+
return bytes32(PoseidonUnit4L.poseidon([part1, part2, part3, part4]));
212+
}
209213
}

contracts/test/Depositor.test.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { increase } from "@nomicfoundation/hardhat-network-helpers/dist/src/help
88

99
import { Depositor } from "@ethers-v6";
1010

11-
import { Reverter, poseidonHash, getPoseidon } from "@utils";
11+
import { Reverter, getPoseidon } from "@utils";
1212
import { impersonateAccount, setBalance, time } from "@nomicfoundation/hardhat-network-helpers";
1313

1414
describe("Taprootized Atomic Swaps", () => {
@@ -38,16 +38,14 @@ 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 = "0x" + parts.map((hexString: string) => hexString.replace("0x", "")).join("");
4644

47-
const inputs = [part1, part2, part3, part4].map((v) => BigInt(v));
45+
const inputs = parts.map((v) => BigInt(v));
4846
const secretHash = ethers.toBeHex(Poseidon.hash(inputs), 32);
4947

50-
return [inputs.map((v) => ethers.toBeHex(v, 32)), secretHash];
48+
return [wholeSecret, secretHash];
5149
}
5250

5351
it("should deposit ETH with correct details", async () => {
@@ -80,7 +78,7 @@ describe("Taprootized Atomic Swaps", () => {
8078
});
8179

8280
it("should revert if trying to deposit with same secret hash", async () => {
83-
const [secret, secretHash] = generateSecret();
81+
const [, secretHash] = generateSecret();
8482

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

@@ -90,15 +88,15 @@ describe("Taprootized Atomic Swaps", () => {
9088
});
9189

9290
it("should reject deposit to zero address", async () => {
93-
const [secret, secretHash] = generateSecret();
91+
const [, secretHash] = generateSecret();
9492

9593
await expect(
9694
depositor.deposit(ethers.ZeroAddress, secretHash, LOCK_TIME, { value: DEPOSIT_AMOUNT })
9795
).to.be.revertedWithCustomError(depositor, "ZeroAddressNotAllowed");
9896
});
9997

10098
it("should reject deposit with insufficient amount", async () => {
101-
const [secret, secretHash] = generateSecret();
99+
const [, secretHash] = generateSecret();
102100

103101
await expect(depositor.deposit(USER2.address, secretHash, LOCK_TIME, { value: 0 })).to.be.revertedWithCustomError(
104102
depositor,
@@ -112,7 +110,7 @@ describe("Taprootized Atomic Swaps", () => {
112110

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

115-
await expect(depositor.withdraw([incorrectSecret[0], incorrectSecret[1], incorrectSecret[2], incorrectSecret[3]]))
113+
await expect(depositor.withdraw(incorrectSecret))
116114
.to.be.revertedWithCustomError(depositor, "DepositDoesNotExist")
117115
.withArgs(incorrectSecretHash);
118116
});
@@ -122,7 +120,7 @@ describe("Taprootized Atomic Swaps", () => {
122120

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

125-
await expect(depositor.withdraw([secret[0], secret[1], secret[2], secret[3]]))
123+
await expect(depositor.withdraw(secret))
126124
.to.emit(depositor, "Withdrawn")
127125
.withArgs(USER2.address, DEPOSIT_AMOUNT, secret, secretHash);
128126
});
@@ -132,9 +130,9 @@ describe("Taprootized Atomic Swaps", () => {
132130

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

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

137-
await expect(depositor.withdraw([secret[0], secret[1], secret[2], secret[3]]))
135+
await expect(depositor.withdraw(secret))
138136
.to.be.revertedWithCustomError(depositor, "DepositAlreadyWithdrawn")
139137
.withArgs(secretHash);
140138
});
@@ -144,14 +142,11 @@ describe("Taprootized Atomic Swaps", () => {
144142

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

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

153148
it("should reject restoring before lock time expires", async () => {
154-
const [secret, secretHash] = generateSecret();
149+
const [, secretHash] = generateSecret();
155150

156151
const nextBlockTimestamp = (await time.latest()) + 1;
157152
await time.setNextBlockTimestamp(nextBlockTimestamp);
@@ -172,7 +167,7 @@ describe("Taprootized Atomic Swaps", () => {
172167
});
173168

174169
it("should reject restoring if the ETH transfer fails", async () => {
175-
const [secret, secretHash] = generateSecret();
170+
const [, secretHash] = generateSecret();
176171

177172
await impersonateAccount(await depositor.getAddress());
178173
const depositorAsSigner = await ethers.getSigner(await depositor.getAddress());
@@ -190,7 +185,7 @@ describe("Taprootized Atomic Swaps", () => {
190185

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

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

195190
await increase(LOCK_TIME);
196191

0 commit comments

Comments
 (0)