@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/node.cjs CHANGED
@@ -2474,13 +2474,13 @@ var serialize = (permit) => {
2474
2474
  var deserialize = (serialized) => {
2475
2475
  return PermitUtils.deserialize(serialized);
2476
2476
  };
2477
- var getPermit2 = async (chainId, account, hash) => {
2477
+ var getPermit2 = (chainId, account, hash) => {
2478
2478
  return permitStore.getPermit(chainId, account, hash);
2479
2479
  };
2480
- var getPermits2 = async (chainId, account) => {
2480
+ var getPermits2 = (chainId, account) => {
2481
2481
  return permitStore.getPermits(chainId, account);
2482
2482
  };
2483
- var getActivePermit2 = async (chainId, account) => {
2483
+ var getActivePermit2 = (chainId, account) => {
2484
2484
  return permitStore.getActivePermit(chainId, account);
2485
2485
  };
2486
2486
  var getActivePermitHash2 = (chainId, account) => {
@@ -3249,7 +3249,7 @@ async function cofheMocksDecryptForTx(ctHash, utype, permit, publicClient) {
3249
3249
  signature
3250
3250
  };
3251
3251
  }
3252
- function normalizeSignature(signature) {
3252
+ function normalizeTnSignature(signature) {
3253
3253
  if (typeof signature !== "string") {
3254
3254
  throw new CofheError({
3255
3255
  code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
@@ -3305,57 +3305,77 @@ function parseDecryptedBytesToBigInt(decrypted) {
3305
3305
  }
3306
3306
  return BigInt(`0x${hex}`);
3307
3307
  }
3308
- function assertTnDecryptResponse(value) {
3308
+
3309
+ // core/decrypt/tnDecryptV2.ts
3310
+ var POLL_INTERVAL_MS2 = 1e3;
3311
+ var POLL_TIMEOUT_MS2 = 5 * 60 * 1e3;
3312
+ function assertDecryptSubmitResponseV2(value) {
3309
3313
  if (value == null || typeof value !== "object") {
3310
3314
  throw new CofheError({
3311
3315
  code: "DECRYPT_FAILED" /* DecryptFailed */,
3312
- message: "decrypt response must be a JSON object",
3316
+ message: "decrypt submit response must be a JSON object",
3313
3317
  context: {
3314
3318
  value
3315
3319
  }
3316
3320
  });
3317
3321
  }
3318
3322
  const v = value;
3319
- const decrypted = v.decrypted;
3320
- const signature = v.signature;
3321
- const encryptionType = v.encryption_type;
3322
- const errorMessage = v.error_message;
3323
- if (!Array.isArray(decrypted)) {
3323
+ if (typeof v.request_id !== "string" || v.request_id.trim().length === 0) {
3324
3324
  throw new CofheError({
3325
- code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
3326
- message: "decrypt response missing <decrypted> byte array",
3327
- context: { decryptResponse: value }
3325
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3326
+ message: "decrypt submit response missing request_id",
3327
+ context: {
3328
+ value
3329
+ }
3328
3330
  });
3329
3331
  }
3330
- if (typeof signature !== "string") {
3332
+ return { request_id: v.request_id };
3333
+ }
3334
+ function assertDecryptStatusResponseV2(value) {
3335
+ if (value == null || typeof value !== "object") {
3331
3336
  throw new CofheError({
3332
- code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
3333
- message: "decrypt response missing <signature> string",
3334
- context: { decryptResponse: value }
3337
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3338
+ message: "decrypt status response must be a JSON object",
3339
+ context: {
3340
+ value
3341
+ }
3342
+ });
3343
+ }
3344
+ const v = value;
3345
+ const requestId = v.request_id;
3346
+ const status = v.status;
3347
+ const submittedAt = v.submitted_at;
3348
+ if (typeof requestId !== "string" || requestId.trim().length === 0) {
3349
+ throw new CofheError({
3350
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3351
+ message: "decrypt status response missing request_id",
3352
+ context: {
3353
+ value
3354
+ }
3335
3355
  });
3336
3356
  }
3337
- if (typeof encryptionType !== "number") {
3357
+ if (status !== "PROCESSING" && status !== "COMPLETED") {
3338
3358
  throw new CofheError({
3339
3359
  code: "DECRYPT_FAILED" /* DecryptFailed */,
3340
- message: "decrypt response missing <encryption_type> number",
3341
- context: { decryptResponse: value }
3360
+ message: "decrypt status response has invalid status",
3361
+ context: {
3362
+ value,
3363
+ status
3364
+ }
3342
3365
  });
3343
3366
  }
3344
- if (!(typeof errorMessage === "string" || errorMessage === null)) {
3367
+ if (typeof submittedAt !== "string" || submittedAt.trim().length === 0) {
3345
3368
  throw new CofheError({
3346
3369
  code: "DECRYPT_FAILED" /* DecryptFailed */,
3347
- message: "decrypt response field <error_message> must be string or null",
3348
- context: { decryptResponse: value }
3370
+ message: "decrypt status response missing submitted_at",
3371
+ context: {
3372
+ value
3373
+ }
3349
3374
  });
3350
3375
  }
3351
- return {
3352
- decrypted,
3353
- signature,
3354
- encryption_type: encryptionType,
3355
- error_message: errorMessage
3356
- };
3376
+ return value;
3357
3377
  }
3358
- async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3378
+ async function submitDecryptRequestV2(thresholdNetworkUrl, ctHash, chainId, permission) {
3359
3379
  const body = {
3360
3380
  ct_tempkey: BigInt(ctHash).toString(16).padStart(64, "0"),
3361
3381
  host_chain_id: chainId
@@ -3365,7 +3385,7 @@ async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3365
3385
  }
3366
3386
  let response;
3367
3387
  try {
3368
- response = await fetch(`${thresholdNetworkUrl}/decrypt`, {
3388
+ response = await fetch(`${thresholdNetworkUrl}/v2/decrypt`, {
3369
3389
  method: "POST",
3370
3390
  headers: {
3371
3391
  "Content-Type": "application/json"
@@ -3384,18 +3404,15 @@ async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3384
3404
  }
3385
3405
  });
3386
3406
  }
3387
- const responseText = await response.text();
3388
3407
  if (!response.ok) {
3389
- let errorMessage = response.statusText || `HTTP ${response.status}`;
3408
+ let errorMessage = `HTTP ${response.status}`;
3390
3409
  try {
3391
- const errorBody = JSON.parse(responseText);
3410
+ const errorBody = await response.json();
3392
3411
  const maybeMessage = errorBody.error_message || errorBody.message;
3393
3412
  if (typeof maybeMessage === "string" && maybeMessage.length > 0)
3394
3413
  errorMessage = maybeMessage;
3395
3414
  } catch {
3396
- const trimmed = responseText.trim();
3397
- if (trimmed.length > 0)
3398
- errorMessage = trimmed;
3415
+ errorMessage = response.statusText || errorMessage;
3399
3416
  }
3400
3417
  throw new CofheError({
3401
3418
  code: "DECRYPT_FAILED" /* DecryptFailed */,
@@ -3405,41 +3422,163 @@ async function tnDecrypt(ctHash, chainId, permission, thresholdNetworkUrl) {
3405
3422
  thresholdNetworkUrl,
3406
3423
  status: response.status,
3407
3424
  statusText: response.statusText,
3408
- body,
3409
- responseText
3425
+ body
3410
3426
  }
3411
3427
  });
3412
3428
  }
3413
3429
  let rawJson;
3414
3430
  try {
3415
- rawJson = JSON.parse(responseText);
3431
+ rawJson = await response.json();
3416
3432
  } catch (e) {
3417
3433
  throw new CofheError({
3418
3434
  code: "DECRYPT_FAILED" /* DecryptFailed */,
3419
- message: `Failed to parse decrypt response`,
3435
+ message: `Failed to parse decrypt submit response`,
3420
3436
  cause: e instanceof Error ? e : void 0,
3421
3437
  context: {
3422
3438
  thresholdNetworkUrl,
3423
- body,
3424
- responseText
3439
+ body
3425
3440
  }
3426
3441
  });
3427
3442
  }
3428
- const decryptResponse = assertTnDecryptResponse(rawJson);
3429
- if (decryptResponse.error_message) {
3430
- throw new CofheError({
3431
- code: "DECRYPT_FAILED" /* DecryptFailed */,
3432
- message: `decrypt request failed: ${decryptResponse.error_message}`,
3433
- context: {
3434
- thresholdNetworkUrl,
3435
- body,
3436
- decryptResponse
3443
+ const submitResponse = assertDecryptSubmitResponseV2(rawJson);
3444
+ return submitResponse.request_id;
3445
+ }
3446
+ async function pollDecryptStatusV2(thresholdNetworkUrl, requestId) {
3447
+ const startTime = Date.now();
3448
+ let completed = false;
3449
+ while (!completed) {
3450
+ if (Date.now() - startTime > POLL_TIMEOUT_MS2) {
3451
+ throw new CofheError({
3452
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3453
+ message: `decrypt polling timed out after ${POLL_TIMEOUT_MS2}ms`,
3454
+ hint: "The request may still be processing. Try again later.",
3455
+ context: {
3456
+ thresholdNetworkUrl,
3457
+ requestId,
3458
+ timeoutMs: POLL_TIMEOUT_MS2
3459
+ }
3460
+ });
3461
+ }
3462
+ let response;
3463
+ try {
3464
+ response = await fetch(`${thresholdNetworkUrl}/v2/decrypt/${requestId}`, {
3465
+ method: "GET",
3466
+ headers: {
3467
+ "Content-Type": "application/json"
3468
+ }
3469
+ });
3470
+ } catch (e) {
3471
+ throw new CofheError({
3472
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3473
+ message: `decrypt status poll failed`,
3474
+ hint: "Ensure the threshold network URL is valid and reachable.",
3475
+ cause: e instanceof Error ? e : void 0,
3476
+ context: {
3477
+ thresholdNetworkUrl,
3478
+ requestId
3479
+ }
3480
+ });
3481
+ }
3482
+ if (response.status === 404) {
3483
+ throw new CofheError({
3484
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3485
+ message: `decrypt request not found: ${requestId}`,
3486
+ hint: "The request may have expired or been invalid.",
3487
+ context: {
3488
+ thresholdNetworkUrl,
3489
+ requestId
3490
+ }
3491
+ });
3492
+ }
3493
+ if (!response.ok) {
3494
+ let errorMessage = `HTTP ${response.status}`;
3495
+ try {
3496
+ const errorBody = await response.json();
3497
+ const maybeMessage = errorBody.error_message || errorBody.message;
3498
+ if (typeof maybeMessage === "string" && maybeMessage.length > 0)
3499
+ errorMessage = maybeMessage;
3500
+ } catch {
3501
+ errorMessage = response.statusText || errorMessage;
3437
3502
  }
3438
- });
3503
+ throw new CofheError({
3504
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3505
+ message: `decrypt status poll failed: ${errorMessage}`,
3506
+ context: {
3507
+ thresholdNetworkUrl,
3508
+ requestId,
3509
+ status: response.status,
3510
+ statusText: response.statusText
3511
+ }
3512
+ });
3513
+ }
3514
+ let rawJson;
3515
+ try {
3516
+ rawJson = await response.json();
3517
+ } catch (e) {
3518
+ throw new CofheError({
3519
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3520
+ message: `Failed to parse decrypt status response`,
3521
+ cause: e instanceof Error ? e : void 0,
3522
+ context: {
3523
+ thresholdNetworkUrl,
3524
+ requestId
3525
+ }
3526
+ });
3527
+ }
3528
+ const statusResponse = assertDecryptStatusResponseV2(rawJson);
3529
+ if (statusResponse.status === "COMPLETED") {
3530
+ if (statusResponse.is_succeed === false) {
3531
+ const errorMessage = statusResponse.error_message || "Unknown error";
3532
+ throw new CofheError({
3533
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3534
+ message: `decrypt request failed: ${errorMessage}`,
3535
+ context: {
3536
+ thresholdNetworkUrl,
3537
+ requestId,
3538
+ statusResponse
3539
+ }
3540
+ });
3541
+ }
3542
+ if (statusResponse.error_message) {
3543
+ throw new CofheError({
3544
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3545
+ message: `decrypt request failed: ${statusResponse.error_message}`,
3546
+ context: {
3547
+ thresholdNetworkUrl,
3548
+ requestId,
3549
+ statusResponse
3550
+ }
3551
+ });
3552
+ }
3553
+ if (!Array.isArray(statusResponse.decrypted)) {
3554
+ throw new CofheError({
3555
+ code: "DECRYPT_RETURNED_NULL" /* DecryptReturnedNull */,
3556
+ message: "decrypt completed but response missing <decrypted> byte array",
3557
+ context: {
3558
+ thresholdNetworkUrl,
3559
+ requestId,
3560
+ statusResponse
3561
+ }
3562
+ });
3563
+ }
3564
+ const decryptedValue = parseDecryptedBytesToBigInt(statusResponse.decrypted);
3565
+ const signature = normalizeTnSignature(statusResponse.signature);
3566
+ return { decryptedValue, signature };
3567
+ }
3568
+ await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS2));
3439
3569
  }
3440
- const decryptedValue = parseDecryptedBytesToBigInt(decryptResponse.decrypted);
3441
- const signature = normalizeSignature(decryptResponse.signature);
3442
- return { decryptedValue, signature };
3570
+ throw new CofheError({
3571
+ code: "DECRYPT_FAILED" /* DecryptFailed */,
3572
+ message: "Polling loop exited unexpectedly",
3573
+ context: {
3574
+ thresholdNetworkUrl,
3575
+ requestId
3576
+ }
3577
+ });
3578
+ }
3579
+ async function tnDecryptV2(ctHash, chainId, permission, thresholdNetworkUrl) {
3580
+ const requestId = await submitDecryptRequestV2(thresholdNetworkUrl, ctHash, chainId, permission);
3581
+ return await pollDecryptStatusV2(thresholdNetworkUrl, requestId);
3443
3582
  }
3444
3583
 
3445
3584
  // core/decrypt/decryptForTxBuilder.ts
@@ -3600,7 +3739,7 @@ var DecryptForTxBuilder = class extends BaseBuilder {
3600
3739
  this.assertPublicClient();
3601
3740
  const thresholdNetworkUrl = await this.getThresholdNetworkUrl();
3602
3741
  const permission = permit ? PermitUtils.getPermission(permit, true) : null;
3603
- const { decryptedValue, signature } = await tnDecrypt(this.ctHash, this.chainId, permission, thresholdNetworkUrl);
3742
+ const { decryptedValue, signature } = await tnDecryptV2(this.ctHash, this.chainId, permission, thresholdNetworkUrl);
3604
3743
  return {
3605
3744
  ctHash: this.ctHash,
3606
3745
  decryptedValue,
@@ -3808,19 +3947,19 @@ function createCofheClientBase(opts) {
3808
3947
  return permits.getOrCreateSharingPermit(publicClient, walletClient, options, _chainId, _account);
3809
3948
  },
3810
3949
  // Retrieval methods (auto-fill chainId/account)
3811
- getPermit: async (hash, chainId, account) => {
3950
+ getPermit: (hash, chainId, account) => {
3812
3951
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
3813
3952
  return permits.getPermit(_chainId, _account, hash);
3814
3953
  },
3815
- getPermits: async (chainId, account) => {
3954
+ getPermits: (chainId, account) => {
3816
3955
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
3817
3956
  return permits.getPermits(_chainId, _account);
3818
3957
  },
3819
- getActivePermit: async (chainId, account) => {
3958
+ getActivePermit: (chainId, account) => {
3820
3959
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
3821
3960
  return permits.getActivePermit(_chainId, _account);
3822
3961
  },
3823
- getActivePermitHash: async (chainId, account) => {
3962
+ getActivePermitHash: (chainId, account) => {
3824
3963
  const { chainId: _chainId, account: _account } = _getChainIdAndAccount(chainId, account);
3825
3964
  return permits.getActivePermitHash(_chainId, _account);
3826
3965
  },
package/dist/node.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { C as CofheInputConfig, a as CofheConfig, b as CofheClient } from './clientTypes-Y43CKbOz.cjs';
1
+ import { C as CofheInputConfig, a as CofheConfig, b as CofheClient } from './clientTypes-ACVWbrXL.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-PQha8zes.js';
1
+ import { C as CofheInputConfig, a as CofheConfig, b as CofheClient } from './clientTypes-kkrRdawm.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-LWMRB6SD.js';
1
+ import { createCofheConfigBase, createCofheClientBase } from './chunk-MXND5SVN.js';
2
2
  import './chunk-TBLR7NNE.js';
3
3
  import './chunk-NWDKXBIP.js';
4
4
  import { promises } from 'fs';