From cb397e42b76d86712eb823ffa1663068a25a5f27 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Thu, 21 Sep 2023 14:26:26 +0200 Subject: [PATCH] feat: unify fromJSON() for Profile, Persona and Claim --- src/claim.js | 5 +++-- src/persona.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/profile.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) diff --git a/src/claim.js b/src/claim.js index 2d464ba..c091d6b 100644 --- a/src/claim.js +++ b/src/claim.js @@ -79,10 +79,11 @@ export class Claim { /** * @function * @param {object} claimObject + * @returns {Claim | Error} * @example - * const claimAlt = doip.Claim(JSON.stringify(claim)); + * doip.Claim.fromJSON(JSON.stringify(claim)); */ - static fromJson (claimObject) { + static fromJSON (claimObject) { /** @type {Claim} */ let claim let result diff --git a/src/persona.js b/src/persona.js index 653fb2b..15501d4 100644 --- a/src/persona.js +++ b/src/persona.js @@ -13,6 +13,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ +import { Claim } from './claim.js' + /** * A persona with identity claims * @class @@ -72,6 +74,37 @@ export class Persona { this.isRevoked = false } + /** + * @function + * @param {object} personaObject + * @param {number} profileVersion + * @returns {Persona | Error} + * @example + * doip.Persona.fromJSON(JSON.stringify(persona), 2); + */ + static fromJSON (personaObject, profileVersion) { + /** @type {Persona} */ + let persona + let result + + if (typeof personaObject === 'object' && profileVersion) { + switch (profileVersion) { + case 2: + result = importJsonPersonaVersion2(personaObject) + if (result instanceof Error) { + throw result + } + persona = result + break + + default: + throw new Error('Invalid persona version') + } + } + + return persona + } + /** * @function * @param {string} identifier @@ -136,3 +169,21 @@ export class Persona { } } } + +/** + * @param {object} personaObject + * @returns {Persona | Error} + */ +function importJsonPersonaVersion2 (personaObject) { + const claims = personaObject.claims.map(x => Claim.fromJSON(x)) + + const persona = new Persona(personaObject.name, claims) + + persona.identifier = personaObject.identifier + persona.email = personaObject.email + persona.description = personaObject.description + persona.avatarUrl = personaObject.avatarUrl + persona.isRevoked = personaObject.isRevoked + + return persona +} diff --git a/src/profile.js b/src/profile.js index a5b0846..698d09c 100644 --- a/src/profile.js +++ b/src/profile.js @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ import { PublicKeyFetchMethod, PublicKeyEncoding, PublicKeyType } from './enums.js' +import { Persona } from './persona.js' /** * A profile of personas with identity claims @@ -135,6 +136,36 @@ export class Profile { this.verifiers = [] } + /** + * @function + * @param {object} profileObject + * @returns {Profile | Error} + * @example + * doip.Profile.fromJSON(JSON.stringify(profile)); + */ + static fromJSON (profileObject) { + /** @type {Profile} */ + let profile + let result + + if (typeof profileObject === 'object' && 'profileVersion' in profileObject) { + switch (profileObject.profileVersion) { + case 2: + result = importJsonProfileVersion2(profileObject) + if (result instanceof Error) { + throw result + } + profile = result + break + + default: + throw new Error('Invalid profile version') + } + } + + return profile + } + /** * @function * @param {string} name @@ -171,3 +202,23 @@ export class Profile { } } } + +/** + * @param {object} profileObject + * @returns {Profile | Error} + */ +function importJsonProfileVersion2 (profileObject) { + if (!('profileVersion' in profileObject && profileObject.profileVersion === 2)) { + return new Error('Invalid profile') + } + + const personas = profileObject.personas.map(x => Persona.fromJSON(x, 2)) + + const profile = new Profile(profileObject.profileType, profileObject.identifier, personas) + + profile.primaryPersonaIndex = profileObject.primaryPersonaIndex + profile.publicKey = profileObject.publicKey + profile.verifiers = profileObject.verifiers + + return profile +}