From 6b6b6744b5c985aafef63a0e93f1f2113b839390 Mon Sep 17 00:00:00 2001 From: GopherDID <74898029+vmidyllic@users.noreply.github.com> Date: Tue, 10 Sep 2024 11:50:10 +0300 Subject: [PATCH] update profile nonce, so it can be number or string (#268) * update profile nonce, so it can be number or string * 1.19.0 * add validation check --- package-lock.json | 4 ++-- package.json | 2 +- src/iden3comm/types/packer.ts | 2 +- src/identity/common.ts | 20 +++++++++++++++++--- src/identity/identity-wallet.ts | 10 +++++----- src/proof/proof-service.ts | 1 - src/proof/provers/inputs-generator.ts | 4 ++-- src/storage/entities/identity.ts | 2 +- 8 files changed, 29 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index a24fccbe..f6f18f7f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@0xpolygonid/js-sdk", - "version": "1.18.4", + "version": "1.19.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@0xpolygonid/js-sdk", - "version": "1.18.4", + "version": "1.19.0", "license": "MIT or Apache-2.0", "dependencies": { "@noble/curves": "^1.4.0", diff --git a/package.json b/package.json index c55a9976..da23f5c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@0xpolygonid/js-sdk", - "version": "1.18.4", + "version": "1.19.0", "description": "SDK to work with Polygon ID", "main": "dist/node/cjs/index.js", "module": "dist/node/esm/index.js", diff --git a/src/iden3comm/types/packer.ts b/src/iden3comm/types/packer.ts index ab1dccf7..db4ca2a6 100644 --- a/src/iden3comm/types/packer.ts +++ b/src/iden3comm/types/packer.ts @@ -60,7 +60,7 @@ export type PackerParams = { export type ZKPPackerParams = PackerParams & { senderDID: DID; /** @deprecated */ - profileNonce?: number; + profileNonce?: number | string; provingMethodAlg: ProvingMethodAlg; }; diff --git a/src/identity/common.ts b/src/identity/common.ts index 5992585b..f47ac2f7 100644 --- a/src/identity/common.ts +++ b/src/identity/common.ts @@ -45,11 +45,25 @@ export const defineMerklizedRootPosition = ( * Returns profile DID based on did and profile nonce * * @param {DID} [did] - did from which profile will be derived - * @param {number} [profileNonce] - profile nonce + * @param {number | string} [profileNonce] - profile nonce * @returns {DID} */ -export const generateProfileDID = (did: DID, profileNonce?: number): DID => { +export const generateProfileDID = (did: DID, profileNonce?: number | string): DID => { const id = DID.idFromDID(did); - const profile = Id.profileId(id, BigInt(profileNonce ?? 0)); + + profileNonce = profileNonce ?? 0; + + if (!isBigInt(profileNonce)) { + throw new Error('profile must be number or decimal string'); + } + const profile = Id.profileId(id, BigInt(profileNonce)); return DID.parseFromId(profile); }; + +const isBigInt = (x: number | string): boolean => { + try { + return BigInt(x).toString() === x.toString(); + } catch { + return false; // conversion to BigInt failed, surely it is not a BigInt + } +}; diff --git a/src/identity/identity-wallet.ts b/src/identity/identity-wallet.ts index 980cbb57..fd779f3d 100644 --- a/src/identity/identity-wallet.ts +++ b/src/identity/identity-wallet.ts @@ -169,11 +169,11 @@ export interface IIdentityWallet { * Creates profile based on genesis identifier * * @param {DID} did - identity to derive profile from - * @param {number} nonce - unique integer number to generate a profile + * @param {number |string} nonce - unique integer number to generate a profile * @param {string} verifier - verifier identity/alias in a string from * @returns `Promise` - profile did */ - createProfile(did: DID, nonce: number, verifier: string): Promise; + createProfile(did: DID, nonce: number | string, verifier: string): Promise; /** * Generates a new key @@ -387,7 +387,7 @@ export interface IIdentityWallet { * @param {DID} did - profile that has been derived or genesis identity * @returns `{Promise<{nonce:number, genesisIdentifier: DID}>}` */ - getGenesisDIDMetadata(did: DID): Promise<{ nonce: number; genesisDID: DID }>; + getGenesisDIDMetadata(did: DID): Promise<{ nonce: number | string; genesisDID: DID }>; /** * @@ -786,7 +786,7 @@ export class IdentityWallet implements IIdentityWallet { } /** {@inheritDoc IIdentityWallet.getGenesisDIDMetadata} */ - async getGenesisDIDMetadata(did: DID): Promise<{ nonce: number; genesisDID: DID }> { + async getGenesisDIDMetadata(did: DID): Promise<{ nonce: number | string; genesisDID: DID }> { // check if it is a genesis identity const identity = await this._storage.identity.getIdentity(did.string()); @@ -802,7 +802,7 @@ export class IdentityWallet implements IIdentityWallet { } /** {@inheritDoc IIdentityWallet.createProfile} */ - async createProfile(did: DID, nonce: number, verifier: string): Promise { + async createProfile(did: DID, nonce: number | string, verifier: string): Promise { const profileDID = generateProfileDID(did, nonce); const identityProfiles = await this._storage.identity.getProfilesByGenesisIdentifier( diff --git a/src/proof/proof-service.ts b/src/proof/proof-service.ts index 91ec1719..02719bd1 100644 --- a/src/proof/proof-service.ts +++ b/src/proof/proof-service.ts @@ -126,7 +126,6 @@ export interface IProofService { * * @param {Uint8Array} hash - challenge that will be signed * @param {DID} did - identity that will generate a proof - * @param {Number} profileNonce - identity that will generate a proof * @param {CircuitId} circuitId - circuit id for authentication * @returns `Promise` */ diff --git a/src/proof/provers/inputs-generator.ts b/src/proof/provers/inputs-generator.ts index 37bbf9f3..20cd7874 100644 --- a/src/proof/provers/inputs-generator.ts +++ b/src/proof/provers/inputs-generator.ts @@ -40,8 +40,8 @@ import { import { isEthereumIdentity } from '../../utils'; export type DIDProfileMetadata = { - authProfileNonce: number; - credentialSubjectProfileNonce: number; + authProfileNonce: number | string; + credentialSubjectProfileNonce: number | string; }; export type ProofGenerationOptions = { diff --git a/src/storage/entities/identity.ts b/src/storage/entities/identity.ts index 2b9f3505..1412a814 100644 --- a/src/storage/entities/identity.ts +++ b/src/storage/entities/identity.ts @@ -11,7 +11,7 @@ export type Identity = { /** Profile structure that can be used for profiles storage */ export type Profile = { id: string; - nonce: number; + nonce: number | string; genesisIdentifier: string; verifier: string; };