@zama-fhe/sdk 1.0.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +28 -0
- package/README.md +801 -0
- package/dist/chunk-5QJTGZHY.js +101 -0
- package/dist/chunk-5QJTGZHY.js.map +1 -0
- package/dist/chunk-6JRD26PS.js +114 -0
- package/dist/chunk-6JRD26PS.js.map +1 -0
- package/dist/chunk-PHE3BSIB.js +5143 -0
- package/dist/chunk-PHE3BSIB.js.map +1 -0
- package/dist/chunk-UF47M3QR.js +32 -0
- package/dist/chunk-UF47M3QR.js.map +1 -0
- package/dist/chunk-WYWAO3QE.js +182 -0
- package/dist/chunk-WYWAO3QE.js.map +1 -0
- package/dist/cleartext/index.d.ts +45 -0
- package/dist/cleartext/index.js +522 -0
- package/dist/cleartext/index.js.map +1 -0
- package/dist/ethers/index.d.ts +86 -0
- package/dist/ethers/index.js +148 -0
- package/dist/ethers/index.js.map +1 -0
- package/dist/index.d.ts +33405 -0
- package/dist/index.js +3563 -0
- package/dist/index.js.map +1 -0
- package/dist/node/index.d.ts +195 -0
- package/dist/node/index.js +337 -0
- package/dist/node/index.js.map +1 -0
- package/dist/relayer-sdk-Dh9aQmBm.d.ts +39 -0
- package/dist/relayer-sdk.node-worker.d.ts +2 -0
- package/dist/relayer-sdk.node-worker.js +348 -0
- package/dist/relayer-sdk.node-worker.js.map +1 -0
- package/dist/relayer-sdk.types-CgHZ6qZn.d.ts +327 -0
- package/dist/relayer-sdk.worker.js +511 -0
- package/dist/relayer-sdk.worker.js.map +1 -0
- package/dist/relayer-utils-phBmWrNB.d.ts +10 -0
- package/dist/token.types-CUTkehsp.d.ts +299 -0
- package/dist/transfer-batcher-CNtrNMz6.d.ts +197 -0
- package/dist/viem/index.d.ts +58 -0
- package/dist/viem/index.js +143 -0
- package/dist/viem/index.js.map +1 -0
- package/package.json +90 -0
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
import { ConfigurationError, EncryptionFailedError, DecryptionFailedError } from '../chunk-6JRD26PS.js';
|
|
2
|
+
import { toBytes, parseAbi, toHex, getAddress, concat, pad, keccak256, hexToBigInt, encodePacked, createPublicClient, http, custom } from 'viem';
|
|
3
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
4
|
+
import { mainnet, sepolia } from 'viem/chains';
|
|
5
|
+
|
|
6
|
+
// src/relayer/cleartext/eip712.ts
|
|
7
|
+
var inputDomain = (chainId, verifyingContract) => ({
|
|
8
|
+
name: "InputVerification",
|
|
9
|
+
version: "1",
|
|
10
|
+
chainId: Number(chainId),
|
|
11
|
+
verifyingContract
|
|
12
|
+
});
|
|
13
|
+
var decryptionDomain = (chainId, verifyingContract) => ({
|
|
14
|
+
name: "Decryption",
|
|
15
|
+
version: "1",
|
|
16
|
+
chainId: Number(chainId),
|
|
17
|
+
verifyingContract
|
|
18
|
+
});
|
|
19
|
+
var INPUT_VERIFICATION_EIP712 = {
|
|
20
|
+
domain: inputDomain,
|
|
21
|
+
types: {
|
|
22
|
+
CiphertextVerification: [
|
|
23
|
+
{ name: "ctHandles", type: "bytes32[]" },
|
|
24
|
+
{ name: "userAddress", type: "address" },
|
|
25
|
+
{ name: "contractAddress", type: "address" },
|
|
26
|
+
{ name: "contractChainId", type: "uint256" },
|
|
27
|
+
{ name: "extraData", type: "bytes" }
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
var KMS_DECRYPTION_EIP712 = {
|
|
32
|
+
domain: decryptionDomain,
|
|
33
|
+
types: {
|
|
34
|
+
PublicDecryptVerification: [
|
|
35
|
+
{ name: "ctHandles", type: "bytes32[]" },
|
|
36
|
+
{ name: "decryptedResult", type: "bytes" },
|
|
37
|
+
{ name: "extraData", type: "bytes" }
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var USER_DECRYPT_EIP712 = {
|
|
42
|
+
types: {
|
|
43
|
+
UserDecryptRequestVerification: [
|
|
44
|
+
{ name: "publicKey", type: "bytes" },
|
|
45
|
+
{ name: "contractAddresses", type: "address[]" },
|
|
46
|
+
{ name: "startTimestamp", type: "uint256" },
|
|
47
|
+
{ name: "durationDays", type: "uint256" },
|
|
48
|
+
{ name: "extraData", type: "bytes" }
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var DELEGATED_USER_DECRYPT_EIP712 = {
|
|
53
|
+
types: {
|
|
54
|
+
DelegatedUserDecryptRequestVerification: [
|
|
55
|
+
{ name: "publicKey", type: "bytes" },
|
|
56
|
+
{ name: "contractAddresses", type: "address[]" },
|
|
57
|
+
{ name: "delegatorAddress", type: "address" },
|
|
58
|
+
{ name: "startTimestamp", type: "uint256" },
|
|
59
|
+
{ name: "durationDays", type: "uint256" },
|
|
60
|
+
{ name: "extraData", type: "bytes" }
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// src/relayer/cleartext/constants.ts
|
|
66
|
+
var FHE_BIT_WIDTHS = {
|
|
67
|
+
[0 /* Bool */]: 1,
|
|
68
|
+
[1 /* Uint4 */]: 4,
|
|
69
|
+
[2 /* Uint8 */]: 8,
|
|
70
|
+
[3 /* Uint16 */]: 16,
|
|
71
|
+
[4 /* Uint32 */]: 32,
|
|
72
|
+
[5 /* Uint64 */]: 64,
|
|
73
|
+
[6 /* Uint128 */]: 128,
|
|
74
|
+
[7 /* Uint160 */]: 160,
|
|
75
|
+
[8 /* Uint256 */]: 256
|
|
76
|
+
};
|
|
77
|
+
var HANDLE_VERSION = 0;
|
|
78
|
+
var PREHANDLE_MASK = 0xffffffffffffffffffffffffffffffffffffffffff0000000000000000000000n;
|
|
79
|
+
var MOCK_INPUT_SIGNER_PK = "0x7ec8ada6642fc4ccfb7729bc29c17cf8d21b61abd5642d1db992c0b8672ab901";
|
|
80
|
+
var MOCK_KMS_SIGNER_PK = "0x388b7680e4e1afa06efbfd45cdd1fe39f3c6af381df6555a19661f283b97de91";
|
|
81
|
+
var VERIFYING_CONTRACTS = {
|
|
82
|
+
inputVerification: "0x812b06e1CDCE800494b79fFE4f925A504a9A9810",
|
|
83
|
+
decryption: "0x5ffdaAB0373E62E2ea2944776209aEf29E631A64"
|
|
84
|
+
};
|
|
85
|
+
var GATEWAY_CHAIN_ID = 10901;
|
|
86
|
+
var RAW_CT_HASH_DOMAIN_SEPARATOR = toBytes("ZK-w_rct");
|
|
87
|
+
var HANDLE_HASH_DOMAIN_SEPARATOR = toBytes("ZK-w_hdl");
|
|
88
|
+
function cleartextToBytes(cleartext, fheType) {
|
|
89
|
+
const byteLength = Math.ceil(FHE_BIT_WIDTHS[fheType] / 8);
|
|
90
|
+
return toBytes(pad(toHex(cleartext), { size: byteLength }));
|
|
91
|
+
}
|
|
92
|
+
function computeMockCiphertext(fheType, cleartext, random32) {
|
|
93
|
+
if (random32.length !== 32) {
|
|
94
|
+
throw new EncryptionFailedError("random32 must be exactly 32 bytes");
|
|
95
|
+
}
|
|
96
|
+
const clearBytes = cleartextToBytes(cleartext, fheType);
|
|
97
|
+
const inner = keccak256(
|
|
98
|
+
concat([toHex(new Uint8Array([fheType])), toHex(clearBytes), toHex(random32)])
|
|
99
|
+
);
|
|
100
|
+
return keccak256(concat([toHex(RAW_CT_HASH_DOMAIN_SEPARATOR), inner]));
|
|
101
|
+
}
|
|
102
|
+
function computeInputHandle(mockCiphertext, index, fheType, aclAddress, chainId) {
|
|
103
|
+
if (!Number.isInteger(index) || index < 0 || index > 255) {
|
|
104
|
+
throw new EncryptionFailedError("index must be an integer between 0 and 255");
|
|
105
|
+
}
|
|
106
|
+
const blobHash = keccak256(
|
|
107
|
+
concat([toHex(RAW_CT_HASH_DOMAIN_SEPARATOR), mockCiphertext])
|
|
108
|
+
);
|
|
109
|
+
const handleHash = keccak256(
|
|
110
|
+
encodePacked(
|
|
111
|
+
["bytes", "bytes32", "uint8", "address", "uint256"],
|
|
112
|
+
[
|
|
113
|
+
toHex(HANDLE_HASH_DOMAIN_SEPARATOR),
|
|
114
|
+
blobHash,
|
|
115
|
+
index,
|
|
116
|
+
aclAddress,
|
|
117
|
+
chainId
|
|
118
|
+
]
|
|
119
|
+
)
|
|
120
|
+
);
|
|
121
|
+
const chainId64 = chainId & 0xffffffffffffffffn;
|
|
122
|
+
const handle = BigInt(handleHash) & PREHANDLE_MASK | BigInt(index) << 80n | chainId64 << 16n | BigInt(fheType) << 8n | BigInt(HANDLE_VERSION);
|
|
123
|
+
return toHex(handle, { size: 32 });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/relayer/cleartext/encrypted-input.ts
|
|
127
|
+
var INPUT_SIGNER = privateKeyToAccount(MOCK_INPUT_SIGNER_PK);
|
|
128
|
+
var CleartextEncryptedInput = class {
|
|
129
|
+
#contractAddress;
|
|
130
|
+
#userAddress;
|
|
131
|
+
#config;
|
|
132
|
+
#values = [];
|
|
133
|
+
constructor(contractAddress, userAddress, config) {
|
|
134
|
+
this.#contractAddress = getAddress(contractAddress);
|
|
135
|
+
this.#userAddress = getAddress(userAddress);
|
|
136
|
+
this.#config = config;
|
|
137
|
+
}
|
|
138
|
+
#addValue(fheType, value) {
|
|
139
|
+
if (value < 0n) {
|
|
140
|
+
throw new EncryptionFailedError("Only non-negative cleartext values are supported");
|
|
141
|
+
}
|
|
142
|
+
const bitWidth = FHE_BIT_WIDTHS[fheType];
|
|
143
|
+
const maxValue = (1n << BigInt(bitWidth)) - 1n;
|
|
144
|
+
if (value > maxValue) {
|
|
145
|
+
throw new EncryptionFailedError(
|
|
146
|
+
`Value ${value} exceeds max ${maxValue} for FheType ${fheType}`
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
this.#values.push({ fheType, value });
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
addBool(value) {
|
|
153
|
+
const normalized = typeof value === "boolean" ? value ? 1n : 0n : value;
|
|
154
|
+
if (normalized !== 0n && normalized !== 1n) {
|
|
155
|
+
throw new EncryptionFailedError("Bool value must be 0, 1, true, or false");
|
|
156
|
+
}
|
|
157
|
+
return this.#addValue(0 /* Bool */, normalized);
|
|
158
|
+
}
|
|
159
|
+
add4(value) {
|
|
160
|
+
return this.#addValue(1 /* Uint4 */, value);
|
|
161
|
+
}
|
|
162
|
+
add8(value) {
|
|
163
|
+
return this.#addValue(2 /* Uint8 */, value);
|
|
164
|
+
}
|
|
165
|
+
add16(value) {
|
|
166
|
+
return this.#addValue(3 /* Uint16 */, value);
|
|
167
|
+
}
|
|
168
|
+
add32(value) {
|
|
169
|
+
return this.#addValue(4 /* Uint32 */, value);
|
|
170
|
+
}
|
|
171
|
+
add64(value) {
|
|
172
|
+
return this.#addValue(5 /* Uint64 */, value);
|
|
173
|
+
}
|
|
174
|
+
add128(value) {
|
|
175
|
+
return this.#addValue(6 /* Uint128 */, value);
|
|
176
|
+
}
|
|
177
|
+
addAddress(value) {
|
|
178
|
+
return this.#addValue(7 /* Uint160 */, BigInt(getAddress(value)));
|
|
179
|
+
}
|
|
180
|
+
add256(value) {
|
|
181
|
+
return this.#addValue(8 /* Uint256 */, value);
|
|
182
|
+
}
|
|
183
|
+
async encrypt() {
|
|
184
|
+
const random32List = this.#values.map(() => crypto.getRandomValues(new Uint8Array(32)));
|
|
185
|
+
const mockCiphertexts = this.#values.map(
|
|
186
|
+
({ fheType, value }, index) => computeMockCiphertext(fheType, value, random32List[index])
|
|
187
|
+
);
|
|
188
|
+
const ciphertextParts = mockCiphertexts.map((ciphertext) => ciphertext);
|
|
189
|
+
const ciphertextBlob = keccak256(ciphertextParts.length > 0 ? concat(ciphertextParts) : "0x");
|
|
190
|
+
const handles = this.#values.map(
|
|
191
|
+
({ fheType }, index) => computeInputHandle(
|
|
192
|
+
ciphertextBlob,
|
|
193
|
+
index,
|
|
194
|
+
fheType,
|
|
195
|
+
this.#config.contracts.acl,
|
|
196
|
+
this.#config.chainId
|
|
197
|
+
)
|
|
198
|
+
);
|
|
199
|
+
const cleartextParts = this.#values.map(({ value }) => pad(toHex(value), { size: 32 }));
|
|
200
|
+
const cleartextBytes = cleartextParts.length > 0 ? concat(cleartextParts) : "0x";
|
|
201
|
+
const signature = await INPUT_SIGNER.signTypedData({
|
|
202
|
+
domain: INPUT_VERIFICATION_EIP712.domain(
|
|
203
|
+
this.#config.gatewayChainId,
|
|
204
|
+
this.#config.contracts.verifyingInputVerifier
|
|
205
|
+
),
|
|
206
|
+
types: {
|
|
207
|
+
CiphertextVerification: [...INPUT_VERIFICATION_EIP712.types.CiphertextVerification]
|
|
208
|
+
},
|
|
209
|
+
primaryType: "CiphertextVerification",
|
|
210
|
+
message: {
|
|
211
|
+
ctHandles: handles,
|
|
212
|
+
userAddress: this.#userAddress,
|
|
213
|
+
contractAddress: this.#contractAddress,
|
|
214
|
+
contractChainId: this.#config.chainId,
|
|
215
|
+
extraData: cleartextBytes
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
const inputProof = toBytes(
|
|
219
|
+
concat([
|
|
220
|
+
toHex(new Uint8Array([handles.length])),
|
|
221
|
+
toHex(new Uint8Array([1])),
|
|
222
|
+
...handles.map((handle) => handle),
|
|
223
|
+
signature,
|
|
224
|
+
cleartextBytes
|
|
225
|
+
])
|
|
226
|
+
);
|
|
227
|
+
return {
|
|
228
|
+
handles: handles.map((handle) => toBytes(handle)),
|
|
229
|
+
inputProof
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
// src/relayer/cleartext/cleartext-fhevm-instance.ts
|
|
235
|
+
var KMS_SIGNER = privateKeyToAccount(MOCK_KMS_SIGNER_PK);
|
|
236
|
+
var ACL_ABI = parseAbi([
|
|
237
|
+
"function persistAllowed(bytes32 handle, address account) view returns (bool)",
|
|
238
|
+
"function isAllowedForDecryption(bytes32 handle) view returns (bool)",
|
|
239
|
+
"function isHandleDelegatedForUserDecryption(address delegator, address delegate, address contractAddress, bytes32 handle) view returns (bool)"
|
|
240
|
+
]);
|
|
241
|
+
var EXECUTOR_ABI = parseAbi(["function plaintexts(bytes32 handle) view returns (uint256)"]);
|
|
242
|
+
var USER_DECRYPT_TYPES = {
|
|
243
|
+
UserDecryptRequestVerification: USER_DECRYPT_EIP712.types.UserDecryptRequestVerification
|
|
244
|
+
};
|
|
245
|
+
var DELEGATED_USER_DECRYPT_TYPES = {
|
|
246
|
+
EIP712Domain: [
|
|
247
|
+
{ name: "name", type: "string" },
|
|
248
|
+
{ name: "version", type: "string" },
|
|
249
|
+
{ name: "chainId", type: "uint256" },
|
|
250
|
+
{ name: "verifyingContract", type: "address" }
|
|
251
|
+
],
|
|
252
|
+
DelegatedUserDecryptRequestVerification: DELEGATED_USER_DECRYPT_EIP712.types.DelegatedUserDecryptRequestVerification
|
|
253
|
+
};
|
|
254
|
+
var KMS_DECRYPTION_TYPES = {
|
|
255
|
+
PublicDecryptVerification: KMS_DECRYPTION_EIP712.types.PublicDecryptVerification
|
|
256
|
+
};
|
|
257
|
+
function normalizeHandle(handle) {
|
|
258
|
+
return toHex(hexToBigInt(handle), { size: 32 });
|
|
259
|
+
}
|
|
260
|
+
function decodeDecryptedValue(handle, rawValue) {
|
|
261
|
+
const typeByte = Number(BigInt(handle) >> 8n & 0xffn);
|
|
262
|
+
if (typeByte === 0 /* Bool */) return rawValue !== 0n;
|
|
263
|
+
if (typeByte === 7 /* Uint160 */) return toHex(rawValue, { size: 20 });
|
|
264
|
+
return rawValue;
|
|
265
|
+
}
|
|
266
|
+
var CleartextFhevmInstance = class _CleartextFhevmInstance {
|
|
267
|
+
#client;
|
|
268
|
+
#config;
|
|
269
|
+
static #FORBIDDEN_CHAIN_IDS = /* @__PURE__ */ new Set([BigInt(mainnet.id), BigInt(sepolia.id)]);
|
|
270
|
+
constructor(client, config) {
|
|
271
|
+
if (_CleartextFhevmInstance.#FORBIDDEN_CHAIN_IDS.has(config.chainId)) {
|
|
272
|
+
throw new ConfigurationError(
|
|
273
|
+
`Cleartext mode is not allowed on chain ${config.chainId}. It is intended for local development and testing only.`
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
this.#client = client;
|
|
277
|
+
this.#config = config;
|
|
278
|
+
}
|
|
279
|
+
async generateKeypair() {
|
|
280
|
+
const publicKey = toHex(crypto.getRandomValues(new Uint8Array(32)));
|
|
281
|
+
let privateKey = toHex(crypto.getRandomValues(new Uint8Array(32)));
|
|
282
|
+
while (privateKey === publicKey) {
|
|
283
|
+
privateKey = toHex(crypto.getRandomValues(new Uint8Array(32)));
|
|
284
|
+
}
|
|
285
|
+
return { publicKey, privateKey };
|
|
286
|
+
}
|
|
287
|
+
async createEIP712(publicKey, contractAddresses, startTimestamp, durationDays = 7) {
|
|
288
|
+
const domain = {
|
|
289
|
+
name: "Decryption",
|
|
290
|
+
version: "1",
|
|
291
|
+
chainId: Number(this.#config.chainId),
|
|
292
|
+
verifyingContract: this.#config.contracts.verifyingDecryption
|
|
293
|
+
};
|
|
294
|
+
return {
|
|
295
|
+
domain,
|
|
296
|
+
types: USER_DECRYPT_TYPES,
|
|
297
|
+
primaryType: "UserDecryptRequestVerification",
|
|
298
|
+
message: {
|
|
299
|
+
publicKey,
|
|
300
|
+
contractAddresses,
|
|
301
|
+
startTimestamp: BigInt(startTimestamp),
|
|
302
|
+
durationDays: BigInt(durationDays),
|
|
303
|
+
extraData: "0x00"
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
async encrypt(params) {
|
|
308
|
+
const input = new CleartextEncryptedInput(
|
|
309
|
+
params.contractAddress,
|
|
310
|
+
params.userAddress,
|
|
311
|
+
this.#config
|
|
312
|
+
);
|
|
313
|
+
for (const entry of params.values) {
|
|
314
|
+
const { value, type } = entry;
|
|
315
|
+
switch (type) {
|
|
316
|
+
case "ebool":
|
|
317
|
+
input.addBool(value);
|
|
318
|
+
break;
|
|
319
|
+
case "euint8":
|
|
320
|
+
input.add8(value);
|
|
321
|
+
break;
|
|
322
|
+
case "euint16":
|
|
323
|
+
input.add16(value);
|
|
324
|
+
break;
|
|
325
|
+
case "euint32":
|
|
326
|
+
input.add32(value);
|
|
327
|
+
break;
|
|
328
|
+
case "euint64":
|
|
329
|
+
input.add64(value);
|
|
330
|
+
break;
|
|
331
|
+
case "euint128":
|
|
332
|
+
input.add128(value);
|
|
333
|
+
break;
|
|
334
|
+
case "eaddress":
|
|
335
|
+
input.addAddress(value);
|
|
336
|
+
break;
|
|
337
|
+
case "euint256":
|
|
338
|
+
input.add256(value);
|
|
339
|
+
break;
|
|
340
|
+
default:
|
|
341
|
+
throw new EncryptionFailedError(`Unsupported FHE type: ${type}`);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return input.encrypt();
|
|
345
|
+
}
|
|
346
|
+
async userDecrypt(params) {
|
|
347
|
+
const normalizedSignerAddress = getAddress(params.signerAddress);
|
|
348
|
+
const normalizedHandles = params.handles.map(normalizeHandle);
|
|
349
|
+
const allowedResults = await Promise.all(
|
|
350
|
+
normalizedHandles.map((handle) => this.#persistAllowed(handle, normalizedSignerAddress))
|
|
351
|
+
);
|
|
352
|
+
const unauthorizedIndex = allowedResults.findIndex((isAllowed) => !isAllowed);
|
|
353
|
+
if (unauthorizedIndex !== -1) {
|
|
354
|
+
throw new DecryptionFailedError(
|
|
355
|
+
`Handle ${normalizedHandles[unauthorizedIndex]} is not authorized for user decrypt`
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
return this.#decryptHandles(normalizedHandles);
|
|
359
|
+
}
|
|
360
|
+
async publicDecrypt(handles) {
|
|
361
|
+
const normalizedHandles = handles.map(normalizeHandle);
|
|
362
|
+
const allowedResults = await Promise.all(
|
|
363
|
+
normalizedHandles.map((handle) => this.#isAllowedForDecryption(handle))
|
|
364
|
+
);
|
|
365
|
+
const unauthorizedIndex = allowedResults.findIndex((isAllowed) => !isAllowed);
|
|
366
|
+
if (unauthorizedIndex !== -1) {
|
|
367
|
+
throw new DecryptionFailedError(
|
|
368
|
+
`Handle ${normalizedHandles[unauthorizedIndex]} is not allowed for public decryption`
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
const orderedValues = await Promise.all(
|
|
372
|
+
normalizedHandles.map((handle) => this.#readPlaintext(handle))
|
|
373
|
+
);
|
|
374
|
+
const clearValues = Object.fromEntries(
|
|
375
|
+
normalizedHandles.map((handle, index) => [handle, orderedValues[index]])
|
|
376
|
+
);
|
|
377
|
+
const abiEncodedClearValues = concat(orderedValues.map((v) => pad(toHex(v), { size: 32 })));
|
|
378
|
+
const signature = await KMS_SIGNER.signTypedData({
|
|
379
|
+
domain: KMS_DECRYPTION_EIP712.domain(
|
|
380
|
+
this.#config.gatewayChainId,
|
|
381
|
+
this.#config.contracts.verifyingDecryption
|
|
382
|
+
),
|
|
383
|
+
types: KMS_DECRYPTION_TYPES,
|
|
384
|
+
primaryType: "PublicDecryptVerification",
|
|
385
|
+
message: {
|
|
386
|
+
ctHandles: normalizedHandles,
|
|
387
|
+
decryptedResult: abiEncodedClearValues,
|
|
388
|
+
extraData: "0x"
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
const decryptionProof = concat([toHex(new Uint8Array([1])), signature]);
|
|
392
|
+
return {
|
|
393
|
+
clearValues,
|
|
394
|
+
abiEncodedClearValues,
|
|
395
|
+
decryptionProof
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
async createDelegatedUserDecryptEIP712(publicKey, contractAddresses, delegatorAddress, startTimestamp, durationDays = 7) {
|
|
399
|
+
const domain = {
|
|
400
|
+
name: "Decryption",
|
|
401
|
+
version: "1",
|
|
402
|
+
chainId: this.#config.chainId,
|
|
403
|
+
verifyingContract: this.#config.contracts.verifyingDecryption
|
|
404
|
+
};
|
|
405
|
+
const message = {
|
|
406
|
+
publicKey,
|
|
407
|
+
contractAddresses,
|
|
408
|
+
delegatorAddress,
|
|
409
|
+
startTimestamp: String(startTimestamp),
|
|
410
|
+
durationDays: String(durationDays),
|
|
411
|
+
extraData: "0x00"
|
|
412
|
+
};
|
|
413
|
+
return {
|
|
414
|
+
domain,
|
|
415
|
+
types: DELEGATED_USER_DECRYPT_TYPES,
|
|
416
|
+
primaryType: "DelegatedUserDecryptRequestVerification",
|
|
417
|
+
message
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
async delegatedUserDecrypt(params) {
|
|
421
|
+
const normalizedHandles = params.handles.map(normalizeHandle);
|
|
422
|
+
const delegatedResults = await Promise.all(
|
|
423
|
+
normalizedHandles.map(
|
|
424
|
+
(handle) => this.#isHandleDelegatedForUserDecryption(
|
|
425
|
+
params.delegatorAddress,
|
|
426
|
+
params.delegateAddress,
|
|
427
|
+
params.contractAddress,
|
|
428
|
+
handle
|
|
429
|
+
)
|
|
430
|
+
)
|
|
431
|
+
);
|
|
432
|
+
const unauthorizedIndex = delegatedResults.findIndex((isDelegated) => !isDelegated);
|
|
433
|
+
if (unauthorizedIndex !== -1) {
|
|
434
|
+
throw new DecryptionFailedError(
|
|
435
|
+
`Handle ${normalizedHandles[unauthorizedIndex]} is not delegated for user decryption (delegator=${params.delegatorAddress}, delegate=${params.delegateAddress}, contract=${params.contractAddress})`
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
return this.#decryptHandles(normalizedHandles);
|
|
439
|
+
}
|
|
440
|
+
async requestZKProofVerification(_zkProof) {
|
|
441
|
+
throw new ConfigurationError("Not implemented in cleartext mode");
|
|
442
|
+
}
|
|
443
|
+
async getPublicKey() {
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
async getPublicParams(_bits) {
|
|
447
|
+
return null;
|
|
448
|
+
}
|
|
449
|
+
terminate() {
|
|
450
|
+
}
|
|
451
|
+
async #decryptHandles(normalizedHandles) {
|
|
452
|
+
const values = await Promise.all(
|
|
453
|
+
normalizedHandles.map((handle) => this.#readPlaintext(handle))
|
|
454
|
+
);
|
|
455
|
+
return Object.fromEntries(
|
|
456
|
+
normalizedHandles.map((handle, index) => [
|
|
457
|
+
handle,
|
|
458
|
+
decodeDecryptedValue(handle, values[index])
|
|
459
|
+
])
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
async #persistAllowed(handle, account) {
|
|
463
|
+
return this.#client.readContract({
|
|
464
|
+
address: this.#config.contracts.acl,
|
|
465
|
+
abi: ACL_ABI,
|
|
466
|
+
functionName: "persistAllowed",
|
|
467
|
+
args: [handle, account]
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
async #isAllowedForDecryption(handle) {
|
|
471
|
+
return this.#client.readContract({
|
|
472
|
+
address: this.#config.contracts.acl,
|
|
473
|
+
abi: ACL_ABI,
|
|
474
|
+
functionName: "isAllowedForDecryption",
|
|
475
|
+
args: [handle]
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
async #isHandleDelegatedForUserDecryption(delegator, delegate, contractAddress, handle) {
|
|
479
|
+
return this.#client.readContract({
|
|
480
|
+
address: this.#config.contracts.acl,
|
|
481
|
+
abi: ACL_ABI,
|
|
482
|
+
functionName: "isHandleDelegatedForUserDecryption",
|
|
483
|
+
args: [delegator, delegate, contractAddress, handle]
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
async #readPlaintext(handle) {
|
|
487
|
+
return this.#client.readContract({
|
|
488
|
+
address: this.#config.contracts.executor,
|
|
489
|
+
abi: EXECUTOR_ABI,
|
|
490
|
+
functionName: "plaintexts",
|
|
491
|
+
args: [handle]
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
// src/relayer/cleartext/factory.ts
|
|
497
|
+
function createCleartextRelayer(config) {
|
|
498
|
+
const client = createPublicClient({
|
|
499
|
+
transport: typeof config.rpcUrl === "string" ? http(config.rpcUrl) : custom(config.rpcUrl)
|
|
500
|
+
});
|
|
501
|
+
const { rpcUrl: _, ...internalConfig } = config;
|
|
502
|
+
return new CleartextFhevmInstance(client, internalConfig);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// src/relayer/cleartext/presets.ts
|
|
506
|
+
var hoodi = {
|
|
507
|
+
chainId: 560048n,
|
|
508
|
+
gatewayChainId: 10901,
|
|
509
|
+
rpcUrl: "https://rpc.hoodi.ethpandaops.io",
|
|
510
|
+
contracts: {
|
|
511
|
+
acl: "0xCC505dF6f244AcBf12C915888eC046939a439dB5",
|
|
512
|
+
executor: "0xfeec4C7e8Ad0Af0F42b594A453F4dB6da01e21D6",
|
|
513
|
+
kmsVerifier: "0xdae4028BD86408F59Db79032EDd05170FE350c5d",
|
|
514
|
+
inputVerifier: "0x2cD198C290d8AD6C5F2020C35F7b5868b74bDD55",
|
|
515
|
+
verifyingInputVerifier: "0x812b06e1CDCE800494b79fFE4f925A504a9A9810",
|
|
516
|
+
verifyingDecryption: "0x5ffdaAB0373E62E2ea2944776209aEf29E631A64"
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
export { GATEWAY_CHAIN_ID, VERIFYING_CONTRACTS, createCleartextRelayer, hoodi };
|
|
521
|
+
//# sourceMappingURL=index.js.map
|
|
522
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/relayer/cleartext/eip712.ts","../../src/relayer/cleartext/constants.ts","../../src/relayer/cleartext/handle.ts","../../src/relayer/cleartext/encrypted-input.ts","../../src/relayer/cleartext/cleartext-fhevm-instance.ts","../../src/relayer/cleartext/factory.ts","../../src/relayer/cleartext/presets.ts"],"names":["keccak256","concat","pad","toHex","toBytes","privateKeyToAccount","getAddress"],"mappings":";;;;;;AAIA,IAAM,WAAA,GAA6B,CAAC,OAAA,EAAS,iBAAA,MAAuB;AAAA,EAClE,IAAA,EAAM,mBAAA;AAAA,EACN,OAAA,EAAS,GAAA;AAAA,EACT,OAAA,EAAS,OAAO,OAAO,CAAA;AAAA,EACvB;AACF,CAAA,CAAA;AAEA,IAAM,gBAAA,GAAkC,CAAC,OAAA,EAAS,iBAAA,MAAuB;AAAA,EACvE,IAAA,EAAM,YAAA;AAAA,EACN,OAAA,EAAS,GAAA;AAAA,EACT,OA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AA;AAAA,IACL,QAAA,EAAU,4CAAA;AAAA,IACV,WAAA,EAAa,4CAAA;AAAA,IACb,aAAA,EAAe,4CAAA;AAAA,IACf,sBAAA,EAAwB,4CAAA;AAAA,IACxB,mBAAA,EAAqB;AAAA;AAEzB","file":"index.js","sourcesContent":["import type { TypedDataDomain } from \"viem\";\n\ntype DomainFactory = (chainId: number | bigint, verifyingContract: string) => TypedDataDomain;\n\nconst inputDomain: DomainFactory = (chainId, verifyingContract) => ({\n name: \"InputVerification\",\n version: \"1\",\n chainId: Number(chainId),\n verifyingContract: verifyingContract as `0x${string}`,\n});\n\nconst decryptionDomain: DomainFactory = (chainId, verifyingContract) => ({\n name: \"Decryption\",\n version: \"1\",\n chainId: Number(chainId),\n verifyingContract: verifyingContract as `0x${string}`,\n});\n\nexport const INPUT_VERIFICATION_EIP712 = {\n domain: inputDomain,\n types: {\n CiphertextVerification: [\n { name: \"ctHandles\", type: \"bytes32[]\" },\n { name: \"userAddress\", type: \"address\" },\n { name: \"contractAddress\", type: \"address\" },\n { name: \"contractChainId\", type: \"uint256\" },\n { name: \"extraData\", type: \"bytes\" },\n ],\n },\n} as const;\n\nexport const KMS_DECRYPTION_EIP712 = {\n domain: decryptionDomain,\n types: {\n PublicDecryptVerification: [\n { name: \"ctHandles\", type: \"bytes32[]\" },\n { name: \"decryptedResult\", type: \"bytes\" },\n { name: \"extraData\", type: \"bytes\" },\n ],\n },\n} as const;\n\nexport const USER_DECRYPT_EIP712 = {\n domain: decryptionDomain,\n types: {\n UserDecryptRequestVerification: [\n { name: \"publicKey\", type: \"bytes\" },\n { name: \"contractAddresses\", type: \"address[]\" },\n { name: \"startTimestamp\", type: \"uint256\" },\n { name: \"durationDays\", type: \"uint256\" },\n { name: \"extraData\", type: \"bytes\" },\n ],\n },\n} as const;\n\nexport const DELEGATED_USER_DECRYPT_EIP712 = {\n domain: decryptionDomain,\n types: {\n DelegatedUserDecryptRequestVerification: [\n { name: \"publicKey\", type: \"bytes\" },\n { name: \"contractAddresses\", type: \"address[]\" },\n { name: \"delegatorAddress\", type: \"address\" },\n { name: \"startTimestamp\", type: \"uint256\" },\n { name: \"durationDays\", type: \"uint256\" },\n { name: \"extraData\", type: \"bytes\" },\n ],\n },\n} as const;\n","export enum FheType {\n Bool = 0,\n Uint4 = 1,\n Uint8 = 2,\n Uint16 = 3,\n Uint32 = 4,\n Uint64 = 5,\n Uint128 = 6,\n Uint160 = 7,\n Uint256 = 8,\n}\n\nexport const FHE_BIT_WIDTHS: Record<FheType, number> = {\n [FheType.Bool]: 1,\n [FheType.Uint4]: 4,\n [FheType.Uint8]: 8,\n [FheType.Uint16]: 16,\n [FheType.Uint32]: 32,\n [FheType.Uint64]: 64,\n [FheType.Uint128]: 128,\n [FheType.Uint160]: 160,\n [FheType.Uint256]: 256,\n};\n\nexport const HANDLE_VERSION = 0;\n\nexport const PREHANDLE_MASK = 0xffffffffffffffffffffffffffffffffffffffffff0000000000000000000000n;\n\n// Constants used for instanciation of the cleartext FHEVM instance.\nexport const MOCK_INPUT_SIGNER_PK =\n \"0x7ec8ada6642fc4ccfb7729bc29c17cf8d21b61abd5642d1db992c0b8672ab901\";\nexport const MOCK_KMS_SIGNER_PK =\n \"0x388b7680e4e1afa06efbfd45cdd1fe39f3c6af381df6555a19661f283b97de91\";\n\nexport const VERIFYING_CONTRACTS = {\n inputVerification: \"0x812b06e1CDCE800494b79fFE4f925A504a9A9810\",\n decryption: \"0x5ffdaAB0373E62E2ea2944776209aEf29E631A64\",\n} as const;\n\nexport const GATEWAY_CHAIN_ID = 10_901;\n","import { concat, encodePacked, keccak256, pad, toBytes, toHex } from \"viem\";\nimport { FHE_BIT_WIDTHS, FheType, HANDLE_VERSION, PREHANDLE_MASK } from \"./constants\";\nimport { EncryptionFailedError } from \"../../token/errors\";\n\nconst RAW_CT_HASH_DOMAIN_SEPARATOR = toBytes(\"ZK-w_rct\");\nconst HANDLE_HASH_DOMAIN_SEPARATOR = toBytes(\"ZK-w_hdl\");\n\nfunction cleartextToBytes(cleartext: bigint, fheType: FheType): Uint8Array {\n const byteLength = Math.ceil(FHE_BIT_WIDTHS[fheType] / 8);\n return toBytes(pad(toHex(cleartext), { size: byteLength }));\n}\n\nexport function computeMockCiphertext(\n fheType: FheType,\n cleartext: bigint,\n random32: Uint8Array,\n): string {\n if (random32.length !== 32) {\n throw new EncryptionFailedError(\"random32 must be exactly 32 bytes\");\n }\n\n const clearBytes = cleartextToBytes(cleartext, fheType);\n const inner = keccak256(\n concat([toHex(new Uint8Array([fheType])), toHex(clearBytes), toHex(random32)]),\n );\n\n return keccak256(concat([toHex(RAW_CT_HASH_DOMAIN_SEPARATOR), inner]));\n}\n\nexport function computeInputHandle(\n mockCiphertext: string,\n index: number,\n fheType: FheType,\n aclAddress: string,\n chainId: bigint,\n): string {\n if (!Number.isInteger(index) || index < 0 || index > 255) {\n throw new EncryptionFailedError(\"index must be an integer between 0 and 255\");\n }\n\n const blobHash = keccak256(\n concat([toHex(RAW_CT_HASH_DOMAIN_SEPARATOR), mockCiphertext as `0x${string}`]),\n );\n const handleHash = keccak256(\n encodePacked(\n [\"bytes\", \"bytes32\", \"uint8\", \"address\", \"uint256\"],\n [\n toHex(HANDLE_HASH_DOMAIN_SEPARATOR),\n blobHash as `0x${string}`,\n index,\n aclAddress as `0x${string}`,\n chainId,\n ],\n ),\n );\n\n const chainId64 = chainId & 0xffff_ffff_ffff_ffffn;\n const handle =\n (BigInt(handleHash) & PREHANDLE_MASK) |\n (BigInt(index) << 80n) |\n (chainId64 << 16n) |\n (BigInt(fheType) << 8n) |\n BigInt(HANDLE_VERSION);\n\n return toHex(handle, { size: 32 });\n}\n","import { concat, getAddress, keccak256, pad, toBytes, toHex } from \"viem\";\nimport { privateKeyToAccount } from \"viem/accounts\";\nimport { FHE_BIT_WIDTHS, FheType } from \"./constants\";\nimport { INPUT_VERIFICATION_EIP712 } from \"./eip712\";\nimport { computeInputHandle, computeMockCiphertext } from \"./handle\";\nimport { MOCK_INPUT_SIGNER_PK } from \"./constants\";\nimport type { CleartextConfig } from \"./types\";\nimport { EncryptionFailedError } from \"../../token/errors\";\n\nconst INPUT_SIGNER = privateKeyToAccount(MOCK_INPUT_SIGNER_PK as `0x${string}`);\n\ntype AddedValue = {\n fheType: FheType;\n value: bigint;\n};\n\nexport class CleartextEncryptedInput {\n readonly #contractAddress: string;\n readonly #userAddress: string;\n readonly #config: CleartextConfig;\n readonly #values: AddedValue[] = [];\n\n constructor(contractAddress: string, userAddress: string, config: CleartextConfig) {\n this.#contractAddress = getAddress(contractAddress);\n this.#userAddress = getAddress(userAddress);\n this.#config = config;\n }\n\n #addValue(fheType: FheType, value: bigint): this {\n if (value < 0n) {\n throw new EncryptionFailedError(\"Only non-negative cleartext values are supported\");\n }\n\n const bitWidth = FHE_BIT_WIDTHS[fheType];\n const maxValue = (1n << BigInt(bitWidth)) - 1n;\n if (value > maxValue) {\n throw new EncryptionFailedError(\n `Value ${value} exceeds max ${maxValue} for FheType ${fheType}`,\n );\n }\n\n this.#values.push({ fheType, value });\n return this;\n }\n\n addBool(value: boolean | bigint): this {\n const normalized = typeof value === \"boolean\" ? (value ? 1n : 0n) : value;\n if (normalized !== 0n && normalized !== 1n) {\n throw new EncryptionFailedError(\"Bool value must be 0, 1, true, or false\");\n }\n return this.#addValue(FheType.Bool, normalized);\n }\n\n add4(value: bigint): this {\n return this.#addValue(FheType.Uint4, value);\n }\n\n add8(value: bigint): this {\n return this.#addValue(FheType.Uint8, value);\n }\n\n add16(value: bigint): this {\n return this.#addValue(FheType.Uint16, value);\n }\n\n add32(value: bigint): this {\n return this.#addValue(FheType.Uint32, value);\n }\n\n add64(value: bigint): this {\n return this.#addValue(FheType.Uint64, value);\n }\n\n add128(value: bigint): this {\n return this.#addValue(FheType.Uint128, value);\n }\n\n addAddress(value: string): this {\n return this.#addValue(FheType.Uint160, BigInt(getAddress(value)));\n }\n\n add256(value: bigint): this {\n return this.#addValue(FheType.Uint256, value);\n }\n\n async encrypt(): Promise<{ handles: Uint8Array[]; inputProof: Uint8Array }> {\n const random32List = this.#values.map(() => crypto.getRandomValues(new Uint8Array(32)));\n\n const mockCiphertexts = this.#values.map(({ fheType, value }, index) =>\n computeMockCiphertext(fheType, value, random32List[index]!),\n );\n\n const ciphertextParts = mockCiphertexts.map((ciphertext) => ciphertext as `0x${string}`);\n const ciphertextBlob = keccak256(ciphertextParts.length > 0 ? concat(ciphertextParts) : \"0x\");\n\n const handles = this.#values.map(({ fheType }, index) =>\n computeInputHandle(\n ciphertextBlob,\n index,\n fheType,\n this.#config.contracts.acl,\n this.#config.chainId,\n ),\n );\n\n const cleartextParts = this.#values.map(({ value }) => pad(toHex(value), { size: 32 }));\n const cleartextBytes: `0x${string}` = cleartextParts.length > 0 ? concat(cleartextParts) : \"0x\";\n\n const signature = await INPUT_SIGNER.signTypedData({\n domain: INPUT_VERIFICATION_EIP712.domain(\n this.#config.gatewayChainId,\n this.#config.contracts.verifyingInputVerifier,\n ),\n types: {\n CiphertextVerification: [...INPUT_VERIFICATION_EIP712.types.CiphertextVerification],\n },\n primaryType: \"CiphertextVerification\",\n message: {\n ctHandles: handles as `0x${string}`[],\n userAddress: this.#userAddress as `0x${string}`,\n contractAddress: this.#contractAddress as `0x${string}`,\n contractChainId: this.#config.chainId,\n extraData: cleartextBytes,\n },\n });\n\n const inputProof = toBytes(\n concat([\n toHex(new Uint8Array([handles.length])),\n toHex(new Uint8Array([1])),\n ...handles.map((handle) => handle as `0x${string}`),\n signature,\n cleartextBytes,\n ]),\n );\n\n return {\n handles: handles.map((handle) => toBytes(handle as `0x${string}`)),\n inputProof,\n };\n }\n}\n","import { privateKeyToAccount } from \"viem/accounts\";\nimport { concat, getAddress, hexToBigInt, pad, parseAbi, toHex } from \"viem\";\nimport type { PublicClient } from \"viem\";\nimport { mainnet, sepolia } from \"viem/chains\";\nimport type { RelayerSDK } from \"../relayer-sdk\";\nimport type {\n Address,\n DecryptedValue,\n DelegatedUserDecryptParams,\n EIP712TypedData,\n EncryptInput,\n EncryptParams,\n EncryptResult,\n FHEKeypair,\n InputProofBytesType,\n KmsDelegatedUserDecryptEIP712Type,\n PublicDecryptResult,\n UserDecryptParams,\n ZKProofLike,\n} from \"../relayer-sdk.types\";\nimport {\n DELEGATED_USER_DECRYPT_EIP712,\n KMS_DECRYPTION_EIP712,\n USER_DECRYPT_EIP712,\n} from \"./eip712\";\nimport { CleartextEncryptedInput } from \"./encrypted-input\";\nimport { FheType, MOCK_KMS_SIGNER_PK } from \"./constants\";\nimport type { CleartextConfig } from \"./types\";\nimport {\n ConfigurationError,\n DecryptionFailedError,\n EncryptionFailedError,\n} from \"../../token/errors\";\n\nconst KMS_SIGNER = privateKeyToAccount(MOCK_KMS_SIGNER_PK as `0x${string}`);\n\nconst ACL_ABI = parseAbi([\n \"function persistAllowed(bytes32 handle, address account) view returns (bool)\",\n \"function isAllowedForDecryption(bytes32 handle) view returns (bool)\",\n \"function isHandleDelegatedForUserDecryption(address delegator, address delegate, address contractAddress, bytes32 handle) view returns (bool)\",\n]);\n\nconst EXECUTOR_ABI = parseAbi([\"function plaintexts(bytes32 handle) view returns (uint256)\"]);\n\nconst USER_DECRYPT_TYPES = {\n UserDecryptRequestVerification: USER_DECRYPT_EIP712.types\n .UserDecryptRequestVerification as unknown as Array<{ name: string; type: string }>,\n};\nconst DELEGATED_USER_DECRYPT_TYPES: KmsDelegatedUserDecryptEIP712Type[\"types\"] = {\n EIP712Domain: [\n { name: \"name\", type: \"string\" },\n { name: \"version\", type: \"string\" },\n { name: \"chainId\", type: \"uint256\" },\n { name: \"verifyingContract\", type: \"address\" },\n ],\n DelegatedUserDecryptRequestVerification:\n DELEGATED_USER_DECRYPT_EIP712.types.DelegatedUserDecryptRequestVerification,\n};\nconst KMS_DECRYPTION_TYPES = {\n PublicDecryptVerification: KMS_DECRYPTION_EIP712.types\n .PublicDecryptVerification as unknown as Array<{ name: string; type: string }>,\n};\n\ntype Hex = `0x${string}`;\n\nfunction normalizeHandle(handle: string): Hex {\n return toHex(hexToBigInt(handle as Hex), { size: 32 });\n}\n\nfunction decodeDecryptedValue(handle: string, rawValue: bigint): DecryptedValue {\n const typeByte = Number((BigInt(handle) >> 8n) & 0xffn);\n if (typeByte === FheType.Bool) return rawValue !== 0n;\n if (typeByte === FheType.Uint160) return toHex(rawValue, { size: 20 });\n return rawValue;\n}\n\nexport class CleartextFhevmInstance implements RelayerSDK {\n readonly #client: PublicClient;\n readonly #config: CleartextConfig;\n\n static readonly #FORBIDDEN_CHAIN_IDS = new Set([BigInt(mainnet.id), BigInt(sepolia.id)]);\n\n constructor(client: PublicClient, config: CleartextConfig) {\n if (CleartextFhevmInstance.#FORBIDDEN_CHAIN_IDS.has(config.chainId)) {\n throw new ConfigurationError(\n `Cleartext mode is not allowed on chain ${config.chainId}. ` +\n `It is intended for local development and testing only.`,\n );\n }\n this.#client = client;\n this.#config = config;\n }\n\n async generateKeypair(): Promise<FHEKeypair> {\n const publicKey = toHex(crypto.getRandomValues(new Uint8Array(32)));\n let privateKey = toHex(crypto.getRandomValues(new Uint8Array(32)));\n\n while (privateKey === publicKey) {\n privateKey = toHex(crypto.getRandomValues(new Uint8Array(32)));\n }\n\n return { publicKey, privateKey };\n }\n\n async createEIP712(\n publicKey: string,\n contractAddresses: Address[],\n startTimestamp: number,\n durationDays: number = 7,\n ): Promise<EIP712TypedData> {\n const domain: EIP712TypedData[\"domain\"] = {\n name: \"Decryption\",\n version: \"1\",\n chainId: Number(this.#config.chainId),\n verifyingContract: this.#config.contracts.verifyingDecryption as Address,\n };\n\n return {\n domain,\n types: USER_DECRYPT_TYPES,\n primaryType: \"UserDecryptRequestVerification\",\n message: {\n publicKey,\n contractAddresses,\n startTimestamp: BigInt(startTimestamp),\n durationDays: BigInt(durationDays),\n extraData: \"0x00\",\n },\n };\n }\n\n async encrypt(params: EncryptParams): Promise<EncryptResult> {\n const input = new CleartextEncryptedInput(\n params.contractAddress,\n params.userAddress,\n this.#config,\n );\n\n for (const entry of params.values) {\n const { value, type } = entry as EncryptInput;\n switch (type) {\n case \"ebool\":\n input.addBool(value);\n break;\n case \"euint8\":\n input.add8(value as bigint);\n break;\n case \"euint16\":\n input.add16(value as bigint);\n break;\n case \"euint32\":\n input.add32(value as bigint);\n break;\n case \"euint64\":\n input.add64(value as bigint);\n break;\n case \"euint128\":\n input.add128(value as bigint);\n break;\n case \"eaddress\":\n input.addAddress(value as unknown as string);\n break;\n case \"euint256\":\n input.add256(value as bigint);\n break;\n default:\n throw new EncryptionFailedError(`Unsupported FHE type: ${type satisfies never}`);\n }\n }\n\n return input.encrypt();\n }\n\n async userDecrypt(params: UserDecryptParams): Promise<Record<string, DecryptedValue>> {\n const normalizedSignerAddress = getAddress(params.signerAddress);\n const normalizedHandles = params.handles.map(normalizeHandle);\n\n const allowedResults = await Promise.all(\n normalizedHandles.map((handle) => this.#persistAllowed(handle, normalizedSignerAddress)),\n );\n const unauthorizedIndex = allowedResults.findIndex((isAllowed) => !isAllowed);\n if (unauthorizedIndex !== -1) {\n throw new DecryptionFailedError(\n `Handle ${normalizedHandles[unauthorizedIndex]!} is not authorized for user decrypt`,\n );\n }\n\n return this.#decryptHandles(normalizedHandles);\n }\n\n async publicDecrypt(handles: string[]): Promise<PublicDecryptResult> {\n const normalizedHandles = handles.map(normalizeHandle);\n\n const allowedResults = await Promise.all(\n normalizedHandles.map((handle) => this.#isAllowedForDecryption(handle)),\n );\n const unauthorizedIndex = allowedResults.findIndex((isAllowed) => !isAllowed);\n if (unauthorizedIndex !== -1) {\n throw new DecryptionFailedError(\n `Handle ${normalizedHandles[unauthorizedIndex]!} is not allowed for public decryption`,\n );\n }\n\n const orderedValues = await Promise.all(\n normalizedHandles.map((handle) => this.#readPlaintext(handle)),\n );\n const clearValues = Object.fromEntries(\n normalizedHandles.map((handle, index) => [handle, orderedValues[index]!]),\n ) as Record<string, bigint>;\n\n const abiEncodedClearValues = concat(orderedValues.map((v) => pad(toHex(v), { size: 32 })));\n\n const signature = await KMS_SIGNER.signTypedData({\n domain: KMS_DECRYPTION_EIP712.domain(\n this.#config.gatewayChainId,\n this.#config.contracts.verifyingDecryption,\n ) as {\n name: string;\n version: string;\n chainId: number | bigint;\n verifyingContract: Hex;\n },\n types: KMS_DECRYPTION_TYPES,\n primaryType: \"PublicDecryptVerification\",\n message: {\n ctHandles: normalizedHandles,\n decryptedResult: abiEncodedClearValues,\n extraData: \"0x\",\n },\n });\n\n const decryptionProof = concat([toHex(new Uint8Array([1])), signature]) as Address;\n\n return {\n clearValues,\n abiEncodedClearValues,\n decryptionProof,\n };\n }\n\n async createDelegatedUserDecryptEIP712(\n publicKey: string,\n contractAddresses: Address[],\n delegatorAddress: string,\n startTimestamp: number,\n durationDays: number = 7,\n ): Promise<KmsDelegatedUserDecryptEIP712Type> {\n const domain: KmsDelegatedUserDecryptEIP712Type[\"domain\"] = {\n name: \"Decryption\",\n version: \"1\",\n chainId: this.#config.chainId,\n verifyingContract: this.#config.contracts.verifyingDecryption as Address,\n };\n const message: KmsDelegatedUserDecryptEIP712Type[\"message\"] = {\n publicKey: publicKey as KmsDelegatedUserDecryptEIP712Type[\"message\"][\"publicKey\"],\n contractAddresses:\n contractAddresses as KmsDelegatedUserDecryptEIP712Type[\"message\"][\"contractAddresses\"],\n delegatorAddress:\n delegatorAddress as KmsDelegatedUserDecryptEIP712Type[\"message\"][\"delegatorAddress\"],\n startTimestamp: String(startTimestamp),\n durationDays: String(durationDays),\n extraData: \"0x00\" as KmsDelegatedUserDecryptEIP712Type[\"message\"][\"extraData\"],\n };\n\n return {\n domain,\n types: DELEGATED_USER_DECRYPT_TYPES,\n primaryType: \"DelegatedUserDecryptRequestVerification\",\n message,\n };\n }\n\n async delegatedUserDecrypt(\n params: DelegatedUserDecryptParams,\n ): Promise<Record<string, DecryptedValue>> {\n const normalizedHandles = params.handles.map(normalizeHandle);\n\n const delegatedResults = await Promise.all(\n normalizedHandles.map((handle) =>\n this.#isHandleDelegatedForUserDecryption(\n params.delegatorAddress,\n params.delegateAddress,\n params.contractAddress,\n handle,\n ),\n ),\n );\n const unauthorizedIndex = delegatedResults.findIndex((isDelegated) => !isDelegated);\n if (unauthorizedIndex !== -1) {\n throw new DecryptionFailedError(\n `Handle ${normalizedHandles[unauthorizedIndex]!} is not delegated for user decryption (delegator=${params.delegatorAddress}, delegate=${params.delegateAddress}, contract=${params.contractAddress})`,\n );\n }\n\n return this.#decryptHandles(normalizedHandles);\n }\n\n async requestZKProofVerification(_zkProof: ZKProofLike): Promise<InputProofBytesType> {\n throw new ConfigurationError(\"Not implemented in cleartext mode\");\n }\n\n async getPublicKey(): Promise<{ publicKeyId: string; publicKey: Uint8Array } | null> {\n return null;\n }\n\n async getPublicParams(\n _bits: number,\n ): Promise<{ publicParams: Uint8Array; publicParamsId: string } | null> {\n return null;\n }\n\n terminate(): void {\n // No resources to release in cleartext mode.\n }\n\n async #decryptHandles(normalizedHandles: Hex[]): Promise<Record<string, DecryptedValue>> {\n const values = await Promise.all(\n normalizedHandles.map((handle) => this.#readPlaintext(handle)),\n );\n\n return Object.fromEntries(\n normalizedHandles.map((handle, index) => [\n handle,\n decodeDecryptedValue(handle, values[index]!),\n ]),\n ) as Record<string, DecryptedValue>;\n }\n\n async #persistAllowed(handle: Hex, account: Hex): Promise<boolean> {\n return this.#client.readContract({\n address: this.#config.contracts.acl as Hex,\n abi: ACL_ABI,\n functionName: \"persistAllowed\",\n args: [handle, account],\n });\n }\n\n async #isAllowedForDecryption(handle: Hex): Promise<boolean> {\n return this.#client.readContract({\n address: this.#config.contracts.acl as Hex,\n abi: ACL_ABI,\n functionName: \"isAllowedForDecryption\",\n args: [handle],\n });\n }\n\n async #isHandleDelegatedForUserDecryption(\n delegator: string,\n delegate: string,\n contractAddress: string,\n handle: Hex,\n ): Promise<boolean> {\n return this.#client.readContract({\n address: this.#config.contracts.acl as Hex,\n abi: ACL_ABI,\n functionName: \"isHandleDelegatedForUserDecryption\",\n args: [delegator as Hex, delegate as Hex, contractAddress as Hex, handle],\n });\n }\n\n async #readPlaintext(handle: Hex): Promise<bigint> {\n return this.#client.readContract({\n address: this.#config.contracts.executor as Hex,\n abi: EXECUTOR_ABI,\n functionName: \"plaintexts\",\n args: [handle],\n });\n }\n}\n","import { createPublicClient, custom, http } from \"viem\";\nimport type { RelayerSDK } from \"../relayer-sdk\";\nimport { CleartextFhevmInstance } from \"./cleartext-fhevm-instance\";\nimport type { CleartextChainConfig } from \"./types\";\n\nexport function createCleartextRelayer(config: CleartextChainConfig): RelayerSDK {\n const client = createPublicClient({\n transport: typeof config.rpcUrl === \"string\" ? http(config.rpcUrl) : custom(config.rpcUrl),\n });\n\n const { rpcUrl: _, ...internalConfig } = config;\n\n return new CleartextFhevmInstance(client, internalConfig);\n}\n","import type { CleartextChainConfig } from \"./types\";\n\nexport const hoodi = {\n chainId: 560048n,\n gatewayChainId: 10901,\n rpcUrl: \"https://rpc.hoodi.ethpandaops.io\",\n contracts: {\n acl: \"0xCC505dF6f244AcBf12C915888eC046939a439dB5\",\n executor: \"0xfeec4C7e8Ad0Af0F42b594A453F4dB6da01e21D6\",\n kmsVerifier: \"0xdae4028BD86408F59Db79032EDd05170FE350c5d\",\n inputVerifier: \"0x2cD198C290d8AD6C5F2020C35F7b5868b74bDD55\",\n verifyingInputVerifier: \"0x812b06e1CDCE800494b79fFE4f925A504a9A9810\",\n verifyingDecryption: \"0x5ffdaAB0373E62E2ea2944776209aEf29E631A64\",\n },\n} satisfies CleartextChainConfig;\n"]}
|