@cofhe/sdk 0.2.1 → 0.3.1
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 +34 -0
- package/core/baseBuilder.ts +18 -18
- package/core/client.test.ts +58 -55
- package/core/client.ts +50 -30
- package/core/clientTypes.ts +21 -17
- package/core/config.test.ts +32 -33
- package/core/config.ts +47 -48
- package/core/consts.ts +6 -2
- package/core/decrypt/{MockQueryDecrypterAbi.ts → MockThresholdNetworkAbi.ts} +71 -21
- package/core/decrypt/cofheMocksDecryptForTx.ts +142 -0
- package/core/decrypt/{cofheMocksSealOutput.ts → cofheMocksDecryptForView.ts} +12 -12
- 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 +15 -15
- 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 +14 -11
- package/core/utils.ts +10 -10
- package/dist/{chunk-I5WFEYXX.js → chunk-2TPSCOW3.js} +791 -209
- package/dist/{chunk-R3B5TMVX.js → chunk-NWDKXBIP.js} +3 -2
- package/dist/{clientTypes-RqkgkV2i.d.ts → clientTypes-6aTZPQ_4.d.ts} +204 -85
- package/dist/{clientTypes-e4filDzK.d.cts → clientTypes-Bhq7pCSA.d.cts} +204 -85
- package/dist/core.cjs +799 -214
- package/dist/core.d.cts +25 -23
- package/dist/core.d.ts +25 -23
- package/dist/core.js +2 -2
- package/dist/node.cjs +748 -165
- package/dist/node.d.cts +10 -10
- package/dist/node.d.ts +10 -10
- package/dist/node.js +7 -7
- package/dist/permits.js +1 -1
- package/dist/web.cjs +751 -168
- package/dist/web.d.cts +11 -11
- package/dist/web.d.ts +11 -11
- package/dist/web.js +9 -9
- 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/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/core.cjs
CHANGED
|
@@ -32,53 +32,53 @@ var nacl__namespace = /*#__PURE__*/_interopNamespace(nacl);
|
|
|
32
32
|
// core/client.ts
|
|
33
33
|
|
|
34
34
|
// core/error.ts
|
|
35
|
-
var
|
|
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
|
-
return
|
|
80
|
-
})(
|
|
81
|
-
var
|
|
35
|
+
var CofheErrorCode = /* @__PURE__ */ ((CofheErrorCode2) => {
|
|
36
|
+
CofheErrorCode2["InternalError"] = "INTERNAL_ERROR";
|
|
37
|
+
CofheErrorCode2["UnknownEnvironment"] = "UNKNOWN_ENVIRONMENT";
|
|
38
|
+
CofheErrorCode2["InitTfheFailed"] = "INIT_TFHE_FAILED";
|
|
39
|
+
CofheErrorCode2["InitViemFailed"] = "INIT_VIEM_FAILED";
|
|
40
|
+
CofheErrorCode2["InitEthersFailed"] = "INIT_ETHERS_FAILED";
|
|
41
|
+
CofheErrorCode2["NotConnected"] = "NOT_CONNECTED";
|
|
42
|
+
CofheErrorCode2["MissingPublicClient"] = "MISSING_PUBLIC_CLIENT";
|
|
43
|
+
CofheErrorCode2["MissingWalletClient"] = "MISSING_WALLET_CLIENT";
|
|
44
|
+
CofheErrorCode2["MissingProviderParam"] = "MISSING_PROVIDER_PARAM";
|
|
45
|
+
CofheErrorCode2["EmptySecurityZonesParam"] = "EMPTY_SECURITY_ZONES_PARAM";
|
|
46
|
+
CofheErrorCode2["InvalidPermitData"] = "INVALID_PERMIT_DATA";
|
|
47
|
+
CofheErrorCode2["InvalidPermitDomain"] = "INVALID_PERMIT_DOMAIN";
|
|
48
|
+
CofheErrorCode2["PermitNotFound"] = "PERMIT_NOT_FOUND";
|
|
49
|
+
CofheErrorCode2["CannotRemoveLastPermit"] = "CANNOT_REMOVE_LAST_PERMIT";
|
|
50
|
+
CofheErrorCode2["AccountUninitialized"] = "ACCOUNT_UNINITIALIZED";
|
|
51
|
+
CofheErrorCode2["ChainIdUninitialized"] = "CHAIN_ID_UNINITIALIZED";
|
|
52
|
+
CofheErrorCode2["SealOutputFailed"] = "SEAL_OUTPUT_FAILED";
|
|
53
|
+
CofheErrorCode2["SealOutputReturnedNull"] = "SEAL_OUTPUT_RETURNED_NULL";
|
|
54
|
+
CofheErrorCode2["InvalidUtype"] = "INVALID_UTYPE";
|
|
55
|
+
CofheErrorCode2["DecryptFailed"] = "DECRYPT_FAILED";
|
|
56
|
+
CofheErrorCode2["DecryptReturnedNull"] = "DECRYPT_RETURNED_NULL";
|
|
57
|
+
CofheErrorCode2["ZkMocksInsertCtHashesFailed"] = "ZK_MOCKS_INSERT_CT_HASHES_FAILED";
|
|
58
|
+
CofheErrorCode2["ZkMocksCalcCtHashesFailed"] = "ZK_MOCKS_CALC_CT_HASHES_FAILED";
|
|
59
|
+
CofheErrorCode2["ZkMocksVerifySignFailed"] = "ZK_MOCKS_VERIFY_SIGN_FAILED";
|
|
60
|
+
CofheErrorCode2["ZkMocksCreateProofSignatureFailed"] = "ZK_MOCKS_CREATE_PROOF_SIGNATURE_FAILED";
|
|
61
|
+
CofheErrorCode2["ZkVerifyFailed"] = "ZK_VERIFY_FAILED";
|
|
62
|
+
CofheErrorCode2["ZkPackFailed"] = "ZK_PACK_FAILED";
|
|
63
|
+
CofheErrorCode2["ZkProveFailed"] = "ZK_PROVE_FAILED";
|
|
64
|
+
CofheErrorCode2["EncryptRemainingInItems"] = "ENCRYPT_REMAINING_IN_ITEMS";
|
|
65
|
+
CofheErrorCode2["ZkUninitialized"] = "ZK_UNINITIALIZED";
|
|
66
|
+
CofheErrorCode2["ZkVerifierUrlUninitialized"] = "ZK_VERIFIER_URL_UNINITIALIZED";
|
|
67
|
+
CofheErrorCode2["ThresholdNetworkUrlUninitialized"] = "THRESHOLD_NETWORK_URL_UNINITIALIZED";
|
|
68
|
+
CofheErrorCode2["MissingConfig"] = "MISSING_CONFIG";
|
|
69
|
+
CofheErrorCode2["UnsupportedChain"] = "UNSUPPORTED_CHAIN";
|
|
70
|
+
CofheErrorCode2["MissingZkBuilderAndCrsGenerator"] = "MISSING_ZK_BUILDER_AND_CRS_GENERATOR";
|
|
71
|
+
CofheErrorCode2["MissingTfhePublicKeyDeserializer"] = "MISSING_TFHE_PUBLIC_KEY_DESERIALIZER";
|
|
72
|
+
CofheErrorCode2["MissingCompactPkeCrsDeserializer"] = "MISSING_COMPACT_PKE_CRS_DESERIALIZER";
|
|
73
|
+
CofheErrorCode2["MissingFheKey"] = "MISSING_FHE_KEY";
|
|
74
|
+
CofheErrorCode2["MissingCrs"] = "MISSING_CRS";
|
|
75
|
+
CofheErrorCode2["FetchKeysFailed"] = "FETCH_KEYS_FAILED";
|
|
76
|
+
CofheErrorCode2["PublicWalletGetChainIdFailed"] = "PUBLIC_WALLET_GET_CHAIN_ID_FAILED";
|
|
77
|
+
CofheErrorCode2["PublicWalletGetAddressesFailed"] = "PUBLIC_WALLET_GET_ADDRESSES_FAILED";
|
|
78
|
+
CofheErrorCode2["RehydrateKeysStoreFailed"] = "REHYDRATE_KEYS_STORE_FAILED";
|
|
79
|
+
return CofheErrorCode2;
|
|
80
|
+
})(CofheErrorCode || {});
|
|
81
|
+
var CofheError = class _CofheError extends Error {
|
|
82
82
|
code;
|
|
83
83
|
cause;
|
|
84
84
|
hint;
|
|
@@ -86,25 +86,25 @@ var CofhesdkError = class _CofhesdkError extends Error {
|
|
|
86
86
|
constructor({ code, message, cause, hint, context }) {
|
|
87
87
|
const fullMessage = cause ? `${message} | Caused by: ${cause.message}` : message;
|
|
88
88
|
super(fullMessage);
|
|
89
|
-
this.name = "
|
|
89
|
+
this.name = "CofheError";
|
|
90
90
|
this.code = code;
|
|
91
91
|
this.cause = cause;
|
|
92
92
|
this.hint = hint;
|
|
93
93
|
this.context = context;
|
|
94
94
|
if (Error.captureStackTrace) {
|
|
95
|
-
Error.captureStackTrace(this,
|
|
95
|
+
Error.captureStackTrace(this, _CofheError);
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
/**
|
|
99
|
-
* Creates a
|
|
100
|
-
* If the error is a
|
|
101
|
-
* If a wrapperError is provided, it is used to create the new
|
|
99
|
+
* Creates a CofheError from an unknown error
|
|
100
|
+
* If the error is a CofheError, it is returned unchanged, else a new CofheError is created
|
|
101
|
+
* If a wrapperError is provided, it is used to create the new CofheError, else a default is used
|
|
102
102
|
*/
|
|
103
103
|
static fromError(error, wrapperError) {
|
|
104
|
-
if (
|
|
104
|
+
if (isCofheError(error))
|
|
105
105
|
return error;
|
|
106
106
|
const cause = error instanceof Error ? error : new Error(`${error}`);
|
|
107
|
-
return new
|
|
107
|
+
return new _CofheError({
|
|
108
108
|
code: wrapperError?.code ?? "INTERNAL_ERROR" /* InternalError */,
|
|
109
109
|
message: wrapperError?.message ?? "An internal error occurred",
|
|
110
110
|
hint: wrapperError?.hint,
|
|
@@ -164,7 +164,7 @@ var bigintSafeJsonStringify = (value) => {
|
|
|
164
164
|
return value2;
|
|
165
165
|
});
|
|
166
166
|
};
|
|
167
|
-
var
|
|
167
|
+
var isCofheError = (error) => error instanceof CofheError;
|
|
168
168
|
|
|
169
169
|
// core/types.ts
|
|
170
170
|
var FheTypes = /* @__PURE__ */ ((FheTypes2) => {
|
|
@@ -315,14 +315,14 @@ async function getPublicClientChainID(publicClient) {
|
|
|
315
315
|
try {
|
|
316
316
|
chainId = publicClient.chain?.id ?? await publicClient.getChainId();
|
|
317
317
|
} catch (e) {
|
|
318
|
-
throw new
|
|
318
|
+
throw new CofheError({
|
|
319
319
|
code: "PUBLIC_WALLET_GET_CHAIN_ID_FAILED" /* PublicWalletGetChainIdFailed */,
|
|
320
320
|
message: "getting chain ID from public client failed",
|
|
321
321
|
cause: e instanceof Error ? e : void 0
|
|
322
322
|
});
|
|
323
323
|
}
|
|
324
324
|
if (chainId === null) {
|
|
325
|
-
throw new
|
|
325
|
+
throw new CofheError({
|
|
326
326
|
code: "PUBLIC_WALLET_GET_CHAIN_ID_FAILED" /* PublicWalletGetChainIdFailed */,
|
|
327
327
|
message: "chain ID from public client is null"
|
|
328
328
|
});
|
|
@@ -337,14 +337,14 @@ async function getWalletClientAccount(walletClient) {
|
|
|
337
337
|
address = (await walletClient.getAddresses())?.[0];
|
|
338
338
|
}
|
|
339
339
|
} catch (e) {
|
|
340
|
-
throw new
|
|
340
|
+
throw new CofheError({
|
|
341
341
|
code: "PUBLIC_WALLET_GET_ADDRESSES_FAILED" /* PublicWalletGetAddressesFailed */,
|
|
342
342
|
message: "getting address from wallet client failed",
|
|
343
343
|
cause: e instanceof Error ? e : void 0
|
|
344
344
|
});
|
|
345
345
|
}
|
|
346
346
|
if (!address) {
|
|
347
|
-
throw new
|
|
347
|
+
throw new CofheError({
|
|
348
348
|
code: "PUBLIC_WALLET_GET_ADDRESSES_FAILED" /* PublicWalletGetAddressesFailed */,
|
|
349
349
|
message: "address from wallet client is null"
|
|
350
350
|
});
|
|
@@ -448,7 +448,7 @@ var zkPack = (items, builder) => {
|
|
|
448
448
|
break;
|
|
449
449
|
}
|
|
450
450
|
default: {
|
|
451
|
-
throw new
|
|
451
|
+
throw new CofheError({
|
|
452
452
|
code: "ZK_PACK_FAILED" /* ZkPackFailed */,
|
|
453
453
|
message: `Invalid utype: ${item.utype}`,
|
|
454
454
|
hint: `Ensure that the utype is valid, using the Encryptable type, for example: Encryptable.uint128(100n)`,
|
|
@@ -460,7 +460,7 @@ var zkPack = (items, builder) => {
|
|
|
460
460
|
}
|
|
461
461
|
}
|
|
462
462
|
if (totalBits > MAX_ENCRYPTABLE_BITS) {
|
|
463
|
-
throw new
|
|
463
|
+
throw new CofheError({
|
|
464
464
|
code: "ZK_PACK_FAILED" /* ZkPackFailed */,
|
|
465
465
|
message: `Total bits ${totalBits} exceeds ${MAX_ENCRYPTABLE_BITS}`,
|
|
466
466
|
hint: `Ensure that the total bits of the items to encrypt does not exceed ${MAX_ENCRYPTABLE_BITS}`,
|
|
@@ -524,14 +524,14 @@ var zkVerify = async (verifierUrl, serializedBytes, address, securityZone, chain
|
|
|
524
524
|
});
|
|
525
525
|
if (!response.ok) {
|
|
526
526
|
const errorBody = await response.text();
|
|
527
|
-
throw new
|
|
527
|
+
throw new CofheError({
|
|
528
528
|
code: "ZK_VERIFY_FAILED" /* ZkVerifyFailed */,
|
|
529
529
|
message: `HTTP error! ZK proof verification failed - ${errorBody}`
|
|
530
530
|
});
|
|
531
531
|
}
|
|
532
532
|
const json = await response.json();
|
|
533
533
|
if (json.status !== "success") {
|
|
534
|
-
throw new
|
|
534
|
+
throw new CofheError({
|
|
535
535
|
code: "ZK_VERIFY_FAILED" /* ZkVerifyFailed */,
|
|
536
536
|
message: `ZK proof verification response malformed - ${json.error}`
|
|
537
537
|
});
|
|
@@ -543,7 +543,7 @@ var zkVerify = async (verifierUrl, serializedBytes, address, securityZone, chain
|
|
|
543
543
|
};
|
|
544
544
|
});
|
|
545
545
|
} catch (e) {
|
|
546
|
-
throw new
|
|
546
|
+
throw new CofheError({
|
|
547
547
|
code: "ZK_VERIFY_FAILED" /* ZkVerifyFailed */,
|
|
548
548
|
message: `ZK proof verification failed`,
|
|
549
549
|
cause: e instanceof Error ? e : void 0
|
|
@@ -665,10 +665,11 @@ var MockZkVerifierAbi = [
|
|
|
665
665
|
// core/consts.ts
|
|
666
666
|
var TASK_MANAGER_ADDRESS = "0xeA30c4B8b44078Bbf8a6ef5b9f1eC1626C7848D9";
|
|
667
667
|
var MOCKS_ZK_VERIFIER_ADDRESS = "0x0000000000000000000000000000000000005001";
|
|
668
|
-
var
|
|
668
|
+
var MOCKS_THRESHOLD_NETWORK_ADDRESS = "0x0000000000000000000000000000000000005002";
|
|
669
669
|
var TEST_BED_ADDRESS = "0x0000000000000000000000000000000000005003";
|
|
670
670
|
var MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY = "0x6C8D7F768A6BB4AAFE85E8A2F5A9680355239C7E14646ED62B044E39DE154512";
|
|
671
671
|
var MOCKS_ZK_VERIFIER_SIGNER_ADDRESS = "0x6E12D8C87503D4287c294f2Fdef96ACd9DFf6bd2";
|
|
672
|
+
var MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY = "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d";
|
|
672
673
|
|
|
673
674
|
// core/encrypt/cofheMocksZkVerifySign.ts
|
|
674
675
|
function createMockZkVerifierSigner() {
|
|
@@ -713,7 +714,7 @@ async function cofheMocksCheckEncryptableBits(items) {
|
|
|
713
714
|
}
|
|
714
715
|
}
|
|
715
716
|
if (totalBits > MAX_ENCRYPTABLE_BITS) {
|
|
716
|
-
throw new
|
|
717
|
+
throw new CofheError({
|
|
717
718
|
code: "ZK_PACK_FAILED" /* ZkPackFailed */,
|
|
718
719
|
message: `Total bits ${totalBits} exceeds ${MAX_ENCRYPTABLE_BITS}`,
|
|
719
720
|
hint: `Ensure that the total bits of the items to encrypt does not exceed ${MAX_ENCRYPTABLE_BITS}`,
|
|
@@ -742,7 +743,7 @@ async function calcCtHashes(items, account, securityZone, publicClient) {
|
|
|
742
743
|
args: calcCtHashesArgs
|
|
743
744
|
});
|
|
744
745
|
} catch (err) {
|
|
745
|
-
throw new
|
|
746
|
+
throw new CofheError({
|
|
746
747
|
code: "ZK_MOCKS_CALC_CT_HASHES_FAILED" /* ZkMocksCalcCtHashesFailed */,
|
|
747
748
|
message: `mockZkVerifySign calcCtHashes failed while calling zkVerifyCalcCtHashesPacked`,
|
|
748
749
|
cause: err instanceof Error ? err : void 0,
|
|
@@ -757,7 +758,7 @@ async function calcCtHashes(items, account, securityZone, publicClient) {
|
|
|
757
758
|
});
|
|
758
759
|
}
|
|
759
760
|
if (ctHashes.length !== items.length) {
|
|
760
|
-
throw new
|
|
761
|
+
throw new CofheError({
|
|
761
762
|
code: "ZK_MOCKS_CALC_CT_HASHES_FAILED" /* ZkMocksCalcCtHashesFailed */,
|
|
762
763
|
message: `mockZkVerifySign calcCtHashes returned incorrect number of ctHashes`,
|
|
763
764
|
context: {
|
|
@@ -788,7 +789,7 @@ async function insertCtHashes(items, walletClient) {
|
|
|
788
789
|
account
|
|
789
790
|
});
|
|
790
791
|
} catch (err) {
|
|
791
|
-
throw new
|
|
792
|
+
throw new CofheError({
|
|
792
793
|
code: "ZK_MOCKS_INSERT_CT_HASHES_FAILED" /* ZkMocksInsertCtHashesFailed */,
|
|
793
794
|
message: `mockZkVerifySign insertPackedCtHashes failed while calling insertPackedCtHashes`,
|
|
794
795
|
cause: err instanceof Error ? err : void 0,
|
|
@@ -806,7 +807,7 @@ async function createProofSignatures(items, securityZone) {
|
|
|
806
807
|
try {
|
|
807
808
|
encInputSignerClient = createMockZkVerifierSigner();
|
|
808
809
|
} catch (err) {
|
|
809
|
-
throw new
|
|
810
|
+
throw new CofheError({
|
|
810
811
|
code: "ZK_MOCKS_CREATE_PROOF_SIGNATURE_FAILED" /* ZkMocksCreateProofSignatureFailed */,
|
|
811
812
|
message: `mockZkVerifySign createProofSignatures failed while creating wallet client`,
|
|
812
813
|
cause: err instanceof Error ? err : void 0,
|
|
@@ -827,7 +828,7 @@ async function createProofSignatures(items, securityZone) {
|
|
|
827
828
|
signatures.push(signature);
|
|
828
829
|
}
|
|
829
830
|
} catch (err) {
|
|
830
|
-
throw new
|
|
831
|
+
throw new CofheError({
|
|
831
832
|
code: "ZK_MOCKS_CREATE_PROOF_SIGNATURE_FAILED" /* ZkMocksCreateProofSignatureFailed */,
|
|
832
833
|
message: `mockZkVerifySign createProofSignatures failed while calling signMessage`,
|
|
833
834
|
cause: err instanceof Error ? err : void 0,
|
|
@@ -838,7 +839,7 @@ async function createProofSignatures(items, securityZone) {
|
|
|
838
839
|
});
|
|
839
840
|
}
|
|
840
841
|
if (signatures.length !== items.length) {
|
|
841
|
-
throw new
|
|
842
|
+
throw new CofheError({
|
|
842
843
|
code: "ZK_MOCKS_CREATE_PROOF_SIGNATURE_FAILED" /* ZkMocksCreateProofSignatureFailed */,
|
|
843
844
|
message: `mockZkVerifySign createProofSignatures returned incorrect number of signatures`,
|
|
844
845
|
context: {
|
|
@@ -895,13 +896,11 @@ var hardhat2 = defineChain({
|
|
|
895
896
|
thresholdNetworkUrl: "http://127.0.0.1:3000",
|
|
896
897
|
environment: "MOCK"
|
|
897
898
|
});
|
|
898
|
-
var
|
|
899
|
+
var CofheConfigSchema = zod.z.object({
|
|
899
900
|
/** Environment that the SDK is running in */
|
|
900
901
|
environment: zod.z.enum(["node", "hardhat", "web", "react"]).optional().default("node"),
|
|
901
902
|
/** List of supported chain configurations */
|
|
902
903
|
supportedChains: zod.z.array(zod.z.custom()),
|
|
903
|
-
/** How permits are generated */
|
|
904
|
-
permitGeneration: zod.z.enum(["ON_CONNECT", "ON_DECRYPT_HANDLES", "MANUAL"]).optional().default("ON_CONNECT"),
|
|
905
904
|
/** Default permit expiration in seconds, default is 30 days */
|
|
906
905
|
defaultPermitExpiration: zod.z.number().optional().default(60 * 60 * 24 * 30),
|
|
907
906
|
/** Storage method for fhe keys (defaults to indexedDB on web, filesystem on node) */
|
|
@@ -920,27 +919,28 @@ var CofhesdkConfigSchema = zod.z.object({
|
|
|
920
919
|
useWorkers: zod.z.boolean().optional().default(true),
|
|
921
920
|
/** Mocks configs */
|
|
922
921
|
mocks: zod.z.object({
|
|
923
|
-
|
|
924
|
-
|
|
922
|
+
decryptDelay: zod.z.number().optional().default(0),
|
|
923
|
+
encryptDelay: zod.z.union([zod.z.number(), zod.z.tuple([zod.z.number(), zod.z.number(), zod.z.number(), zod.z.number(), zod.z.number()])]).optional().default([100, 100, 100, 500, 500])
|
|
924
|
+
}).optional().default({ decryptDelay: 0, encryptDelay: [100, 100, 100, 500, 500] }),
|
|
925
925
|
/** Internal configuration */
|
|
926
926
|
_internal: zod.z.object({
|
|
927
927
|
zkvWalletClient: zod.z.any().optional()
|
|
928
928
|
}).optional()
|
|
929
929
|
});
|
|
930
|
-
function
|
|
931
|
-
const result =
|
|
930
|
+
function createCofheConfigBase(config) {
|
|
931
|
+
const result = CofheConfigSchema.safeParse(config);
|
|
932
932
|
if (!result.success) {
|
|
933
|
-
throw new Error(`Invalid
|
|
933
|
+
throw new Error(`Invalid cofhe configuration: ${zod.z.prettifyError(result.error)}`, { cause: result.error });
|
|
934
934
|
}
|
|
935
935
|
return result.data;
|
|
936
936
|
}
|
|
937
|
-
var
|
|
937
|
+
var getCofheConfigItem = (config, key) => {
|
|
938
938
|
return config[key];
|
|
939
939
|
};
|
|
940
940
|
function getSupportedChainOrThrow(config, chainId) {
|
|
941
941
|
const supportedChain = config.supportedChains.find((chain) => chain.id === chainId);
|
|
942
942
|
if (!supportedChain) {
|
|
943
|
-
throw new
|
|
943
|
+
throw new CofheError({
|
|
944
944
|
code: "UNSUPPORTED_CHAIN" /* UnsupportedChain */,
|
|
945
945
|
message: `Config does not support chain <${chainId}>`,
|
|
946
946
|
hint: "Ensure config passed to client has been created with this chain in the config.supportedChains array.",
|
|
@@ -956,7 +956,7 @@ function getCoFheUrlOrThrow(config, chainId) {
|
|
|
956
956
|
const supportedChain = getSupportedChainOrThrow(config, chainId);
|
|
957
957
|
const url = supportedChain.coFheUrl;
|
|
958
958
|
if (!url) {
|
|
959
|
-
throw new
|
|
959
|
+
throw new CofheError({
|
|
960
960
|
code: "MISSING_CONFIG" /* MissingConfig */,
|
|
961
961
|
message: `CoFHE URL is not configured for chain <${chainId}>`,
|
|
962
962
|
hint: "Ensure this chain config includes a coFheUrl property.",
|
|
@@ -969,7 +969,7 @@ function getZkVerifierUrlOrThrow(config, chainId) {
|
|
|
969
969
|
const supportedChain = getSupportedChainOrThrow(config, chainId);
|
|
970
970
|
const url = supportedChain.verifierUrl;
|
|
971
971
|
if (!url) {
|
|
972
|
-
throw new
|
|
972
|
+
throw new CofheError({
|
|
973
973
|
code: "ZK_VERIFIER_URL_UNINITIALIZED" /* ZkVerifierUrlUninitialized */,
|
|
974
974
|
message: `ZK verifier URL is not configured for chain <${chainId}>`,
|
|
975
975
|
hint: "Ensure this chain config includes a verifierUrl property.",
|
|
@@ -982,7 +982,7 @@ function getThresholdNetworkUrlOrThrow(config, chainId) {
|
|
|
982
982
|
const supportedChain = getSupportedChainOrThrow(config, chainId);
|
|
983
983
|
const url = supportedChain.thresholdNetworkUrl;
|
|
984
984
|
if (!url) {
|
|
985
|
-
throw new
|
|
985
|
+
throw new CofheError({
|
|
986
986
|
code: "THRESHOLD_NETWORK_URL_UNINITIALIZED" /* ThresholdNetworkUrlUninitialized */,
|
|
987
987
|
message: `Threshold network URL is not configured for chain <${chainId}>`,
|
|
988
988
|
hint: "Ensure this chain config includes a thresholdNetworkUrl property.",
|
|
@@ -1194,7 +1194,7 @@ var BaseBuilder = class {
|
|
|
1194
1194
|
account;
|
|
1195
1195
|
constructor(params) {
|
|
1196
1196
|
if (!params.config) {
|
|
1197
|
-
throw new
|
|
1197
|
+
throw new CofheError({
|
|
1198
1198
|
code: "MISSING_CONFIG" /* MissingConfig */,
|
|
1199
1199
|
message: "Builder config is undefined",
|
|
1200
1200
|
hint: "Ensure client has been created with a config.",
|
|
@@ -1212,12 +1212,12 @@ var BaseBuilder = class {
|
|
|
1212
1212
|
}
|
|
1213
1213
|
/**
|
|
1214
1214
|
* Asserts that this.chainId is populated
|
|
1215
|
-
* @throws {
|
|
1215
|
+
* @throws {CofheError} If chainId is not set
|
|
1216
1216
|
*/
|
|
1217
1217
|
assertChainId() {
|
|
1218
1218
|
if (this.chainId)
|
|
1219
1219
|
return;
|
|
1220
|
-
throw new
|
|
1220
|
+
throw new CofheError({
|
|
1221
1221
|
code: "CHAIN_ID_UNINITIALIZED" /* ChainIdUninitialized */,
|
|
1222
1222
|
message: "Chain ID is not set",
|
|
1223
1223
|
hint: "Ensure client.connect() has been called and awaited, or use setChainId(...) to set the chainId explicitly.",
|
|
@@ -1228,12 +1228,12 @@ var BaseBuilder = class {
|
|
|
1228
1228
|
}
|
|
1229
1229
|
/**
|
|
1230
1230
|
* Asserts that this.account is populated
|
|
1231
|
-
* @throws {
|
|
1231
|
+
* @throws {CofheError} If account is not set
|
|
1232
1232
|
*/
|
|
1233
1233
|
assertAccount() {
|
|
1234
1234
|
if (this.account)
|
|
1235
1235
|
return;
|
|
1236
|
-
throw new
|
|
1236
|
+
throw new CofheError({
|
|
1237
1237
|
code: "ACCOUNT_UNINITIALIZED" /* AccountUninitialized */,
|
|
1238
1238
|
message: "Account is not set",
|
|
1239
1239
|
hint: "Ensure client.connect() has been called and awaited, or use setAccount(...) to set the account explicitly.",
|
|
@@ -1244,12 +1244,12 @@ var BaseBuilder = class {
|
|
|
1244
1244
|
}
|
|
1245
1245
|
/**
|
|
1246
1246
|
* Asserts that this.publicClient is populated
|
|
1247
|
-
* @throws {
|
|
1247
|
+
* @throws {CofheError} If publicClient is not set
|
|
1248
1248
|
*/
|
|
1249
1249
|
assertPublicClient() {
|
|
1250
1250
|
if (this.publicClient)
|
|
1251
1251
|
return;
|
|
1252
|
-
throw new
|
|
1252
|
+
throw new CofheError({
|
|
1253
1253
|
code: "MISSING_PUBLIC_CLIENT" /* MissingPublicClient */,
|
|
1254
1254
|
message: "Public client not found",
|
|
1255
1255
|
hint: "Ensure client.connect() has been called with a publicClient.",
|
|
@@ -1260,12 +1260,12 @@ var BaseBuilder = class {
|
|
|
1260
1260
|
}
|
|
1261
1261
|
/**
|
|
1262
1262
|
* Asserts that this.walletClient is populated
|
|
1263
|
-
* @throws {
|
|
1263
|
+
* @throws {CofheError} If walletClient is not set
|
|
1264
1264
|
*/
|
|
1265
1265
|
assertWalletClient() {
|
|
1266
1266
|
if (this.walletClient)
|
|
1267
1267
|
return;
|
|
1268
|
-
throw new
|
|
1268
|
+
throw new CofheError({
|
|
1269
1269
|
code: "MISSING_WALLET_CLIENT" /* MissingWalletClient */,
|
|
1270
1270
|
message: "Wallet client not found",
|
|
1271
1271
|
hint: "Ensure client.connect() has been called with a walletClient.",
|
|
@@ -1310,7 +1310,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1310
1310
|
this.securityZone = params.securityZone ?? 0;
|
|
1311
1311
|
this.zkvWalletClient = params.zkvWalletClient;
|
|
1312
1312
|
if (!params.tfhePublicKeyDeserializer) {
|
|
1313
|
-
throw new
|
|
1313
|
+
throw new CofheError({
|
|
1314
1314
|
code: "MISSING_TFHE_PUBLIC_KEY_DESERIALIZER" /* MissingTfhePublicKeyDeserializer */,
|
|
1315
1315
|
message: "EncryptInputsBuilder tfhePublicKeyDeserializer is undefined",
|
|
1316
1316
|
hint: "Ensure client has been created with a tfhePublicKeyDeserializer.",
|
|
@@ -1321,7 +1321,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1321
1321
|
}
|
|
1322
1322
|
this.tfhePublicKeyDeserializer = params.tfhePublicKeyDeserializer;
|
|
1323
1323
|
if (!params.compactPkeCrsDeserializer) {
|
|
1324
|
-
throw new
|
|
1324
|
+
throw new CofheError({
|
|
1325
1325
|
code: "MISSING_COMPACT_PKE_CRS_DESERIALIZER" /* MissingCompactPkeCrsDeserializer */,
|
|
1326
1326
|
message: "EncryptInputsBuilder compactPkeCrsDeserializer is undefined",
|
|
1327
1327
|
hint: "Ensure client has been created with a compactPkeCrsDeserializer.",
|
|
@@ -1332,7 +1332,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1332
1332
|
}
|
|
1333
1333
|
this.compactPkeCrsDeserializer = params.compactPkeCrsDeserializer;
|
|
1334
1334
|
if (!params.zkBuilderAndCrsGenerator) {
|
|
1335
|
-
throw new
|
|
1335
|
+
throw new CofheError({
|
|
1336
1336
|
code: "MISSING_ZK_BUILDER_AND_CRS_GENERATOR" /* MissingZkBuilderAndCrsGenerator */,
|
|
1337
1337
|
message: "EncryptInputsBuilder zkBuilderAndCrsGenerator is undefined",
|
|
1338
1338
|
hint: "Ensure client has been created with a zkBuilderAndCrsGenerator.",
|
|
@@ -1356,7 +1356,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1356
1356
|
* ```typescript
|
|
1357
1357
|
* const encrypted = await encryptInputs([Encryptable.uint128(10n)])
|
|
1358
1358
|
* .setAccount("0x123")
|
|
1359
|
-
* .
|
|
1359
|
+
* .execute();
|
|
1360
1360
|
* ```
|
|
1361
1361
|
*
|
|
1362
1362
|
* @returns The chainable EncryptInputsBuilder instance.
|
|
@@ -1377,7 +1377,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1377
1377
|
* ```typescript
|
|
1378
1378
|
* const encrypted = await encryptInputs([Encryptable.uint128(10n)])
|
|
1379
1379
|
* .setChainId(11155111)
|
|
1380
|
-
* .
|
|
1380
|
+
* .execute();
|
|
1381
1381
|
* ```
|
|
1382
1382
|
*
|
|
1383
1383
|
* @returns The chainable EncryptInputsBuilder instance.
|
|
@@ -1398,7 +1398,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1398
1398
|
* ```typescript
|
|
1399
1399
|
* const encrypted = await encryptInputs([Encryptable.uint128(10n)])
|
|
1400
1400
|
* .setSecurityZone(1)
|
|
1401
|
-
* .
|
|
1401
|
+
* .execute();
|
|
1402
1402
|
* ```
|
|
1403
1403
|
*
|
|
1404
1404
|
* @returns The chainable EncryptInputsBuilder instance.
|
|
@@ -1419,7 +1419,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1419
1419
|
* ```typescript
|
|
1420
1420
|
* const encrypted = await encryptInputs([Encryptable.uint128(10n)])
|
|
1421
1421
|
* .setUseWorker(false)
|
|
1422
|
-
* .
|
|
1422
|
+
* .execute();
|
|
1423
1423
|
* ```
|
|
1424
1424
|
*
|
|
1425
1425
|
* @returns The chainable EncryptInputsBuilder instance.
|
|
@@ -1453,13 +1453,13 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1453
1453
|
* Example:
|
|
1454
1454
|
* ```typescript
|
|
1455
1455
|
* const encrypted = await encryptInputs([Encryptable.uint128(10n)])
|
|
1456
|
-
* .
|
|
1457
|
-
* .
|
|
1456
|
+
* .onStep((step: EncryptStep) => console.log(step))
|
|
1457
|
+
* .execute();
|
|
1458
1458
|
* ```
|
|
1459
1459
|
*
|
|
1460
1460
|
* @returns The EncryptInputsBuilder instance.
|
|
1461
1461
|
*/
|
|
1462
|
-
|
|
1462
|
+
onStep(callback) {
|
|
1463
1463
|
this.stepCallback = callback;
|
|
1464
1464
|
return this;
|
|
1465
1465
|
}
|
|
@@ -1482,7 +1482,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1482
1482
|
this.stepCallback(step, { ...context, isStart: false, isEnd: true, duration });
|
|
1483
1483
|
}
|
|
1484
1484
|
/**
|
|
1485
|
-
* zkVerifierUrl is included in the chains exported from
|
|
1485
|
+
* zkVerifierUrl is included in the chains exported from @cofhe/sdk/chains for use in CofheConfig.supportedChains
|
|
1486
1486
|
* Users should generally not set this manually.
|
|
1487
1487
|
*/
|
|
1488
1488
|
async getZkVerifierUrl() {
|
|
@@ -1490,7 +1490,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1490
1490
|
return getZkVerifierUrlOrThrow(this.config, this.chainId);
|
|
1491
1491
|
}
|
|
1492
1492
|
/**
|
|
1493
|
-
* initTfhe is a platform-specific dependency injected into core/
|
|
1493
|
+
* initTfhe is a platform-specific dependency injected into core/createCofheClientBase by web/createCofheClient and node/createCofheClient
|
|
1494
1494
|
* web/ uses zama "tfhe"
|
|
1495
1495
|
* node/ uses zama "node-tfhe"
|
|
1496
1496
|
* Users should not set this manually.
|
|
@@ -1501,7 +1501,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1501
1501
|
try {
|
|
1502
1502
|
return await this.initTfhe();
|
|
1503
1503
|
} catch (error) {
|
|
1504
|
-
throw
|
|
1504
|
+
throw CofheError.fromError(error, {
|
|
1505
1505
|
code: "INIT_TFHE_FAILED" /* InitTfheFailed */,
|
|
1506
1506
|
message: `Failed to initialize TFHE`,
|
|
1507
1507
|
context: {
|
|
@@ -1520,7 +1520,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1520
1520
|
try {
|
|
1521
1521
|
await this.keysStorage?.rehydrateKeysStore();
|
|
1522
1522
|
} catch (error) {
|
|
1523
|
-
throw
|
|
1523
|
+
throw CofheError.fromError(error, {
|
|
1524
1524
|
code: "REHYDRATE_KEYS_STORE_FAILED" /* RehydrateKeysStoreFailed */,
|
|
1525
1525
|
message: `Failed to rehydrate keys store`,
|
|
1526
1526
|
context: {
|
|
@@ -1542,7 +1542,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1542
1542
|
this.keysStorage
|
|
1543
1543
|
);
|
|
1544
1544
|
} catch (error) {
|
|
1545
|
-
throw
|
|
1545
|
+
throw CofheError.fromError(error, {
|
|
1546
1546
|
code: "FETCH_KEYS_FAILED" /* FetchKeysFailed */,
|
|
1547
1547
|
message: `Failed to fetch FHE key and CRS`,
|
|
1548
1548
|
context: {
|
|
@@ -1555,7 +1555,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1555
1555
|
});
|
|
1556
1556
|
}
|
|
1557
1557
|
if (!fheKey) {
|
|
1558
|
-
throw new
|
|
1558
|
+
throw new CofheError({
|
|
1559
1559
|
code: "MISSING_FHE_KEY" /* MissingFheKey */,
|
|
1560
1560
|
message: `FHE key not found`,
|
|
1561
1561
|
context: {
|
|
@@ -1565,7 +1565,7 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1565
1565
|
});
|
|
1566
1566
|
}
|
|
1567
1567
|
if (!crs) {
|
|
1568
|
-
throw new
|
|
1568
|
+
throw new CofheError({
|
|
1569
1569
|
code: "MISSING_CRS" /* MissingCrs */,
|
|
1570
1570
|
message: `CRS not found for chainId <${this.chainId}>`,
|
|
1571
1571
|
context: {
|
|
@@ -1575,6 +1575,17 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1575
1575
|
}
|
|
1576
1576
|
return { fheKey, fheKeyFetchedFromCoFHE, crs, crsFetchedFromCoFHE };
|
|
1577
1577
|
}
|
|
1578
|
+
/**
|
|
1579
|
+
* Resolves the encryptDelay config into an array of 5 per-step delays.
|
|
1580
|
+
* A single number is broadcast to all steps; a tuple is used as-is.
|
|
1581
|
+
*/
|
|
1582
|
+
resolveEncryptDelays() {
|
|
1583
|
+
const encryptDelay = this.config?.mocks?.encryptDelay ?? [100, 100, 100, 500, 500];
|
|
1584
|
+
if (typeof encryptDelay === "number") {
|
|
1585
|
+
return [encryptDelay, encryptDelay, encryptDelay, encryptDelay, encryptDelay];
|
|
1586
|
+
}
|
|
1587
|
+
return encryptDelay;
|
|
1588
|
+
}
|
|
1578
1589
|
/**
|
|
1579
1590
|
* @dev Encrypt against the cofheMocks instead of CoFHE
|
|
1580
1591
|
*
|
|
@@ -1582,25 +1593,35 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1582
1593
|
* cofheMocksInsertPackedHashes - stores the ctHashes and their plaintext values for on-chain mocking of FHE operations.
|
|
1583
1594
|
* cofheMocksZkCreateProofSignatures - creates signatures to be included in the encrypted inputs. The signers address is known and verified in the mock contracts.
|
|
1584
1595
|
*/
|
|
1585
|
-
async
|
|
1596
|
+
async mocksExecute() {
|
|
1586
1597
|
this.assertAccount();
|
|
1587
1598
|
this.assertPublicClient();
|
|
1588
1599
|
this.assertWalletClient();
|
|
1600
|
+
const [initTfheDelay, fetchKeysDelay, packDelay, proveDelay, verifyDelay] = this.resolveEncryptDelays();
|
|
1589
1601
|
this.fireStepStart("initTfhe" /* InitTfhe */);
|
|
1590
|
-
await sleep(
|
|
1591
|
-
this.fireStepEnd("initTfhe" /* InitTfhe */, {
|
|
1602
|
+
await sleep(initTfheDelay);
|
|
1603
|
+
this.fireStepEnd("initTfhe" /* InitTfhe */, {
|
|
1604
|
+
tfheInitializationExecuted: false,
|
|
1605
|
+
isMocks: true,
|
|
1606
|
+
mockSleep: initTfheDelay
|
|
1607
|
+
});
|
|
1592
1608
|
this.fireStepStart("fetchKeys" /* FetchKeys */);
|
|
1593
|
-
await sleep(
|
|
1594
|
-
this.fireStepEnd("fetchKeys" /* FetchKeys */, {
|
|
1609
|
+
await sleep(fetchKeysDelay);
|
|
1610
|
+
this.fireStepEnd("fetchKeys" /* FetchKeys */, {
|
|
1611
|
+
fheKeyFetchedFromCoFHE: false,
|
|
1612
|
+
crsFetchedFromCoFHE: false,
|
|
1613
|
+
isMocks: true,
|
|
1614
|
+
mockSleep: fetchKeysDelay
|
|
1615
|
+
});
|
|
1595
1616
|
this.fireStepStart("pack" /* Pack */);
|
|
1596
1617
|
await cofheMocksCheckEncryptableBits(this.inputItems);
|
|
1597
|
-
await sleep(
|
|
1598
|
-
this.fireStepEnd("pack" /* Pack
|
|
1618
|
+
await sleep(packDelay);
|
|
1619
|
+
this.fireStepEnd("pack" /* Pack */, { isMocks: true, mockSleep: packDelay });
|
|
1599
1620
|
this.fireStepStart("prove" /* Prove */);
|
|
1600
|
-
await sleep(
|
|
1601
|
-
this.fireStepEnd("prove" /* Prove
|
|
1621
|
+
await sleep(proveDelay);
|
|
1622
|
+
this.fireStepEnd("prove" /* Prove */, { isMocks: true, mockSleep: proveDelay });
|
|
1602
1623
|
this.fireStepStart("verify" /* Verify */);
|
|
1603
|
-
await sleep(
|
|
1624
|
+
await sleep(verifyDelay);
|
|
1604
1625
|
const signedResults = await cofheMocksZkVerifySign(
|
|
1605
1626
|
this.inputItems,
|
|
1606
1627
|
this.account,
|
|
@@ -1615,13 +1636,13 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1615
1636
|
utype: this.inputItems[index].utype,
|
|
1616
1637
|
signature
|
|
1617
1638
|
}));
|
|
1618
|
-
this.fireStepEnd("verify" /* Verify
|
|
1639
|
+
this.fireStepEnd("verify" /* Verify */, { isMocks: true, mockSleep: verifyDelay });
|
|
1619
1640
|
return encryptedInputs;
|
|
1620
1641
|
}
|
|
1621
1642
|
/**
|
|
1622
1643
|
* In the production context, perform a true encryption with the CoFHE coprocessor.
|
|
1623
1644
|
*/
|
|
1624
|
-
async
|
|
1645
|
+
async productionExecute() {
|
|
1625
1646
|
this.assertAccount();
|
|
1626
1647
|
this.assertChainId();
|
|
1627
1648
|
this.fireStepStart("initTfhe" /* InitTfhe */);
|
|
@@ -1684,15 +1705,15 @@ var EncryptInputsBuilder = class extends BaseBuilder {
|
|
|
1684
1705
|
* const encrypted = await encryptInputs([Encryptable.uint128(10n)])
|
|
1685
1706
|
* .setAccount('0x123...890') // optional
|
|
1686
1707
|
* .setChainId(11155111) // optional
|
|
1687
|
-
* .
|
|
1708
|
+
* .execute(); // execute
|
|
1688
1709
|
* ```
|
|
1689
1710
|
*
|
|
1690
1711
|
* @returns The encrypted inputs.
|
|
1691
1712
|
*/
|
|
1692
|
-
async
|
|
1713
|
+
async execute() {
|
|
1693
1714
|
if (this.chainId === chains.hardhat.id)
|
|
1694
|
-
return this.
|
|
1695
|
-
return this.
|
|
1715
|
+
return this.mocksExecute();
|
|
1716
|
+
return this.productionExecute();
|
|
1696
1717
|
}
|
|
1697
1718
|
};
|
|
1698
1719
|
|
|
@@ -2714,8 +2735,8 @@ var convertViaUtype = (utype, value) => {
|
|
|
2714
2735
|
}
|
|
2715
2736
|
};
|
|
2716
2737
|
|
|
2717
|
-
// core/decrypt/
|
|
2718
|
-
var
|
|
2738
|
+
// core/decrypt/MockThresholdNetworkAbi.ts
|
|
2739
|
+
var MockThresholdNetworkAbi = [
|
|
2719
2740
|
{
|
|
2720
2741
|
type: "function",
|
|
2721
2742
|
name: "acl",
|
|
@@ -2762,11 +2783,7 @@ var MockQueryDecrypterAbi = [
|
|
|
2762
2783
|
{ name: "expiration", type: "uint64", internalType: "uint64" },
|
|
2763
2784
|
{ name: "recipient", type: "address", internalType: "address" },
|
|
2764
2785
|
{ name: "validatorId", type: "uint256", internalType: "uint256" },
|
|
2765
|
-
{
|
|
2766
|
-
name: "validatorContract",
|
|
2767
|
-
type: "address",
|
|
2768
|
-
internalType: "address"
|
|
2769
|
-
},
|
|
2786
|
+
{ name: "validatorContract", type: "address", internalType: "address" },
|
|
2770
2787
|
{ name: "sealingKey", type: "bytes32", internalType: "bytes32" },
|
|
2771
2788
|
{ name: "issuerSignature", type: "bytes", internalType: "bytes" },
|
|
2772
2789
|
{ name: "recipientSignature", type: "bytes", internalType: "bytes" }
|
|
@@ -2795,11 +2812,7 @@ var MockQueryDecrypterAbi = [
|
|
|
2795
2812
|
{ name: "expiration", type: "uint64", internalType: "uint64" },
|
|
2796
2813
|
{ name: "recipient", type: "address", internalType: "address" },
|
|
2797
2814
|
{ name: "validatorId", type: "uint256", internalType: "uint256" },
|
|
2798
|
-
{
|
|
2799
|
-
name: "validatorContract",
|
|
2800
|
-
type: "address",
|
|
2801
|
-
internalType: "address"
|
|
2802
|
-
},
|
|
2815
|
+
{ name: "validatorContract", type: "address", internalType: "address" },
|
|
2803
2816
|
{ name: "sealingKey", type: "bytes32", internalType: "bytes32" },
|
|
2804
2817
|
{ name: "issuerSignature", type: "bytes", internalType: "bytes" },
|
|
2805
2818
|
{ name: "recipientSignature", type: "bytes", internalType: "bytes" }
|
|
@@ -2823,13 +2836,6 @@ var MockQueryDecrypterAbi = [
|
|
|
2823
2836
|
outputs: [{ name: "", type: "bytes32", internalType: "bytes32" }],
|
|
2824
2837
|
stateMutability: "pure"
|
|
2825
2838
|
},
|
|
2826
|
-
{
|
|
2827
|
-
type: "function",
|
|
2828
|
-
name: "taskManager",
|
|
2829
|
-
inputs: [],
|
|
2830
|
-
outputs: [{ name: "", type: "address", internalType: "contract TaskManager" }],
|
|
2831
|
-
stateMutability: "view"
|
|
2832
|
-
},
|
|
2833
2839
|
{
|
|
2834
2840
|
type: "function",
|
|
2835
2841
|
name: "unseal",
|
|
@@ -2840,15 +2846,80 @@ var MockQueryDecrypterAbi = [
|
|
|
2840
2846
|
outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
|
|
2841
2847
|
stateMutability: "pure"
|
|
2842
2848
|
},
|
|
2843
|
-
{
|
|
2844
|
-
|
|
2845
|
-
|
|
2849
|
+
{
|
|
2850
|
+
type: "function",
|
|
2851
|
+
name: "mockAcl",
|
|
2852
|
+
inputs: [],
|
|
2853
|
+
outputs: [{ name: "", type: "address", internalType: "contract MockACL" }],
|
|
2854
|
+
stateMutability: "view"
|
|
2855
|
+
},
|
|
2856
|
+
{
|
|
2857
|
+
type: "function",
|
|
2858
|
+
name: "mockTaskManager",
|
|
2859
|
+
inputs: [],
|
|
2860
|
+
outputs: [{ name: "", type: "address", internalType: "contract MockTaskManager" }],
|
|
2861
|
+
stateMutability: "view"
|
|
2862
|
+
},
|
|
2863
|
+
{
|
|
2864
|
+
type: "function",
|
|
2865
|
+
name: "mockQueryDecrypt",
|
|
2866
|
+
inputs: [
|
|
2867
|
+
{ name: "ctHash", type: "uint256", internalType: "uint256" },
|
|
2868
|
+
{ name: "", type: "uint256", internalType: "uint256" },
|
|
2869
|
+
{ name: "issuer", type: "address", internalType: "address" }
|
|
2870
|
+
],
|
|
2871
|
+
outputs: [
|
|
2872
|
+
{ name: "allowed", type: "bool", internalType: "bool" },
|
|
2873
|
+
{ name: "error", type: "string", internalType: "string" },
|
|
2874
|
+
{ name: "", type: "uint256", internalType: "uint256" }
|
|
2875
|
+
],
|
|
2876
|
+
stateMutability: "view"
|
|
2877
|
+
},
|
|
2878
|
+
{
|
|
2879
|
+
type: "function",
|
|
2880
|
+
name: "decryptForTxWithPermit",
|
|
2881
|
+
inputs: [
|
|
2882
|
+
{ name: "ctHash", type: "uint256", internalType: "uint256" },
|
|
2883
|
+
{
|
|
2884
|
+
name: "permission",
|
|
2885
|
+
type: "tuple",
|
|
2886
|
+
internalType: "struct Permission",
|
|
2887
|
+
components: [
|
|
2888
|
+
{ name: "issuer", type: "address", internalType: "address" },
|
|
2889
|
+
{ name: "expiration", type: "uint64", internalType: "uint64" },
|
|
2890
|
+
{ name: "recipient", type: "address", internalType: "address" },
|
|
2891
|
+
{ name: "validatorId", type: "uint256", internalType: "uint256" },
|
|
2892
|
+
{ name: "validatorContract", type: "address", internalType: "address" },
|
|
2893
|
+
{ name: "sealingKey", type: "bytes32", internalType: "bytes32" },
|
|
2894
|
+
{ name: "issuerSignature", type: "bytes", internalType: "bytes" },
|
|
2895
|
+
{ name: "recipientSignature", type: "bytes", internalType: "bytes" }
|
|
2896
|
+
]
|
|
2897
|
+
}
|
|
2898
|
+
],
|
|
2899
|
+
outputs: [
|
|
2900
|
+
{ name: "allowed", type: "bool", internalType: "bool" },
|
|
2901
|
+
{ name: "error", type: "string", internalType: "string" },
|
|
2902
|
+
{ name: "decryptedValue", type: "uint256", internalType: "uint256" }
|
|
2903
|
+
],
|
|
2904
|
+
stateMutability: "view"
|
|
2905
|
+
},
|
|
2906
|
+
{
|
|
2907
|
+
type: "function",
|
|
2908
|
+
name: "decryptForTxWithoutPermit",
|
|
2909
|
+
inputs: [{ name: "ctHash", type: "uint256", internalType: "uint256" }],
|
|
2910
|
+
outputs: [
|
|
2911
|
+
{ name: "allowed", type: "bool", internalType: "bool" },
|
|
2912
|
+
{ name: "error", type: "string", internalType: "string" },
|
|
2913
|
+
{ name: "decryptedValue", type: "uint256", internalType: "uint256" }
|
|
2914
|
+
],
|
|
2915
|
+
stateMutability: "view"
|
|
2916
|
+
}
|
|
2846
2917
|
];
|
|
2847
2918
|
|
|
2848
|
-
// core/decrypt/
|
|
2849
|
-
async function
|
|
2850
|
-
if (
|
|
2851
|
-
await sleep(
|
|
2919
|
+
// core/decrypt/cofheMocksDecryptForView.ts
|
|
2920
|
+
async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient, mocksDecryptDelay) {
|
|
2921
|
+
if (mocksDecryptDelay > 0)
|
|
2922
|
+
await sleep(mocksDecryptDelay);
|
|
2852
2923
|
const permission = PermitUtils.getPermission(permit, true);
|
|
2853
2924
|
const permissionWithBigInts = {
|
|
2854
2925
|
...permission,
|
|
@@ -2856,19 +2927,19 @@ async function cofheMocksSealOutput(ctHash, utype, permit, publicClient, mocksSe
|
|
|
2856
2927
|
validatorId: BigInt(permission.validatorId)
|
|
2857
2928
|
};
|
|
2858
2929
|
const [allowed, error, result] = await publicClient.readContract({
|
|
2859
|
-
address:
|
|
2860
|
-
abi:
|
|
2930
|
+
address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
|
|
2931
|
+
abi: MockThresholdNetworkAbi,
|
|
2861
2932
|
functionName: "querySealOutput",
|
|
2862
2933
|
args: [ctHash, BigInt(utype), permissionWithBigInts]
|
|
2863
2934
|
});
|
|
2864
2935
|
if (error != "") {
|
|
2865
|
-
throw new
|
|
2936
|
+
throw new CofheError({
|
|
2866
2937
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
2867
2938
|
message: `mocks querySealOutput call failed: ${error}`
|
|
2868
2939
|
});
|
|
2869
2940
|
}
|
|
2870
2941
|
if (allowed == false) {
|
|
2871
|
-
throw new
|
|
2942
|
+
throw new CofheError({
|
|
2872
2943
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
2873
2944
|
message: `mocks querySealOutput call failed: ACL Access Denied (NotAllowed)`
|
|
2874
2945
|
});
|
|
@@ -2887,7 +2958,7 @@ function numberArrayToUint8Array(arr) {
|
|
|
2887
2958
|
}
|
|
2888
2959
|
function convertSealedData(sealed) {
|
|
2889
2960
|
if (!sealed) {
|
|
2890
|
-
throw new
|
|
2961
|
+
throw new CofheError({
|
|
2891
2962
|
code: "SEAL_OUTPUT_RETURNED_NULL" /* SealOutputReturnedNull */,
|
|
2892
2963
|
message: "Sealed data is missing from completed response"
|
|
2893
2964
|
});
|
|
@@ -2914,7 +2985,7 @@ async function submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, per
|
|
|
2914
2985
|
body: JSON.stringify(body)
|
|
2915
2986
|
});
|
|
2916
2987
|
} catch (e) {
|
|
2917
|
-
throw new
|
|
2988
|
+
throw new CofheError({
|
|
2918
2989
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
2919
2990
|
message: `sealOutput request failed`,
|
|
2920
2991
|
hint: "Ensure the threshold network URL is valid and reachable.",
|
|
@@ -2933,7 +3004,7 @@ async function submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, per
|
|
|
2933
3004
|
} catch {
|
|
2934
3005
|
errorMessage = response.statusText || errorMessage;
|
|
2935
3006
|
}
|
|
2936
|
-
throw new
|
|
3007
|
+
throw new CofheError({
|
|
2937
3008
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
2938
3009
|
message: `sealOutput request failed: ${errorMessage}`,
|
|
2939
3010
|
hint: "Check the threshold network URL and request parameters.",
|
|
@@ -2949,7 +3020,7 @@ async function submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, per
|
|
|
2949
3020
|
try {
|
|
2950
3021
|
submitResponse = await response.json();
|
|
2951
3022
|
} catch (e) {
|
|
2952
|
-
throw new
|
|
3023
|
+
throw new CofheError({
|
|
2953
3024
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
2954
3025
|
message: `Failed to parse sealOutput submit response`,
|
|
2955
3026
|
cause: e instanceof Error ? e : void 0,
|
|
@@ -2960,7 +3031,7 @@ async function submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, per
|
|
|
2960
3031
|
});
|
|
2961
3032
|
}
|
|
2962
3033
|
if (!submitResponse.request_id) {
|
|
2963
|
-
throw new
|
|
3034
|
+
throw new CofheError({
|
|
2964
3035
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
2965
3036
|
message: `sealOutput submit response missing request_id`,
|
|
2966
3037
|
context: {
|
|
@@ -2977,7 +3048,7 @@ async function pollSealOutputStatus(thresholdNetworkUrl, requestId) {
|
|
|
2977
3048
|
let completed = false;
|
|
2978
3049
|
while (!completed) {
|
|
2979
3050
|
if (Date.now() - startTime > POLL_TIMEOUT_MS) {
|
|
2980
|
-
throw new
|
|
3051
|
+
throw new CofheError({
|
|
2981
3052
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
2982
3053
|
message: `sealOutput polling timed out after ${POLL_TIMEOUT_MS}ms`,
|
|
2983
3054
|
hint: "The request may still be processing. Try again later.",
|
|
@@ -2997,7 +3068,7 @@ async function pollSealOutputStatus(thresholdNetworkUrl, requestId) {
|
|
|
2997
3068
|
}
|
|
2998
3069
|
});
|
|
2999
3070
|
} catch (e) {
|
|
3000
|
-
throw new
|
|
3071
|
+
throw new CofheError({
|
|
3001
3072
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
3002
3073
|
message: `sealOutput status poll failed`,
|
|
3003
3074
|
hint: "Ensure the threshold network URL is valid and reachable.",
|
|
@@ -3009,7 +3080,7 @@ async function pollSealOutputStatus(thresholdNetworkUrl, requestId) {
|
|
|
3009
3080
|
});
|
|
3010
3081
|
}
|
|
3011
3082
|
if (response.status === 404) {
|
|
3012
|
-
throw new
|
|
3083
|
+
throw new CofheError({
|
|
3013
3084
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
3014
3085
|
message: `sealOutput request not found: ${requestId}`,
|
|
3015
3086
|
hint: "The request may have expired or been invalid.",
|
|
@@ -3027,7 +3098,7 @@ async function pollSealOutputStatus(thresholdNetworkUrl, requestId) {
|
|
|
3027
3098
|
} catch {
|
|
3028
3099
|
errorMessage = response.statusText || errorMessage;
|
|
3029
3100
|
}
|
|
3030
|
-
throw new
|
|
3101
|
+
throw new CofheError({
|
|
3031
3102
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
3032
3103
|
message: `sealOutput status poll failed: ${errorMessage}`,
|
|
3033
3104
|
context: {
|
|
@@ -3042,7 +3113,7 @@ async function pollSealOutputStatus(thresholdNetworkUrl, requestId) {
|
|
|
3042
3113
|
try {
|
|
3043
3114
|
statusResponse = await response.json();
|
|
3044
3115
|
} catch (e) {
|
|
3045
|
-
throw new
|
|
3116
|
+
throw new CofheError({
|
|
3046
3117
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
3047
3118
|
message: `Failed to parse sealOutput status response`,
|
|
3048
3119
|
cause: e instanceof Error ? e : void 0,
|
|
@@ -3055,7 +3126,7 @@ async function pollSealOutputStatus(thresholdNetworkUrl, requestId) {
|
|
|
3055
3126
|
if (statusResponse.status === "COMPLETED") {
|
|
3056
3127
|
if (statusResponse.is_succeed === false) {
|
|
3057
3128
|
const errorMessage = statusResponse.error_message || "Unknown error";
|
|
3058
|
-
throw new
|
|
3129
|
+
throw new CofheError({
|
|
3059
3130
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
3060
3131
|
message: `sealOutput request failed: ${errorMessage}`,
|
|
3061
3132
|
context: {
|
|
@@ -3066,7 +3137,7 @@ async function pollSealOutputStatus(thresholdNetworkUrl, requestId) {
|
|
|
3066
3137
|
});
|
|
3067
3138
|
}
|
|
3068
3139
|
if (!statusResponse.sealed) {
|
|
3069
|
-
throw new
|
|
3140
|
+
throw new CofheError({
|
|
3070
3141
|
code: "SEAL_OUTPUT_RETURNED_NULL" /* SealOutputReturnedNull */,
|
|
3071
3142
|
message: `sealOutput request completed but returned no sealed data`,
|
|
3072
3143
|
context: {
|
|
@@ -3080,7 +3151,7 @@ async function pollSealOutputStatus(thresholdNetworkUrl, requestId) {
|
|
|
3080
3151
|
}
|
|
3081
3152
|
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
|
|
3082
3153
|
}
|
|
3083
|
-
throw new
|
|
3154
|
+
throw new CofheError({
|
|
3084
3155
|
code: "SEAL_OUTPUT_FAILED" /* SealOutputFailed */,
|
|
3085
3156
|
message: "Polling loop exited unexpectedly",
|
|
3086
3157
|
context: {
|
|
@@ -3093,9 +3164,99 @@ async function tnSealOutputV2(ctHash, chainId, permission, thresholdNetworkUrl)
|
|
|
3093
3164
|
const requestId = await submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, permission);
|
|
3094
3165
|
return await pollSealOutputStatus(thresholdNetworkUrl, requestId);
|
|
3095
3166
|
}
|
|
3167
|
+
async function cofheMocksDecryptForTx(ctHash, utype, permit, publicClient, mocksDecryptForTxDelay) {
|
|
3168
|
+
if (mocksDecryptForTxDelay > 0)
|
|
3169
|
+
await sleep(mocksDecryptForTxDelay);
|
|
3170
|
+
if (permit !== null) {
|
|
3171
|
+
let permission = PermitUtils.getPermission(permit, true);
|
|
3172
|
+
const permissionWithBigInts = {
|
|
3173
|
+
...permission,
|
|
3174
|
+
expiration: BigInt(permission.expiration),
|
|
3175
|
+
validatorId: BigInt(permission.validatorId)
|
|
3176
|
+
};
|
|
3177
|
+
const [allowed2, error2, result2] = await publicClient.readContract({
|
|
3178
|
+
address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
|
|
3179
|
+
abi: MockThresholdNetworkAbi,
|
|
3180
|
+
functionName: "decryptForTxWithPermit",
|
|
3181
|
+
args: [ctHash, permissionWithBigInts]
|
|
3182
|
+
});
|
|
3183
|
+
if (error2 != "") {
|
|
3184
|
+
throw new CofheError({
|
|
3185
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3186
|
+
message: `mocks decryptForTx call failed: ${error2}`
|
|
3187
|
+
});
|
|
3188
|
+
}
|
|
3189
|
+
if (allowed2 == false) {
|
|
3190
|
+
throw new CofheError({
|
|
3191
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3192
|
+
message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
|
|
3193
|
+
});
|
|
3194
|
+
}
|
|
3195
|
+
const chainId2 = await publicClient.getChainId();
|
|
3196
|
+
const ctHashBigInt2 = BigInt(ctHash);
|
|
3197
|
+
const resultBigInt2 = BigInt(result2);
|
|
3198
|
+
const encryptionType2 = Number((ctHashBigInt2 & 0x7fn << 8n) >> 8n);
|
|
3199
|
+
const ctHashBytes322 = viem.pad(viem.toHex(ctHashBigInt2), { size: 32 });
|
|
3200
|
+
const packed2 = viem.encodePacked(
|
|
3201
|
+
["uint256", "uint32", "uint64", "bytes32"],
|
|
3202
|
+
[resultBigInt2, encryptionType2, BigInt(chainId2), ctHashBytes322]
|
|
3203
|
+
);
|
|
3204
|
+
const messageHash2 = viem.keccak256(packed2);
|
|
3205
|
+
const signatureHex2 = await accounts.sign({
|
|
3206
|
+
hash: messageHash2,
|
|
3207
|
+
privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
|
|
3208
|
+
to: "hex"
|
|
3209
|
+
});
|
|
3210
|
+
const signature2 = signatureHex2.slice(2);
|
|
3211
|
+
return {
|
|
3212
|
+
ctHash,
|
|
3213
|
+
decryptedValue: BigInt(result2),
|
|
3214
|
+
signature: signature2
|
|
3215
|
+
};
|
|
3216
|
+
}
|
|
3217
|
+
const [allowed, error, result] = await publicClient.readContract({
|
|
3218
|
+
address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
|
|
3219
|
+
abi: MockThresholdNetworkAbi,
|
|
3220
|
+
functionName: "decryptForTxWithoutPermit",
|
|
3221
|
+
args: [ctHash]
|
|
3222
|
+
});
|
|
3223
|
+
if (error != "") {
|
|
3224
|
+
throw new CofheError({
|
|
3225
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3226
|
+
message: `mocks decryptForTx call failed: ${error}`
|
|
3227
|
+
});
|
|
3228
|
+
}
|
|
3229
|
+
if (allowed == false) {
|
|
3230
|
+
throw new CofheError({
|
|
3231
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3232
|
+
message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
|
|
3233
|
+
});
|
|
3234
|
+
}
|
|
3235
|
+
const chainId = await publicClient.getChainId();
|
|
3236
|
+
const ctHashBigInt = BigInt(ctHash);
|
|
3237
|
+
const resultBigInt = BigInt(result);
|
|
3238
|
+
const encryptionType = Number((ctHashBigInt & 0x7fn << 8n) >> 8n);
|
|
3239
|
+
const ctHashBytes32 = viem.pad(viem.toHex(ctHashBigInt), { size: 32 });
|
|
3240
|
+
const packed = viem.encodePacked(
|
|
3241
|
+
["uint256", "uint32", "uint64", "bytes32"],
|
|
3242
|
+
[resultBigInt, encryptionType, BigInt(chainId), ctHashBytes32]
|
|
3243
|
+
);
|
|
3244
|
+
const messageHash = viem.keccak256(packed);
|
|
3245
|
+
const signatureHex = await accounts.sign({
|
|
3246
|
+
hash: messageHash,
|
|
3247
|
+
privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
|
|
3248
|
+
to: "hex"
|
|
3249
|
+
});
|
|
3250
|
+
const signature = signatureHex.slice(2);
|
|
3251
|
+
return {
|
|
3252
|
+
ctHash,
|
|
3253
|
+
decryptedValue: BigInt(result),
|
|
3254
|
+
signature
|
|
3255
|
+
};
|
|
3256
|
+
}
|
|
3096
3257
|
|
|
3097
|
-
// core/decrypt/
|
|
3098
|
-
var
|
|
3258
|
+
// core/decrypt/decryptForViewBuilder.ts
|
|
3259
|
+
var DecryptForViewBuilder = class extends BaseBuilder {
|
|
3099
3260
|
ctHash;
|
|
3100
3261
|
utype;
|
|
3101
3262
|
permitHash;
|
|
@@ -3121,12 +3282,12 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3121
3282
|
*
|
|
3122
3283
|
* Example:
|
|
3123
3284
|
* ```typescript
|
|
3124
|
-
* const unsealed = await
|
|
3285
|
+
* const unsealed = await client.decryptForView(ctHash, utype)
|
|
3125
3286
|
* .setChainId(11155111)
|
|
3126
|
-
* .
|
|
3287
|
+
* .execute();
|
|
3127
3288
|
* ```
|
|
3128
3289
|
*
|
|
3129
|
-
* @returns The chainable
|
|
3290
|
+
* @returns The chainable DecryptForViewBuilder instance.
|
|
3130
3291
|
*/
|
|
3131
3292
|
setChainId(chainId) {
|
|
3132
3293
|
this.chainId = chainId;
|
|
@@ -3142,12 +3303,12 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3142
3303
|
*
|
|
3143
3304
|
* Example:
|
|
3144
3305
|
* ```typescript
|
|
3145
|
-
* const unsealed = await
|
|
3306
|
+
* const unsealed = await client.decryptForView(ctHash, utype)
|
|
3146
3307
|
* .setAccount('0x1234567890123456789012345678901234567890')
|
|
3147
|
-
* .
|
|
3308
|
+
* .execute();
|
|
3148
3309
|
* ```
|
|
3149
3310
|
*
|
|
3150
|
-
* @returns The chainable
|
|
3311
|
+
* @returns The chainable DecryptForViewBuilder instance.
|
|
3151
3312
|
*/
|
|
3152
3313
|
setAccount(account) {
|
|
3153
3314
|
this.account = account;
|
|
@@ -3156,6 +3317,19 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3156
3317
|
getAccount() {
|
|
3157
3318
|
return this.account;
|
|
3158
3319
|
}
|
|
3320
|
+
withPermit(permitOrPermitHash) {
|
|
3321
|
+
if (typeof permitOrPermitHash === "string") {
|
|
3322
|
+
this.permitHash = permitOrPermitHash;
|
|
3323
|
+
this.permit = void 0;
|
|
3324
|
+
} else if (permitOrPermitHash === void 0) {
|
|
3325
|
+
this.permitHash = void 0;
|
|
3326
|
+
this.permit = void 0;
|
|
3327
|
+
} else {
|
|
3328
|
+
this.permit = permitOrPermitHash;
|
|
3329
|
+
this.permitHash = void 0;
|
|
3330
|
+
}
|
|
3331
|
+
return this;
|
|
3332
|
+
}
|
|
3159
3333
|
/**
|
|
3160
3334
|
* @param permitHash - Permit hash to decrypt values from. Used to fetch the correct permit.
|
|
3161
3335
|
*
|
|
@@ -3164,16 +3338,16 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3164
3338
|
*
|
|
3165
3339
|
* Example:
|
|
3166
3340
|
* ```typescript
|
|
3167
|
-
* const unsealed = await
|
|
3341
|
+
* const unsealed = await client.decryptForView(ctHash, utype)
|
|
3168
3342
|
* .setPermitHash('0x1234567890123456789012345678901234567890')
|
|
3169
|
-
* .
|
|
3343
|
+
* .execute();
|
|
3170
3344
|
* ```
|
|
3171
3345
|
*
|
|
3172
|
-
* @returns The chainable
|
|
3346
|
+
* @returns The chainable DecryptForViewBuilder instance.
|
|
3173
3347
|
*/
|
|
3348
|
+
/** @deprecated Use `withPermit(permitHash)` instead. */
|
|
3174
3349
|
setPermitHash(permitHash) {
|
|
3175
|
-
this.permitHash
|
|
3176
|
-
return this;
|
|
3350
|
+
return this.withPermit(permitHash);
|
|
3177
3351
|
}
|
|
3178
3352
|
getPermitHash() {
|
|
3179
3353
|
return this.permitHash;
|
|
@@ -3185,16 +3359,16 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3185
3359
|
*
|
|
3186
3360
|
* Example:
|
|
3187
3361
|
* ```typescript
|
|
3188
|
-
* const unsealed = await
|
|
3362
|
+
* const unsealed = await client.decryptForView(ctHash, utype)
|
|
3189
3363
|
* .setPermit(permit)
|
|
3190
|
-
* .
|
|
3364
|
+
* .execute();
|
|
3191
3365
|
* ```
|
|
3192
3366
|
*
|
|
3193
|
-
* @returns The chainable
|
|
3367
|
+
* @returns The chainable DecryptForViewBuilder instance.
|
|
3194
3368
|
*/
|
|
3369
|
+
/** @deprecated Use `withPermit(permit)` instead. */
|
|
3195
3370
|
setPermit(permit) {
|
|
3196
|
-
this.permit
|
|
3197
|
-
return this;
|
|
3371
|
+
return this.withPermit(permit);
|
|
3198
3372
|
}
|
|
3199
3373
|
getPermit() {
|
|
3200
3374
|
return this.permit;
|
|
@@ -3205,7 +3379,7 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3205
3379
|
}
|
|
3206
3380
|
validateUtypeOrThrow() {
|
|
3207
3381
|
if (!isValidUtype(this.utype))
|
|
3208
|
-
throw new
|
|
3382
|
+
throw new CofheError({
|
|
3209
3383
|
code: "INVALID_UTYPE" /* InvalidUtype */,
|
|
3210
3384
|
message: `Invalid utype to decrypt to`,
|
|
3211
3385
|
context: {
|
|
@@ -3221,7 +3395,7 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3221
3395
|
if (this.permitHash) {
|
|
3222
3396
|
const permit2 = await permits.getPermit(this.chainId, this.account, this.permitHash);
|
|
3223
3397
|
if (!permit2) {
|
|
3224
|
-
throw new
|
|
3398
|
+
throw new CofheError({
|
|
3225
3399
|
code: "PERMIT_NOT_FOUND" /* PermitNotFound */,
|
|
3226
3400
|
message: `Permit with hash <${this.permitHash}> not found for account <${this.account}> and chainId <${this.chainId}>`,
|
|
3227
3401
|
hint: "Ensure the permit exists and is valid.",
|
|
@@ -3236,7 +3410,7 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3236
3410
|
}
|
|
3237
3411
|
const permit = await permits.getActivePermit(this.chainId, this.account);
|
|
3238
3412
|
if (!permit) {
|
|
3239
|
-
throw new
|
|
3413
|
+
throw new CofheError({
|
|
3240
3414
|
code: "PERMIT_NOT_FOUND" /* PermitNotFound */,
|
|
3241
3415
|
message: `Active permit not found for chainId <${this.chainId}> and account <${this.account}>`,
|
|
3242
3416
|
hint: "Ensure a permit exists for this account on this chain.",
|
|
@@ -3253,8 +3427,8 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3253
3427
|
*/
|
|
3254
3428
|
async mocksSealOutput(permit) {
|
|
3255
3429
|
this.assertPublicClient();
|
|
3256
|
-
const
|
|
3257
|
-
return
|
|
3430
|
+
const mocksDecryptDelay = this.config.mocks.decryptDelay;
|
|
3431
|
+
return cofheMocksDecryptForView(this.ctHash, this.utype, permit, this.publicClient, mocksDecryptDelay);
|
|
3258
3432
|
}
|
|
3259
3433
|
/**
|
|
3260
3434
|
* In the production context, perform a true decryption with the CoFHE coprocessor.
|
|
@@ -3279,15 +3453,16 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3279
3453
|
*
|
|
3280
3454
|
* Example:
|
|
3281
3455
|
* ```typescript
|
|
3282
|
-
* const unsealed = await
|
|
3456
|
+
* const unsealed = await client.decryptForView(ctHash, utype)
|
|
3283
3457
|
* .setChainId(11155111) // optional
|
|
3284
3458
|
* .setAccount('0x123...890') // optional
|
|
3285
|
-
* .
|
|
3459
|
+
* .withPermit() // optional
|
|
3460
|
+
* .execute(); // execute
|
|
3286
3461
|
* ```
|
|
3287
3462
|
*
|
|
3288
3463
|
* @returns The unsealed item.
|
|
3289
3464
|
*/
|
|
3290
|
-
async
|
|
3465
|
+
async execute() {
|
|
3291
3466
|
this.validateUtypeOrThrow();
|
|
3292
3467
|
const permit = await this.getResolvedPermit();
|
|
3293
3468
|
PermitUtils.validate(permit);
|
|
@@ -3303,6 +3478,394 @@ var DecryptHandlesBuilder = class extends BaseBuilder {
|
|
|
3303
3478
|
}
|
|
3304
3479
|
};
|
|
3305
3480
|
|
|
3481
|
+
// core/decrypt/tnDecrypt.ts
|
|
3482
|
+
function normalizeSignature(signature) {
|
|
3483
|
+
if (typeof signature !== "string") {
|
|
3484
|
+
throw new CofheError({
|
|
3485
|
+
code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
|
|
3486
|
+
message: "decrypt response missing signature",
|
|
3487
|
+
context: {
|
|
3488
|
+
signature
|
|
3489
|
+
}
|
|
3490
|
+
});
|
|
3491
|
+
}
|
|
3492
|
+
const trimmed = signature.trim();
|
|
3493
|
+
if (trimmed.length === 0) {
|
|
3494
|
+
throw new CofheError({
|
|
3495
|
+
code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
|
|
3496
|
+
message: "decrypt response returned empty signature"
|
|
3497
|
+
});
|
|
3498
|
+
}
|
|
3499
|
+
return trimmed.startsWith("0x") ? trimmed.slice(2) : trimmed;
|
|
3500
|
+
}
|
|
3501
|
+
function parseDecryptedBytesToBigInt(decrypted) {
|
|
3502
|
+
if (!Array.isArray(decrypted)) {
|
|
3503
|
+
throw new CofheError({
|
|
3504
|
+
code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
|
|
3505
|
+
message: "decrypt response field <decrypted> must be a byte array",
|
|
3506
|
+
context: {
|
|
3507
|
+
decrypted
|
|
3508
|
+
}
|
|
3509
|
+
});
|
|
3510
|
+
}
|
|
3511
|
+
if (decrypted.length === 0) {
|
|
3512
|
+
throw new CofheError({
|
|
3513
|
+
code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
|
|
3514
|
+
message: "decrypt response field <decrypted> was an empty byte array",
|
|
3515
|
+
context: {
|
|
3516
|
+
decrypted
|
|
3517
|
+
}
|
|
3518
|
+
});
|
|
3519
|
+
}
|
|
3520
|
+
let hex = "";
|
|
3521
|
+
for (const b of decrypted) {
|
|
3522
|
+
if (typeof b !== "number" || !Number.isInteger(b) || b < 0 || b > 255) {
|
|
3523
|
+
throw new CofheError({
|
|
3524
|
+
code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
|
|
3525
|
+
message: "decrypt response field <decrypted> contained a non-byte value",
|
|
3526
|
+
context: {
|
|
3527
|
+
badElement: b,
|
|
3528
|
+
decrypted
|
|
3529
|
+
}
|
|
3530
|
+
});
|
|
3531
|
+
}
|
|
3532
|
+
hex += b.toString(16).padStart(2, "0");
|
|
3533
|
+
}
|
|
3534
|
+
return BigInt(`0x${hex}`);
|
|
3535
|
+
}
|
|
3536
|
+
function assertTnDecryptResponse(value) {
|
|
3537
|
+
if (value == null || typeof value !== "object") {
|
|
3538
|
+
throw new CofheError({
|
|
3539
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3540
|
+
message: "decrypt response must be a JSON object",
|
|
3541
|
+
context: {
|
|
3542
|
+
value
|
|
3543
|
+
}
|
|
3544
|
+
});
|
|
3545
|
+
}
|
|
3546
|
+
const v = value;
|
|
3547
|
+
const decrypted = v.decrypted;
|
|
3548
|
+
const signature = v.signature;
|
|
3549
|
+
const encryptionType = v.encryption_type;
|
|
3550
|
+
const errorMessage = v.error_message;
|
|
3551
|
+
if (!Array.isArray(decrypted)) {
|
|
3552
|
+
throw new CofheError({
|
|
3553
|
+
code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
|
|
3554
|
+
message: "decrypt response missing <decrypted> byte array",
|
|
3555
|
+
context: { decryptResponse: value }
|
|
3556
|
+
});
|
|
3557
|
+
}
|
|
3558
|
+
if (typeof signature !== "string") {
|
|
3559
|
+
throw new CofheError({
|
|
3560
|
+
code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
|
|
3561
|
+
message: "decrypt response missing <signature> string",
|
|
3562
|
+
context: { decryptResponse: value }
|
|
3563
|
+
});
|
|
3564
|
+
}
|
|
3565
|
+
if (typeof encryptionType !== "number") {
|
|
3566
|
+
throw new CofheError({
|
|
3567
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3568
|
+
message: "decrypt response missing <encryption_type> number",
|
|
3569
|
+
context: { decryptResponse: value }
|
|
3570
|
+
});
|
|
3571
|
+
}
|
|
3572
|
+
if (!(typeof errorMessage === "string" || errorMessage === null)) {
|
|
3573
|
+
throw new CofheError({
|
|
3574
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3575
|
+
message: "decrypt response field <error_message> must be string or null",
|
|
3576
|
+
context: { decryptResponse: value }
|
|
3577
|
+
});
|
|
3578
|
+
}
|
|
3579
|
+
return {
|
|
3580
|
+
decrypted,
|
|
3581
|
+
signature,
|
|
3582
|
+
encryption_type: encryptionType,
|
|
3583
|
+
error_message: errorMessage
|
|
3584
|
+
};
|
|
3585
|
+
}
|
|
3586
|
+
async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
|
|
3587
|
+
const body = {
|
|
3588
|
+
ct_tempkey: ctHash.toString(16).padStart(64, "0"),
|
|
3589
|
+
host_chain_id: chainId
|
|
3590
|
+
};
|
|
3591
|
+
if (permission) {
|
|
3592
|
+
body.permit = permission;
|
|
3593
|
+
}
|
|
3594
|
+
let response;
|
|
3595
|
+
try {
|
|
3596
|
+
response = await fetch(`${thresholdNetworkUrl}/decrypt`, {
|
|
3597
|
+
method: "POST",
|
|
3598
|
+
headers: {
|
|
3599
|
+
"Content-Type": "application/json"
|
|
3600
|
+
},
|
|
3601
|
+
body: JSON.stringify(body)
|
|
3602
|
+
});
|
|
3603
|
+
} catch (e) {
|
|
3604
|
+
throw new CofheError({
|
|
3605
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3606
|
+
message: `decrypt request failed`,
|
|
3607
|
+
hint: "Ensure the threshold network URL is valid and reachable.",
|
|
3608
|
+
cause: e instanceof Error ? e : void 0,
|
|
3609
|
+
context: {
|
|
3610
|
+
thresholdNetworkUrl,
|
|
3611
|
+
body
|
|
3612
|
+
}
|
|
3613
|
+
});
|
|
3614
|
+
}
|
|
3615
|
+
const responseText = await response.text();
|
|
3616
|
+
if (!response.ok) {
|
|
3617
|
+
let errorMessage = response.statusText || `HTTP ${response.status}`;
|
|
3618
|
+
try {
|
|
3619
|
+
const errorBody = JSON.parse(responseText);
|
|
3620
|
+
const maybeMessage = errorBody.error_message || errorBody.message;
|
|
3621
|
+
if (typeof maybeMessage === "string" && maybeMessage.length > 0)
|
|
3622
|
+
errorMessage = maybeMessage;
|
|
3623
|
+
} catch {
|
|
3624
|
+
const trimmed = responseText.trim();
|
|
3625
|
+
if (trimmed.length > 0)
|
|
3626
|
+
errorMessage = trimmed;
|
|
3627
|
+
}
|
|
3628
|
+
throw new CofheError({
|
|
3629
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3630
|
+
message: `decrypt request failed: ${errorMessage}`,
|
|
3631
|
+
hint: "Check the threshold network URL and request parameters.",
|
|
3632
|
+
context: {
|
|
3633
|
+
thresholdNetworkUrl,
|
|
3634
|
+
status: response.status,
|
|
3635
|
+
statusText: response.statusText,
|
|
3636
|
+
body,
|
|
3637
|
+
responseText
|
|
3638
|
+
}
|
|
3639
|
+
});
|
|
3640
|
+
}
|
|
3641
|
+
let rawJson;
|
|
3642
|
+
try {
|
|
3643
|
+
rawJson = JSON.parse(responseText);
|
|
3644
|
+
} catch (e) {
|
|
3645
|
+
throw new CofheError({
|
|
3646
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3647
|
+
message: `Failed to parse decrypt response`,
|
|
3648
|
+
cause: e instanceof Error ? e : void 0,
|
|
3649
|
+
context: {
|
|
3650
|
+
thresholdNetworkUrl,
|
|
3651
|
+
body,
|
|
3652
|
+
responseText
|
|
3653
|
+
}
|
|
3654
|
+
});
|
|
3655
|
+
}
|
|
3656
|
+
const decryptResponse = assertTnDecryptResponse(rawJson);
|
|
3657
|
+
if (decryptResponse.error_message) {
|
|
3658
|
+
throw new CofheError({
|
|
3659
|
+
code: "DECRYPT_FAILED" /* DecryptFailed */,
|
|
3660
|
+
message: `decrypt request failed: ${decryptResponse.error_message}`,
|
|
3661
|
+
context: {
|
|
3662
|
+
thresholdNetworkUrl,
|
|
3663
|
+
body,
|
|
3664
|
+
decryptResponse
|
|
3665
|
+
}
|
|
3666
|
+
});
|
|
3667
|
+
}
|
|
3668
|
+
const decryptedValue = parseDecryptedBytesToBigInt(decryptResponse.decrypted);
|
|
3669
|
+
const signature = normalizeSignature(decryptResponse.signature);
|
|
3670
|
+
return { decryptedValue, signature };
|
|
3671
|
+
}
|
|
3672
|
+
|
|
3673
|
+
// core/decrypt/decryptForTxBuilder.ts
|
|
3674
|
+
var DecryptForTxBuilder = class extends BaseBuilder {
|
|
3675
|
+
ctHash;
|
|
3676
|
+
permitHash;
|
|
3677
|
+
permit;
|
|
3678
|
+
permitSelection = "unset";
|
|
3679
|
+
constructor(params) {
|
|
3680
|
+
super({
|
|
3681
|
+
config: params.config,
|
|
3682
|
+
publicClient: params.publicClient,
|
|
3683
|
+
walletClient: params.walletClient,
|
|
3684
|
+
chainId: params.chainId,
|
|
3685
|
+
account: params.account,
|
|
3686
|
+
requireConnected: params.requireConnected
|
|
3687
|
+
});
|
|
3688
|
+
this.ctHash = params.ctHash;
|
|
3689
|
+
}
|
|
3690
|
+
setChainId(chainId) {
|
|
3691
|
+
this.chainId = chainId;
|
|
3692
|
+
return this;
|
|
3693
|
+
}
|
|
3694
|
+
getChainId() {
|
|
3695
|
+
return this.chainId;
|
|
3696
|
+
}
|
|
3697
|
+
setAccount(account) {
|
|
3698
|
+
this.account = account;
|
|
3699
|
+
return this;
|
|
3700
|
+
}
|
|
3701
|
+
getAccount() {
|
|
3702
|
+
return this.account;
|
|
3703
|
+
}
|
|
3704
|
+
withPermit(permitOrPermitHash) {
|
|
3705
|
+
if (this.permitSelection === "with-permit") {
|
|
3706
|
+
throw new CofheError({
|
|
3707
|
+
code: "INTERNAL_ERROR" /* InternalError */,
|
|
3708
|
+
message: "decryptForTx: withPermit() can only be selected once.",
|
|
3709
|
+
hint: "Choose the permit mode once. If you need a different permit, start a new decryptForTx() builder chain."
|
|
3710
|
+
});
|
|
3711
|
+
}
|
|
3712
|
+
if (this.permitSelection === "without-permit") {
|
|
3713
|
+
throw new CofheError({
|
|
3714
|
+
code: "INTERNAL_ERROR" /* InternalError */,
|
|
3715
|
+
message: "decryptForTx: cannot call withPermit() after withoutPermit() has been selected.",
|
|
3716
|
+
hint: "Choose exactly one permit mode: either call .withPermit(...) or .withoutPermit(), but not both."
|
|
3717
|
+
});
|
|
3718
|
+
}
|
|
3719
|
+
this.permitSelection = "with-permit";
|
|
3720
|
+
if (typeof permitOrPermitHash === "string") {
|
|
3721
|
+
this.permitHash = permitOrPermitHash;
|
|
3722
|
+
this.permit = void 0;
|
|
3723
|
+
} else if (permitOrPermitHash === void 0) {
|
|
3724
|
+
this.permitHash = void 0;
|
|
3725
|
+
this.permit = void 0;
|
|
3726
|
+
} else {
|
|
3727
|
+
this.permit = permitOrPermitHash;
|
|
3728
|
+
this.permitHash = void 0;
|
|
3729
|
+
}
|
|
3730
|
+
return this;
|
|
3731
|
+
}
|
|
3732
|
+
/**
|
|
3733
|
+
* Select "no permit" mode.
|
|
3734
|
+
*
|
|
3735
|
+
* This uses global allowance (no permit required) and sends an empty permission payload to `/decrypt`.
|
|
3736
|
+
*/
|
|
3737
|
+
withoutPermit() {
|
|
3738
|
+
if (this.permitSelection === "without-permit") {
|
|
3739
|
+
throw new CofheError({
|
|
3740
|
+
code: "INTERNAL_ERROR" /* InternalError */,
|
|
3741
|
+
message: "decryptForTx: withoutPermit() can only be selected once.",
|
|
3742
|
+
hint: "Choose the permit mode once. If you need a different mode, start a new decryptForTx() builder chain."
|
|
3743
|
+
});
|
|
3744
|
+
}
|
|
3745
|
+
if (this.permitSelection === "with-permit") {
|
|
3746
|
+
throw new CofheError({
|
|
3747
|
+
code: "INTERNAL_ERROR" /* InternalError */,
|
|
3748
|
+
message: "decryptForTx: cannot call withoutPermit() after withPermit() has been selected.",
|
|
3749
|
+
hint: "Choose exactly one permit mode: either call .withPermit(...) or .withoutPermit(), but not both."
|
|
3750
|
+
});
|
|
3751
|
+
}
|
|
3752
|
+
this.permitSelection = "without-permit";
|
|
3753
|
+
this.permitHash = void 0;
|
|
3754
|
+
this.permit = void 0;
|
|
3755
|
+
return this;
|
|
3756
|
+
}
|
|
3757
|
+
getPermit() {
|
|
3758
|
+
return this.permit;
|
|
3759
|
+
}
|
|
3760
|
+
getPermitHash() {
|
|
3761
|
+
return this.permitHash;
|
|
3762
|
+
}
|
|
3763
|
+
async getThresholdNetworkUrl() {
|
|
3764
|
+
this.assertChainId();
|
|
3765
|
+
return getThresholdNetworkUrlOrThrow(this.config, this.chainId);
|
|
3766
|
+
}
|
|
3767
|
+
async getResolvedPermit() {
|
|
3768
|
+
if (this.permitSelection === "unset") {
|
|
3769
|
+
throw new CofheError({
|
|
3770
|
+
code: "INTERNAL_ERROR" /* InternalError */,
|
|
3771
|
+
message: "decryptForTx: missing permit selection; call withPermit(...) or withoutPermit() before execute().",
|
|
3772
|
+
hint: "Call .withPermit() to use the active permit, or .withoutPermit() for global allowance."
|
|
3773
|
+
});
|
|
3774
|
+
}
|
|
3775
|
+
if (this.permitSelection === "without-permit") {
|
|
3776
|
+
return null;
|
|
3777
|
+
}
|
|
3778
|
+
if (this.permit)
|
|
3779
|
+
return this.permit;
|
|
3780
|
+
this.assertChainId();
|
|
3781
|
+
this.assertAccount();
|
|
3782
|
+
if (this.permitHash) {
|
|
3783
|
+
const permit2 = await permits.getPermit(this.chainId, this.account, this.permitHash);
|
|
3784
|
+
if (!permit2) {
|
|
3785
|
+
throw new CofheError({
|
|
3786
|
+
code: "PERMIT_NOT_FOUND" /* PermitNotFound */,
|
|
3787
|
+
message: `Permit with hash <${this.permitHash}> not found for account <${this.account}> and chainId <${this.chainId}>`,
|
|
3788
|
+
hint: "Ensure the permit exists and is valid.",
|
|
3789
|
+
context: {
|
|
3790
|
+
chainId: this.chainId,
|
|
3791
|
+
account: this.account,
|
|
3792
|
+
permitHash: this.permitHash
|
|
3793
|
+
}
|
|
3794
|
+
});
|
|
3795
|
+
}
|
|
3796
|
+
return permit2;
|
|
3797
|
+
}
|
|
3798
|
+
const permit = await permits.getActivePermit(this.chainId, this.account);
|
|
3799
|
+
if (!permit) {
|
|
3800
|
+
throw new CofheError({
|
|
3801
|
+
code: "PERMIT_NOT_FOUND" /* PermitNotFound */,
|
|
3802
|
+
message: `Active permit not found for chainId <${this.chainId}> and account <${this.account}>`,
|
|
3803
|
+
hint: "Create a permit (e.g. client.permits.createSelf(...)) and/or set it active (client.permits.selectActivePermit(hash)).",
|
|
3804
|
+
context: {
|
|
3805
|
+
chainId: this.chainId,
|
|
3806
|
+
account: this.account
|
|
3807
|
+
}
|
|
3808
|
+
});
|
|
3809
|
+
}
|
|
3810
|
+
return permit;
|
|
3811
|
+
}
|
|
3812
|
+
/**
|
|
3813
|
+
* On hardhat, interact with MockThresholdNetwork contract
|
|
3814
|
+
*/
|
|
3815
|
+
async mocksDecryptForTx(permit) {
|
|
3816
|
+
this.assertPublicClient();
|
|
3817
|
+
const delay = this.config.mocks.decryptDelay;
|
|
3818
|
+
const result = await cofheMocksDecryptForTx(this.ctHash, 0, permit, this.publicClient, delay);
|
|
3819
|
+
return result;
|
|
3820
|
+
}
|
|
3821
|
+
/**
|
|
3822
|
+
* In the production context, perform a true decryption with the CoFHE coprocessor.
|
|
3823
|
+
*/
|
|
3824
|
+
async productionDecryptForTx(permit) {
|
|
3825
|
+
this.assertChainId();
|
|
3826
|
+
this.assertPublicClient();
|
|
3827
|
+
const thresholdNetworkUrl = await this.getThresholdNetworkUrl();
|
|
3828
|
+
const permission = permit ? PermitUtils.getPermission(permit, true) : null;
|
|
3829
|
+
const { decryptedValue, signature } = await tnDecrypt(this.ctHash, this.chainId, permission, thresholdNetworkUrl);
|
|
3830
|
+
return {
|
|
3831
|
+
ctHash: this.ctHash,
|
|
3832
|
+
decryptedValue,
|
|
3833
|
+
signature
|
|
3834
|
+
};
|
|
3835
|
+
}
|
|
3836
|
+
/**
|
|
3837
|
+
* Final step of the decryptForTx process. MUST BE CALLED LAST IN THE CHAIN.
|
|
3838
|
+
*
|
|
3839
|
+
* You must explicitly choose one permit mode before calling `execute()`:
|
|
3840
|
+
* - `withPermit(permit)` / `withPermit(permitHash)` / `withPermit()` (active permit)
|
|
3841
|
+
* - `withoutPermit()` (global allowance)
|
|
3842
|
+
*/
|
|
3843
|
+
async execute() {
|
|
3844
|
+
const permit = await this.getResolvedPermit();
|
|
3845
|
+
if (permit !== null) {
|
|
3846
|
+
PermitUtils.validate(permit);
|
|
3847
|
+
PermitUtils.isValid(permit);
|
|
3848
|
+
const chainId = permit._signedDomain.chainId;
|
|
3849
|
+
if (chainId === hardhat2.id) {
|
|
3850
|
+
return await this.mocksDecryptForTx(permit);
|
|
3851
|
+
} else {
|
|
3852
|
+
return await this.productionDecryptForTx(permit);
|
|
3853
|
+
}
|
|
3854
|
+
} else {
|
|
3855
|
+
if (!this.chainId) {
|
|
3856
|
+
this.assertPublicClient();
|
|
3857
|
+
this.chainId = await getPublicClientChainID(this.publicClient);
|
|
3858
|
+
}
|
|
3859
|
+
this.assertChainId();
|
|
3860
|
+
if (this.chainId === hardhat2.id) {
|
|
3861
|
+
return await this.mocksDecryptForTx(null);
|
|
3862
|
+
} else {
|
|
3863
|
+
return await this.productionDecryptForTx(null);
|
|
3864
|
+
}
|
|
3865
|
+
}
|
|
3866
|
+
}
|
|
3867
|
+
};
|
|
3868
|
+
|
|
3306
3869
|
// core/client.ts
|
|
3307
3870
|
var InitialConnectStore = {
|
|
3308
3871
|
connected: false,
|
|
@@ -3313,7 +3876,7 @@ var InitialConnectStore = {
|
|
|
3313
3876
|
publicClient: void 0,
|
|
3314
3877
|
walletClient: void 0
|
|
3315
3878
|
};
|
|
3316
|
-
function
|
|
3879
|
+
function createCofheClientBase(opts) {
|
|
3317
3880
|
const keysStorage = createKeysStore(opts.config.fheKeyStorage);
|
|
3318
3881
|
const connectStore = vanilla.createStore(() => InitialConnectStore);
|
|
3319
3882
|
let connectAttemptId = 0;
|
|
@@ -3324,7 +3887,7 @@ function createCofhesdkClientBase(opts) {
|
|
|
3324
3887
|
const state = connectStore.getState();
|
|
3325
3888
|
const notConnected = !state.connected || !state.account || !state.chainId || !state.publicClient || !state.walletClient;
|
|
3326
3889
|
if (notConnected) {
|
|
3327
|
-
throw new
|
|
3890
|
+
throw new CofheError({
|
|
3328
3891
|
code: "NOT_CONNECTED" /* NotConnected */,
|
|
3329
3892
|
message: "Client must be connected, account and chainId must be initialized",
|
|
3330
3893
|
hint: "Ensure client.connect() has been called and awaited.",
|
|
@@ -3395,16 +3958,28 @@ function createCofhesdkClientBase(opts) {
|
|
|
3395
3958
|
requireConnected: _requireConnected
|
|
3396
3959
|
});
|
|
3397
3960
|
}
|
|
3398
|
-
function
|
|
3961
|
+
function decryptForView(ctHash, utype) {
|
|
3399
3962
|
const state = connectStore.getState();
|
|
3400
|
-
return new
|
|
3963
|
+
return new DecryptForViewBuilder({
|
|
3401
3964
|
ctHash,
|
|
3402
3965
|
utype,
|
|
3403
|
-
chainId: state.chainId
|
|
3404
|
-
account: state.account
|
|
3966
|
+
chainId: state.chainId,
|
|
3967
|
+
account: state.account,
|
|
3405
3968
|
config: opts.config,
|
|
3406
|
-
publicClient: state.publicClient
|
|
3407
|
-
walletClient: state.walletClient
|
|
3969
|
+
publicClient: state.publicClient,
|
|
3970
|
+
walletClient: state.walletClient,
|
|
3971
|
+
requireConnected: _requireConnected
|
|
3972
|
+
});
|
|
3973
|
+
}
|
|
3974
|
+
function decryptForTx(ctHash) {
|
|
3975
|
+
const state = connectStore.getState();
|
|
3976
|
+
return new DecryptForTxBuilder({
|
|
3977
|
+
ctHash,
|
|
3978
|
+
chainId: state.chainId,
|
|
3979
|
+
account: state.account,
|
|
3980
|
+
config: opts.config,
|
|
3981
|
+
publicClient: state.publicClient,
|
|
3982
|
+
walletClient: state.walletClient,
|
|
3408
3983
|
requireConnected: _requireConnected
|
|
3409
3984
|
});
|
|
3410
3985
|
}
|
|
@@ -3413,7 +3988,7 @@ function createCofhesdkClientBase(opts) {
|
|
|
3413
3988
|
const _chainId = chainId ?? state.chainId;
|
|
3414
3989
|
const _account = account ?? state.account;
|
|
3415
3990
|
if (_chainId == null || _account == null) {
|
|
3416
|
-
throw new
|
|
3991
|
+
throw new CofheError({
|
|
3417
3992
|
code: "NOT_CONNECTED" /* NotConnected */,
|
|
3418
3993
|
message: "ChainId or account not available.",
|
|
3419
3994
|
hint: "Ensure client.connect() has been called, or provide chainId and account explicitly.",
|
|
@@ -3498,6 +4073,9 @@ function createCofhesdkClientBase(opts) {
|
|
|
3498
4073
|
getSnapshot: connectStore.getState,
|
|
3499
4074
|
subscribe: connectStore.subscribe,
|
|
3500
4075
|
// flags (read-only: reflect snapshot)
|
|
4076
|
+
get connection() {
|
|
4077
|
+
return connectStore.getState();
|
|
4078
|
+
},
|
|
3501
4079
|
get connected() {
|
|
3502
4080
|
return connectStore.getState().connected;
|
|
3503
4081
|
},
|
|
@@ -3509,7 +4087,12 @@ function createCofhesdkClientBase(opts) {
|
|
|
3509
4087
|
connect,
|
|
3510
4088
|
disconnect,
|
|
3511
4089
|
encryptInputs,
|
|
3512
|
-
|
|
4090
|
+
decryptForView,
|
|
4091
|
+
/**
|
|
4092
|
+
* @deprecated Use `decryptForView` instead. Kept for backward compatibility.
|
|
4093
|
+
*/
|
|
4094
|
+
decryptHandle: decryptForView,
|
|
4095
|
+
decryptForTx,
|
|
3513
4096
|
permits: clientPermits
|
|
3514
4097
|
// Add SDK-specific methods below that require connection
|
|
3515
4098
|
// Example:
|
|
@@ -3521,29 +4104,31 @@ function createCofhesdkClientBase(opts) {
|
|
|
3521
4104
|
}
|
|
3522
4105
|
|
|
3523
4106
|
exports.CONNECT_STORE_DEFAULTS = InitialConnectStore;
|
|
3524
|
-
exports.
|
|
3525
|
-
exports.
|
|
3526
|
-
exports.
|
|
4107
|
+
exports.CofheError = CofheError;
|
|
4108
|
+
exports.CofheErrorCode = CofheErrorCode;
|
|
4109
|
+
exports.DecryptForTxBuilder = DecryptForTxBuilder;
|
|
4110
|
+
exports.DecryptForViewBuilder = DecryptForViewBuilder;
|
|
3527
4111
|
exports.EncryptInputsBuilder = EncryptInputsBuilder;
|
|
3528
4112
|
exports.EncryptStep = EncryptStep;
|
|
3529
4113
|
exports.Encryptable = Encryptable;
|
|
3530
4114
|
exports.FheAllUTypes = FheAllUTypes;
|
|
3531
4115
|
exports.FheTypes = FheTypes;
|
|
3532
4116
|
exports.FheUintUTypes = FheUintUTypes;
|
|
3533
|
-
exports.
|
|
4117
|
+
exports.MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY = MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY;
|
|
4118
|
+
exports.MOCKS_THRESHOLD_NETWORK_ADDRESS = MOCKS_THRESHOLD_NETWORK_ADDRESS;
|
|
3534
4119
|
exports.MOCKS_ZK_VERIFIER_ADDRESS = MOCKS_ZK_VERIFIER_ADDRESS;
|
|
3535
4120
|
exports.MOCKS_ZK_VERIFIER_SIGNER_ADDRESS = MOCKS_ZK_VERIFIER_SIGNER_ADDRESS;
|
|
3536
4121
|
exports.MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY = MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY;
|
|
3537
4122
|
exports.TASK_MANAGER_ADDRESS = TASK_MANAGER_ADDRESS;
|
|
3538
4123
|
exports.TEST_BED_ADDRESS = TEST_BED_ADDRESS;
|
|
3539
4124
|
exports.assertCorrectEncryptedItemInput = assertCorrectEncryptedItemInput;
|
|
3540
|
-
exports.
|
|
3541
|
-
exports.
|
|
4125
|
+
exports.createCofheClientBase = createCofheClientBase;
|
|
4126
|
+
exports.createCofheConfigBase = createCofheConfigBase;
|
|
3542
4127
|
exports.createKeysStore = createKeysStore;
|
|
3543
4128
|
exports.fetchKeys = fetchKeys;
|
|
3544
4129
|
exports.fheTypeToString = fheTypeToString;
|
|
3545
|
-
exports.
|
|
3546
|
-
exports.
|
|
4130
|
+
exports.getCofheConfigItem = getCofheConfigItem;
|
|
4131
|
+
exports.isCofheError = isCofheError;
|
|
3547
4132
|
exports.isEncryptableItem = isEncryptableItem;
|
|
3548
4133
|
exports.isLastEncryptionStep = isLastEncryptionStep;
|
|
3549
4134
|
exports.zkProveWithWorker = zkProveWithWorker;
|