forked from karagozemin/OverSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.ts
More file actions
148 lines (129 loc) · 4.1 KB
/
Copy pathsim.ts
File metadata and controls
148 lines (129 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { keccak256, sha256 } from "viem";
export type Hex = `0x${string}`;
export type OrderStatus = "Funded" | "Claimed" | "Refunded";
export interface CreateOrderInput {
hashlock: Hex;
timelockSeconds: number;
}
export interface OrderView {
id: bigint;
hashlock: Hex;
timelockAbsolute: number;
status: OrderStatus;
createdAt: number;
finalisedAt: number;
}
export type SimErrorCode =
| "InvalidHashlock"
| "InvalidTimelock"
| "OrderNotFound"
| "OrderNotClaimable"
| "OrderNotRefundable"
| "InvalidPreimage"
| "Expired"
| "NotExpired";
export class SimError extends Error {
constructor(public readonly code: SimErrorCode) {
super(code);
this.name = "SimError";
}
}
export interface HtlcSim {
readonly name: "evm" | "soroban";
createOrder(input: CreateOrderInput): bigint;
claimOrder(id: bigint, preimage: Hex): void;
refundOrder(id: bigint): void;
getOrder(id: bigint): OrderView;
advanceTime(seconds: number): void;
}
// Mirrors the [MIN_TIMELOCK, MAX_TIMELOCK] bounds enforced by both
// HTLCEscrow.sol and the Soroban htlc contract.
const MIN_TIMELOCK = 300;
const MAX_TIMELOCK = 24 * 60 * 60;
abstract class BaseHtlcSim {
protected readonly orders = new Map<bigint, OrderView>();
protected nextId = 1n;
protected now: number;
constructor() {
this.now = Math.floor(Date.now() / 1000);
}
advanceTime(seconds: number): void {
this.now += seconds;
}
createOrder(input: CreateOrderInput): bigint {
if (!/^0x[0-9a-fA-F]{64}$/.test(input.hashlock) || /^0x0+$/.test(input.hashlock)) {
throw new SimError("InvalidHashlock");
}
if (input.timelockSeconds < MIN_TIMELOCK || input.timelockSeconds > MAX_TIMELOCK) {
throw new SimError("InvalidTimelock");
}
const id = this.nextId++;
this.orders.set(id, {
id,
hashlock: input.hashlock,
timelockAbsolute: this.now + input.timelockSeconds,
status: "Funded",
createdAt: this.now,
finalisedAt: 0
});
return id;
}
getOrder(id: bigint): OrderView {
const o = this.orders.get(id);
if (!o) throw new SimError("OrderNotFound");
return { ...o };
}
protected getMutable(id: bigint): OrderView {
const o = this.orders.get(id);
if (!o) throw new SimError("OrderNotFound");
return o;
}
refundOrder(id: bigint): void {
const o = this.getMutable(id);
if (o.status !== "Funded") throw new SimError("OrderNotRefundable");
if (this.now <= o.timelockAbsolute) throw new SimError("NotExpired");
o.status = "Refunded";
o.finalisedAt = this.now;
}
}
/**
* Faithful re-encoding of HTLCEscrow.sol's claim/refund branch logic.
* The contract accepts a preimage if EITHER sha256(preimage) OR
* keccak256(preimage) equals the stored hashlock, which is how a
* single hashlock can interop with Soroban (sha256-only) and classic
* keccak-flavoured EVM counterparties.
*/
export class EvmHtlcSim extends BaseHtlcSim implements HtlcSim {
readonly name = "evm" as const;
claimOrder(id: bigint, preimage: Hex): void {
const o = this.getMutable(id);
if (o.status !== "Funded") throw new SimError("OrderNotClaimable");
if (this.now > o.timelockAbsolute) throw new SimError("Expired");
const sha = sha256(preimage);
const kek = keccak256(preimage);
if (sha !== o.hashlock && kek !== o.hashlock) {
throw new SimError("InvalidPreimage");
}
o.status = "Claimed";
o.finalisedAt = this.now;
}
}
/**
* Faithful re-encoding of the Soroban oversync-htlc claim branch. The
* Soroban contract accepts a preimage only when sha256(preimage) equals
* the stored hashlock — keccak256 is not consulted.
*/
export class SorobanHtlcSim extends BaseHtlcSim implements HtlcSim {
readonly name = "soroban" as const;
claimOrder(id: bigint, preimage: Hex): void {
const o = this.getMutable(id);
if (o.status !== "Funded") throw new SimError("OrderNotClaimable");
if (this.now > o.timelockAbsolute) throw new SimError("Expired");
const sha = sha256(preimage);
if (sha !== o.hashlock) {
throw new SimError("InvalidPreimage");
}
o.status = "Claimed";
o.finalisedAt = this.now;
}
}