From 9b6f2cba7103ddcfea2d122d41bb6d44e9d03b60 Mon Sep 17 00:00:00 2001 From: Michael Avoyan Date: Thu, 11 Jul 2024 17:47:01 +0300 Subject: [PATCH] refactor cont --- .../repositories/CheckForOffersRepository.ts | 2 +- .../repositories/GenerateOffersRepository.ts | 2 +- packages/sample-server/src/routes/Routes.ts | 61 ++++++++++--------- packages/sample-server/src/utils/Converter.ts | 58 +++++++++--------- 4 files changed, 64 insertions(+), 59 deletions(-) diff --git a/packages/sample-app/src/repositories/CheckForOffersRepository.ts b/packages/sample-app/src/repositories/CheckForOffersRepository.ts index 2a894ac..43793a0 100644 --- a/packages/sample-app/src/repositories/CheckForOffersRepository.ts +++ b/packages/sample-app/src/repositories/CheckForOffersRepository.ts @@ -15,7 +15,7 @@ export const checkOffers = async ( const config = { url: Urls.checkOffers, method: 'POST', - data: { generateOffersDescriptor, sessionToken } + data: { ...generateOffersDescriptor, sessionToken } }; return await fetcher(config); } \ No newline at end of file diff --git a/packages/sample-app/src/repositories/GenerateOffersRepository.ts b/packages/sample-app/src/repositories/GenerateOffersRepository.ts index 4ce61a2..29325ab 100644 --- a/packages/sample-app/src/repositories/GenerateOffersRepository.ts +++ b/packages/sample-app/src/repositories/GenerateOffersRepository.ts @@ -14,7 +14,7 @@ export const generateOffers = async ( const config = { url: Urls.generateOffers, method: 'POST', - data: { generateOffersDescriptor }, + data: generateOffersDescriptor }; return await fetcher(config); } \ No newline at end of file diff --git a/packages/sample-server/src/routes/Routes.ts b/packages/sample-server/src/routes/Routes.ts index 6b13058..b970f39 100644 --- a/packages/sample-server/src/routes/Routes.ts +++ b/packages/sample-server/src/routes/Routes.ts @@ -6,15 +6,20 @@ */ import { - credentialManifestDescriptorFromJson, - credentialTypesUIFormSchemaDescriptorFromJson, didJwkDescriptorFromJson, didJwkFromJson, - finalizeOffersDescriptorFromJson, - generateOffersDescriptorFromJson, jwtDescriptorFromJson, jwtFromJson, - organizationsSearchDescriptorFromJson, - presentationRequestDescriptorFromJson, - presentationSubmissionFromJson, publicJwkFromJson, - submissionResultFromJson, - tokenFromString, verifiedProfileDescriptorFromJson + credentialManifestDescriptorFrom, + credentialTypesUIFormSchemaDescriptorFrom, + didJwkDescriptorFrom, + didJwkFrom, + finalizeOffersDescriptorFrom, + generateOffersDescriptorFrom, + jwtDescriptorFrom, + jwtFromJson, + organizationsSearchDescriptorFrom, + presentationRequestDescriptorFrom, + presentationSubmissionFrom, publicJwkFrom, + submissionResultFrom, + tokenFrom, + verifiedProfileDescriptorFrom } from "../utils/Converter"; import { VCLExchangeDescriptor } from "@velocitycareerlabs/vnf-nodejs-wallet-sdk/src"; @@ -41,7 +46,7 @@ export default async function routes(fastify) { "/getPresentationRequest", async (req, reply) => { reply.send( - await req.vclSdk.getPresentationRequest(presentationRequestDescriptorFromJson(req.body, req.didJwk)) + await req.vclSdk.getPresentationRequest(presentationRequestDescriptorFrom(req.body, req.didJwk)) ); } ); @@ -49,7 +54,7 @@ export default async function routes(fastify) { "/submitPresentation", async (req, reply) => { reply.send( - await req.vclSdk.submitPresentation(presentationSubmissionFromJson(req.body)) + await req.vclSdk.submitPresentation(presentationSubmissionFrom(req.body)) ); } ); @@ -59,8 +64,8 @@ export default async function routes(fastify) { reply.send( await req.vclSdk.getExchangeProgress( new VCLExchangeDescriptor( - presentationSubmissionFromJson(req.body.presentationSubmission), - submissionResultFromJson(req.body.submissionResult) + presentationSubmissionFrom(req.body.presentationSubmission), + submissionResultFrom(req.body.submissionResult) ) )); } @@ -69,7 +74,7 @@ export default async function routes(fastify) { "/searchForOrganizations", async (req, reply) => { reply.send( - await req.vclSdk.searchForOrganizations(organizationsSearchDescriptorFromJson(req.body)) + await req.vclSdk.searchForOrganizations(organizationsSearchDescriptorFrom(req.body)) ); } ); @@ -78,7 +83,7 @@ export default async function routes(fastify) { async (req, reply) => { reply.send( await req.vclSdk.getCredentialManifest( - credentialManifestDescriptorFromJson(req.body, req.didJwk) + credentialManifestDescriptorFrom(req.body, req.didJwk) ) ) } @@ -87,7 +92,7 @@ export default async function routes(fastify) { "/generateOffers", async (req, reply) => { reply.send( - await req.vclSdk.generateOffers(generateOffersDescriptorFromJson(req.body)) + await req.vclSdk.generateOffers(generateOffersDescriptorFrom(req.body)) ); } ); @@ -96,8 +101,8 @@ export default async function routes(fastify) { async (req, reply) => { reply.send( await req.vclSdk.checkForOffers( - generateOffersDescriptorFromJson(req.body), - tokenFromString(req.body.sessionToken.value) + generateOffersDescriptorFrom(req.body), + tokenFrom(req.body.sessionToken) ) ); } @@ -107,8 +112,8 @@ export default async function routes(fastify) { async (req, reply) => { reply.send( await req.vclSdk.finalizeOffers( - finalizeOffersDescriptorFromJson(req.body), - tokenFromString(req.body.sessionToken.value) + finalizeOffersDescriptorFrom(req.body.finalizeOffersDescriptor), + tokenFrom(req.body.sessionToken) ) ); } @@ -117,7 +122,7 @@ export default async function routes(fastify) { "/getCredentialTypesUIFormSchema", async (req, reply) => { reply.send( - await req.vclSdk.getCredentialTypesUIFormSchema(credentialTypesUIFormSchemaDescriptorFromJson(req.body)) + await req.vclSdk.getCredentialTypesUIFormSchema(credentialTypesUIFormSchemaDescriptorFrom(req.body)) ); } ); @@ -125,7 +130,7 @@ export default async function routes(fastify) { "/getVerifiedProfile", async (req, reply) => { reply.send( - await req.vclSdk.getVerifiedProfile(verifiedProfileDescriptorFromJson(req.body)) + await req.vclSdk.getVerifiedProfile(verifiedProfileDescriptorFrom(req.body)) ); } ); @@ -135,8 +140,8 @@ export default async function routes(fastify) { reply.send( await req.vclSdk.verifyJwt( jwtFromJson(req.body.jwt), - publicJwkFromJson(req.body.publicJwk), - tokenFromString(req.body.remoteCryptoServicesToken) + publicJwkFrom(req.body.publicJwk), + tokenFrom(req.body.remoteCryptoServicesToken) ) ); } @@ -146,9 +151,9 @@ export default async function routes(fastify) { async (req, reply) => { reply.send( await req.vclSdk.generateSignedJwt( - jwtDescriptorFromJson(req.body.jwtDescriptor), - didJwkFromJson(req.body.didJwk), - tokenFromString(req.body.remoteCryptoServicesToken) + jwtDescriptorFrom(req.body.jwtDescriptor), + didJwkFrom(req.body.didJwk), + tokenFrom(req.body.remoteCryptoServicesToken) ) ); } @@ -157,7 +162,7 @@ export default async function routes(fastify) { "/generateDidJwk", async (req, reply) => { reply.send( - await req.vclSdk.generateDidJwk(didJwkDescriptorFromJson(req.body)) + await req.vclSdk.generateDidJwk(didJwkDescriptorFrom(req.body)) ); } ); diff --git a/packages/sample-server/src/utils/Converter.ts b/packages/sample-server/src/utils/Converter.ts index 1886a0b..fd68bb6 100644 --- a/packages/sample-server/src/utils/Converter.ts +++ b/packages/sample-server/src/utils/Converter.ts @@ -36,28 +36,28 @@ import { VCLDidJwkDescriptor } from "@velocitycareerlabs/vnf-nodejs-wallet-sdk/src"; -export const deepLinkFromString = (str: string): VCLDeepLink => { - return new VCLDeepLink(str); +export const deepLinkFrom = (deepLink: any): VCLDeepLink => { + return new VCLDeepLink(deepLink.value ?? deepLink); } -export const tokenFromString = (str: string): VCLToken => { - return new VCLToken(str); +export const tokenFrom = (token: any): VCLToken => { + return new VCLToken(token.value ?? token); } export const jwtFromJson = (json: Dictionary): VCLJwt => { return VCLJwt.fromEncodedJwt(json.encodedJwt); } -export const publicJwkFromJson = (json: Dictionary): VCLPublicJwk => { +export const publicJwkFrom = (json: Dictionary): VCLPublicJwk => { return VCLPublicJwk.fromJSON(json); } -export const didJwkFromJson = (json: Dictionary): VCLDidJwk => { +export const didJwkFrom = (json: Dictionary): VCLDidJwk => { return VCLDidJwk.fromJSON(json); } -export const presentationRequestDescriptorFromJson = (json: Dictionary, didJwk: VCLDidJwk): VCLPresentationRequestDescriptor => { - const deepLink = deepLinkFromString(json.value) +export const presentationRequestDescriptorFrom = (json: Dictionary, didJwk: VCLDidJwk): VCLPresentationRequestDescriptor => { + const deepLink = deepLinkFrom(json) return new VCLPresentationRequestDescriptor( deepLink, null, @@ -66,7 +66,7 @@ export const presentationRequestDescriptorFromJson = (json: Dictionary, did ) } -export const presentationSubmissionFromJson = (json: Dictionary): VCLPresentationSubmission => { +export const presentationSubmissionFrom = (json: Dictionary): VCLPresentationSubmission => { const presentationRequestJson = json.presentationRequest; const verifiableCredentials = json.verifiableCredentials const presentationRequest = new VCLPresentationRequest( @@ -74,7 +74,7 @@ export const presentationSubmissionFromJson = (json: Dictionary): VCLPresen new VCLVerifiedProfile(presentationRequestJson.verifiedProfile.payload), new VCLDeepLink(presentationRequestJson.deepLink.value), null, - didJwkFromJson(presentationRequestJson.didJwk), + didJwkFrom(presentationRequestJson.didJwk), json.remoteCryptoServicesToken ? new VCLToken(json.remoteCryptoServicesToken) : null ) return new VCLPresentationSubmission( @@ -83,35 +83,35 @@ export const presentationSubmissionFromJson = (json: Dictionary): VCLPresen ); } -export const submissionResultFromJson = (json: Dictionary): VCLSubmissionResult => { +export const submissionResultFrom = (json: Dictionary): VCLSubmissionResult => { const submissionResultJson = json; return new VCLSubmissionResult( - tokenFromString(submissionResultJson.sessionToken.value), + tokenFrom(submissionResultJson.sessionToken.value), new VCLExchange(submissionResultJson.exchange), submissionResultJson.jti, submissionResultJson.submissionId ); } -export const organizationsSearchDescriptorFromJson = (json: Dictionary): VCLOrganizationsSearchDescriptor => { +export const organizationsSearchDescriptorFrom = (json: Dictionary): VCLOrganizationsSearchDescriptor => { return new VCLOrganizationsSearchDescriptor( new VCLFilter(json.filter.did, null, null), ); } -const credentialManifestDescriptorByDeepLinkFromJson = ( +const credentialManifestDescriptorByDeepLinkFrom = ( json: Dictionary, didJwk: VCLDidJwk ): VCLCredentialManifestDescriptorByDeepLink => { return new VCLCredentialManifestDescriptorByDeepLink( - deepLinkFromString(json.value), + deepLinkFrom(json), VCLIssuingType.Career, null, didJwk, ); } -const credentialManifestDescriptorByServiceFromJson = ( +const credentialManifestDescriptorByServiceFrom = ( json: Dictionary, didJwk: VCLDidJwk ): VCLCredentialManifestDescriptorByService => { @@ -125,29 +125,29 @@ const credentialManifestDescriptorByServiceFromJson = ( ); } -export const credentialManifestDescriptorFromJson = ( +export const credentialManifestDescriptorFrom = ( json: Dictionary, didJwk: VCLDidJwk ): VCLCredentialManifestDescriptor => { return json.value ? // deep link - credentialManifestDescriptorByDeepLinkFromJson(json, didJwk) : - credentialManifestDescriptorByServiceFromJson(json, didJwk) + credentialManifestDescriptorByDeepLinkFrom(json, didJwk) : + credentialManifestDescriptorByServiceFrom(json, didJwk) } -export const credentialManifestFromJson = (json: Dictionary): VCLCredentialManifest => { +export const credentialManifestFrom = (json: Dictionary): VCLCredentialManifest => { return new VCLCredentialManifest( jwtFromJson(json.jwt), json.vendorOriginContext, new VCLVerifiedProfile(json.verifiedProfile.payload), json.deepLink ? new VCLDeepLink(json.deepLink.value) : null, - didJwkFromJson(json.didJwk), + didJwkFrom(json.didJwk), json.remoteCryptoServicesToken ? new VCLToken(json.remoteCryptoServicesToken) : null ); } -export const generateOffersDescriptorFromJson = (json: Dictionary): VCLGenerateOffersDescriptor => { - const credentialManifest = credentialManifestFromJson(json.credentialManifest) +export const generateOffersDescriptorFrom = (json: Dictionary): VCLGenerateOffersDescriptor => { + const credentialManifest = credentialManifestFrom(json.credentialManifest) const identificationVerifiableCredentials = json.identificationVerifiableCredentials.map((vc: Dictionary) => VCLVerifiableCredential.fromJSON(vc)) return new VCLGenerateOffersDescriptor( @@ -158,8 +158,8 @@ export const generateOffersDescriptorFromJson = (json: Dictionary): VCLGene ); } -export const finalizeOffersDescriptorFromJson = (json: Dictionary): VCLFinalizeOffersDescriptor => { - const credentialManifest = credentialManifestFromJson(json.credentialManifest) +export const finalizeOffersDescriptorFrom = (json: Dictionary): VCLFinalizeOffersDescriptor => { + const credentialManifest = credentialManifestFrom(json.credentialManifest) return new VCLFinalizeOffersDescriptor( credentialManifest, json.challenge, @@ -168,21 +168,21 @@ export const finalizeOffersDescriptorFromJson = (json: Dictionary): VCLFina ); } -export const credentialTypesUIFormSchemaDescriptorFromJson = (json: Dictionary): VCLCredentialTypesUIFormSchemaDescriptor => { +export const credentialTypesUIFormSchemaDescriptorFrom = (json: Dictionary): VCLCredentialTypesUIFormSchemaDescriptor => { return new VCLCredentialTypesUIFormSchemaDescriptor( json.credentialType, json.countryCode ); } -export function verifiedProfileDescriptorFromJson(json: Dictionary): VCLVerifiedProfileDescriptor { +export const verifiedProfileDescriptorFrom = (json: Dictionary): VCLVerifiedProfileDescriptor => { return new VCLVerifiedProfileDescriptor(json.did) } -export const jwtDescriptorFromJson = (json: Dictionary): VCLJwtDescriptor => { +export const jwtDescriptorFrom = (json: Dictionary): VCLJwtDescriptor => { return new VCLJwtDescriptor(json.payload, json.jti, json.iss, json.aud); } -export const didJwkDescriptorFromJson = (json: Dictionary): VCLDidJwkDescriptor => { +export const didJwkDescriptorFrom = (json: Dictionary): VCLDidJwkDescriptor => { return new VCLDidJwkDescriptor(json.signatureAlgorithm, null); } \ No newline at end of file