From 16e06e8c2b879c6fe706568a48e254ab2693bf78 Mon Sep 17 00:00:00 2001 From: Niels Klomp Date: Sun, 2 Jun 2024 23:41:42 +0200 Subject: [PATCH] feat: Allow to pass in state for url handler handle methods, allowing a statemachine to continue, without database persistence --- packages/oid4vci-holder/src/link-handler/index.ts | 11 ++++++----- .../ssi-sdk-core/src/link-handlers/LinkHandlers.ts | 2 +- packages/ssi-sdk-core/src/link-handlers/types.ts | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/oid4vci-holder/src/link-handler/index.ts b/packages/oid4vci-holder/src/link-handler/index.ts index 2d3ccc838..31deffaf4 100644 --- a/packages/oid4vci-holder/src/link-handler/index.ts +++ b/packages/oid4vci-holder/src/link-handler/index.ts @@ -1,7 +1,7 @@ import { CredentialOfferClient } from '@sphereon/oid4vci-client' import { AuthorizationRequestOpts, convertURIToJsonObject } from '@sphereon/oid4vci-common' import { DefaultLinkPriorities, LinkHandlerAdapter } from '@sphereon/ssi-sdk.core' -import { IMachineStatePersistence, interpreterStartOrResume } from '@sphereon/ssi-sdk.xstate-machine-persistence' +import { IMachineStatePersistence, interpreterStartOrResume, SerializableState } from '@sphereon/ssi-sdk.xstate-machine-persistence' import { IAgentContext } from '@veramo/core' import { GetMachineArgs, IOID4VCIHolder, OID4VCIMachineEvents, OID4VCIMachineInterpreter, OID4VCIMachineState } from '../types/IOID4VCIHolder' @@ -28,7 +28,7 @@ export class OID4VCIHolderLinkHandler extends LinkHandlerAdapter { this.stateNavigationListener = args.stateNavigationListener } - async handle(url: string | URL, authorizationRequestOpts?: AuthorizationRequestOpts): Promise { + async handle(url: string | URL, opts?: { machineState?: SerializableState; authorizationRequestOpts?: AuthorizationRequestOpts }): Promise { const uri = new URL(url).toString() const offerData = convertURIToJsonObject(uri) as Record const hasCode = 'code' in offerData && !!offerData.code && !('issuer' in offerData) @@ -41,13 +41,13 @@ export class OID4VCIHolderLinkHandler extends LinkHandlerAdapter { ...(hasCode && { code: code }), uri, }, - authorizationRequestOpts: { ...this.authorizationRequestOpts, ...authorizationRequestOpts }, + authorizationRequestOpts: { ...this.authorizationRequestOpts, ...opts?.authorizationRequestOpts }, stateNavigationListener: this.stateNavigationListener, }) const interpreter = oid4vciMachine.interpreter //FIXME we need a better way to check if the state persistence plugin is available in the agent - if (this.context.agent.availableMethods().includes('machineStatesFindActive')) { + if (!opts?.machineState && this.context.agent.availableMethods().includes('machineStatesFindActive')) { const stateType = hasCode ? 'existing' : 'new' await interpreterStartOrResume({ stateType, @@ -59,7 +59,8 @@ export class OID4VCIHolderLinkHandler extends LinkHandlerAdapter { noRegistration: this.noStateMachinePersistence, }) } else { - interpreter.start() + // @ts-ignore + interpreter.start(opts?.machineState) } if (hasCode) { diff --git a/packages/ssi-sdk-core/src/link-handlers/LinkHandlers.ts b/packages/ssi-sdk-core/src/link-handlers/LinkHandlers.ts index a49daf608..aeb47e3b9 100644 --- a/packages/ssi-sdk-core/src/link-handlers/LinkHandlers.ts +++ b/packages/ssi-sdk-core/src/link-handlers/LinkHandlers.ts @@ -147,7 +147,7 @@ export abstract class LinkHandlerAdapter implements LinkHandler { this._priority = value } - handle(url: string | URL): Promise { + handle(url: string | URL, opts?: Record): Promise { return Promise.reject(new Error(`Adapter does not handle a URL. Please implement`)) } diff --git a/packages/ssi-sdk-core/src/link-handlers/types.ts b/packages/ssi-sdk-core/src/link-handlers/types.ts index b4a8076ad..bbf1afb81 100644 --- a/packages/ssi-sdk-core/src/link-handlers/types.ts +++ b/packages/ssi-sdk-core/src/link-handlers/types.ts @@ -11,7 +11,7 @@ export type LinkHandler = { priority?: number | DefaultLinkPriorities supports: (url: string | URL) => boolean protocols: Array - handle: (url: string | URL) => Promise + handle: (url: string | URL, opts?: Record) => Promise } export type LinkHandlerRegistry = {