@0xpolygonid/js-sdk 1.18.3 → 1.19.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.
@@ -41,12 +41,24 @@ exports.defineMerklizedRootPosition = defineMerklizedRootPosition;
41
41
  * Returns profile DID based on did and profile nonce
42
42
  *
43
43
  * @param {DID} [did] - did from which profile will be derived
44
- * @param {number} [profileNonce] - profile nonce
44
+ * @param {number | string} [profileNonce] - profile nonce
45
45
  * @returns {DID}
46
46
  */
47
47
  const generateProfileDID = (did, profileNonce) => {
48
48
  const id = js_iden3_core_1.DID.idFromDID(did);
49
- const profile = js_iden3_core_1.Id.profileId(id, BigInt(profileNonce ?? 0));
49
+ profileNonce = profileNonce ?? 0;
50
+ if (!isBigInt(profileNonce)) {
51
+ throw new Error('profile must be number or decimal string');
52
+ }
53
+ const profile = js_iden3_core_1.Id.profileId(id, BigInt(profileNonce));
50
54
  return js_iden3_core_1.DID.parseFromId(profile);
51
55
  };
52
56
  exports.generateProfileDID = generateProfileDID;
57
+ const isBigInt = (x) => {
58
+ try {
59
+ return BigInt(x).toString() === x.toString();
60
+ }
61
+ catch {
62
+ return false; // conversion to BigInt failed, surely it is not a BigInt
63
+ }
64
+ };
@@ -176,12 +176,20 @@ class IdentityWallet {
176
176
  },
177
177
  allowedIssuers: [did.string()]
178
178
  });
179
- if (credentials.length) {
179
+ // if credential exists with the same credential status type we return this credential
180
+ if (credentials.length === 1 &&
181
+ credentials[0].credentialStatus.type === opts.revocationOpts.type) {
180
182
  return {
181
183
  did,
182
184
  credential: credentials[0]
183
185
  };
184
186
  }
187
+ // otherwise something is already wrong with storage as it has more than 1 credential in it or credential status type of existing credential is different from what user provides - We should remove everything and create new credential.
188
+ // in this way credential status of auth credential can be upgraded
189
+ for (let i = 0; i < credentials.length; i++) {
190
+ await this._credentialWallet.remove(credentials[i].id);
191
+ }
192
+ // otherwise we create a new credential
185
193
  const credential = await this.createAuthBJJCredential(did, pubKey, authClaim, currentState, opts.revocationOpts);
186
194
  const index = authClaim.hIndex();
187
195
  const { proof } = await claimsTree.generateProof(index, ctr);
@@ -199,10 +207,13 @@ class IdentityWallet {
199
207
  coreClaim: authClaim
200
208
  });
201
209
  credential.proof = [mtpProof];
202
- await this.publishRevocationInfoByCredentialStatusType(did, opts.revocationOpts.type, {
203
- rhsUrl: opts.revocationOpts.id,
204
- onChain: opts.revocationOpts.onChain
205
- });
210
+ // only if user specified that genesis state publishing is not needed we won't do this.
211
+ if (!opts.revocationOpts.genesisPublishingDisabled) {
212
+ await this.publishRevocationInfoByCredentialStatusType(did, opts.revocationOpts.type, {
213
+ rhsUrl: opts.revocationOpts.id,
214
+ onChain: opts.revocationOpts.onChain
215
+ });
216
+ }
206
217
  await this._credentialWallet.save(credential);
207
218
  return {
208
219
  did,
@@ -565,23 +576,13 @@ class IdentityWallet {
565
576
  throw new Error(`there is no registered publisher to save hash is not registered for ${credentialStatusType} is not registered`);
566
577
  }
567
578
  let nodes = [];
568
- if (opts?.treeModel) {
569
- nodes = await (0, credentials_1.getNodesRepresentation)(opts.revokedNonces, {
570
- revocationTree: opts.treeModel.revocationTree,
571
- claimsTree: opts.treeModel.claimsTree,
572
- state: opts.treeModel.state,
573
- rootsTree: opts.treeModel.rootsTree
574
- }, opts.treeModel.state);
575
- }
576
- else {
577
- const treeState = await this.getDIDTreeModel(issuerDID);
578
- nodes = await (0, credentials_1.getNodesRepresentation)(opts?.revokedNonces, {
579
- revocationTree: treeState.revocationTree,
580
- claimsTree: treeState.claimsTree,
581
- state: treeState.state,
582
- rootsTree: treeState.rootsTree
583
- }, treeState.state);
584
- }
579
+ const tree = opts?.treeModel ?? (await this.getDIDTreeModel(issuerDID));
580
+ nodes = await (0, credentials_1.getNodesRepresentation)(opts?.revokedNonces ?? [], {
581
+ revocationTree: tree.revocationTree,
582
+ claimsTree: tree.claimsTree,
583
+ state: tree.state,
584
+ rootsTree: tree.rootsTree
585
+ }, tree.state);
585
586
  if (!nodes.length) {
586
587
  return;
587
588
  }
@@ -36,11 +36,23 @@ export const defineMerklizedRootPosition = (metadata, position) => {
36
36
  * Returns profile DID based on did and profile nonce
37
37
  *
38
38
  * @param {DID} [did] - did from which profile will be derived
39
- * @param {number} [profileNonce] - profile nonce
39
+ * @param {number | string} [profileNonce] - profile nonce
40
40
  * @returns {DID}
41
41
  */
42
42
  export const generateProfileDID = (did, profileNonce) => {
43
43
  const id = DID.idFromDID(did);
44
- const profile = Id.profileId(id, BigInt(profileNonce ?? 0));
44
+ profileNonce = profileNonce ?? 0;
45
+ if (!isBigInt(profileNonce)) {
46
+ throw new Error('profile must be number or decimal string');
47
+ }
48
+ const profile = Id.profileId(id, BigInt(profileNonce));
45
49
  return DID.parseFromId(profile);
46
50
  };
51
+ const isBigInt = (x) => {
52
+ try {
53
+ return BigInt(x).toString() === x.toString();
54
+ }
55
+ catch {
56
+ return false; // conversion to BigInt failed, surely it is not a BigInt
57
+ }
58
+ };
@@ -150,12 +150,20 @@ export class IdentityWallet {
150
150
  },
151
151
  allowedIssuers: [did.string()]
152
152
  });
153
- if (credentials.length) {
153
+ // if credential exists with the same credential status type we return this credential
154
+ if (credentials.length === 1 &&
155
+ credentials[0].credentialStatus.type === opts.revocationOpts.type) {
154
156
  return {
155
157
  did,
156
158
  credential: credentials[0]
157
159
  };
158
160
  }
161
+ // otherwise something is already wrong with storage as it has more than 1 credential in it or credential status type of existing credential is different from what user provides - We should remove everything and create new credential.
162
+ // in this way credential status of auth credential can be upgraded
163
+ for (let i = 0; i < credentials.length; i++) {
164
+ await this._credentialWallet.remove(credentials[i].id);
165
+ }
166
+ // otherwise we create a new credential
159
167
  const credential = await this.createAuthBJJCredential(did, pubKey, authClaim, currentState, opts.revocationOpts);
160
168
  const index = authClaim.hIndex();
161
169
  const { proof } = await claimsTree.generateProof(index, ctr);
@@ -173,10 +181,13 @@ export class IdentityWallet {
173
181
  coreClaim: authClaim
174
182
  });
175
183
  credential.proof = [mtpProof];
176
- await this.publishRevocationInfoByCredentialStatusType(did, opts.revocationOpts.type, {
177
- rhsUrl: opts.revocationOpts.id,
178
- onChain: opts.revocationOpts.onChain
179
- });
184
+ // only if user specified that genesis state publishing is not needed we won't do this.
185
+ if (!opts.revocationOpts.genesisPublishingDisabled) {
186
+ await this.publishRevocationInfoByCredentialStatusType(did, opts.revocationOpts.type, {
187
+ rhsUrl: opts.revocationOpts.id,
188
+ onChain: opts.revocationOpts.onChain
189
+ });
190
+ }
180
191
  await this._credentialWallet.save(credential);
181
192
  return {
182
193
  did,
@@ -539,23 +550,13 @@ export class IdentityWallet {
539
550
  throw new Error(`there is no registered publisher to save hash is not registered for ${credentialStatusType} is not registered`);
540
551
  }
541
552
  let nodes = [];
542
- if (opts?.treeModel) {
543
- nodes = await getNodesRepresentation(opts.revokedNonces, {
544
- revocationTree: opts.treeModel.revocationTree,
545
- claimsTree: opts.treeModel.claimsTree,
546
- state: opts.treeModel.state,
547
- rootsTree: opts.treeModel.rootsTree
548
- }, opts.treeModel.state);
549
- }
550
- else {
551
- const treeState = await this.getDIDTreeModel(issuerDID);
552
- nodes = await getNodesRepresentation(opts?.revokedNonces, {
553
- revocationTree: treeState.revocationTree,
554
- claimsTree: treeState.claimsTree,
555
- state: treeState.state,
556
- rootsTree: treeState.rootsTree
557
- }, treeState.state);
558
- }
553
+ const tree = opts?.treeModel ?? (await this.getDIDTreeModel(issuerDID));
554
+ nodes = await getNodesRepresentation(opts?.revokedNonces ?? [], {
555
+ revocationTree: tree.revocationTree,
556
+ claimsTree: tree.claimsTree,
557
+ state: tree.state,
558
+ rootsTree: tree.rootsTree
559
+ }, tree.state);
559
560
  if (!nodes.length) {
560
561
  return;
561
562
  }
@@ -50,7 +50,7 @@ export type PackerParams = {
50
50
  export type ZKPPackerParams = PackerParams & {
51
51
  senderDID: DID;
52
52
  /** @deprecated */
53
- profileNonce?: number;
53
+ profileNonce?: number | string;
54
54
  provingMethodAlg: ProvingMethodAlg;
55
55
  };
56
56
  /**
@@ -20,7 +20,7 @@ export declare const defineMerklizedRootPosition: (metadata?: SchemaMetadata, po
20
20
  * Returns profile DID based on did and profile nonce
21
21
  *
22
22
  * @param {DID} [did] - did from which profile will be derived
23
- * @param {number} [profileNonce] - profile nonce
23
+ * @param {number | string} [profileNonce] - profile nonce
24
24
  * @returns {DID}
25
25
  */
26
- export declare const generateProfileDID: (did: DID, profileNonce?: number) => DID;
26
+ export declare const generateProfileDID: (did: DID, profileNonce?: number | string) => DID;
@@ -24,13 +24,19 @@ export type IdentityCreationOptions = {
24
24
  /**
25
25
  * Options for creating Auth BJJ credential
26
26
  * seed - seed to generate BJJ key pair
27
- * revocationOpts -
27
+ * revocationOpts
28
+ * nonce - explicit revocation nonce to use
29
+ * onChain - onchain status related option
30
+ * txCallback - defines how the TransactionReceipt is handled
31
+ * publishMode - specifies the work of transaction polling type: sync / async / callback
32
+ * genesisPublishingDisabled - genesis is publishing by default. Set `true` to prevent genesis publishing
28
33
  */
29
34
  export type AuthBJJCredentialCreationOptions = {
30
35
  revocationOpts: {
31
36
  id: string;
32
37
  type: CredentialStatusType;
33
38
  nonce?: number;
39
+ genesisPublishingDisabled?: boolean;
34
40
  onChain?: {
35
41
  txCallback?: (tx: TransactionReceipt) => Promise<void>;
36
42
  publishMode?: PublishMode;
@@ -109,11 +115,11 @@ export interface IIdentityWallet {
109
115
  * Creates profile based on genesis identifier
110
116
  *
111
117
  * @param {DID} did - identity to derive profile from
112
- * @param {number} nonce - unique integer number to generate a profile
118
+ * @param {number |string} nonce - unique integer number to generate a profile
113
119
  * @param {string} verifier - verifier identity/alias in a string from
114
120
  * @returns `Promise<DID>` - profile did
115
121
  */
116
- createProfile(did: DID, nonce: number, verifier: string): Promise<DID>;
122
+ createProfile(did: DID, nonce: number | string, verifier: string): Promise<DID>;
117
123
  /**
118
124
  * Generates a new key
119
125
  *
@@ -271,7 +277,7 @@ export interface IIdentityWallet {
271
277
  * @returns `{Promise<{nonce:number, genesisIdentifier: DID}>}`
272
278
  */
273
279
  getGenesisDIDMetadata(did: DID): Promise<{
274
- nonce: number;
280
+ nonce: number | string;
275
281
  genesisDID: DID;
276
282
  }>;
277
283
  /**
@@ -388,11 +394,11 @@ export declare class IdentityWallet implements IIdentityWallet {
388
394
  }>;
389
395
  /** {@inheritDoc IIdentityWallet.getGenesisDIDMetadata} */
390
396
  getGenesisDIDMetadata(did: DID): Promise<{
391
- nonce: number;
397
+ nonce: number | string;
392
398
  genesisDID: DID;
393
399
  }>;
394
400
  /** {@inheritDoc IIdentityWallet.createProfile} */
395
- createProfile(did: DID, nonce: number, verifier: string): Promise<DID>;
401
+ createProfile(did: DID, nonce: number | string, verifier: string): Promise<DID>;
396
402
  /**
397
403
  *
398
404
  * gets profile identity by genesis identifiers
@@ -69,7 +69,6 @@ export interface IProofService {
69
69
  *
70
70
  * @param {Uint8Array} hash - challenge that will be signed
71
71
  * @param {DID} did - identity that will generate a proof
72
- * @param {Number} profileNonce - identity that will generate a proof
73
72
  * @param {CircuitId} circuitId - circuit id for authentication
74
73
  * @returns `Promise<Uint8Array>`
75
74
  */
@@ -7,8 +7,8 @@ import { IIdentityWallet } from '../../identity';
7
7
  import { IStateStorage } from '../../storage';
8
8
  import { ICredentialWallet } from '../../credentials';
9
9
  export type DIDProfileMetadata = {
10
- authProfileNonce: number;
11
- credentialSubjectProfileNonce: number;
10
+ authProfileNonce: number | string;
11
+ credentialSubjectProfileNonce: number | string;
12
12
  };
13
13
  export type ProofGenerationOptions = {
14
14
  skipRevocation: boolean;
@@ -9,7 +9,7 @@ export type Identity = {
9
9
  /** Profile structure that can be used for profiles storage */
10
10
  export type Profile = {
11
11
  id: string;
12
- nonce: number;
12
+ nonce: number | string;
13
13
  genesisIdentifier: string;
14
14
  verifier: string;
15
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xpolygonid/js-sdk",
3
- "version": "1.18.3",
3
+ "version": "1.19.0",
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",