@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.
package/dist/node.cjs CHANGED
@@ -613,7 +613,7 @@ async function insertCtHashes(items, walletClient) {
613
613
  });
614
614
  }
615
615
  }
616
- async function createProofSignatures(items, securityZone) {
616
+ async function createProofSignatures(items, securityZone, account) {
617
617
  let signatures = [];
618
618
  let encInputSignerClient;
619
619
  try {
@@ -630,12 +630,15 @@ async function createProofSignatures(items, securityZone) {
630
630
  }
631
631
  try {
632
632
  for (const item of items) {
633
- const packedData = viem.encodePacked(["uint256", "int32", "uint8"], [BigInt(item.data), securityZone, item.utype]);
633
+ const packedData = viem.encodePacked(
634
+ ["uint256", "uint8", "uint8", "address", "uint256"],
635
+ [BigInt(item.ctHash), item.utype, securityZone, account, BigInt(chains.hardhat.id)]
636
+ );
634
637
  const messageHash = viem.keccak256(packedData);
635
- const ethSignedHash = viem.hashMessage({ raw: viem.toBytes(messageHash) });
636
- const signature = await encInputSignerClient.signMessage({
637
- message: { raw: viem.toBytes(ethSignedHash) },
638
- account: encInputSignerClient.account
638
+ const signature = await accounts.sign({
639
+ hash: messageHash,
640
+ privateKey: MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY,
641
+ to: "hex"
639
642
  });
640
643
  signatures.push(signature);
641
644
  }
@@ -666,7 +669,7 @@ async function cofheMocksZkVerifySign(items, account, securityZone, publicClient
666
669
  const _walletClient = zkvWalletClient ?? createMockZkVerifierSigner();
667
670
  const encryptableItems = await calcCtHashes(items, account, securityZone, publicClient);
668
671
  await insertCtHashes(encryptableItems, _walletClient);
669
- const signatures = await createProofSignatures(encryptableItems, securityZone);
672
+ const signatures = await createProofSignatures(encryptableItems, securityZone, account);
670
673
  return encryptableItems.map((item, index) => ({
671
674
  ct_hash: item.ctHash.toString(),
672
675
  signature: signatures[index]
@@ -2726,9 +2729,7 @@ var MockThresholdNetworkAbi = [
2726
2729
  ];
2727
2730
 
2728
2731
  // core/decrypt/cofheMocksDecryptForView.ts
2729
- async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient, mocksDecryptDelay) {
2730
- if (mocksDecryptDelay > 0)
2731
- await sleep(mocksDecryptDelay);
2732
+ async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient) {
2732
2733
  const permission = PermitUtils.getPermission(permit, true);
2733
2734
  const permissionWithBigInts = {
2734
2735
  ...permission,
@@ -2739,7 +2740,7 @@ async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient, moc
2739
2740
  address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
2740
2741
  abi: MockThresholdNetworkAbi,
2741
2742
  functionName: "querySealOutput",
2742
- args: [ctHash, BigInt(utype), permissionWithBigInts]
2743
+ args: [BigInt(ctHash), BigInt(utype), permissionWithBigInts]
2743
2744
  });
2744
2745
  if (error != "") {
2745
2746
  throw new CofheError({
@@ -2780,7 +2781,7 @@ function convertSealedData(sealed) {
2780
2781
  }
2781
2782
  async function submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, permission) {
2782
2783
  const body = {
2783
- ct_tempkey: ctHash.toString(16).padStart(64, "0"),
2784
+ ct_tempkey: BigInt(ctHash).toString(16).padStart(64, "0"),
2784
2785
  host_chain_id: chainId,
2785
2786
  permit: permission
2786
2787
  };
@@ -2973,96 +2974,6 @@ async function tnSealOutputV2(ctHash, chainId, permission, thresholdNetworkUrl)
2973
2974
  const requestId = await submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, permission);
2974
2975
  return await pollSealOutputStatus(thresholdNetworkUrl, requestId);
2975
2976
  }
2976
- async function cofheMocksDecryptForTx(ctHash, utype, permit, publicClient, mocksDecryptForTxDelay) {
2977
- if (mocksDecryptForTxDelay > 0)
2978
- await sleep(mocksDecryptForTxDelay);
2979
- if (permit !== null) {
2980
- let permission = PermitUtils.getPermission(permit, true);
2981
- const permissionWithBigInts = {
2982
- ...permission,
2983
- expiration: BigInt(permission.expiration),
2984
- validatorId: BigInt(permission.validatorId)
2985
- };
2986
- const [allowed2, error2, result2] = await publicClient.readContract({
2987
- address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
2988
- abi: MockThresholdNetworkAbi,
2989
- functionName: "decryptForTxWithPermit",
2990
- args: [ctHash, permissionWithBigInts]
2991
- });
2992
- if (error2 != "") {
2993
- throw new CofheError({
2994
- code: "DECRYPT_FAILED" /* DecryptFailed */,
2995
- message: `mocks decryptForTx call failed: ${error2}`
2996
- });
2997
- }
2998
- if (allowed2 == false) {
2999
- throw new CofheError({
3000
- code: "DECRYPT_FAILED" /* DecryptFailed */,
3001
- message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
3002
- });
3003
- }
3004
- const chainId2 = await publicClient.getChainId();
3005
- const ctHashBigInt2 = BigInt(ctHash);
3006
- const resultBigInt2 = BigInt(result2);
3007
- const encryptionType2 = Number((ctHashBigInt2 & 0x7fn << 8n) >> 8n);
3008
- const ctHashBytes322 = viem.pad(viem.toHex(ctHashBigInt2), { size: 32 });
3009
- const packed2 = viem.encodePacked(
3010
- ["uint256", "uint32", "uint64", "bytes32"],
3011
- [resultBigInt2, encryptionType2, BigInt(chainId2), ctHashBytes322]
3012
- );
3013
- const messageHash2 = viem.keccak256(packed2);
3014
- const signatureHex2 = await accounts.sign({
3015
- hash: messageHash2,
3016
- privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
3017
- to: "hex"
3018
- });
3019
- const signature2 = signatureHex2.slice(2);
3020
- return {
3021
- ctHash,
3022
- decryptedValue: BigInt(result2),
3023
- signature: signature2
3024
- };
3025
- }
3026
- const [allowed, error, result] = await publicClient.readContract({
3027
- address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
3028
- abi: MockThresholdNetworkAbi,
3029
- functionName: "decryptForTxWithoutPermit",
3030
- args: [ctHash]
3031
- });
3032
- if (error != "") {
3033
- throw new CofheError({
3034
- code: "DECRYPT_FAILED" /* DecryptFailed */,
3035
- message: `mocks decryptForTx call failed: ${error}`
3036
- });
3037
- }
3038
- if (allowed == false) {
3039
- throw new CofheError({
3040
- code: "DECRYPT_FAILED" /* DecryptFailed */,
3041
- message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
3042
- });
3043
- }
3044
- const chainId = await publicClient.getChainId();
3045
- const ctHashBigInt = BigInt(ctHash);
3046
- const resultBigInt = BigInt(result);
3047
- const encryptionType = Number((ctHashBigInt & 0x7fn << 8n) >> 8n);
3048
- const ctHashBytes32 = viem.pad(viem.toHex(ctHashBigInt), { size: 32 });
3049
- const packed = viem.encodePacked(
3050
- ["uint256", "uint32", "uint64", "bytes32"],
3051
- [resultBigInt, encryptionType, BigInt(chainId), ctHashBytes32]
3052
- );
3053
- const messageHash = viem.keccak256(packed);
3054
- const signatureHex = await accounts.sign({
3055
- hash: messageHash,
3056
- privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
3057
- to: "hex"
3058
- });
3059
- const signature = signatureHex.slice(2);
3060
- return {
3061
- ctHash,
3062
- decryptedValue: BigInt(result),
3063
- signature
3064
- };
3065
- }
3066
2977
 
3067
2978
  // core/decrypt/decryptForViewBuilder.ts
3068
2979
  var DecryptForViewBuilder = class extends BaseBuilder {
@@ -3237,7 +3148,9 @@ var DecryptForViewBuilder = class extends BaseBuilder {
3237
3148
  async mocksSealOutput(permit) {
3238
3149
  this.assertPublicClient();
3239
3150
  const mocksDecryptDelay = this.config.mocks.decryptDelay;
3240
- return cofheMocksDecryptForView(this.ctHash, this.utype, permit, this.publicClient, mocksDecryptDelay);
3151
+ if (mocksDecryptDelay > 0)
3152
+ await sleep(mocksDecryptDelay);
3153
+ return cofheMocksDecryptForView(this.ctHash, this.utype, permit, this.publicClient);
3241
3154
  }
3242
3155
  /**
3243
3156
  * In the production context, perform a true decryption with the CoFHE coprocessor.
@@ -3286,8 +3199,56 @@ var DecryptForViewBuilder = class extends BaseBuilder {
3286
3199
  return convertViaUtype(this.utype, unsealed);
3287
3200
  }
3288
3201
  };
3289
-
3290
- // core/decrypt/tnDecrypt.ts
3202
+ async function cofheMocksDecryptForTx(ctHash, utype, permit, publicClient) {
3203
+ let allowed;
3204
+ let error;
3205
+ let decryptedValue;
3206
+ if (permit !== null) {
3207
+ let permission = PermitUtils.getPermission(permit, true);
3208
+ const permissionWithBigInts = {
3209
+ ...permission,
3210
+ expiration: BigInt(permission.expiration),
3211
+ validatorId: BigInt(permission.validatorId)
3212
+ };
3213
+ [allowed, error, decryptedValue] = await publicClient.readContract({
3214
+ address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
3215
+ abi: MockThresholdNetworkAbi,
3216
+ functionName: "decryptForTxWithPermit",
3217
+ args: [BigInt(ctHash), permissionWithBigInts]
3218
+ });
3219
+ } else {
3220
+ [allowed, error, decryptedValue] = await publicClient.readContract({
3221
+ address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
3222
+ abi: MockThresholdNetworkAbi,
3223
+ functionName: "decryptForTxWithoutPermit",
3224
+ args: [BigInt(ctHash)]
3225
+ });
3226
+ }
3227
+ if (error != "") {
3228
+ throw new CofheError({
3229
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3230
+ message: `mocks decryptForTx call failed: ${error}`
3231
+ });
3232
+ }
3233
+ if (allowed == false) {
3234
+ throw new CofheError({
3235
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3236
+ message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
3237
+ });
3238
+ }
3239
+ const packed = viem.encodePacked(["uint256", "uint256"], [BigInt(ctHash), decryptedValue]);
3240
+ const messageHash = viem.keccak256(packed);
3241
+ const signature = await accounts.sign({
3242
+ hash: messageHash,
3243
+ privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
3244
+ to: "hex"
3245
+ });
3246
+ return {
3247
+ ctHash,
3248
+ decryptedValue,
3249
+ signature
3250
+ };
3251
+ }
3291
3252
  function normalizeSignature(signature) {
3292
3253
  if (typeof signature !== "string") {
3293
3254
  throw new CofheError({
@@ -3305,7 +3266,9 @@ function normalizeSignature(signature) {
3305
3266
  message: "decrypt response returned empty signature"
3306
3267
  });
3307
3268
  }
3308
- return trimmed.startsWith("0x") ? trimmed.slice(2) : trimmed;
3269
+ const prefixed = trimmed.startsWith("0x") ? trimmed : `0x${trimmed}`;
3270
+ const parsed = viem.parseSignature(prefixed);
3271
+ return viem.serializeSignature(parsed);
3309
3272
  }
3310
3273
  function parseDecryptedBytesToBigInt(decrypted) {
3311
3274
  if (!Array.isArray(decrypted)) {
@@ -3394,7 +3357,7 @@ function assertTnDecryptResponse(value) {
3394
3357
  }
3395
3358
  async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3396
3359
  const body = {
3397
- ct_tempkey: ctHash.toString(16).padStart(64, "0"),
3360
+ ct_tempkey: BigInt(ctHash).toString(16).padStart(64, "0"),
3398
3361
  host_chain_id: chainId
3399
3362
  };
3400
3363
  if (permission) {
@@ -3624,7 +3587,9 @@ var DecryptForTxBuilder = class extends BaseBuilder {
3624
3587
  async mocksDecryptForTx(permit) {
3625
3588
  this.assertPublicClient();
3626
3589
  const delay = this.config.mocks.decryptDelay;
3627
- const result = await cofheMocksDecryptForTx(this.ctHash, 0, permit, this.publicClient, delay);
3590
+ if (delay > 0)
3591
+ await sleep(delay);
3592
+ const result = await cofheMocksDecryptForTx(this.ctHash, 0, permit, this.publicClient);
3628
3593
  return result;
3629
3594
  }
3630
3595
  /**
package/dist/node.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as CofheInputConfig, a as CofheConfig, b as CofheClient } from './clientTypes-Bhq7pCSA.cjs';
1
+ import { C as CofheInputConfig, a as CofheConfig, b as CofheClient } from './clientTypes-Y43CKbOz.cjs';
2
2
  import 'viem';
3
3
  import './types-YiAC4gig.cjs';
4
4
  import 'zod';
package/dist/node.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as CofheInputConfig, a as CofheConfig, b as CofheClient } from './clientTypes-6aTZPQ_4.js';
1
+ import { C as CofheInputConfig, a as CofheConfig, b as CofheClient } from './clientTypes-PQha8zes.js';
2
2
  import 'viem';
3
3
  import './types-YiAC4gig.js';
4
4
  import 'zod';
package/dist/node.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createCofheConfigBase, createCofheClientBase } from './chunk-2TPSCOW3.js';
1
+ import { createCofheConfigBase, createCofheClientBase } from './chunk-LWMRB6SD.js';
2
2
  import './chunk-TBLR7NNE.js';
3
3
  import './chunk-NWDKXBIP.js';
4
4
  import { promises } from 'fs';
package/dist/web.cjs CHANGED
@@ -652,7 +652,7 @@ async function insertCtHashes(items, walletClient) {
652
652
  });
653
653
  }
654
654
  }
655
- async function createProofSignatures(items, securityZone) {
655
+ async function createProofSignatures(items, securityZone, account) {
656
656
  let signatures = [];
657
657
  let encInputSignerClient;
658
658
  try {
@@ -669,12 +669,15 @@ async function createProofSignatures(items, securityZone) {
669
669
  }
670
670
  try {
671
671
  for (const item of items) {
672
- const packedData = viem.encodePacked(["uint256", "int32", "uint8"], [BigInt(item.data), securityZone, item.utype]);
672
+ const packedData = viem.encodePacked(
673
+ ["uint256", "uint8", "uint8", "address", "uint256"],
674
+ [BigInt(item.ctHash), item.utype, securityZone, account, BigInt(chains.hardhat.id)]
675
+ );
673
676
  const messageHash = viem.keccak256(packedData);
674
- const ethSignedHash = viem.hashMessage({ raw: viem.toBytes(messageHash) });
675
- const signature = await encInputSignerClient.signMessage({
676
- message: { raw: viem.toBytes(ethSignedHash) },
677
- account: encInputSignerClient.account
677
+ const signature = await accounts.sign({
678
+ hash: messageHash,
679
+ privateKey: MOCKS_ZK_VERIFIER_SIGNER_PRIVATE_KEY,
680
+ to: "hex"
678
681
  });
679
682
  signatures.push(signature);
680
683
  }
@@ -705,7 +708,7 @@ async function cofheMocksZkVerifySign(items, account, securityZone, publicClient
705
708
  const _walletClient = zkvWalletClient ?? createMockZkVerifierSigner();
706
709
  const encryptableItems = await calcCtHashes(items, account, securityZone, publicClient);
707
710
  await insertCtHashes(encryptableItems, _walletClient);
708
- const signatures = await createProofSignatures(encryptableItems, securityZone);
711
+ const signatures = await createProofSignatures(encryptableItems, securityZone, account);
709
712
  return encryptableItems.map((item, index) => ({
710
713
  ct_hash: item.ctHash.toString(),
711
714
  signature: signatures[index]
@@ -2765,9 +2768,7 @@ var MockThresholdNetworkAbi = [
2765
2768
  ];
2766
2769
 
2767
2770
  // core/decrypt/cofheMocksDecryptForView.ts
2768
- async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient, mocksDecryptDelay) {
2769
- if (mocksDecryptDelay > 0)
2770
- await sleep(mocksDecryptDelay);
2771
+ async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient) {
2771
2772
  const permission = PermitUtils.getPermission(permit, true);
2772
2773
  const permissionWithBigInts = {
2773
2774
  ...permission,
@@ -2778,7 +2779,7 @@ async function cofheMocksDecryptForView(ctHash, utype, permit, publicClient, moc
2778
2779
  address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
2779
2780
  abi: MockThresholdNetworkAbi,
2780
2781
  functionName: "querySealOutput",
2781
- args: [ctHash, BigInt(utype), permissionWithBigInts]
2782
+ args: [BigInt(ctHash), BigInt(utype), permissionWithBigInts]
2782
2783
  });
2783
2784
  if (error != "") {
2784
2785
  throw new CofheError({
@@ -2819,7 +2820,7 @@ function convertSealedData(sealed) {
2819
2820
  }
2820
2821
  async function submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, permission) {
2821
2822
  const body = {
2822
- ct_tempkey: ctHash.toString(16).padStart(64, "0"),
2823
+ ct_tempkey: BigInt(ctHash).toString(16).padStart(64, "0"),
2823
2824
  host_chain_id: chainId,
2824
2825
  permit: permission
2825
2826
  };
@@ -3012,96 +3013,6 @@ async function tnSealOutputV2(ctHash, chainId, permission, thresholdNetworkUrl)
3012
3013
  const requestId = await submitSealOutputRequest(thresholdNetworkUrl, ctHash, chainId, permission);
3013
3014
  return await pollSealOutputStatus(thresholdNetworkUrl, requestId);
3014
3015
  }
3015
- async function cofheMocksDecryptForTx(ctHash, utype, permit, publicClient, mocksDecryptForTxDelay) {
3016
- if (mocksDecryptForTxDelay > 0)
3017
- await sleep(mocksDecryptForTxDelay);
3018
- if (permit !== null) {
3019
- let permission = PermitUtils.getPermission(permit, true);
3020
- const permissionWithBigInts = {
3021
- ...permission,
3022
- expiration: BigInt(permission.expiration),
3023
- validatorId: BigInt(permission.validatorId)
3024
- };
3025
- const [allowed2, error2, result2] = await publicClient.readContract({
3026
- address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
3027
- abi: MockThresholdNetworkAbi,
3028
- functionName: "decryptForTxWithPermit",
3029
- args: [ctHash, permissionWithBigInts]
3030
- });
3031
- if (error2 != "") {
3032
- throw new CofheError({
3033
- code: "DECRYPT_FAILED" /* DecryptFailed */,
3034
- message: `mocks decryptForTx call failed: ${error2}`
3035
- });
3036
- }
3037
- if (allowed2 == false) {
3038
- throw new CofheError({
3039
- code: "DECRYPT_FAILED" /* DecryptFailed */,
3040
- message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
3041
- });
3042
- }
3043
- const chainId2 = await publicClient.getChainId();
3044
- const ctHashBigInt2 = BigInt(ctHash);
3045
- const resultBigInt2 = BigInt(result2);
3046
- const encryptionType2 = Number((ctHashBigInt2 & 0x7fn << 8n) >> 8n);
3047
- const ctHashBytes322 = viem.pad(viem.toHex(ctHashBigInt2), { size: 32 });
3048
- const packed2 = viem.encodePacked(
3049
- ["uint256", "uint32", "uint64", "bytes32"],
3050
- [resultBigInt2, encryptionType2, BigInt(chainId2), ctHashBytes322]
3051
- );
3052
- const messageHash2 = viem.keccak256(packed2);
3053
- const signatureHex2 = await accounts.sign({
3054
- hash: messageHash2,
3055
- privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
3056
- to: "hex"
3057
- });
3058
- const signature2 = signatureHex2.slice(2);
3059
- return {
3060
- ctHash,
3061
- decryptedValue: BigInt(result2),
3062
- signature: signature2
3063
- };
3064
- }
3065
- const [allowed, error, result] = await publicClient.readContract({
3066
- address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
3067
- abi: MockThresholdNetworkAbi,
3068
- functionName: "decryptForTxWithoutPermit",
3069
- args: [ctHash]
3070
- });
3071
- if (error != "") {
3072
- throw new CofheError({
3073
- code: "DECRYPT_FAILED" /* DecryptFailed */,
3074
- message: `mocks decryptForTx call failed: ${error}`
3075
- });
3076
- }
3077
- if (allowed == false) {
3078
- throw new CofheError({
3079
- code: "DECRYPT_FAILED" /* DecryptFailed */,
3080
- message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
3081
- });
3082
- }
3083
- const chainId = await publicClient.getChainId();
3084
- const ctHashBigInt = BigInt(ctHash);
3085
- const resultBigInt = BigInt(result);
3086
- const encryptionType = Number((ctHashBigInt & 0x7fn << 8n) >> 8n);
3087
- const ctHashBytes32 = viem.pad(viem.toHex(ctHashBigInt), { size: 32 });
3088
- const packed = viem.encodePacked(
3089
- ["uint256", "uint32", "uint64", "bytes32"],
3090
- [resultBigInt, encryptionType, BigInt(chainId), ctHashBytes32]
3091
- );
3092
- const messageHash = viem.keccak256(packed);
3093
- const signatureHex = await accounts.sign({
3094
- hash: messageHash,
3095
- privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
3096
- to: "hex"
3097
- });
3098
- const signature = signatureHex.slice(2);
3099
- return {
3100
- ctHash,
3101
- decryptedValue: BigInt(result),
3102
- signature
3103
- };
3104
- }
3105
3016
 
3106
3017
  // core/decrypt/decryptForViewBuilder.ts
3107
3018
  var DecryptForViewBuilder = class extends BaseBuilder {
@@ -3276,7 +3187,9 @@ var DecryptForViewBuilder = class extends BaseBuilder {
3276
3187
  async mocksSealOutput(permit) {
3277
3188
  this.assertPublicClient();
3278
3189
  const mocksDecryptDelay = this.config.mocks.decryptDelay;
3279
- return cofheMocksDecryptForView(this.ctHash, this.utype, permit, this.publicClient, mocksDecryptDelay);
3190
+ if (mocksDecryptDelay > 0)
3191
+ await sleep(mocksDecryptDelay);
3192
+ return cofheMocksDecryptForView(this.ctHash, this.utype, permit, this.publicClient);
3280
3193
  }
3281
3194
  /**
3282
3195
  * In the production context, perform a true decryption with the CoFHE coprocessor.
@@ -3325,8 +3238,56 @@ var DecryptForViewBuilder = class extends BaseBuilder {
3325
3238
  return convertViaUtype(this.utype, unsealed);
3326
3239
  }
3327
3240
  };
3328
-
3329
- // core/decrypt/tnDecrypt.ts
3241
+ async function cofheMocksDecryptForTx(ctHash, utype, permit, publicClient) {
3242
+ let allowed;
3243
+ let error;
3244
+ let decryptedValue;
3245
+ if (permit !== null) {
3246
+ let permission = PermitUtils.getPermission(permit, true);
3247
+ const permissionWithBigInts = {
3248
+ ...permission,
3249
+ expiration: BigInt(permission.expiration),
3250
+ validatorId: BigInt(permission.validatorId)
3251
+ };
3252
+ [allowed, error, decryptedValue] = await publicClient.readContract({
3253
+ address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
3254
+ abi: MockThresholdNetworkAbi,
3255
+ functionName: "decryptForTxWithPermit",
3256
+ args: [BigInt(ctHash), permissionWithBigInts]
3257
+ });
3258
+ } else {
3259
+ [allowed, error, decryptedValue] = await publicClient.readContract({
3260
+ address: MOCKS_THRESHOLD_NETWORK_ADDRESS,
3261
+ abi: MockThresholdNetworkAbi,
3262
+ functionName: "decryptForTxWithoutPermit",
3263
+ args: [BigInt(ctHash)]
3264
+ });
3265
+ }
3266
+ if (error != "") {
3267
+ throw new CofheError({
3268
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3269
+ message: `mocks decryptForTx call failed: ${error}`
3270
+ });
3271
+ }
3272
+ if (allowed == false) {
3273
+ throw new CofheError({
3274
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3275
+ message: `mocks decryptForTx call failed: ACL Access Denied (NotAllowed)`
3276
+ });
3277
+ }
3278
+ const packed = viem.encodePacked(["uint256", "uint256"], [BigInt(ctHash), decryptedValue]);
3279
+ const messageHash = viem.keccak256(packed);
3280
+ const signature = await accounts.sign({
3281
+ hash: messageHash,
3282
+ privateKey: MOCKS_DECRYPT_RESULT_SIGNER_PRIVATE_KEY,
3283
+ to: "hex"
3284
+ });
3285
+ return {
3286
+ ctHash,
3287
+ decryptedValue,
3288
+ signature
3289
+ };
3290
+ }
3330
3291
  function normalizeSignature(signature) {
3331
3292
  if (typeof signature !== "string") {
3332
3293
  throw new CofheError({
@@ -3344,7 +3305,9 @@ function normalizeSignature(signature) {
3344
3305
  message: "decrypt response returned empty signature"
3345
3306
  });
3346
3307
  }
3347
- return trimmed.startsWith("0x") ? trimmed.slice(2) : trimmed;
3308
+ const prefixed = trimmed.startsWith("0x") ? trimmed : `0x${trimmed}`;
3309
+ const parsed = viem.parseSignature(prefixed);
3310
+ return viem.serializeSignature(parsed);
3348
3311
  }
3349
3312
  function parseDecryptedBytesToBigInt(decrypted) {
3350
3313
  if (!Array.isArray(decrypted)) {
@@ -3433,7 +3396,7 @@ function assertTnDecryptResponse(value) {
3433
3396
  }
3434
3397
  async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3435
3398
  const body = {
3436
- ct_tempkey: ctHash.toString(16).padStart(64, "0"),
3399
+ ct_tempkey: BigInt(ctHash).toString(16).padStart(64, "0"),
3437
3400
  host_chain_id: chainId
3438
3401
  };
3439
3402
  if (permission) {
@@ -3663,7 +3626,9 @@ var DecryptForTxBuilder = class extends BaseBuilder {
3663
3626
  async mocksDecryptForTx(permit) {
3664
3627
  this.assertPublicClient();
3665
3628
  const delay = this.config.mocks.decryptDelay;
3666
- const result = await cofheMocksDecryptForTx(this.ctHash, 0, permit, this.publicClient, delay);
3629
+ if (delay > 0)
3630
+ await sleep(delay);
3631
+ const result = await cofheMocksDecryptForTx(this.ctHash, 0, permit, this.publicClient);
3667
3632
  return result;
3668
3633
  }
3669
3634
  /**
package/dist/web.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-Bhq7pCSA.cjs';
1
+ import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-Y43CKbOz.cjs';
2
2
  import 'viem';
3
3
  import './types-YiAC4gig.cjs';
4
4
  import 'zod';
package/dist/web.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-6aTZPQ_4.js';
1
+ import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-PQha8zes.js';
2
2
  import 'viem';
3
3
  import './types-YiAC4gig.js';
4
4
  import 'zod';
package/dist/web.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createCofheConfigBase, createCofheClientBase, fheTypeToString } from './chunk-2TPSCOW3.js';
1
+ import { createCofheConfigBase, createCofheClientBase, fheTypeToString } from './chunk-LWMRB6SD.js';
2
2
  import './chunk-TBLR7NNE.js';
3
3
  import './chunk-NWDKXBIP.js';
4
4
  import { constructClient } from 'iframe-shared-storage';
@@ -136,7 +136,7 @@ describe('@cofhe/node - Client Integration Tests', () => {
136
136
  it('should create decrypt builder after connection', async () => {
137
137
  await cofheClient.connect(publicClient, walletClient);
138
138
 
139
- const builder = cofheClient.decryptForView(123n, 2);
139
+ const builder = cofheClient.decryptForView('0x123', 2);
140
140
 
141
141
  expect(builder).toBeDefined();
142
142
  expect(typeof builder.setChainId).toBe('function');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cofhe/sdk",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "description": "SDK for Fhenix COFHE coprocessor interaction",
6
6
  "main": "./dist/core.cjs",
@@ -136,7 +136,7 @@ describe('@cofhe/web - Client', () => {
136
136
  it('should create decrypt builder after connection', async () => {
137
137
  await cofheClient.connect(publicClient, walletClient);
138
138
 
139
- const builder = cofheClient.decryptForView(123n, 2);
139
+ const builder = cofheClient.decryptForView('0x123', 2);
140
140
 
141
141
  expect(builder).toBeDefined();
142
142
  expect(typeof builder.setChainId).toBe('function');