@0xpolygonid/js-sdk 1.31.1 → 1.31.3

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 (30) hide show
  1. package/dist/browser/esm/index.js +773 -321
  2. package/dist/browser/esm/index.js.map +1 -1
  3. package/dist/browser/umd/index.js +773 -319
  4. package/dist/browser/umd/index.js.map +1 -1
  5. package/dist/node/cjs/credentials/status/on-chain-revocation.js +3 -10
  6. package/dist/node/cjs/identity/identity-wallet.js +17 -4
  7. package/dist/node/cjs/storage/blockchain/onchain-zkp-verifier.js +0 -1
  8. package/dist/node/cjs/storage/blockchain/state.js +120 -3
  9. package/dist/node/cjs/storage/memory/cache-lru.js +25 -0
  10. package/dist/node/cjs/storage/memory/index.js +1 -0
  11. package/dist/node/cjs/storage/shared/identity-storage.js +7 -0
  12. package/dist/node/cjs/verifiable/constants.js +6 -1
  13. package/dist/node/esm/credentials/status/on-chain-revocation.js +4 -11
  14. package/dist/node/esm/identity/identity-wallet.js +17 -4
  15. package/dist/node/esm/storage/blockchain/onchain-zkp-verifier.js +0 -1
  16. package/dist/node/esm/storage/blockchain/state.js +120 -3
  17. package/dist/node/esm/storage/memory/cache-lru.js +18 -0
  18. package/dist/node/esm/storage/memory/index.js +1 -0
  19. package/dist/node/esm/storage/shared/identity-storage.js +7 -0
  20. package/dist/node/esm/verifiable/constants.js +5 -0
  21. package/dist/types/credentials/status/on-chain-revocation.d.ts +6 -2
  22. package/dist/types/identity/identity-wallet.d.ts +18 -3
  23. package/dist/types/storage/blockchain/state.d.ts +65 -1
  24. package/dist/types/storage/entities/identity.d.ts +1 -0
  25. package/dist/types/storage/interfaces/identity.d.ts +8 -1
  26. package/dist/types/storage/memory/cache-lru.d.ts +10 -0
  27. package/dist/types/storage/memory/index.d.ts +1 -0
  28. package/dist/types/storage/shared/identity-storage.d.ts +5 -0
  29. package/dist/types/verifiable/constants.d.ts +5 -0
  30. package/package.json +3 -2
@@ -0,0 +1,18 @@
1
+ import QuickLRU from 'quick-lru';
2
+ export function createInMemoryCache(params) {
3
+ const cache = new QuickLRU({ maxSize: params.maxSize, maxAge: params.ttl });
4
+ return {
5
+ get: async (key) => {
6
+ return cache.get(key);
7
+ },
8
+ set: async (key, value, ttl) => {
9
+ cache.set(key, value, { maxAge: ttl ?? params.ttl });
10
+ },
11
+ clear: async () => {
12
+ cache.clear();
13
+ },
14
+ delete: async (key) => {
15
+ cache.delete(key);
16
+ }
17
+ };
18
+ }
@@ -1,2 +1,3 @@
1
1
  export * from './merkletree';
2
2
  export * from './data-source';
3
+ export * from './cache-lru';
@@ -24,9 +24,16 @@ export class IdentityStorage {
24
24
  await this._profileDataSource.save(element.id, element);
25
25
  }
26
26
  }
27
+ /**
28
+ * @deprecated The method should not be used. It returns only one profile per verifier, which can potentially restrict business use cases
29
+ * Use getProfilesByVerifier instead.
30
+ */
27
31
  async getProfileByVerifier(verifier) {
28
32
  return this._profileDataSource.get(verifier, 'verifier');
29
33
  }
34
+ async getProfilesByVerifier(verifier, tags) {
35
+ return (await this._profileDataSource.load()).filter((p) => p.verifier === verifier && (!tags || tags.every((tag) => p.tags?.includes(tag))));
36
+ }
30
37
  async getProfileById(profileId) {
31
38
  return this._profileDataSource.get(profileId);
32
39
  }
@@ -16,6 +16,7 @@ export const VerifiableConstants = Object.freeze({
16
16
  ID_WALLET_CORE_CLAIM_IS_NOT_SET: 'core claim is not set in credential proofs',
17
17
  ID_WALLET_PROFILE_OR_IDENTITY_NOT_FOUND: 'profile or identity not found',
18
18
  ID_WALLET_PROFILE_ALREADY_EXISTS: 'profile with given nonce or verifier already exists',
19
+ ID_WALLET_PROFILE_ALREADY_EXISTS_VERIFIER_TAGS: 'profile with given verifier and tags already exists',
19
20
  ID_WALLET_ISSUER_AUTH_BJJ_CRED_MUST_HAVE_ANY_PROOF: 'issuer auth credential must have proof',
20
21
  ID_WALLET_ISSUER_AUTH_BJJ_CRED_MUST_HAVE_MTP_PROOF: 'mtp is required for auth bjj key to issue new credentials',
21
22
  // proof service
@@ -197,3 +198,7 @@ export var DisplayMethodType;
197
198
  (function (DisplayMethodType) {
198
199
  DisplayMethodType["Iden3BasicDisplayMethodV1"] = "Iden3BasicDisplayMethodV1";
199
200
  })(DisplayMethodType || (DisplayMethodType = {}));
201
+ /**
202
+ * Default cache max size for in-memory cache
203
+ */
204
+ export const DEFAULT_CACHE_MAX_SIZE = 10000;
@@ -2,6 +2,10 @@ import { RevocationStatus, CredentialStatus } from '../../verifiable';
2
2
  import { EthConnectionConfig } from '../../storage/blockchain';
3
3
  import { CredentialStatusResolver, CredentialStatusResolveOptions } from './resolver';
4
4
  import { DID } from '@iden3/js-iden3-core';
5
+ import { EthStateStorageOptions } from '../../storage/blockchain/state';
6
+ export type OnChainResolverOptions = {
7
+ stateStorageOptions?: EthStateStorageOptions;
8
+ };
5
9
  /**
6
10
  * OnChainIssuer is a class that allows to interact with the onchain contract
7
11
  * and build the revocation status.
@@ -11,13 +15,14 @@ import { DID } from '@iden3/js-iden3-core';
11
15
  */
12
16
  export declare class OnChainResolver implements CredentialStatusResolver {
13
17
  private readonly _configs;
18
+ private readonly _stateStorage;
14
19
  /**
15
20
  *
16
21
  * Creates an instance of OnChainIssuer.
17
22
  * @public
18
23
  * @param {Array<EthConnectionConfig>} _configs - list of ethereum network connections
19
24
  */
20
- constructor(_configs: EthConnectionConfig[]);
25
+ constructor(_configs: EthConnectionConfig[], _opts?: OnChainResolverOptions);
21
26
  /**
22
27
  * resolve is a method to resolve a credential status from the blockchain.
23
28
  *
@@ -48,6 +53,5 @@ export declare class OnChainResolver implements CredentialStatusResolver {
48
53
  stateHex: string;
49
54
  };
50
55
  networkByChainId(chainId: number): EthConnectionConfig;
51
- private _getStateStorageForIssuer;
52
56
  private _getOnChainRevocationStorageForIssuer;
53
57
  }
@@ -117,9 +117,10 @@ export interface IIdentityWallet {
117
117
  * @param {DID} did - identity to derive profile from
118
118
  * @param {number |string} nonce - unique integer number to generate a profile
119
119
  * @param {string} verifier - verifier identity/alias in a string from
120
+ * @param {string[]} tags - optional tag that can be assigned to profile by client
120
121
  * @returns `Promise<DID>` - profile did
121
122
  */
122
- createProfile(did: DID, nonce: number | string, verifier: string): Promise<DID>;
123
+ createProfile(did: DID, nonce: number | string, verifier: string, tags?: string[]): Promise<DID>;
123
124
  /**
124
125
  * Generates a new key
125
126
  *
@@ -291,11 +292,19 @@ export interface IIdentityWallet {
291
292
  /**
292
293
  *
293
294
  * gets profile identity by verifier
294
- *
295
+ * @deprecated The method should not be used. It returns only one profile per verifier, which can potentially restrict business use cases
295
296
  * @param {string} verifier - identifier of the verifier
296
297
  * @returns `{Promise<Profile>}`
297
298
  */
298
299
  getProfileByVerifier(verifier: string): Promise<Profile | undefined>;
300
+ /**
301
+ * gets profile by verifiers
302
+ *
303
+ * @param {string} verifier - verifier to which profile has been shared
304
+ * @param {string} tags - optional, tags to filter profile entry
305
+ * @returns `{Promise<Profile[]>}`
306
+ */
307
+ getProfilesByVerifier(verifier: string, tags?: string[]): Promise<Profile[]>;
299
308
  /**
300
309
  *
301
310
  * updates latest identity state in storage with given state or latest from the trees.
@@ -398,7 +407,7 @@ export declare class IdentityWallet implements IIdentityWallet {
398
407
  genesisDID: DID;
399
408
  }>;
400
409
  /** {@inheritDoc IIdentityWallet.createProfile} */
401
- createProfile(did: DID, nonce: number | string, verifier: string): Promise<DID>;
410
+ createProfile(did: DID, nonce: number | string, verifier: string, tags?: string[]): Promise<DID>;
402
411
  /**
403
412
  *
404
413
  * gets profile identity by genesis identifiers
@@ -409,7 +418,13 @@ export declare class IdentityWallet implements IIdentityWallet {
409
418
  getProfilesByDID(did: DID): Promise<Profile[]>;
410
419
  /** {@inheritDoc IIdentityWallet.generateKey} */
411
420
  generateKey(keyType: KmsKeyType): Promise<KmsKeyId>;
421
+ /**
422
+ * @deprecated The method should not be used. It returns only one profile per verifier, which can potentially restrict business use cases
423
+ * {@inheritDoc IIdentityWallet.getProfileByVerifier}
424
+ */
412
425
  getProfileByVerifier(verifier: string): Promise<Profile | undefined>;
426
+ /** {@inheritDoc IIdentityWallet.getProfilesByVerifier} */
427
+ getProfilesByVerifier(verifier: string, tags?: string[]): Promise<Profile[]>;
413
428
  /** {@inheritDoc IIdentityWallet.getDIDTreeModel} */
414
429
  getDIDTreeModel(did: DID): Promise<TreesModel>;
415
430
  /** {@inheritDoc IIdentityWallet.generateClaimMtp} */
@@ -3,6 +3,53 @@ import { ZKProof } from '@iden3/js-jwz';
3
3
  import { IStateStorage, UserStateTransitionInfo } from '../interfaces/state';
4
4
  import { Contract, JsonRpcProvider, Signer } from 'ethers';
5
5
  import { StateInfo } from '../entities/state';
6
+ import { ICache } from '../memory';
7
+ /**
8
+ * Configuration options for caching behavior
9
+ */
10
+ export type ResolverCacheOptions = {
11
+ /** TTL in milliseconds for latest states/roots (shorter since they can change) */
12
+ notReplacedTtl?: number;
13
+ /** TTL in milliseconds for historical states/roots (longer since they can change with less probability) */
14
+ replacedTtl?: number;
15
+ /** Maximum number of entries to store in cache */
16
+ maxSize?: number;
17
+ };
18
+ /**
19
+ * Simplified cache config with a single TTL (used for latest/gist proofs etc.)
20
+ */
21
+ export type SimpleCacheOptions = {
22
+ /** TTL in milliseconds for latest states/gist proof */
23
+ ttl?: number;
24
+ /** Maximum number of entries to store in cache */
25
+ maxSize?: number;
26
+ };
27
+ /**
28
+ * EthStateStorageOptions options for the Ethereum state storage.
29
+ */
30
+ export type EthStateStorageOptions = {
31
+ /** Disable caching */
32
+ disableCache?: boolean;
33
+ /** Configuration for latest state resolution caching */
34
+ latestStateCacheOptions?: {
35
+ /** Custom cache implementation (if not provided, uses in-memory cache) */
36
+ cache?: ICache<StateInfo>;
37
+ } & SimpleCacheOptions;
38
+ /** Configuration for state resolution caching */
39
+ stateCacheOptions?: {
40
+ /** Custom cache implementation (if not provided, uses in-memory cache) */
41
+ cache?: ICache<StateInfo>;
42
+ } & ResolverCacheOptions;
43
+ /** Configuration for GIST root resolution caching */
44
+ rootCacheOptions?: {
45
+ /** Custom cache implementation (if not provided, uses in-memory cache) */
46
+ cache?: ICache<RootInfo>;
47
+ } & ResolverCacheOptions;
48
+ gistProofCacheOptions?: {
49
+ /** Custom cache implementation for GIST proofs (if not provided, uses in-memory cache) */
50
+ cache?: ICache<StateProof>;
51
+ } & SimpleCacheOptions;
52
+ };
6
53
  /**
7
54
  * Configuration of ethereum based blockchain connection
8
55
  *
@@ -38,11 +85,20 @@ export declare class EthStateStorage implements IStateStorage {
38
85
  readonly stateContract: Contract;
39
86
  private readonly provider;
40
87
  private readonly _transactionService;
88
+ private _latestStateResolveCache;
89
+ private _stateResolveCache;
90
+ private _rootResolveCache;
91
+ private _gistProofResolveCache;
92
+ private _latestStateCacheOptions;
93
+ private _stateCacheOptions;
94
+ private _rootCacheOptions;
95
+ private _gistProofCacheOptions;
96
+ private _disableCache;
41
97
  /**
42
98
  * Creates an instance of EthStateStorage.
43
99
  * @param {EthConnectionConfig} [ethConfig=defaultEthConnectionConfig]
44
100
  */
45
- constructor(ethConfig: EthConnectionConfig | EthConnectionConfig[]);
101
+ constructor(ethConfig: EthConnectionConfig | EthConnectionConfig[], options?: EthStateStorageOptions);
46
102
  /** {@inheritdoc IStateStorage.getLatestStateById} */
47
103
  getLatestStateById(id: bigint): Promise<StateInfo>;
48
104
  /** {@inheritdoc IStateStorage.getStateInfoByIdAndState} */
@@ -57,6 +113,14 @@ export declare class EthStateStorage implements IStateStorage {
57
113
  getGISTRootInfo(root: bigint, id: bigint): Promise<RootInfo>;
58
114
  /** {@inheritdoc IStateStorage.getRpcProvider} */
59
115
  getRpcProvider(): JsonRpcProvider;
116
+ /** enable caching */
117
+ enableCache(): void;
118
+ /** disable caching */
119
+ disableCache(): void;
60
120
  private getStateContractAndProviderForId;
61
121
  private networkByChainId;
122
+ private getGistProofCacheKey;
123
+ private getLatestStateCacheKey;
124
+ private getStateCacheKey;
125
+ private getRootCacheKey;
62
126
  }
@@ -12,4 +12,5 @@ export type Profile = {
12
12
  nonce: number | string;
13
13
  genesisIdentifier: string;
14
14
  verifier: string;
15
+ tags?: string[];
15
16
  };
@@ -39,11 +39,18 @@ export interface IIdentityStorage {
39
39
  saveProfile(profile: Profile): Promise<void>;
40
40
  /**
41
41
  * gets profile by verifier
42
- *
42
+ * @deprecated The method should not be used. It returns only one profile per verifier, which can potentially restrict business use cases
43
43
  * @param {string} verifier - verifier to which profile has been shared
44
44
  * @returns `{Promise<Profile>}`
45
45
  */
46
46
  getProfileByVerifier(verifier: string): Promise<Profile | undefined>;
47
+ /**
48
+ * gets profile by verifiers
49
+ * @param {string} verifier - verifier to which profile has been shared
50
+ * @param {string[]} tags - optional tag to filter profile entry
51
+ * @returns `{Promise<Profile[]>}`
52
+ */
53
+ getProfilesByVerifier(verifier: string, tags?: string[]): Promise<Profile[]>;
47
54
  /**
48
55
  * gets profile by identifier
49
56
  *
@@ -0,0 +1,10 @@
1
+ export interface ICache<T> {
2
+ get(key: string): Promise<T | undefined>;
3
+ set(key: string, value: T, ttl?: number): Promise<void>;
4
+ delete(key: string): Promise<void>;
5
+ clear(): Promise<void>;
6
+ }
7
+ export declare function createInMemoryCache<T>(params: {
8
+ ttl?: number;
9
+ maxSize: number;
10
+ }): ICache<T>;
@@ -1,2 +1,3 @@
1
1
  export * from './merkletree';
2
2
  export * from './data-source';
3
+ export * from './cache-lru';
@@ -30,7 +30,12 @@ export declare class IdentityStorage implements IIdentityStorage {
30
30
  */
31
31
  constructor(_identityDataSource: IDataSource<Identity>, _profileDataSource: IDataSource<Profile>);
32
32
  saveProfile(profile: Profile): Promise<void>;
33
+ /**
34
+ * @deprecated The method should not be used. It returns only one profile per verifier, which can potentially restrict business use cases
35
+ * Use getProfilesByVerifier instead.
36
+ */
33
37
  getProfileByVerifier(verifier: string): Promise<Profile | undefined>;
38
+ getProfilesByVerifier(verifier: string, tags?: string[]): Promise<Profile[]>;
34
39
  getProfileById(profileId: string): Promise<Profile | undefined>;
35
40
  getProfilesByGenesisIdentifier(genesisIdentifier: string): Promise<Profile[]>;
36
41
  getAllIdentities(): Promise<Identity[]>;
@@ -14,6 +14,7 @@ export declare const VerifiableConstants: Readonly<{
14
14
  ID_WALLET_CORE_CLAIM_IS_NOT_SET: string;
15
15
  ID_WALLET_PROFILE_OR_IDENTITY_NOT_FOUND: string;
16
16
  ID_WALLET_PROFILE_ALREADY_EXISTS: string;
17
+ ID_WALLET_PROFILE_ALREADY_EXISTS_VERIFIER_TAGS: string;
17
18
  ID_WALLET_ISSUER_AUTH_BJJ_CRED_MUST_HAVE_ANY_PROOF: string;
18
19
  ID_WALLET_ISSUER_AUTH_BJJ_CRED_MUST_HAVE_MTP_PROOF: string;
19
20
  PROOF_SERVICE_NO_CREDENTIAL_FOR_IDENTITY_OR_PROFILE: string;
@@ -162,3 +163,7 @@ export declare enum PaymentFeatures {
162
163
  export declare enum DisplayMethodType {
163
164
  Iden3BasicDisplayMethodV1 = "Iden3BasicDisplayMethodV1"
164
165
  }
166
+ /**
167
+ * Default cache max size for in-memory cache
168
+ */
169
+ export declare const DEFAULT_CACHE_MAX_SIZE = 10000;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xpolygonid/js-sdk",
3
- "version": "1.31.1",
3
+ "version": "1.31.3",
4
4
  "description": "SDK to work with Polygon ID",
5
5
  "main": "dist/node/cjs/index.js",
6
6
  "module": "dist/node/esm/index.js",
@@ -99,7 +99,7 @@
99
99
  },
100
100
  "dependencies": {
101
101
  "@iden3/onchain-non-merklized-issuer-base-abi": "^0.0.3",
102
- "@iden3/universal-verifier-v2-abi": "^2.0.1",
102
+ "@iden3/universal-verifier-v2-abi": "^2.0.2",
103
103
  "@noble/curves": "^1.9.2",
104
104
  "ajv": "8.12.0",
105
105
  "ajv-formats": "2.1.1",
@@ -110,6 +110,7 @@
110
110
  "js-sha3": "0.9.3",
111
111
  "jsonld": "8.3.1",
112
112
  "pubsub-js": "1.9.4",
113
+ "quick-lru": "^7.0.1",
113
114
  "uuid": "9.0.1"
114
115
  },
115
116
  "browserslist": {