@cofhe/sdk 0.6.1 → 0.7.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.
Files changed (113) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/acps/acp.ts +411 -0
  3. package/acps/index.ts +70 -0
  4. package/{permits → acps}/onchain-utils.ts +49 -24
  5. package/acps/sealing.ts +90 -0
  6. package/acps/signature.ts +89 -0
  7. package/acps/store.ts +172 -0
  8. package/acps/test/acp.test.ts +615 -0
  9. package/acps/test/localstorage.test.ts +105 -0
  10. package/acps/test/sealing.test.ts +77 -0
  11. package/acps/test/store.test.ts +88 -0
  12. package/acps/test/validation.test.ts +361 -0
  13. package/acps/test-utils.ts +32 -0
  14. package/acps/types.ts +252 -0
  15. package/acps/validation.ts +392 -0
  16. package/adapters/test/ethers5.test.ts +4 -1
  17. package/adapters/test/ethers6.test.ts +4 -1
  18. package/adapters/test/wagmi.test.ts +5 -2
  19. package/chains/chains/stagingCofhe.ts +21 -0
  20. package/chains/index.ts +3 -1
  21. package/chains/test/chains.test.ts +2 -1
  22. package/core/acps.ts +625 -0
  23. package/core/client.ts +136 -39
  24. package/core/clientTypes.ts +52 -41
  25. package/core/config.ts +66 -5
  26. package/core/decrypt/MockThresholdNetworkAbi.ts +20 -11
  27. package/core/decrypt/apiError.ts +104 -0
  28. package/core/decrypt/cofheMocksDecryptForTx.ts +11 -11
  29. package/core/decrypt/cofheMocksDecryptForView.ts +7 -7
  30. package/core/decrypt/decryptForTxBuilder.ts +102 -102
  31. package/core/decrypt/decryptForViewBuilder.ts +90 -90
  32. package/core/decrypt/submitRetry.ts +38 -30
  33. package/core/decrypt/tnDecryptV1.ts +5 -5
  34. package/core/decrypt/tnDecryptV2.ts +25 -21
  35. package/core/decrypt/tnSealOutputV1.ts +4 -3
  36. package/core/decrypt/tnSealOutputV2.ts +24 -18
  37. package/core/encrypt/cofheMocksZkVerifySign.ts +59 -74
  38. package/core/encrypt/encryptInputsBuilder.ts +86 -52
  39. package/core/encrypt/zkPackProveVerify.ts +25 -18
  40. package/core/error.ts +34 -6
  41. package/core/index.ts +4 -14
  42. package/core/test/acpDefaults.test.ts +52 -0
  43. package/core/test/acps.test.ts +596 -0
  44. package/core/test/apiError.test.ts +130 -0
  45. package/core/test/client.test.ts +22 -19
  46. package/core/test/config.test.ts +25 -5
  47. package/core/test/decrypt.test.ts +40 -35
  48. package/core/test/decryptBuilders.test.ts +68 -68
  49. package/core/test/decryptErrorCodes.test.ts +217 -0
  50. package/core/test/encryptInputsBuilder.test.ts +72 -41
  51. package/core/test/pollCallbacks.test.ts +89 -18
  52. package/core/test/stagingRedirect.ts +18 -0
  53. package/core/test/submitRetry.test.ts +182 -0
  54. package/core/types.ts +9 -69
  55. package/dist/acp-Wi6isVQI.d.cts +407 -0
  56. package/dist/acp-Wi6isVQI.d.ts +407 -0
  57. package/dist/acps.cjs +1081 -0
  58. package/dist/acps.d.cts +480 -0
  59. package/dist/acps.d.ts +480 -0
  60. package/dist/acps.js +2 -0
  61. package/dist/chains.cjs +14 -1
  62. package/dist/chains.d.cts +30 -1
  63. package/dist/chains.d.ts +30 -1
  64. package/dist/chains.js +1 -1
  65. package/dist/{chunk-NOC3PYB7.js → chunk-43USWPEH.js} +930 -580
  66. package/dist/{chunk-MTRAXQXC.js → chunk-N6IDQRRU.js} +14 -2
  67. package/dist/chunk-Q7CBWGQX.js +1029 -0
  68. package/dist/{clientTypes-CyUvRRzA.d.ts → clientTypes-BN3nbzYM.d.ts} +342 -165
  69. package/dist/{clientTypes-BDy1qIBu.d.cts → clientTypes-CYZjFznO.d.cts} +342 -165
  70. package/dist/core.cjs +1358 -1002
  71. package/dist/core.d.cts +31 -9
  72. package/dist/core.d.ts +31 -9
  73. package/dist/core.js +3 -3
  74. package/dist/node.cjs +1293 -952
  75. package/dist/node.d.cts +2 -2
  76. package/dist/node.d.ts +2 -2
  77. package/dist/node.js +3 -3
  78. package/dist/web.cjs +1293 -952
  79. package/dist/web.d.cts +2 -2
  80. package/dist/web.d.ts +2 -2
  81. package/dist/web.js +3 -3
  82. package/node/test/inherited.test.ts +75 -65
  83. package/node/test/tfheinit.test.ts +23 -8
  84. package/package.json +6 -6
  85. package/web/test/client.web.test.ts +5 -1
  86. package/web/test/inherited.web.test.ts +75 -65
  87. package/web/test/tfheinit.web.test.ts +14 -5
  88. package/web/test/worker.config.web.test.ts +38 -23
  89. package/web/test/worker.output.web.test.ts +25 -24
  90. package/core/encrypt/encryptUtils.ts +0 -67
  91. package/core/permits.ts +0 -216
  92. package/core/test/permits.test.ts +0 -596
  93. package/dist/chunk-VB62WYPL.js +0 -978
  94. package/dist/permit-DnVMDT5h.d.cts +0 -376
  95. package/dist/permit-DnVMDT5h.d.ts +0 -376
  96. package/dist/permits.cjs +0 -1026
  97. package/dist/permits.d.cts +0 -353
  98. package/dist/permits.d.ts +0 -353
  99. package/dist/permits.js +0 -2
  100. package/permits/index.ts +0 -68
  101. package/permits/permit.ts +0 -385
  102. package/permits/sealing.ts +0 -131
  103. package/permits/signature.ts +0 -79
  104. package/permits/store.ts +0 -157
  105. package/permits/test/localstorage.test.ts +0 -113
  106. package/permits/test/permit.test.ts +0 -557
  107. package/permits/test/sealing.test.ts +0 -84
  108. package/permits/test/store.test.ts +0 -88
  109. package/permits/test/validation.test.ts +0 -361
  110. package/permits/test-utils.ts +0 -28
  111. package/permits/types.ts +0 -204
  112. package/permits/validation.ts +0 -327
  113. /package/{permits → acps}/utils.ts +0 -0
package/core/error.ts CHANGED
@@ -9,12 +9,13 @@ export enum CofheErrorCode {
9
9
  MissingWalletClient = 'MISSING_WALLET_CLIENT',
10
10
  MissingProviderParam = 'MISSING_PROVIDER_PARAM',
11
11
  EmptySecurityZonesParam = 'EMPTY_SECURITY_ZONES_PARAM',
12
- InvalidPermitData = 'INVALID_PERMIT_DATA',
13
- InvalidPermitDomain = 'INVALID_PERMIT_DOMAIN',
14
- PermitNotFound = 'PERMIT_NOT_FOUND',
15
- CannotRemoveLastPermit = 'CANNOT_REMOVE_LAST_PERMIT',
12
+ InvalidACPData = 'INVALID_ACP_DATA',
13
+ InvalidACPDomain = 'INVALID_ACP_DOMAIN',
14
+ ACPNotFound = 'ACP_NOT_FOUND',
15
+ CannotRemoveLastACP = 'CANNOT_REMOVE_LAST_ACP',
16
16
  AccountUninitialized = 'ACCOUNT_UNINITIALIZED',
17
17
  ChainIdUninitialized = 'CHAIN_ID_UNINITIALIZED',
18
+ ConsumingContractUninitialized = 'CONSUMING_CONTRACT_UNINITIALIZED',
18
19
  SealOutputFailed = 'SEAL_OUTPUT_FAILED',
19
20
  SealOutputReturnedNull = 'SEAL_OUTPUT_RETURNED_NULL',
20
21
  InvalidUtype = 'INVALID_UTYPE',
@@ -42,6 +43,26 @@ export enum CofheErrorCode {
42
43
  PublicWalletGetChainIdFailed = 'PUBLIC_WALLET_GET_CHAIN_ID_FAILED',
43
44
  PublicWalletGetAddressesFailed = 'PUBLIC_WALLET_GET_ADDRESSES_FAILED',
44
45
  RehydrateKeysStoreFailed = 'REHYDRATE_KEYS_STORE_FAILED',
46
+
47
+ // Threshold-network stable error codes (decrypt/sealoutput), see API-RESPONSES.md
48
+ BadRequest = 'BAD_REQUEST',
49
+ UnknownChain = 'UNKNOWN_CHAIN',
50
+ ACPMalformed = 'ACP_MALFORMED',
51
+ ACPDenied = 'ACP_DENIED',
52
+ ACPExpired = 'ACP_EXPIRED',
53
+ ACPInvalid = 'ACP_INVALID',
54
+ ACPRevoked = 'ACP_REVOKED',
55
+ NotPubliclyAllowed = 'NOT_PUBLICLY_ALLOWED',
56
+ CtNotFound = 'CT_NOT_FOUND',
57
+ UnsupportedSecurityZone = 'UNSUPPORTED_SECURITY_ZONE',
58
+ UnsupportedType = 'UNSUPPORTED_TYPE',
59
+ SigningFailed = 'SIGNING_FAILED',
60
+ CtSourceError = 'CT_SOURCE_ERROR',
61
+ ACPVerifierError = 'ACP_VERIFIER_ERROR',
62
+ CtSourceTimeout = 'CT_SOURCE_TIMEOUT',
63
+ ACPVerifierTimeout = 'ACP_VERIFIER_TIMEOUT',
64
+ ACPRequired = 'ACP_REQUIRED',
65
+ SealFailed = 'SEAL_FAILED',
45
66
  }
46
67
 
47
68
  export type CofheErrorParams = {
@@ -50,6 +71,8 @@ export type CofheErrorParams = {
50
71
  cause?: Error;
51
72
  hint?: string;
52
73
  context?: Record<string, unknown>;
74
+ /** Raw backend `error` string, when this error originated from a threshold-network response. */
75
+ apiErrorCode?: string;
53
76
  };
54
77
 
55
78
  /**
@@ -68,8 +91,9 @@ export class CofheError extends Error {
68
91
  public readonly cause?: Error;
69
92
  public readonly hint?: string;
70
93
  public readonly context?: Record<string, unknown>;
94
+ public readonly apiErrorCode?: string;
71
95
 
72
- constructor({ code, message, cause, hint, context }: CofheErrorParams) {
96
+ constructor({ code, message, cause, hint, context, apiErrorCode }: CofheErrorParams) {
73
97
  // If there's a cause, append its message to provide full context
74
98
  const fullMessage = cause ? `${message} | Caused by: ${cause.message}` : message;
75
99
 
@@ -79,6 +103,7 @@ export class CofheError extends Error {
79
103
  this.cause = cause;
80
104
  this.hint = hint;
81
105
  this.context = context;
106
+ this.apiErrorCode = apiErrorCode;
82
107
 
83
108
  // Maintains proper stack trace for where our error was thrown (only available on V8)
84
109
  if (Error.captureStackTrace) {
@@ -101,6 +126,7 @@ export class CofheError extends Error {
101
126
  message: wrapperError?.message ?? 'An internal error occurred',
102
127
  hint: wrapperError?.hint,
103
128
  context: wrapperError?.context,
129
+ apiErrorCode: wrapperError?.apiErrorCode,
104
130
  cause: cause,
105
131
  });
106
132
  }
@@ -115,6 +141,7 @@ export class CofheError extends Error {
115
141
  message: this.message,
116
142
  hint: this.hint,
117
143
  context: this.context,
144
+ apiErrorCode: this.apiErrorCode,
118
145
  cause: this.cause
119
146
  ? {
120
147
  name: this.cause.name,
@@ -130,7 +157,8 @@ export class CofheError extends Error {
130
157
  * Returns a human-readable string representation of the error
131
158
  */
132
159
  toString(): string {
133
- const parts = [`${this.name} [${this.code}]: ${this.message}`];
160
+ const codeSuffix = this.apiErrorCode ? ` (api: ${this.apiErrorCode})` : '';
161
+ const parts = [`${this.name} [${this.code}]${codeSuffix}: ${this.message}`];
134
162
 
135
163
  if (this.hint) {
136
164
  parts.push(`Hint: ${this.hint}`);
package/core/index.ts CHANGED
@@ -12,7 +12,7 @@ export type {
12
12
  } from './debug.js';
13
13
 
14
14
  // Configuration (base implementations)
15
- export { createCofheConfigBase, getCofheConfigItem } from './config.js';
15
+ export { createCofheConfigBase, getCofheConfigItem, assertNoRenamedConfigKeys } from './config.js';
16
16
  export type { CofheConfig, CofheInputConfig, CofheInternalConfig } from './config.js';
17
17
 
18
18
  // Types
@@ -21,7 +21,7 @@ export type {
21
21
  CofheClient as CofheClient,
22
22
  CofheClientParams as CofheClientParams,
23
23
  CofheClientConnectionState as CofheClientConnectionState,
24
- CofheClientPermits as CofheClientPermits,
24
+ CofheClientACPs as CofheClientACPs,
25
25
  } from './clientTypes.js';
26
26
 
27
27
  export type {
@@ -40,18 +40,8 @@ export type {
40
40
  EncryptableAddress,
41
41
  // Encrypted types
42
42
  EncryptedNumber,
43
- EncryptedItemInput,
44
- EncryptedBoolInput,
45
- EncryptedUint8Input,
46
- EncryptedUint16Input,
47
- EncryptedUint32Input,
48
- EncryptedUint64Input,
49
- EncryptedUint128Input,
50
- EncryptedAddressInput,
51
- EncryptedItemInputs,
52
- EncryptableToEncryptedItemInputMap,
53
43
  FheTypeValue,
54
- // Hash-plus-proof external types
44
+ // Batch-verified external hash + signature types
55
45
  ExternalBoolHash,
56
46
  ExternalUint8Hash,
57
47
  ExternalUint16Hash,
@@ -81,7 +71,6 @@ export {
81
71
  isEncryptableItem,
82
72
  EncryptStep,
83
73
  isLastEncryptionStep,
84
- assertCorrectEncryptedItemInput,
85
74
  } from './types.js';
86
75
 
87
76
  // Error handling
@@ -98,6 +87,7 @@ export type { KeysStorage, KeysStore } from './keyStore.js';
98
87
 
99
88
  // Builders (exported via client, but can be imported directly for typing)
100
89
  export { EncryptInputsBuilder } from './encrypt/encryptInputsBuilder.js';
90
+ export type { EncryptInputsBuilderUnset } from './encrypt/encryptInputsBuilder.js';
101
91
  export { DecryptForViewBuilder } from './decrypt/decryptForViewBuilder.js';
102
92
  export { DecryptForTxBuilder } from './decrypt/decryptForTxBuilder.js';
103
93
  export type { DecryptForTxResult } from './decrypt/decryptForTxBuilder.js';
@@ -0,0 +1,52 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { acps } from '../acps.js';
3
+
4
+ type ScopeOpts = {
5
+ revokerData?: number;
6
+ revokerContract?: string;
7
+ scope?: number;
8
+ contracts?: string[];
9
+ handles?: bigint[];
10
+ };
11
+ const opts = (o: ScopeOpts): ScopeOpts => o;
12
+
13
+ const CHAIN = 31337;
14
+ const REVOKER = '0x00000000000000000000000000000000000000aa' as `0x${string}`;
15
+ const CONTRACT_A = '0x00000000000000000000000000000000000000bb' as `0x${string}`;
16
+
17
+ const acpConfig = {
18
+ defaultRevoker: { [CHAIN]: REVOKER },
19
+ defaultContractScopes: { [CHAIN]: [CONTRACT_A] },
20
+ };
21
+
22
+ describe('applyACPDefaults', () => {
23
+ it('injects default revoker (contract + creation timestamp) when no revoker options given', () => {
24
+ const before = Math.round(Date.now() / 1000);
25
+ const result = acps.applyACPDefaults(opts({}), acpConfig, CHAIN);
26
+ expect(result.revokerContract).toBe(REVOKER);
27
+ expect(result.revokerData).toBeGreaterThanOrEqual(before - 60); // 60s clock-skew backdating
28
+ expect(result.revokerData).toBeLessThanOrEqual(Math.round(Date.now() / 1000) - 59);
29
+ });
30
+
31
+ it('does not override an explicit revoker pair', () => {
32
+ const result = acps.applyACPDefaults({ revokerData: 42, revokerContract: CONTRACT_A }, acpConfig, CHAIN);
33
+ expect(result.revokerData).toBe(42);
34
+ expect(result.revokerContract).toBe(CONTRACT_A);
35
+ });
36
+
37
+ it('injects default contract scopes when no scope options given', () => {
38
+ const result = acps.applyACPDefaults(opts({}), acpConfig, CHAIN);
39
+ expect(result.contracts).toEqual([CONTRACT_A]);
40
+ });
41
+
42
+ it('does not inject scopes when the caller provides any scope option (incl. explicit scope)', () => {
43
+ expect(acps.applyACPDefaults(opts({ scope: 0 }), acpConfig, CHAIN).contracts).toBeUndefined();
44
+ expect(acps.applyACPDefaults(opts({ handles: [1n] }), acpConfig, CHAIN).contracts).toBeUndefined();
45
+ expect(acps.applyACPDefaults(opts({ contracts: [] }), acpConfig, CHAIN).contracts).toEqual([]);
46
+ });
47
+
48
+ it('no-op for chains without defaults and for missing config', () => {
49
+ expect(acps.applyACPDefaults(opts({}), acpConfig, 999)).toEqual({});
50
+ expect(acps.applyACPDefaults(opts({}), undefined, CHAIN)).toEqual({});
51
+ });
52
+ });