Skip to content

Commit 32b063a

Browse files
Refactor withdraw function
Refactored 'withdraw' function
2 parents e38b82c + a1eb99a commit 32b063a

File tree

6 files changed

+55
-97
lines changed

6 files changed

+55
-97
lines changed

build.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ const DEPOSITOR_RUST_BINDINGS_PATH: &str = "src/depositor_contract.rs";
1010

1111
fn main() {
1212
let cargo_manifest_path = env::var("CARGO_MANIFEST_DIR").unwrap();
13-
println!(
14-
"cargo:rerun-if-changed={}/contracts/contracts/Depositor.sol",
15-
cargo_manifest_path,
16-
);
1713

1814
compile_depositor_bindings(cargo_manifest_path)
1915
}
2016

2117
fn compile_depositor_bindings(cargo_manifest_path: String) {
18+
let depositor_bindings_path =
19+
format!("{}/{}", cargo_manifest_path, DEPOSITOR_RUST_BINDINGS_PATH);
20+
21+
if Path::new(&depositor_bindings_path).exists() {
22+
return;
23+
}
24+
2225
let abi_src_path = format!("{}/{}", cargo_manifest_path, DEPOSITOR_ABI_ARTIFACTS_PATH);
2326

2427
// Check if JSON ABI file of the Depositor contract is exists.
2528
if !Path::new(&abi_src_path).exists() {
2629
return;
2730
}
2831

29-
let depositor_bindings_path =
30-
format!("{}/{}", cargo_manifest_path, DEPOSITOR_RUST_BINDINGS_PATH);
31-
3232
Abigen::new("Depositor", abi_src_path)
3333
.unwrap()
3434
.generate()

config.example.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
atomic_swap_contract_address = "0xC44f8853B1288F58B7CA8555208F502C36f87361"
1+
atomic_swap_contract_address = "0x85BEaB7f80B375175BeCC3f68Bf86d33099fD576"
22
# You can use `ganache` to run a local Ethereum node
33
ethereum_ws_rpc_url = "wss://127.0.0.1:8545"
44

55
[swap_params]
6-
sats_to_swap = 125000
6+
sats_to_swap = 2000
77
gwei_to_swap = 2500000
88
bitcoin_csv_delay = 8 # 80 minutes MUST be greater than ethereum_timelock_secs
99
ethereum_timelock_secs = 3600 # 60 minutes MUST be less than bitcoin_csv_delay

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([part4, part3, part2, part1]));
212+
}
209213
}

contracts/test/Depositor.test.ts

Lines changed: 22 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,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

Comments
 (0)