forked from programmablehq/PROGRAMMABLE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeep-v2-runtime-binding.ts
More file actions
282 lines (265 loc) · 7.15 KB
/
Copy pathdeep-v2-runtime-binding.ts
File metadata and controls
282 lines (265 loc) · 7.15 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import {
isAddress,
isHex,
keccak256,
parseAbi,
type Address,
type Hex,
} from "viem";
export const deepV2KeeperExecutorReadAbi = parseAbi([
"function automation() view returns (address)",
]);
export type DeepV2RuntimeBindingClient = {
getChainId(): Promise<number>;
getFinalizedBlock(): Promise<{ number: bigint; hash: Hex | null }>;
getBlock(input: {
blockNumber: bigint;
}): Promise<{ number: bigint; hash: Hex | null }>;
getCode(input: {
address: Address;
blockNumber: bigint;
}): Promise<Hex | undefined>;
readKeeperAutomation(input: {
address: Address;
blockNumber: bigint;
}): Promise<Address>;
readAutomationLauncher(input: {
address: Address;
blockNumber: bigint;
}): Promise<Address>;
readLauncherAutomation(input: {
address: Address;
blockNumber: bigint;
}): Promise<Address>;
};
export function requireIndependentDeepV2RpcUrls(
primary: string | undefined,
secondary: string | undefined,
): readonly [string, string] {
const endpoints = [primary?.trim(), secondary?.trim()];
if (!endpoints[0] || !endpoints[1]) {
throw new Error("Deep V2 launch verification requires two RPC URLs");
}
let urls: [URL, URL];
try {
urls = [new URL(endpoints[0]), new URL(endpoints[1])];
} catch {
throw new Error("Deep V2 launch verification requires valid RPC URLs");
}
if (urls.some((url) => url.protocol !== "https:")) {
throw new Error("Deep V2 launch verification requires HTTPS RPC URLs");
}
if (urls[0].hostname.toLowerCase() === urls[1].hostname.toLowerCase()) {
throw new Error(
"Deep V2 launch verification requires independent RPC providers",
);
}
return [endpoints[0], endpoints[1]];
}
type DeepV2RuntimeBindingRelease = {
launcher: Address;
automation: Address;
keeperExecutor: Address;
deploymentBlock: number;
keeperExecutorDeploymentBlock: number;
keeperExecutorRuntimeCodeHash: Hex;
runtimeCodeHashes: {
automation: Hex;
};
};
function validHash(value: unknown): value is Hex {
return (
typeof value === "string" &&
isHex(value, { strict: true }) &&
value.length === 66
);
}
function sameHex(left: string, right: string) {
return left.toLowerCase() === right.toLowerCase();
}
function sameAddress(left: unknown, right: Address) {
return (
typeof left === "string" &&
isAddress(left) &&
left.toLowerCase() === right.toLowerCase()
);
}
function validRelease(release: DeepV2RuntimeBindingRelease) {
return (
isAddress(release.launcher) &&
isAddress(release.automation) &&
isAddress(release.keeperExecutor) &&
release.automation.toLowerCase() !==
release.keeperExecutor.toLowerCase() &&
Number.isSafeInteger(release.deploymentBlock) &&
release.deploymentBlock > 0 &&
Number.isSafeInteger(release.keeperExecutorDeploymentBlock) &&
release.keeperExecutorDeploymentBlock >= release.deploymentBlock &&
validHash(release.keeperExecutorRuntimeCodeHash) &&
validHash(release.runtimeCodeHashes.automation)
);
}
export async function assertDeepV2KeeperRuntimeBinding(input: {
clients: readonly DeepV2RuntimeBindingClient[];
chainId: number;
release: DeepV2RuntimeBindingRelease;
}): Promise<{ blockNumber: bigint; blockHash: Hex }> {
const { clients, chainId, release } = input;
if (clients.length !== 2) {
throw new Error(
"Deep V2 launch verification requires exactly two RPC clients",
);
}
if (!Number.isSafeInteger(chainId) || chainId <= 0 || !validRelease(release)) {
throw new Error("Deep V2 runtime release binding is invalid");
}
const chainIds = await Promise.all(
clients.map((client) => client.getChainId()),
);
if (chainIds.some((actual) => actual !== chainId)) {
throw new Error("Deep V2 runtime RPC chain does not match the release");
}
const finalized = await Promise.all(
clients.map((client) => client.getFinalizedBlock()),
);
if (
finalized.some(
(block) =>
block.number <= 0n ||
!block.hash ||
!validHash(block.hash),
)
) {
throw new Error("Deep V2 RPC did not return a finalized block");
}
const blockNumber =
finalized[0].number < finalized[1].number
? finalized[0].number
: finalized[1].number;
if (
blockNumber < BigInt(release.deploymentBlock) ||
blockNumber < BigInt(release.keeperExecutorDeploymentBlock)
) {
throw new Error(
"Deep V2 finalized block predates the reviewed deployment",
);
}
const canonicalBlocks = await Promise.all(
clients.map((client) => client.getBlock({ blockNumber })),
);
const canonicalHash = canonicalBlocks[0].hash;
if (
canonicalBlocks.some(
(block) =>
block.number !== blockNumber ||
!block.hash ||
!validHash(block.hash),
) ||
!canonicalHash ||
!sameHex(canonicalHash, canonicalBlocks[1].hash as Hex)
) {
throw new Error(
"Independent Deep V2 RPCs disagree on the finalized block",
);
}
const verifyRuntime = async (
address: Address,
expectedHash: Hex,
label: string,
) => {
const codes = await Promise.all(
clients.map((client) =>
client.getCode({ address, blockNumber }),
),
);
if (
!codes[0] ||
codes[0] === "0x" ||
!codes[1] ||
codes[1] === "0x" ||
!sameHex(codes[0], codes[1])
) {
throw new Error(
`Independent Deep V2 RPCs disagree on the ${label} runtime`,
);
}
if (!sameHex(keccak256(codes[0]), expectedHash)) {
throw new Error(
`Deep V2 ${label} runtime does not match the reviewed release`,
);
}
};
await Promise.all([
verifyRuntime(
release.keeperExecutor,
release.keeperExecutorRuntimeCodeHash,
"keeper executor",
),
verifyRuntime(
release.automation,
release.runtimeCodeHashes.automation,
"automation",
),
]);
const [
keeperAutomation,
automationLauncher,
launcherAutomation,
] = await Promise.all([
Promise.all(
clients.map((client) =>
client.readKeeperAutomation({
address: release.keeperExecutor,
blockNumber,
}),
),
),
Promise.all(
clients.map((client) =>
client.readAutomationLauncher({
address: release.automation,
blockNumber,
}),
),
),
Promise.all(
clients.map((client) =>
client.readLauncherAutomation({
address: release.launcher,
blockNumber,
}),
),
),
]);
if (
keeperAutomation.some(
(value) => !sameAddress(value, release.automation),
)
) {
throw new Error(
"Deep V2 keeper executor automation binding does not match the reviewed release",
);
}
if (
automationLauncher.some(
(value) => !sameAddress(value, release.launcher),
)
) {
throw new Error(
"Deep V2 automation launcher binding does not match the reviewed release",
);
}
if (
launcherAutomation.some(
(value) => !sameAddress(value, release.automation),
)
) {
throw new Error(
"Deep V2 launcher automation binding does not match the reviewed release",
);
}
return {
blockNumber,
blockHash: canonicalHash,
};
}