@cofhe/sdk 0.3.1 → 0.3.2

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.
@@ -1,7 +1,7 @@
1
1
  import { hardhat as hardhat$1 } from './chunk-TBLR7NNE.js';
2
2
  import { permitStore, PermitUtils, MOCKS_THRESHOLD_NETWORK_ADDRESS, MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY, MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY, MOCKS_ZK_VERIFIER_ADDRESS } from './chunk-NWDKXBIP.js';
3
3
  import { createStore } from 'zustand/vanilla';
4
- import { pad, toHex, encodePacked, keccak256, createWalletClient, http, toBytes, hashMessage, getAddress } from 'viem';
4
+ import { encodePacked, keccak256, createWalletClient, http, getAddress, parseSignature, serializeSignature } from 'viem';
5
5
  import { hardhat } from 'viem/chains';
6
6
  import { sign, privateKeyToAccount } from 'viem/accounts';
7
7
  import { z } from 'zod';
@@ -767,7 +767,7 @@ async function insertCtHashes(items, walletClient) {
767
767
  });
768
768
  }
769
769
  }
770
- async function createProofSignatures(items, securityZone) {
770
+ async function createProofSignatures(items, securityZone, account) {
771
771
  let signatures = [];
772
772
  let encInputSignerClient;
773
773
  try {
@@ -784,12 +784,15 @@ async function createProofSignatures(items, securityZone) {
784
784
  }
785
785
  try {
786
786
  for (const item of items) {
787
- const packedData = encodePacked(["uint256", "int32", "uint8"], [BigInt(item.data), securityZone, item.utype]);
787
+ const packedData = encodePacked(
788
+ ["uint256", "uint8", "uint8", "address", "uint256"],
789
+ [BigInt(item.ctHash), item.utype, securityZone, account, BigInt(hardhat.id)]
790
+ );
788
791
  const messageHash = keccak256(packedData);
789
- const ethSignedHash = hashMessage({ raw: toBytes(messageHash) });
790
- const signature = await encInputSignerClient.signMessage({
791
- message: { raw: toBytes(ethSignedHash) },
792
- account: encInputSignerClient.account
792
+ const signature = await sign({
793
+ hash: messageHash,
794
+ privateKey: MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY,
795
+ to: "hex"
793
796
  });
794
797
  signatures.push(signature);
795
798
  }
@@ -820,7 +823,7 @@ async function cofheMocksZkVerifySign(items, account, securityZone, publicClient
820
823
  const _walletClient = zkvWalletClient ?? createMockZkVerifierSigner();
821
824
  const encryptableItems = await calcCtHashes(items, account, securityZone, publicClient);
822
825
  await insertCtHashes(encryptableItems, _walletClient);
823
- const signatures = await createProofSignatures(encryptableItems, securityZone);
826
+ const signatures = await createProofSignatures(encryptableItems, securityZone, account);
824
827
  return encryptableItems.map((item, index) => ({
825
828
  ct_hash: item.ctHash.toString(),
826
829
  signature: signatures[index]
@@ -1930,9 +1933,7 @@ var MockThresholdNetworkAbi = [
1930
1933
  ];
1931
1934
 
1932
1935
  // core/decrypt/cofheMocksDecryptForView.ts
1933
- async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient, mocksDecryptDelay) {
1934
- if (mocksDecryptDelay > 0)
1935
- await sleep(mocksDecryptDelay);
1936
+ async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient) {
1936
1937
  const permission = PermitUtils.getPermission(permit, true);
1937
1938
  const permissionWithBigInts = {
1938
1939
  ...permission,
@@ -1943,7 +1944,7 @@ async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient, moc
1943
1944
  address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
1944
1945
  abi: MockThresholdNetworkAbi,
1945
1946
  functionName: "querySealOutput",
1946
- args: [ctHash, BigInt(utype), permissionWithBigInts]
1947
+ args: [BigInt(ctHash), BigInt(utype), permissionWithBigInts]
1947
1948
  });
1948
1949
  if (error != "") {
1949
1950
  throw new CofheError({
@@ -1984,7 +1985,7 @@ function convertSealedData(sealed) {
1984
1985
  }
1985
1986
  async function submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, permission) {
1986
1987
  const body = {
1987
- ct_tempkey: ctHash.toString(16).padStart(64, "0"),
1988
+ ct_tempkey: BigInt(ctHash).toString(16).padStart(64, "0"),
1988
1989
  host_chain_id: chainId,
1989
1990
  permit: permission
1990
1991
  };
@@ -2177,96 +2178,6 @@ async function tnSealOutputV2(ctHash, chainId, permission, thresholdNetworkUrl)
2177
2178
  const requestId = await submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, permission);
2178
2179
  return await pollSealOutputStatus(thresholdNetworkUrl, requestId);
2179
2180
  }
2180
- async function cofheMocksDecryptForTx(ctHash, utype, permit, publicClient, mocksDecryptForTxDelay) {
2181
- if (mocksDecryptForTxDelay > 0)
2182
- await sleep(mocksDecryptForTxDelay);
2183
- if (permit !== null) {
2184
- let permission = PermitUtils.getPermission(permit, true);
2185
- const permissionWithBigInts = {
2186
- ...permission,
2187
- expiration: BigInt(permission.expiration),
2188
- validatorId: BigInt(permission.validatorId)
2189
- };
2190
- const [allowed2, error2, result2] = await publicClient.readContract({
2191
- address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
2192
- abi: MockThresholdNetworkAbi,
2193
- functionName: "decryptForTxWithPermit",
2194
- args: [ctHash, permissionWithBigInts]
2195
- });
2196
- if (error2 != "") {
2197
- throw new CofheError({
2198
- code: "DECRYPT_FAILED" /* DecryptFailed */,
2199
- message: `mocks decryptForTx call failed: ${error2}`
2200
- });
2201
- }
2202
- if (allowed2 == false) {
2203
- throw new CofheError({
2204
- code: "DECRYPT_FAILED" /* DecryptFailed */,
2205
- message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
2206
- });
2207
- }
2208
- const chainId2 = await publicClient.getChainId();
2209
- const ctHashBigInt2 = BigInt(ctHash);
2210
- const resultBigInt2 = BigInt(result2);
2211
- const encryptionType2 = Number((ctHashBigInt2 & 0x7fn << 8n) >> 8n);
2212
- const ctHashBytes322 = pad(toHex(ctHashBigInt2), { size: 32 });
2213
- const packed2 = encodePacked(
2214
- ["uint256", "uint32", "uint64", "bytes32"],
2215
- [resultBigInt2, encryptionType2, BigInt(chainId2), ctHashBytes322]
2216
- );
2217
- const messageHash2 = keccak256(packed2);
2218
- const signatureHex2 = await sign({
2219
- hash: messageHash2,
2220
- privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
2221
- to: "hex"
2222
- });
2223
- const signature2 = signatureHex2.slice(2);
2224
- return {
2225
- ctHash,
2226
- decryptedValue: BigInt(result2),
2227
- signature: signature2
2228
- };
2229
- }
2230
- const [allowed, error, result] = await publicClient.readContract({
2231
- address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
2232
- abi: MockThresholdNetworkAbi,
2233
- functionName: "decryptForTxWithoutPermit",
2234
- args: [ctHash]
2235
- });
2236
- if (error != "") {
2237
- throw new CofheError({
2238
- code: "DECRYPT_FAILED" /* DecryptFailed */,
2239
- message: `mocks decryptForTx call failed: ${error}`
2240
- });
2241
- }
2242
- if (allowed == false) {
2243
- throw new CofheError({
2244
- code: "DECRYPT_FAILED" /* DecryptFailed */,
2245
- message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
2246
- });
2247
- }
2248
- const chainId = await publicClient.getChainId();
2249
- const ctHashBigInt = BigInt(ctHash);
2250
- const resultBigInt = BigInt(result);
2251
- const encryptionType = Number((ctHashBigInt & 0x7fn << 8n) >> 8n);
2252
- const ctHashBytes32 = pad(toHex(ctHashBigInt), { size: 32 });
2253
- const packed = encodePacked(
2254
- ["uint256", "uint32", "uint64", "bytes32"],
2255
- [resultBigInt, encryptionType, BigInt(chainId), ctHashBytes32]
2256
- );
2257
- const messageHash = keccak256(packed);
2258
- const signatureHex = await sign({
2259
- hash: messageHash,
2260
- privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
2261
- to: "hex"
2262
- });
2263
- const signature = signatureHex.slice(2);
2264
- return {
2265
- ctHash,
2266
- decryptedValue: BigInt(result),
2267
- signature
2268
- };
2269
- }
2270
2181
 
2271
2182
  // core/decrypt/decryptForViewBuilder.ts
2272
2183
  var DecryptForViewBuilder = class extends BaseBuilder {
@@ -2441,7 +2352,9 @@ var DecryptForViewBuilder = class extends BaseBuilder {
2441
2352
  async mocksSealOutput(permit) {
2442
2353
  this.assertPublicClient();
2443
2354
  const mocksDecryptDelay = this.config.mocks.decryptDelay;
2444
- return cofheMocksDecryptForView(this.ctHash, this.utype, permit, this.publicClient, mocksDecryptDelay);
2355
+ if (mocksDecryptDelay > 0)
2356
+ await sleep(mocksDecryptDelay);
2357
+ return cofheMocksDecryptForView(this.ctHash, this.utype, permit, this.publicClient);
2445
2358
  }
2446
2359
  /**
2447
2360
  * In the production context, perform a true decryption with the CoFHE coprocessor.
@@ -2490,8 +2403,56 @@ var DecryptForViewBuilder = class extends BaseBuilder {
2490
2403
  return convertViaUtype(this.utype, unsealed);
2491
2404
  }
2492
2405
  };
2493
-
2494
- // core/decrypt/tnDecrypt.ts
2406
+ async function cofheMocksDecryptForTx(ctHash, utype, permit, publicClient) {
2407
+ let allowed;
2408
+ let error;
2409
+ let decryptedValue;
2410
+ if (permit !== null) {
2411
+ let permission = PermitUtils.getPermission(permit, true);
2412
+ const permissionWithBigInts = {
2413
+ ...permission,
2414
+ expiration: BigInt(permission.expiration),
2415
+ validatorId: BigInt(permission.validatorId)
2416
+ };
2417
+ [allowed, error, decryptedValue] = await publicClient.readContract({
2418
+ address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
2419
+ abi: MockThresholdNetworkAbi,
2420
+ functionName: "decryptForTxWithPermit",
2421
+ args: [BigInt(ctHash), permissionWithBigInts]
2422
+ });
2423
+ } else {
2424
+ [allowed, error, decryptedValue] = await publicClient.readContract({
2425
+ address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
2426
+ abi: MockThresholdNetworkAbi,
2427
+ functionName: "decryptForTxWithoutPermit",
2428
+ args: [BigInt(ctHash)]
2429
+ });
2430
+ }
2431
+ if (error != "") {
2432
+ throw new CofheError({
2433
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
2434
+ message: `mocks decryptForTx call failed: ${error}`
2435
+ });
2436
+ }
2437
+ if (allowed == false) {
2438
+ throw new CofheError({
2439
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
2440
+ message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
2441
+ });
2442
+ }
2443
+ const packed = encodePacked(["uint256", "uint256"], [BigInt(ctHash), decryptedValue]);
2444
+ const messageHash = keccak256(packed);
2445
+ const signature = await sign({
2446
+ hash: messageHash,
2447
+ privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
2448
+ to: "hex"
2449
+ });
2450
+ return {
2451
+ ctHash,
2452
+ decryptedValue,
2453
+ signature
2454
+ };
2455
+ }
2495
2456
  function normalizeSignature(signature) {
2496
2457
  if (typeof signature !== "string") {
2497
2458
  throw new CofheError({
@@ -2509,7 +2470,9 @@ function normalizeSignature(signature) {
2509
2470
  message: "decrypt response returned empty signature"
2510
2471
  });
2511
2472
  }
2512
- return trimmed.startsWith("0x") ? trimmed.slice(2) : trimmed;
2473
+ const prefixed = trimmed.startsWith("0x") ? trimmed : `0x${trimmed}`;
2474
+ const parsed = parseSignature(prefixed);
2475
+ return serializeSignature(parsed);
2513
2476
  }
2514
2477
  function parseDecryptedBytesToBigInt(decrypted) {
2515
2478
  if (!Array.isArray(decrypted)) {
@@ -2598,7 +2561,7 @@ function assertTnDecryptResponse(value) {
2598
2561
  }
2599
2562
  async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
2600
2563
  const body = {
2601
- ct_tempkey: ctHash.toString(16).padStart(64, "0"),
2564
+ ct_tempkey: BigInt(ctHash).toString(16).padStart(64, "0"),
2602
2565
  host_chain_id: chainId
2603
2566
  };
2604
2567
  if (permission) {
@@ -2828,7 +2791,9 @@ var DecryptForTxBuilder = class extends BaseBuilder {
2828
2791
  async mocksDecryptForTx(permit) {
2829
2792
  this.assertPublicClient();
2830
2793
  const delay = this.config.mocks.decryptDelay;
2831
- const result = await cofheMocksDecryptForTx(this.ctHash, 0, permit, this.publicClient, delay);
2794
+ if (delay > 0)
2795
+ await sleep(delay);
2796
+ const result = await cofheMocksDecryptForTx(this.ctHash, 0, permit, this.publicClient);
2832
2797
  return result;
2833
2798
  }
2834
2799
  /**
@@ -342,7 +342,7 @@ declare abstract class BaseBuilder {
342
342
  * Returns the unsealed item.
343
343
  */
344
344
  type DecryptForViewBuilderParams<U extends FheTypes> = BaseBuilderParams & {
345
- ctHash: bigint;
345
+ ctHash: bigint | string;
346
346
  utype: U;
347
347
  permitHash?: string;
348
348
  permit?: Permit;
@@ -466,12 +466,12 @@ declare class DecryptForViewBuilder<U extends FheTypes> extends BaseBuilder {
466
466
  }
467
467
 
468
468
  type DecryptForTxBuilderParams = BaseBuilderParams & {
469
- ctHash: bigint;
469
+ ctHash: bigint | string;
470
470
  };
471
471
  type DecryptForTxResult = {
472
- ctHash: bigint;
472
+ ctHash: bigint | string;
473
473
  decryptedValue: bigint;
474
- signature: string;
474
+ signature: `0x${string}`;
475
475
  };
476
476
  /**
477
477
  * Type-level gating:
@@ -924,9 +924,9 @@ type CofheClient<TConfig extends CofheConfig = CofheConfig> = {
924
924
  /**
925
925
  * @deprecated Use `decryptForView` instead. Kept for backward compatibility.
926
926
  */
927
- decryptHandle<U extends FheTypes>(ctHash: bigint, utype: U): DecryptForViewBuilder<U>;
928
- decryptForView<U extends FheTypes>(ctHash: bigint, utype: U): DecryptForViewBuilder<U>;
929
- decryptForTx(ctHash: bigint): DecryptForTxBuilderUnset;
927
+ decryptHandle<U extends FheTypes>(ctHash: bigint | string, utype: U): DecryptForViewBuilder<U>;
928
+ decryptForView<U extends FheTypes>(ctHash: bigint | string, utype: U): DecryptForViewBuilder<U>;
929
+ decryptForTx(ctHash: bigint | string): DecryptForTxBuilderUnset;
930
930
  permits: CofheClientPermits;
931
931
  };
932
932
  type CofheClientConnectionState = {
@@ -342,7 +342,7 @@ declare abstract class BaseBuilder {
342
342
  * Returns the unsealed item.
343
343
  */
344
344
  type DecryptForViewBuilderParams<U extends FheTypes> = BaseBuilderParams & {
345
- ctHash: bigint;
345
+ ctHash: bigint | string;
346
346
  utype: U;
347
347
  permitHash?: string;
348
348
  permit?: Permit;
@@ -466,12 +466,12 @@ declare class DecryptForViewBuilder<U extends FheTypes> extends BaseBuilder {
466
466
  }
467
467
 
468
468
  type DecryptForTxBuilderParams = BaseBuilderParams & {
469
- ctHash: bigint;
469
+ ctHash: bigint | string;
470
470
  };
471
471
  type DecryptForTxResult = {
472
- ctHash: bigint;
472
+ ctHash: bigint | string;
473
473
  decryptedValue: bigint;
474
- signature: string;
474
+ signature: `0x${string}`;
475
475
  };
476
476
  /**
477
477
  * Type-level gating:
@@ -924,9 +924,9 @@ type CofheClient<TConfig extends CofheConfig = CofheConfig> = {
924
924
  /**
925
925
  * @deprecated Use `decryptForView` instead. Kept for backward compatibility.
926
926
  */
927
- decryptHandle<U extends FheTypes>(ctHash: bigint, utype: U): DecryptForViewBuilder<U>;
928
- decryptForView<U extends FheTypes>(ctHash: bigint, utype: U): DecryptForViewBuilder<U>;
929
- decryptForTx(ctHash: bigint): DecryptForTxBuilderUnset;
927
+ decryptHandle<U extends FheTypes>(ctHash: bigint | string, utype: U): DecryptForViewBuilder<U>;
928
+ decryptForView<U extends FheTypes>(ctHash: bigint | string, utype: U): DecryptForViewBuilder<U>;
929
+ decryptForTx(ctHash: bigint | string): DecryptForTxBuilderUnset;
930
930
  permits: CofheClientPermits;
931
931
  };
932
932
  type CofheClientConnectionState = {
package/dist/core.cjs CHANGED
@@ -801,7 +801,7 @@ async function insertCtHashes(items, walletClient) {
801
801
  });
802
802
  }
803
803
  }
804
- async function createProofSignatures(items, securityZone) {
804
+ async function createProofSignatures(items, securityZone, account) {
805
805
  let signatures = [];
806
806
  let encInputSignerClient;
807
807
  try {
@@ -818,12 +818,15 @@ async function createProofSignatures(items, securityZone) {
818
818
  }
819
819
  try {
820
820
  for (const item of items) {
821
- const packedData = viem.encodePacked(["uint256", "int32", "uint8"], [BigInt(item.data), securityZone, item.utype]);
821
+ const packedData = viem.encodePacked(
822
+ ["uint256", "uint8", "uint8", "address", "uint256"],
823
+ [BigInt(item.ctHash), item.utype, securityZone, account, BigInt(chains.hardhat.id)]
824
+ );
822
825
  const messageHash = viem.keccak256(packedData);
823
- const ethSignedHash = viem.hashMessage({ raw: viem.toBytes(messageHash) });
824
- const signature = await encInputSignerClient.signMessage({
825
- message: { raw: viem.toBytes(ethSignedHash) },
826
- account: encInputSignerClient.account
826
+ const signature = await accounts.sign({
827
+ hash: messageHash,
828
+ privateKey: MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY,
829
+ to: "hex"
827
830
  });
828
831
  signatures.push(signature);
829
832
  }
@@ -854,7 +857,7 @@ async function cofheMocksZkVerifySign(items, account, securityZone, publicClient
854
857
  const _walletClient = zkvWalletClient ?? createMockZkVerifierSigner();
855
858
  const encryptableItems = await calcCtHashes(items, account, securityZone, publicClient);
856
859
  await insertCtHashes(encryptableItems, _walletClient);
857
- const signatures = await createProofSignatures(encryptableItems, securityZone);
860
+ const signatures = await createProofSignatures(encryptableItems, securityZone, account);
858
861
  return encryptableItems.map((item, index) => ({
859
862
  ct_hash: item.ctHash.toString(),
860
863
  signature: signatures[index]
@@ -2917,9 +2920,7 @@ var MockThresholdNetworkAbi = [
2917
2920
  ];
2918
2921
 
2919
2922
  // core/decrypt/cofheMocksDecryptForView.ts
2920
- async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient, mocksDecryptDelay) {
2921
- if (mocksDecryptDelay > 0)
2922
- await sleep(mocksDecryptDelay);
2923
+ async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient) {
2923
2924
  const permission = PermitUtils.getPermission(permit, true);
2924
2925
  const permissionWithBigInts = {
2925
2926
  ...permission,
@@ -2930,7 +2931,7 @@ async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient, moc
2930
2931
  address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
2931
2932
  abi: MockThresholdNetworkAbi,
2932
2933
  functionName: "querySealOutput",
2933
- args: [ctHash, BigInt(utype), permissionWithBigInts]
2934
+ args: [BigInt(ctHash), BigInt(utype), permissionWithBigInts]
2934
2935
  });
2935
2936
  if (error != "") {
2936
2937
  throw new CofheError({
@@ -2971,7 +2972,7 @@ function convertSealedData(sealed) {
2971
2972
  }
2972
2973
  async function submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, permission) {
2973
2974
  const body = {
2974
- ct_tempkey: ctHash.toString(16).padStart(64, "0"),
2975
+ ct_tempkey: BigInt(ctHash).toString(16).padStart(64, "0"),
2975
2976
  host_chain_id: chainId,
2976
2977
  permit: permission
2977
2978
  };
@@ -3164,96 +3165,6 @@ async function tnSealOutputV2(ctHash, chainId, permission, thresholdNetworkUrl)
3164
3165
  const requestId = await submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, permission);
3165
3166
  return await pollSealOutputStatus(thresholdNetworkUrl, requestId);
3166
3167
  }
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
- }
3257
3168
 
3258
3169
  // core/decrypt/decryptForViewBuilder.ts
3259
3170
  var DecryptForViewBuilder = class extends BaseBuilder {
@@ -3428,7 +3339,9 @@ var DecryptForViewBuilder = class extends BaseBuilder {
3428
3339
  async mocksSealOutput(permit) {
3429
3340
  this.assertPublicClient();
3430
3341
  const mocksDecryptDelay = this.config.mocks.decryptDelay;
3431
- return cofheMocksDecryptForView(this.ctHash, this.utype, permit, this.publicClient, mocksDecryptDelay);
3342
+ if (mocksDecryptDelay > 0)
3343
+ await sleep(mocksDecryptDelay);
3344
+ return cofheMocksDecryptForView(this.ctHash, this.utype, permit, this.publicClient);
3432
3345
  }
3433
3346
  /**
3434
3347
  * In the production context, perform a true decryption with the CoFHE coprocessor.
@@ -3477,8 +3390,56 @@ var DecryptForViewBuilder = class extends BaseBuilder {
3477
3390
  return convertViaUtype(this.utype, unsealed);
3478
3391
  }
3479
3392
  };
3480
-
3481
- // core/decrypt/tnDecrypt.ts
3393
+ async function cofheMocksDecryptForTx(ctHash, utype, permit, publicClient) {
3394
+ let allowed;
3395
+ let error;
3396
+ let decryptedValue;
3397
+ if (permit !== null) {
3398
+ let permission = PermitUtils.getPermission(permit, true);
3399
+ const permissionWithBigInts = {
3400
+ ...permission,
3401
+ expiration: BigInt(permission.expiration),
3402
+ validatorId: BigInt(permission.validatorId)
3403
+ };
3404
+ [allowed, error, decryptedValue] = await publicClient.readContract({
3405
+ address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
3406
+ abi: MockThresholdNetworkAbi,
3407
+ functionName: "decryptForTxWithPermit",
3408
+ args: [BigInt(ctHash), permissionWithBigInts]
3409
+ });
3410
+ } else {
3411
+ [allowed, error, decryptedValue] = await publicClient.readContract({
3412
+ address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
3413
+ abi: MockThresholdNetworkAbi,
3414
+ functionName: "decryptForTxWithoutPermit",
3415
+ args: [BigInt(ctHash)]
3416
+ });
3417
+ }
3418
+ if (error != "") {
3419
+ throw new CofheError({
3420
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3421
+ message: `mocks decryptForTx call failed: ${error}`
3422
+ });
3423
+ }
3424
+ if (allowed == false) {
3425
+ throw new CofheError({
3426
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3427
+ message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
3428
+ });
3429
+ }
3430
+ const packed = viem.encodePacked(["uint256", "uint256"], [BigInt(ctHash), decryptedValue]);
3431
+ const messageHash = viem.keccak256(packed);
3432
+ const signature = await accounts.sign({
3433
+ hash: messageHash,
3434
+ privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
3435
+ to: "hex"
3436
+ });
3437
+ return {
3438
+ ctHash,
3439
+ decryptedValue,
3440
+ signature
3441
+ };
3442
+ }
3482
3443
  function normalizeSignature(signature) {
3483
3444
  if (typeof signature !== "string") {
3484
3445
  throw new CofheError({
@@ -3496,7 +3457,9 @@ function normalizeSignature(signature) {
3496
3457
  message: "decrypt response returned empty signature"
3497
3458
  });
3498
3459
  }
3499
- return trimmed.startsWith("0x") ? trimmed.slice(2) : trimmed;
3460
+ const prefixed = trimmed.startsWith("0x") ? trimmed : `0x${trimmed}`;
3461
+ const parsed = viem.parseSignature(prefixed);
3462
+ return viem.serializeSignature(parsed);
3500
3463
  }
3501
3464
  function parseDecryptedBytesToBigInt(decrypted) {
3502
3465
  if (!Array.isArray(decrypted)) {
@@ -3585,7 +3548,7 @@ function assertTnDecryptResponse(value) {
3585
3548
  }
3586
3549
  async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3587
3550
  const body = {
3588
- ct_tempkey: ctHash.toString(16).padStart(64, "0"),
3551
+ ct_tempkey: BigInt(ctHash).toString(16).padStart(64, "0"),
3589
3552
  host_chain_id: chainId
3590
3553
  };
3591
3554
  if (permission) {
@@ -3815,7 +3778,9 @@ var DecryptForTxBuilder = class extends BaseBuilder {
3815
3778
  async mocksDecryptForTx(permit) {
3816
3779
  this.assertPublicClient();
3817
3780
  const delay = this.config.mocks.decryptDelay;
3818
- const result = await cofheMocksDecryptForTx(this.ctHash, 0, permit, this.publicClient, delay);
3781
+ if (delay > 0)
3782
+ await sleep(delay);
3783
+ const result = await cofheMocksDecryptForTx(this.ctHash, 0, permit, this.publicClient);
3819
3784
  return result;
3820
3785
  }
3821
3786
  /**
package/dist/core.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { c as CofheClientConnectionState, a as CofheConfig, d as CofheClientParams, b as CofheClient, F as FheTypes } from './clientTypes-Bhq7pCSA.cjs';
2
- export { h as CofheClientPermits, C as CofheInputConfig, f as CofheInternalConfig, Y as DecryptForTxBuilder, Z as DecryptForTxResult, X as DecryptForViewBuilder, W as EncryptInputsBuilder, B as EncryptSetStateFn, M as EncryptStep, D as EncryptStepCallbackContext, J as Encryptable, o as EncryptableAddress, i as EncryptableBool, E as EncryptableItem, z as EncryptableToEncryptedItemInputMap, n as EncryptableUint128, k as EncryptableUint16, l as EncryptableUint32, m as EncryptableUint64, j as EncryptableUint8, x as EncryptedAddressInput, r as EncryptedBoolInput, q as EncryptedItemInput, y as EncryptedItemInputs, p as EncryptedNumber, w as EncryptedUint128Input, t as EncryptedUint16Input, u as EncryptedUint32Input, v as EncryptedUint64Input, s as EncryptedUint8Input, H as FheAllUTypes, R as FheKeyDeserializer, A as FheTypeValue, G as FheUintUTypes, I as IStorage, T as KeysStorage, V as KeysStore, L as LiteralToPrimitive, P as Primitive, U as UnsealedItem, _ as ZkBuilderAndCrsGenerator, $ as ZkProveWorkerFunction, a0 as ZkProveWorkerRequest, a1 as ZkProveWorkerResponse, O as assertCorrectEncryptedItemInput, e as createCofheConfigBase, S as createKeysStore, Q as fetchKeys, g as getCofheConfigItem, K as isEncryptableItem, N as isLastEncryptionStep, a2 as zkProveWithWorker } from './clientTypes-Bhq7pCSA.cjs';
1
+ import { c as CofheClientConnectionState, a as CofheConfig, d as CofheClientParams, b as CofheClient, F as FheTypes } from './clientTypes-Y43CKbOz.cjs';
2
+ export { h as CofheClientPermits, C as CofheInputConfig, f as CofheInternalConfig, Y as DecryptForTxBuilder, Z as DecryptForTxResult, X as DecryptForViewBuilder, W as EncryptInputsBuilder, B as EncryptSetStateFn, M as EncryptStep, D as EncryptStepCallbackContext, J as Encryptable, o as EncryptableAddress, i as EncryptableBool, E as EncryptableItem, z as EncryptableToEncryptedItemInputMap, n as EncryptableUint128, k as EncryptableUint16, l as EncryptableUint32, m as EncryptableUint64, j as EncryptableUint8, x as EncryptedAddressInput, r as EncryptedBoolInput, q as EncryptedItemInput, y as EncryptedItemInputs, p as EncryptedNumber, w as EncryptedUint128Input, t as EncryptedUint16Input, u as EncryptedUint32Input, v as EncryptedUint64Input, s as EncryptedUint8Input, H as FheAllUTypes, R as FheKeyDeserializer, A as FheTypeValue, G as FheUintUTypes, I as IStorage, T as KeysStorage, V as KeysStore, L as LiteralToPrimitive, P as Primitive, U as UnsealedItem, _ as ZkBuilderAndCrsGenerator, $ as ZkProveWorkerFunction, a0 as ZkProveWorkerRequest, a1 as ZkProveWorkerResponse, O as assertCorrectEncryptedItemInput, e as createCofheConfigBase, S as createKeysStore, Q as fetchKeys, g as getCofheConfigItem, K as isEncryptableItem, N as isLastEncryptionStep, a2 as zkProveWithWorker } from './clientTypes-Y43CKbOz.cjs';
3
3
  import 'viem';
4
4
  import './types-YiAC4gig.cjs';
5
5
  import 'zod';
package/dist/core.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { c as CofheClientConnectionState, a as CofheConfig, d as CofheClientParams, b as CofheClient, F as FheTypes } from './clientTypes-6aTZPQ_4.js';
2
- export { h as CofheClientPermits, C as CofheInputConfig, f as CofheInternalConfig, Y as DecryptForTxBuilder, Z as DecryptForTxResult, X as DecryptForViewBuilder, W as EncryptInputsBuilder, B as EncryptSetStateFn, M as EncryptStep, D as EncryptStepCallbackContext, J as Encryptable, o as EncryptableAddress, i as EncryptableBool, E as EncryptableItem, z as EncryptableToEncryptedItemInputMap, n as EncryptableUint128, k as EncryptableUint16, l as EncryptableUint32, m as EncryptableUint64, j as EncryptableUint8, x as EncryptedAddressInput, r as EncryptedBoolInput, q as EncryptedItemInput, y as EncryptedItemInputs, p as EncryptedNumber, w as EncryptedUint128Input, t as EncryptedUint16Input, u as EncryptedUint32Input, v as EncryptedUint64Input, s as EncryptedUint8Input, H as FheAllUTypes, R as FheKeyDeserializer, A as FheTypeValue, G as FheUintUTypes, I as IStorage, T as KeysStorage, V as KeysStore, L as LiteralToPrimitive, P as Primitive, U as UnsealedItem, _ as ZkBuilderAndCrsGenerator, $ as ZkProveWorkerFunction, a0 as ZkProveWorkerRequest, a1 as ZkProveWorkerResponse, O as assertCorrectEncryptedItemInput, e as createCofheConfigBase, S as createKeysStore, Q as fetchKeys, g as getCofheConfigItem, K as isEncryptableItem, N as isLastEncryptionStep, a2 as zkProveWithWorker } from './clientTypes-6aTZPQ_4.js';
1
+ import { c as CofheClientConnectionState, a as CofheConfig, d as CofheClientParams, b as CofheClient, F as FheTypes } from './clientTypes-PQha8zes.js';
2
+ export { h as CofheClientPermits, C as CofheInputConfig, f as CofheInternalConfig, Y as DecryptForTxBuilder, Z as DecryptForTxResult, X as DecryptForViewBuilder, W as EncryptInputsBuilder, B as EncryptSetStateFn, M as EncryptStep, D as EncryptStepCallbackContext, J as Encryptable, o as EncryptableAddress, i as EncryptableBool, E as EncryptableItem, z as EncryptableToEncryptedItemInputMap, n as EncryptableUint128, k as EncryptableUint16, l as EncryptableUint32, m as EncryptableUint64, j as EncryptableUint8, x as EncryptedAddressInput, r as EncryptedBoolInput, q as EncryptedItemInput, y as EncryptedItemInputs, p as EncryptedNumber, w as EncryptedUint128Input, t as EncryptedUint16Input, u as EncryptedUint32Input, v as EncryptedUint64Input, s as EncryptedUint8Input, H as FheAllUTypes, R as FheKeyDeserializer, A as FheTypeValue, G as FheUintUTypes, I as IStorage, T as KeysStorage, V as KeysStore, L as LiteralToPrimitive, P as Primitive, U as UnsealedItem, _ as ZkBuilderAndCrsGenerator, $ as ZkProveWorkerFunction, a0 as ZkProveWorkerRequest, a1 as ZkProveWorkerResponse, O as assertCorrectEncryptedItemInput, e as createCofheConfigBase, S as createKeysStore, Q as fetchKeys, g as getCofheConfigItem, K as isEncryptableItem, N as isLastEncryptionStep, a2 as zkProveWithWorker } from './clientTypes-PQha8zes.js';
3
3
  import 'viem';
4
4
  import './types-YiAC4gig.js';
5
5
  import 'zod';
package/dist/core.js CHANGED
@@ -1,3 +1,3 @@
1
- export { InitialConnectStore as CONNECT_STORE_DEFAULTS, CofheError, CofheErrorCode, DecryptForTxBuilder, DecryptForViewBuilder, EncryptInputsBuilder, EncryptStep, Encryptable, FheAllUTypes, FheTypes, FheUintUTypes, assertCorrectEncryptedItemInput, createCofheClientBase, createCofheConfigBase, createKeysStore, fetchKeys, fheTypeToString, getCofheConfigItem, isCofheError, isEncryptableItem, isLastEncryptionStep, zkProveWithWorker } from './chunk-2TPSCOW3.js';
1
+ export { InitialConnectStore as CONNECT_STORE_DEFAULTS, CofheError, CofheErrorCode, DecryptForTxBuilder, DecryptForViewBuilder, EncryptInputsBuilder, EncryptStep, Encryptable, FheAllUTypes, FheTypes, FheUintUTypes, assertCorrectEncryptedItemInput, createCofheClientBase, createCofheConfigBase, createKeysStore, fetchKeys, fheTypeToString, getCofheConfigItem, isCofheError, isEncryptableItem, isLastEncryptionStep, zkProveWithWorker } from './chunk-LWMRB6SD.js';
2
2
  import './chunk-TBLR7NNE.js';
3
3
  export { MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY, MOCKS_THRESHOLD_NETWORK_ADDRESS, MOCKS_ZK_VERIFIER_ADDRESS, MOCKS_ZK_VERIFIER_SIGNER_ADDRESS, MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY, TASK_MANAGER_ADDRESS, TEST_BED_ADDRESS } from './chunk-NWDKXBIP.js';