@cofhe/sdk 0.2.0 → 0.3.0
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/CHANGELOG.md +36 -0
- package/chains/defineChain.ts +2 -2
- package/chains/types.ts +3 -3
- package/core/baseBuilder.ts +18 -18
- package/core/client.test.ts +155 -41
- package/core/client.ts +72 -32
- package/core/clientTypes.ts +28 -18
- package/core/config.test.ts +40 -33
- package/core/config.ts +56 -51
- package/core/consts.ts +22 -0
- package/core/decrypt/{MockQueryDecrypterAbi.ts → MockThresholdNetworkAbi.ts} +71 -21
- package/core/decrypt/cofheMocksDecryptForTx.ts +142 -0
- package/core/decrypt/{cofheMocksSealOutput.ts → cofheMocksDecryptForView.ts} +12 -14
- package/core/decrypt/decryptForTxBuilder.ts +340 -0
- package/core/decrypt/{decryptHandleBuilder.ts → decryptForViewBuilder.ts} +75 -42
- package/core/decrypt/tnDecrypt.ts +232 -0
- package/core/decrypt/tnSealOutputV1.ts +5 -5
- package/core/decrypt/tnSealOutputV2.ts +27 -27
- package/core/encrypt/cofheMocksZkVerifySign.ts +19 -26
- package/core/encrypt/encryptInputsBuilder.test.ts +57 -61
- package/core/encrypt/encryptInputsBuilder.ts +65 -42
- package/core/encrypt/zkPackProveVerify.ts +11 -11
- package/core/error.ts +18 -18
- package/core/fetchKeys.test.ts +3 -3
- package/core/fetchKeys.ts +3 -3
- package/core/index.ts +22 -11
- package/core/permits.test.ts +5 -6
- package/core/permits.ts +5 -4
- package/core/utils.ts +10 -10
- package/dist/chains.cjs +4 -7
- package/dist/chains.d.cts +12 -12
- package/dist/chains.d.ts +12 -12
- package/dist/chains.js +1 -1
- package/dist/{chunk-WGCRJCBR.js → chunk-2TPSCOW3.js} +820 -224
- package/dist/{chunk-UGBVZNRT.js → chunk-NWDKXBIP.js} +309 -189
- package/dist/{chunk-WEAZ25JO.js → chunk-TBLR7NNE.js} +4 -7
- package/dist/{clientTypes-5_1nwtUe.d.cts → clientTypes-6aTZPQ_4.d.ts} +233 -173
- package/dist/{clientTypes-Es7fyi65.d.ts → clientTypes-Bhq7pCSA.d.cts} +233 -173
- package/dist/core.cjs +1138 -418
- package/dist/core.d.cts +37 -24
- package/dist/core.d.ts +37 -24
- package/dist/core.js +3 -3
- package/dist/node.cjs +1082 -370
- package/dist/node.d.cts +12 -12
- package/dist/node.d.ts +12 -12
- package/dist/node.js +8 -8
- package/dist/{permit-fUSe6KKq.d.cts → permit-MZ502UBl.d.cts} +30 -33
- package/dist/{permit-fUSe6KKq.d.ts → permit-MZ502UBl.d.ts} +30 -33
- package/dist/permits.cjs +305 -187
- package/dist/permits.d.cts +111 -812
- package/dist/permits.d.ts +111 -812
- package/dist/permits.js +1 -1
- package/dist/types-YiAC4gig.d.cts +33 -0
- package/dist/types-YiAC4gig.d.ts +33 -0
- package/dist/web.cjs +1085 -373
- package/dist/web.d.cts +13 -13
- package/dist/web.d.ts +13 -13
- package/dist/web.js +10 -10
- package/node/client.test.ts +34 -34
- package/node/config.test.ts +11 -11
- package/node/encryptInputs.test.ts +29 -29
- package/node/index.ts +15 -15
- package/package.json +3 -3
- package/permits/localstorage.test.ts +9 -13
- package/permits/onchain-utils.ts +221 -0
- package/permits/permit.test.ts +51 -5
- package/permits/permit.ts +28 -74
- package/permits/store.test.ts +10 -50
- package/permits/store.ts +4 -14
- package/permits/test-utils.ts +10 -2
- package/permits/types.ts +22 -9
- package/permits/utils.ts +0 -4
- package/permits/validation.test.ts +29 -32
- package/permits/validation.ts +112 -194
- package/web/client.web.test.ts +34 -34
- package/web/config.web.test.ts +11 -11
- package/web/encryptInputs.web.test.ts +29 -29
- package/web/index.ts +19 -19
- package/web/worker.builder.web.test.ts +28 -28
- package/web/worker.config.web.test.ts +47 -47
- package/web/worker.output.web.test.ts +10 -10
- package/dist/types-KImPrEIe.d.cts +0 -48
- package/dist/types-KImPrEIe.d.ts +0 -48
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { type Permission } from '@/permits';
|
|
2
|
+
|
|
3
|
+
import { CofheError, CofheErrorCode } from '../error.js';
|
|
4
|
+
|
|
5
|
+
type TnDecryptResponse = {
|
|
6
|
+
// TN returns bytes in big-endian order, e.g. [0,0,0,42]
|
|
7
|
+
decrypted: number[];
|
|
8
|
+
signature: string;
|
|
9
|
+
encryption_type: number;
|
|
10
|
+
error_message: string | null;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function normalizeSignature(signature: unknown): string {
|
|
14
|
+
if (typeof signature !== 'string') {
|
|
15
|
+
throw new CofheError({
|
|
16
|
+
code: CofheErrorCode.DecryptReturnedNull,
|
|
17
|
+
message: 'decrypt response missing signature',
|
|
18
|
+
context: {
|
|
19
|
+
signature,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const trimmed = signature.trim();
|
|
25
|
+
if (trimmed.length === 0) {
|
|
26
|
+
throw new CofheError({
|
|
27
|
+
code: CofheErrorCode.DecryptReturnedNull,
|
|
28
|
+
message: 'decrypt response returned empty signature',
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// SDK uses "no-0x" signatures in mocks/tests; normalize to that format.
|
|
33
|
+
return trimmed.startsWith('0x') ? trimmed.slice(2) : trimmed;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseDecryptedBytesToBigInt(decrypted: unknown): bigint {
|
|
37
|
+
if (!Array.isArray(decrypted)) {
|
|
38
|
+
throw new CofheError({
|
|
39
|
+
code: CofheErrorCode.DecryptReturnedNull,
|
|
40
|
+
message: 'decrypt response field <decrypted> must be a byte array',
|
|
41
|
+
context: {
|
|
42
|
+
decrypted,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (decrypted.length === 0) {
|
|
48
|
+
throw new CofheError({
|
|
49
|
+
code: CofheErrorCode.DecryptReturnedNull,
|
|
50
|
+
message: 'decrypt response field <decrypted> was an empty byte array',
|
|
51
|
+
context: {
|
|
52
|
+
decrypted,
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let hex = '';
|
|
58
|
+
for (const b of decrypted as unknown[]) {
|
|
59
|
+
if (typeof b !== 'number' || !Number.isInteger(b) || b < 0 || b > 255) {
|
|
60
|
+
throw new CofheError({
|
|
61
|
+
code: CofheErrorCode.DecryptReturnedNull,
|
|
62
|
+
message: 'decrypt response field <decrypted> contained a non-byte value',
|
|
63
|
+
context: {
|
|
64
|
+
badElement: b,
|
|
65
|
+
decrypted,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
hex += b.toString(16).padStart(2, '0');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return BigInt(`0x${hex}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function assertTnDecryptResponse(value: unknown): TnDecryptResponse {
|
|
76
|
+
if (value == null || typeof value !== 'object') {
|
|
77
|
+
throw new CofheError({
|
|
78
|
+
code: CofheErrorCode.DecryptFailed,
|
|
79
|
+
message: 'decrypt response must be a JSON object',
|
|
80
|
+
context: {
|
|
81
|
+
value,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const v = value as Record<string, unknown>;
|
|
87
|
+
const decrypted = v.decrypted;
|
|
88
|
+
const signature = v.signature;
|
|
89
|
+
const encryptionType = v.encryption_type;
|
|
90
|
+
const errorMessage = v.error_message;
|
|
91
|
+
|
|
92
|
+
if (!Array.isArray(decrypted)) {
|
|
93
|
+
throw new CofheError({
|
|
94
|
+
code: CofheErrorCode.DecryptReturnedNull,
|
|
95
|
+
message: 'decrypt response missing <decrypted> byte array',
|
|
96
|
+
context: { decryptResponse: value },
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (typeof signature !== 'string') {
|
|
100
|
+
throw new CofheError({
|
|
101
|
+
code: CofheErrorCode.DecryptReturnedNull,
|
|
102
|
+
message: 'decrypt response missing <signature> string',
|
|
103
|
+
context: { decryptResponse: value },
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (typeof encryptionType !== 'number') {
|
|
107
|
+
throw new CofheError({
|
|
108
|
+
code: CofheErrorCode.DecryptFailed,
|
|
109
|
+
message: 'decrypt response missing <encryption_type> number',
|
|
110
|
+
context: { decryptResponse: value },
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (!(typeof errorMessage === 'string' || errorMessage === null)) {
|
|
114
|
+
throw new CofheError({
|
|
115
|
+
code: CofheErrorCode.DecryptFailed,
|
|
116
|
+
message: 'decrypt response field <error_message> must be string or null',
|
|
117
|
+
context: { decryptResponse: value },
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
decrypted: decrypted as number[],
|
|
123
|
+
signature,
|
|
124
|
+
encryption_type: encryptionType,
|
|
125
|
+
error_message: errorMessage,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function tnDecrypt(
|
|
130
|
+
ctHash: bigint,
|
|
131
|
+
chainId: number,
|
|
132
|
+
permission: Permission | null,
|
|
133
|
+
thresholdNetworkUrl: string
|
|
134
|
+
): Promise<{ decryptedValue: bigint; signature: string }> {
|
|
135
|
+
const body: {
|
|
136
|
+
ct_tempkey: string;
|
|
137
|
+
host_chain_id: number;
|
|
138
|
+
permit?: Permission;
|
|
139
|
+
} = {
|
|
140
|
+
ct_tempkey: ctHash.toString(16).padStart(64, '0'),
|
|
141
|
+
host_chain_id: chainId,
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
if (permission) {
|
|
145
|
+
body.permit = permission;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let response: Response;
|
|
149
|
+
try {
|
|
150
|
+
response = await fetch(`${thresholdNetworkUrl}/decrypt`, {
|
|
151
|
+
method: 'POST',
|
|
152
|
+
headers: {
|
|
153
|
+
'Content-Type': 'application/json',
|
|
154
|
+
},
|
|
155
|
+
body: JSON.stringify(body),
|
|
156
|
+
});
|
|
157
|
+
} catch (e) {
|
|
158
|
+
throw new CofheError({
|
|
159
|
+
code: CofheErrorCode.DecryptFailed,
|
|
160
|
+
message: `decrypt request failed`,
|
|
161
|
+
hint: 'Ensure the threshold network URL is valid and reachable.',
|
|
162
|
+
cause: e instanceof Error ? e : undefined,
|
|
163
|
+
context: {
|
|
164
|
+
thresholdNetworkUrl,
|
|
165
|
+
body,
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const responseText = await response.text();
|
|
171
|
+
|
|
172
|
+
// Even on non-200 responses, TN may return JSON with { error_message }.
|
|
173
|
+
if (!response.ok) {
|
|
174
|
+
let errorMessage = response.statusText || `HTTP ${response.status}`;
|
|
175
|
+
try {
|
|
176
|
+
const errorBody = JSON.parse(responseText) as Record<string, unknown>;
|
|
177
|
+
const maybeMessage = (errorBody.error_message || errorBody.message) as unknown;
|
|
178
|
+
if (typeof maybeMessage === 'string' && maybeMessage.length > 0) errorMessage = maybeMessage;
|
|
179
|
+
} catch {
|
|
180
|
+
const trimmed = responseText.trim();
|
|
181
|
+
if (trimmed.length > 0) errorMessage = trimmed;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
throw new CofheError({
|
|
185
|
+
code: CofheErrorCode.DecryptFailed,
|
|
186
|
+
message: `decrypt request failed: ${errorMessage}`,
|
|
187
|
+
hint: 'Check the threshold network URL and request parameters.',
|
|
188
|
+
context: {
|
|
189
|
+
thresholdNetworkUrl,
|
|
190
|
+
status: response.status,
|
|
191
|
+
statusText: response.statusText,
|
|
192
|
+
body,
|
|
193
|
+
responseText,
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let rawJson: unknown;
|
|
199
|
+
try {
|
|
200
|
+
rawJson = JSON.parse(responseText) as unknown;
|
|
201
|
+
} catch (e) {
|
|
202
|
+
throw new CofheError({
|
|
203
|
+
code: CofheErrorCode.DecryptFailed,
|
|
204
|
+
message: `Failed to parse decrypt response`,
|
|
205
|
+
cause: e instanceof Error ? e : undefined,
|
|
206
|
+
context: {
|
|
207
|
+
thresholdNetworkUrl,
|
|
208
|
+
body,
|
|
209
|
+
responseText,
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const decryptResponse = assertTnDecryptResponse(rawJson);
|
|
215
|
+
|
|
216
|
+
if (decryptResponse.error_message) {
|
|
217
|
+
throw new CofheError({
|
|
218
|
+
code: CofheErrorCode.DecryptFailed,
|
|
219
|
+
message: `decrypt request failed: ${decryptResponse.error_message}`,
|
|
220
|
+
context: {
|
|
221
|
+
thresholdNetworkUrl,
|
|
222
|
+
body,
|
|
223
|
+
decryptResponse,
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const decryptedValue = parseDecryptedBytesToBigInt(decryptResponse.decrypted);
|
|
229
|
+
const signature = normalizeSignature(decryptResponse.signature);
|
|
230
|
+
|
|
231
|
+
return { decryptedValue, signature };
|
|
232
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Permission, type EthEncryptedData } from '@/permits';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { CofheError, CofheErrorCode } from '../error.js';
|
|
4
4
|
|
|
5
5
|
export async function tnSealOutputV1(
|
|
6
6
|
ctHash: bigint,
|
|
@@ -31,8 +31,8 @@ export async function tnSealOutputV1(
|
|
|
31
31
|
sealed = sealOutputResult.sealed;
|
|
32
32
|
errorMessage = sealOutputResult.error_message;
|
|
33
33
|
} catch (e) {
|
|
34
|
-
throw new
|
|
35
|
-
code:
|
|
34
|
+
throw new CofheError({
|
|
35
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
36
36
|
message: `sealOutput request failed`,
|
|
37
37
|
hint: 'Ensure the threshold network URL is valid.',
|
|
38
38
|
cause: e instanceof Error ? e : undefined,
|
|
@@ -44,8 +44,8 @@ export async function tnSealOutputV1(
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
if (sealed == null) {
|
|
47
|
-
throw new
|
|
48
|
-
code:
|
|
47
|
+
throw new CofheError({
|
|
48
|
+
code: CofheErrorCode.SealOutputReturnedNull,
|
|
49
49
|
message: `sealOutput request returned no data | Caused by: ${errorMessage}`,
|
|
50
50
|
context: {
|
|
51
51
|
thresholdNetworkUrl,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Permission, type EthEncryptedData } from '@/permits';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { CofheError, CofheErrorCode } from '../error.js';
|
|
4
4
|
|
|
5
5
|
// Polling configuration
|
|
6
6
|
const POLL_INTERVAL_MS = 1000; // 1 second
|
|
@@ -39,8 +39,8 @@ function numberArrayToUint8Array(arr: number[]): Uint8Array {
|
|
|
39
39
|
*/
|
|
40
40
|
function convertSealedData(sealed: SealOutputStatusResponse['sealed']): EthEncryptedData {
|
|
41
41
|
if (!sealed) {
|
|
42
|
-
throw new
|
|
43
|
-
code:
|
|
42
|
+
throw new CofheError({
|
|
43
|
+
code: CofheErrorCode.SealOutputReturnedNull,
|
|
44
44
|
message: 'Sealed data is missing from completed response',
|
|
45
45
|
});
|
|
46
46
|
}
|
|
@@ -77,8 +77,8 @@ async function submitSealOutputRequest(
|
|
|
77
77
|
body: JSON.stringify(body),
|
|
78
78
|
});
|
|
79
79
|
} catch (e) {
|
|
80
|
-
throw new
|
|
81
|
-
code:
|
|
80
|
+
throw new CofheError({
|
|
81
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
82
82
|
message: `sealOutput request failed`,
|
|
83
83
|
hint: 'Ensure the threshold network URL is valid and reachable.',
|
|
84
84
|
cause: e instanceof Error ? e : undefined,
|
|
@@ -100,8 +100,8 @@ async function submitSealOutputRequest(
|
|
|
100
100
|
errorMessage = response.statusText || errorMessage;
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
throw new
|
|
104
|
-
code:
|
|
103
|
+
throw new CofheError({
|
|
104
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
105
105
|
message: `sealOutput request failed: ${errorMessage}`,
|
|
106
106
|
hint: 'Check the threshold network URL and request parameters.',
|
|
107
107
|
context: {
|
|
@@ -117,8 +117,8 @@ async function submitSealOutputRequest(
|
|
|
117
117
|
try {
|
|
118
118
|
submitResponse = (await response.json()) as SealOutputSubmitResponse;
|
|
119
119
|
} catch (e) {
|
|
120
|
-
throw new
|
|
121
|
-
code:
|
|
120
|
+
throw new CofheError({
|
|
121
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
122
122
|
message: `Failed to parse sealOutput submit response`,
|
|
123
123
|
cause: e instanceof Error ? e : undefined,
|
|
124
124
|
context: {
|
|
@@ -129,8 +129,8 @@ async function submitSealOutputRequest(
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
if (!submitResponse.request_id) {
|
|
132
|
-
throw new
|
|
133
|
-
code:
|
|
132
|
+
throw new CofheError({
|
|
133
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
134
134
|
message: `sealOutput submit response missing request_id`,
|
|
135
135
|
context: {
|
|
136
136
|
thresholdNetworkUrl,
|
|
@@ -153,8 +153,8 @@ async function pollSealOutputStatus(thresholdNetworkUrl: string, requestId: stri
|
|
|
153
153
|
while (!completed) {
|
|
154
154
|
// Check timeout
|
|
155
155
|
if (Date.now() - startTime > POLL_TIMEOUT_MS) {
|
|
156
|
-
throw new
|
|
157
|
-
code:
|
|
156
|
+
throw new CofheError({
|
|
157
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
158
158
|
message: `sealOutput polling timed out after ${POLL_TIMEOUT_MS}ms`,
|
|
159
159
|
hint: 'The request may still be processing. Try again later.',
|
|
160
160
|
context: {
|
|
@@ -174,8 +174,8 @@ async function pollSealOutputStatus(thresholdNetworkUrl: string, requestId: stri
|
|
|
174
174
|
},
|
|
175
175
|
});
|
|
176
176
|
} catch (e) {
|
|
177
|
-
throw new
|
|
178
|
-
code:
|
|
177
|
+
throw new CofheError({
|
|
178
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
179
179
|
message: `sealOutput status poll failed`,
|
|
180
180
|
hint: 'Ensure the threshold network URL is valid and reachable.',
|
|
181
181
|
cause: e instanceof Error ? e : undefined,
|
|
@@ -188,8 +188,8 @@ async function pollSealOutputStatus(thresholdNetworkUrl: string, requestId: stri
|
|
|
188
188
|
|
|
189
189
|
// Handle 404 - request not found
|
|
190
190
|
if (response.status === 404) {
|
|
191
|
-
throw new
|
|
192
|
-
code:
|
|
191
|
+
throw new CofheError({
|
|
192
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
193
193
|
message: `sealOutput request not found: ${requestId}`,
|
|
194
194
|
hint: 'The request may have expired or been invalid.',
|
|
195
195
|
context: {
|
|
@@ -209,8 +209,8 @@ async function pollSealOutputStatus(thresholdNetworkUrl: string, requestId: stri
|
|
|
209
209
|
errorMessage = response.statusText || errorMessage;
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
-
throw new
|
|
213
|
-
code:
|
|
212
|
+
throw new CofheError({
|
|
213
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
214
214
|
message: `sealOutput status poll failed: ${errorMessage}`,
|
|
215
215
|
context: {
|
|
216
216
|
thresholdNetworkUrl,
|
|
@@ -225,8 +225,8 @@ async function pollSealOutputStatus(thresholdNetworkUrl: string, requestId: stri
|
|
|
225
225
|
try {
|
|
226
226
|
statusResponse = (await response.json()) as SealOutputStatusResponse;
|
|
227
227
|
} catch (e) {
|
|
228
|
-
throw new
|
|
229
|
-
code:
|
|
228
|
+
throw new CofheError({
|
|
229
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
230
230
|
message: `Failed to parse sealOutput status response`,
|
|
231
231
|
cause: e instanceof Error ? e : undefined,
|
|
232
232
|
context: {
|
|
@@ -241,8 +241,8 @@ async function pollSealOutputStatus(thresholdNetworkUrl: string, requestId: stri
|
|
|
241
241
|
// Check if succeeded
|
|
242
242
|
if (statusResponse.is_succeed === false) {
|
|
243
243
|
const errorMessage = statusResponse.error_message || 'Unknown error';
|
|
244
|
-
throw new
|
|
245
|
-
code:
|
|
244
|
+
throw new CofheError({
|
|
245
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
246
246
|
message: `sealOutput request failed: ${errorMessage}`,
|
|
247
247
|
context: {
|
|
248
248
|
thresholdNetworkUrl,
|
|
@@ -254,8 +254,8 @@ async function pollSealOutputStatus(thresholdNetworkUrl: string, requestId: stri
|
|
|
254
254
|
|
|
255
255
|
// Check if sealed data exists
|
|
256
256
|
if (!statusResponse.sealed) {
|
|
257
|
-
throw new
|
|
258
|
-
code:
|
|
257
|
+
throw new CofheError({
|
|
258
|
+
code: CofheErrorCode.SealOutputReturnedNull,
|
|
259
259
|
message: `sealOutput request completed but returned no sealed data`,
|
|
260
260
|
context: {
|
|
261
261
|
thresholdNetworkUrl,
|
|
@@ -274,8 +274,8 @@ async function pollSealOutputStatus(thresholdNetworkUrl: string, requestId: stri
|
|
|
274
274
|
}
|
|
275
275
|
|
|
276
276
|
// This should never be reached, but TypeScript requires it
|
|
277
|
-
throw new
|
|
278
|
-
code:
|
|
277
|
+
throw new CofheError({
|
|
278
|
+
code: CofheErrorCode.SealOutputFailed,
|
|
279
279
|
message: 'Polling loop exited unexpectedly',
|
|
280
280
|
context: {
|
|
281
281
|
thresholdNetworkUrl,
|
|
@@ -12,16 +12,9 @@ import {
|
|
|
12
12
|
} from 'viem';
|
|
13
13
|
import { MockZkVerifierAbi } from './MockZkVerifierAbi.js';
|
|
14
14
|
import { hardhat } from 'viem/chains';
|
|
15
|
-
import {
|
|
15
|
+
import { CofheError, CofheErrorCode } from '../error.js';
|
|
16
16
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
17
|
-
|
|
18
|
-
// Address the Mock ZkVerifier contract is deployed to on the Hardhat chain
|
|
19
|
-
export const MocksZkVerifierAddress = '0x0000000000000000000000000000000000000100';
|
|
20
|
-
|
|
21
|
-
// PK & address pair for zk verifier
|
|
22
|
-
export const MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY =
|
|
23
|
-
'0x6C8D7F768A6BB4AAFE85E8A2F5A9680355239C7E14646ED62B044E39DE154512';
|
|
24
|
-
export const MOCKS_ZK_VERIFIER_SIGNER_ADDRESS = '0x6E12D8C87503D4287c294f2Fdef96ACd9DFf6bd2';
|
|
17
|
+
import { MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY, MOCKS_ZK_VERIFIER_ADDRESS } from '../consts.js';
|
|
25
18
|
|
|
26
19
|
type EncryptableItemWithCtHash = EncryptableItem & {
|
|
27
20
|
ctHash: bigint;
|
|
@@ -77,8 +70,8 @@ export async function cofheMocksCheckEncryptableBits(items: EncryptableItem[]):
|
|
|
77
70
|
}
|
|
78
71
|
}
|
|
79
72
|
if (totalBits > MAX_ENCRYPTABLE_BITS) {
|
|
80
|
-
throw new
|
|
81
|
-
code:
|
|
73
|
+
throw new CofheError({
|
|
74
|
+
code: CofheErrorCode.ZkPackFailed,
|
|
82
75
|
message: `Total bits ${totalBits} exceeds ${MAX_ENCRYPTABLE_BITS}`,
|
|
83
76
|
hint: `Ensure that the total bits of the items to encrypt does not exceed ${MAX_ENCRYPTABLE_BITS}`,
|
|
84
77
|
context: {
|
|
@@ -111,18 +104,18 @@ async function calcCtHashes(
|
|
|
111
104
|
|
|
112
105
|
try {
|
|
113
106
|
ctHashes = (await publicClient.readContract({
|
|
114
|
-
address:
|
|
107
|
+
address: MOCKS_ZK_VERIFIER_ADDRESS,
|
|
115
108
|
abi: MockZkVerifierAbi,
|
|
116
109
|
functionName: 'zkVerifyCalcCtHashesPacked',
|
|
117
110
|
args: calcCtHashesArgs,
|
|
118
111
|
})) as bigint[];
|
|
119
112
|
} catch (err) {
|
|
120
|
-
throw new
|
|
121
|
-
code:
|
|
113
|
+
throw new CofheError({
|
|
114
|
+
code: CofheErrorCode.ZkMocksCalcCtHashesFailed,
|
|
122
115
|
message: `mockZkVerifySign calcCtHashes failed while calling zkVerifyCalcCtHashesPacked`,
|
|
123
116
|
cause: err instanceof Error ? err : undefined,
|
|
124
117
|
context: {
|
|
125
|
-
address:
|
|
118
|
+
address: MOCKS_ZK_VERIFIER_ADDRESS,
|
|
126
119
|
items,
|
|
127
120
|
account,
|
|
128
121
|
securityZone,
|
|
@@ -133,8 +126,8 @@ async function calcCtHashes(
|
|
|
133
126
|
}
|
|
134
127
|
|
|
135
128
|
if (ctHashes.length !== items.length) {
|
|
136
|
-
throw new
|
|
137
|
-
code:
|
|
129
|
+
throw new CofheError({
|
|
130
|
+
code: CofheErrorCode.ZkMocksCalcCtHashesFailed,
|
|
138
131
|
message: `mockZkVerifySign calcCtHashes returned incorrect number of ctHashes`,
|
|
139
132
|
context: {
|
|
140
133
|
items,
|
|
@@ -163,7 +156,7 @@ async function insertCtHashes(items: EncryptableItemWithCtHash[], walletClient:
|
|
|
163
156
|
const account = walletClient.account!;
|
|
164
157
|
|
|
165
158
|
await walletClient.writeContract({
|
|
166
|
-
address:
|
|
159
|
+
address: MOCKS_ZK_VERIFIER_ADDRESS,
|
|
167
160
|
abi: MockZkVerifierAbi,
|
|
168
161
|
functionName: 'insertPackedCtHashes',
|
|
169
162
|
args: insertPackedCtHashesArgs,
|
|
@@ -171,8 +164,8 @@ async function insertCtHashes(items: EncryptableItemWithCtHash[], walletClient:
|
|
|
171
164
|
account: account,
|
|
172
165
|
});
|
|
173
166
|
} catch (err) {
|
|
174
|
-
throw new
|
|
175
|
-
code:
|
|
167
|
+
throw new CofheError({
|
|
168
|
+
code: CofheErrorCode.ZkMocksInsertCtHashesFailed,
|
|
176
169
|
message: `mockZkVerifySign insertPackedCtHashes failed while calling insertPackedCtHashes`,
|
|
177
170
|
cause: err instanceof Error ? err : undefined,
|
|
178
171
|
context: {
|
|
@@ -199,8 +192,8 @@ async function createProofSignatures(items: EncryptableItemWithCtHash[], securit
|
|
|
199
192
|
try {
|
|
200
193
|
encInputSignerClient = createMockZkVerifierSigner();
|
|
201
194
|
} catch (err) {
|
|
202
|
-
throw new
|
|
203
|
-
code:
|
|
195
|
+
throw new CofheError({
|
|
196
|
+
code: CofheErrorCode.ZkMocksCreateProofSignatureFailed,
|
|
204
197
|
message: `mockZkVerifySign createProofSignatures failed while creating wallet client`,
|
|
205
198
|
cause: err instanceof Error ? err : undefined,
|
|
206
199
|
context: {
|
|
@@ -227,8 +220,8 @@ async function createProofSignatures(items: EncryptableItemWithCtHash[], securit
|
|
|
227
220
|
signatures.push(signature);
|
|
228
221
|
}
|
|
229
222
|
} catch (err) {
|
|
230
|
-
throw new
|
|
231
|
-
code:
|
|
223
|
+
throw new CofheError({
|
|
224
|
+
code: CofheErrorCode.ZkMocksCreateProofSignatureFailed,
|
|
232
225
|
message: `mockZkVerifySign createProofSignatures failed while calling signMessage`,
|
|
233
226
|
cause: err instanceof Error ? err : undefined,
|
|
234
227
|
context: {
|
|
@@ -239,8 +232,8 @@ async function createProofSignatures(items: EncryptableItemWithCtHash[], securit
|
|
|
239
232
|
}
|
|
240
233
|
|
|
241
234
|
if (signatures.length !== items.length) {
|
|
242
|
-
throw new
|
|
243
|
-
code:
|
|
235
|
+
throw new CofheError({
|
|
236
|
+
code: CofheErrorCode.ZkMocksCreateProofSignatureFailed,
|
|
244
237
|
message: `mockZkVerifySign createProofSignatures returned incorrect number of signatures`,
|
|
245
238
|
context: {
|
|
246
239
|
items,
|