@cofhe/sdk 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/web.cjs CHANGED
@@ -2513,13 +2513,13 @@ var serialize = (permit) => {
2513
2513
  var deserialize = (serialized) => {
2514
2514
  return PermitUtils.deserialize(serialized);
2515
2515
  };
2516
- var getPermit2 = async (chainId, account, hash) => {
2516
+ var getPermit2 = (chainId, account, hash) => {
2517
2517
  return permitStore.getPermit(chainId, account, hash);
2518
2518
  };
2519
- var getPermits2 = async (chainId, account) => {
2519
+ var getPermits2 = (chainId, account) => {
2520
2520
  return permitStore.getPermits(chainId, account);
2521
2521
  };
2522
- var getActivePermit2 = async (chainId, account) => {
2522
+ var getActivePermit2 = (chainId, account) => {
2523
2523
  return permitStore.getActivePermit(chainId, account);
2524
2524
  };
2525
2525
  var getActivePermitHash2 = (chainId, account) => {
@@ -3288,7 +3288,7 @@ async function cofheMocksDecryptForTx(ctHash, utype, permit, publicClient) {
3288
3288
  signature
3289
3289
  };
3290
3290
  }
3291
- function normalizeSignature(signature) {
3291
+ function normalizeTnSignature(signature) {
3292
3292
  if (typeof signature !== "string") {
3293
3293
  throw new CofheError({
3294
3294
  code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
@@ -3344,57 +3344,77 @@ function parseDecryptedBytesToBigInt(decrypted) {
3344
3344
  }
3345
3345
  return BigInt(`0x${hex}`);
3346
3346
  }
3347
- function assertTnDecryptResponse(value) {
3347
+
3348
+ // core/decrypt/tnDecryptV2.ts
3349
+ var POLL_INTERVAL_MS2 = 1e3;
3350
+ var POLL_TIMEOUT_MS2 = 5 * 60 * 1e3;
3351
+ function assertDecryptSubmitResponseV2(value) {
3348
3352
  if (value == null || typeof value !== "object") {
3349
3353
  throw new CofheError({
3350
3354
  code: "DECRYPT_FAILED" /* DecryptFailed */,
3351
- message: "decrypt response must be a JSON object",
3355
+ message: "decrypt submit response must be a JSON object",
3352
3356
  context: {
3353
3357
  value
3354
3358
  }
3355
3359
  });
3356
3360
  }
3357
3361
  const v = value;
3358
- const decrypted = v.decrypted;
3359
- const signature = v.signature;
3360
- const encryptionType = v.encryption_type;
3361
- const errorMessage = v.error_message;
3362
- if (!Array.isArray(decrypted)) {
3362
+ if (typeof v.request_id !== "string" || v.request_id.trim().length === 0) {
3363
3363
  throw new CofheError({
3364
- code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
3365
- message: "decrypt response missing <decrypted> byte array",
3366
- context: { decryptResponse: value }
3364
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3365
+ message: "decrypt submit response missing request_id",
3366
+ context: {
3367
+ value
3368
+ }
3367
3369
  });
3368
3370
  }
3369
- if (typeof signature !== "string") {
3371
+ return { request_id: v.request_id };
3372
+ }
3373
+ function assertDecryptStatusResponseV2(value) {
3374
+ if (value == null || typeof value !== "object") {
3370
3375
  throw new CofheError({
3371
- code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
3372
- message: "decrypt response missing <signature> string",
3373
- context: { decryptResponse: value }
3376
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3377
+ message: "decrypt status response must be a JSON object",
3378
+ context: {
3379
+ value
3380
+ }
3381
+ });
3382
+ }
3383
+ const v = value;
3384
+ const requestId = v.request_id;
3385
+ const status = v.status;
3386
+ const submittedAt = v.submitted_at;
3387
+ if (typeof requestId !== "string" || requestId.trim().length === 0) {
3388
+ throw new CofheError({
3389
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3390
+ message: "decrypt status response missing request_id",
3391
+ context: {
3392
+ value
3393
+ }
3374
3394
  });
3375
3395
  }
3376
- if (typeof encryptionType !== "number") {
3396
+ if (status !== "PROCESSING" && status !== "COMPLETED") {
3377
3397
  throw new CofheError({
3378
3398
  code: "DECRYPT_FAILED" /* DecryptFailed */,
3379
- message: "decrypt response missing <encryption_type> number",
3380
- context: { decryptResponse: value }
3399
+ message: "decrypt status response has invalid status",
3400
+ context: {
3401
+ value,
3402
+ status
3403
+ }
3381
3404
  });
3382
3405
  }
3383
- if (!(typeof errorMessage === "string" || errorMessage === null)) {
3406
+ if (typeof submittedAt !== "string" || submittedAt.trim().length === 0) {
3384
3407
  throw new CofheError({
3385
3408
  code: "DECRYPT_FAILED" /* DecryptFailed */,
3386
- message: "decrypt response field <error_message> must be string or null",
3387
- context: { decryptResponse: value }
3409
+ message: "decrypt status response missing submitted_at",
3410
+ context: {
3411
+ value
3412
+ }
3388
3413
  });
3389
3414
  }
3390
- return {
3391
- decrypted,
3392
- signature,
3393
- encryption_type: encryptionType,
3394
- error_message: errorMessage
3395
- };
3415
+ return value;
3396
3416
  }
3397
- async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3417
+ async function submitDecryptRequestV2(thresholdNetworkUrl, ctHash, chainId, permission) {
3398
3418
  const body = {
3399
3419
  ct_tempkey: BigInt(ctHash).toString(16).padStart(64, "0"),
3400
3420
  host_chain_id: chainId
@@ -3404,7 +3424,7 @@ async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3404
3424
  }
3405
3425
  let response;
3406
3426
  try {
3407
- response = await fetch(`${thresholdNetworkUrl}/decrypt`, {
3427
+ response = await fetch(`${thresholdNetworkUrl}/v2/decrypt`, {
3408
3428
  method: "POST",
3409
3429
  headers: {
3410
3430
  "Content-Type": "application/json"
@@ -3423,18 +3443,15 @@ async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3423
3443
  }
3424
3444
  });
3425
3445
  }
3426
- const responseText = await response.text();
3427
3446
  if (!response.ok) {
3428
- let errorMessage = response.statusText || `HTTP ${response.status}`;
3447
+ let errorMessage = `HTTP ${response.status}`;
3429
3448
  try {
3430
- const errorBody = JSON.parse(responseText);
3449
+ const errorBody = await response.json();
3431
3450
  const maybeMessage = errorBody.error_message || errorBody.message;
3432
3451
  if (typeof maybeMessage === "string" && maybeMessage.length > 0)
3433
3452
  errorMessage = maybeMessage;
3434
3453
  } catch {
3435
- const trimmed = responseText.trim();
3436
- if (trimmed.length > 0)
3437
- errorMessage = trimmed;
3454
+ errorMessage = response.statusText || errorMessage;
3438
3455
  }
3439
3456
  throw new CofheError({
3440
3457
  code: "DECRYPT_FAILED" /* DecryptFailed */,
@@ -3444,41 +3461,163 @@ async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3444
3461
  thresholdNetworkUrl,
3445
3462
  status: response.status,
3446
3463
  statusText: response.statusText,
3447
- body,
3448
- responseText
3464
+ body
3449
3465
  }
3450
3466
  });
3451
3467
  }
3452
3468
  let rawJson;
3453
3469
  try {
3454
- rawJson = JSON.parse(responseText);
3470
+ rawJson = await response.json();
3455
3471
  } catch (e) {
3456
3472
  throw new CofheError({
3457
3473
  code: "DECRYPT_FAILED" /* DecryptFailed */,
3458
- message: `Failed to parse decrypt response`,
3474
+ message: `Failed to parse decrypt submit response`,
3459
3475
  cause: e instanceof Error ? e : void 0,
3460
3476
  context: {
3461
3477
  thresholdNetworkUrl,
3462
- body,
3463
- responseText
3478
+ body
3464
3479
  }
3465
3480
  });
3466
3481
  }
3467
- const decryptResponse = assertTnDecryptResponse(rawJson);
3468
- if (decryptResponse.error_message) {
3469
- throw new CofheError({
3470
- code: "DECRYPT_FAILED" /* DecryptFailed */,
3471
- message: `decrypt request failed: ${decryptResponse.error_message}`,
3472
- context: {
3473
- thresholdNetworkUrl,
3474
- body,
3475
- decryptResponse
3482
+ const submitResponse = assertDecryptSubmitResponseV2(rawJson);
3483
+ return submitResponse.request_id;
3484
+ }
3485
+ async function pollDecryptStatusV2(thresholdNetworkUrl, requestId) {
3486
+ const startTime = Date.now();
3487
+ let completed = false;
3488
+ while (!completed) {
3489
+ if (Date.now() - startTime > POLL_TIMEOUT_MS2) {
3490
+ throw new CofheError({
3491
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3492
+ message: `decrypt polling timed out after ${POLL_TIMEOUT_MS2}ms`,
3493
+ hint: "The request may still be processing. Try again later.",
3494
+ context: {
3495
+ thresholdNetworkUrl,
3496
+ requestId,
3497
+ timeoutMs: POLL_TIMEOUT_MS2
3498
+ }
3499
+ });
3500
+ }
3501
+ let response;
3502
+ try {
3503
+ response = await fetch(`${thresholdNetworkUrl}/v2/decrypt/${requestId}`, {
3504
+ method: "GET",
3505
+ headers: {
3506
+ "Content-Type": "application/json"
3507
+ }
3508
+ });
3509
+ } catch (e) {
3510
+ throw new CofheError({
3511
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3512
+ message: `decrypt status poll failed`,
3513
+ hint: "Ensure the threshold network URL is valid and reachable.",
3514
+ cause: e instanceof Error ? e : void 0,
3515
+ context: {
3516
+ thresholdNetworkUrl,
3517
+ requestId
3518
+ }
3519
+ });
3520
+ }
3521
+ if (response.status === 404) {
3522
+ throw new CofheError({
3523
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3524
+ message: `decrypt request not found: ${requestId}`,
3525
+ hint: "The request may have expired or been invalid.",
3526
+ context: {
3527
+ thresholdNetworkUrl,
3528
+ requestId
3529
+ }
3530
+ });
3531
+ }
3532
+ if (!response.ok) {
3533
+ let errorMessage = `HTTP ${response.status}`;
3534
+ try {
3535
+ const errorBody = await response.json();
3536
+ const maybeMessage = errorBody.error_message || errorBody.message;
3537
+ if (typeof maybeMessage === "string" && maybeMessage.length > 0)
3538
+ errorMessage = maybeMessage;
3539
+ } catch {
3540
+ errorMessage = response.statusText || errorMessage;
3476
3541
  }
3477
- });
3542
+ throw new CofheError({
3543
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3544
+ message: `decrypt status poll failed: ${errorMessage}`,
3545
+ context: {
3546
+ thresholdNetworkUrl,
3547
+ requestId,
3548
+ status: response.status,
3549
+ statusText: response.statusText
3550
+ }
3551
+ });
3552
+ }
3553
+ let rawJson;
3554
+ try {
3555
+ rawJson = await response.json();
3556
+ } catch (e) {
3557
+ throw new CofheError({
3558
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3559
+ message: `Failed to parse decrypt status response`,
3560
+ cause: e instanceof Error ? e : void 0,
3561
+ context: {
3562
+ thresholdNetworkUrl,
3563
+ requestId
3564
+ }
3565
+ });
3566
+ }
3567
+ const statusResponse = assertDecryptStatusResponseV2(rawJson);
3568
+ if (statusResponse.status === "COMPLETED") {
3569
+ if (statusResponse.is_succeed === false) {
3570
+ const errorMessage = statusResponse.error_message || "Unknown error";
3571
+ throw new CofheError({
3572
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3573
+ message: `decrypt request failed: ${errorMessage}`,
3574
+ context: {
3575
+ thresholdNetworkUrl,
3576
+ requestId,
3577
+ statusResponse
3578
+ }
3579
+ });
3580
+ }
3581
+ if (statusResponse.error_message) {
3582
+ throw new CofheError({
3583
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3584
+ message: `decrypt request failed: ${statusResponse.error_message}`,
3585
+ context: {
3586
+ thresholdNetworkUrl,
3587
+ requestId,
3588
+ statusResponse
3589
+ }
3590
+ });
3591
+ }
3592
+ if (!Array.isArray(statusResponse.decrypted)) {
3593
+ throw new CofheError({
3594
+ code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
3595
+ message: "decrypt completed but response missing <decrypted> byte array",
3596
+ context: {
3597
+ thresholdNetworkUrl,
3598
+ requestId,
3599
+ statusResponse
3600
+ }
3601
+ });
3602
+ }
3603
+ const decryptedValue = parseDecryptedBytesToBigInt(statusResponse.decrypted);
3604
+ const signature = normalizeTnSignature(statusResponse.signature);
3605
+ return { decryptedValue, signature };
3606
+ }
3607
+ await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS2));
3478
3608
  }
3479
- const decryptedValue = parseDecryptedBytesToBigInt(decryptResponse.decrypted);
3480
- const signature = normalizeSignature(decryptResponse.signature);
3481
- return { decryptedValue, signature };
3609
+ throw new CofheError({
3610
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3611
+ message: "Polling loop exited unexpectedly",
3612
+ context: {
3613
+ thresholdNetworkUrl,
3614
+ requestId
3615
+ }
3616
+ });
3617
+ }
3618
+ async function tnDecryptV2(ctHash, chainId, permission, thresholdNetworkUrl) {
3619
+ const requestId = await submitDecryptRequestV2(thresholdNetworkUrl, ctHash, chainId, permission);
3620
+ return await pollDecryptStatusV2(thresholdNetworkUrl, requestId);
3482
3621
  }
3483
3622
 
3484
3623
  // core/decrypt/decryptForTxBuilder.ts
@@ -3639,7 +3778,7 @@ var DecryptForTxBuilder = class extends BaseBuilder {
3639
3778
  this.assertPublicClient();
3640
3779
  const thresholdNetworkUrl = await this.getThresholdNetworkUrl();
3641
3780
  const permission = permit ? PermitUtils.getPermission(permit, true) : null;
3642
- const { decryptedValue, signature } = await tnDecrypt(this.ctHash, this.chainId, permission, thresholdNetworkUrl);
3781
+ const { decryptedValue, signature } = await tnDecryptV2(this.ctHash, this.chainId, permission, thresholdNetworkUrl);
3643
3782
  return {
3644
3783
  ctHash: this.ctHash,
3645
3784
  decryptedValue,
@@ -3847,19 +3986,19 @@ function createCofheClientBase(opts) {
3847
3986
  return permits.getOrCreateSharingPermit(publicClient, walletClient, options, _chainId, _account);
3848
3987
  },
3849
3988
  // Retrieval methods (auto-fill chainId/account)
3850
- getPermit: async (hash, chainId, account) => {
3989
+ getPermit: (hash, chainId, account) => {
3851
3990
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
3852
3991
  return permits.getPermit(_chainId, _account, hash);
3853
3992
  },
3854
- getPermits: async (chainId, account) => {
3993
+ getPermits: (chainId, account) => {
3855
3994
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
3856
3995
  return permits.getPermits(_chainId, _account);
3857
3996
  },
3858
- getActivePermit: async (chainId, account) => {
3997
+ getActivePermit: (chainId, account) => {
3859
3998
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
3860
3999
  return permits.getActivePermit(_chainId, _account);
3861
4000
  },
3862
- getActivePermitHash: async (chainId, account) => {
4001
+ getActivePermitHash: (chainId, account) => {
3863
4002
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
3864
4003
  return permits.getActivePermitHash(_chainId, _account);
3865
4004
  },
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-Y43CKbOz.cjs';
1
+ import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-ACVWbrXL.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-PQha8zes.js';
1
+ import { C as CofheInputConfig, a as CofheConfig, b as CofheClient, E as EncryptableItem } from './clientTypes-kkrRdawm.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-LWMRB6SD.js';
1
+ import { createCofheConfigBase, createCofheClientBase, fheTypeToString } from './chunk-MXND5SVN.js';
2
2
  import './chunk-TBLR7NNE.js';
3
3
  import './chunk-NWDKXBIP.js';
4
4
  import { constructClient } from 'iframe-shared-storage';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cofhe/sdk",
3
- "version": "0.3.2",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "description": "SDK for Fhenix COFHE coprocessor interaction",
6
6
  "main": "./dist/core.cjs",