Skip to content

Commit

Permalink
feat: add remote presentation with Potential specifications
Browse files Browse the repository at this point in the history
  • Loading branch information
gispada committed Jan 31, 2025
1 parent fced5a1 commit aed712b
Show file tree
Hide file tree
Showing 13 changed files with 670 additions and 274 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"dependencies": {
"js-base64": "^3.7.7",
"js-sha256": "^0.9.0",
"jsonpath-plus": "^10.2.0",
"parse-url": "^9.2.0",
"react-native-url-polyfill": "^2.0.0",
"react-native-uuid": "^2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/credential/presentation/03-get-request-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type GetRequestObject = (
* @param rpConf The Relying Party's configuration
* @param context.wiaCryptoContext The context to access the key associated with the Wallet Instance Attestation
* @param context.walletInstanceAttestation The Wallet Instance Attestation token
* @param context.walletCapabilities (optional) An object containing the wallet technical capabilities
* @param context.walletCapabilities (optional) An object containing the wallet technical capabilities that will be sent with a POST request
* @param context.appFetch (optional) fetch api implementation. Default: built-in fetch
* @returns The Request Object that describes the presentation
*/
Expand Down
168 changes: 0 additions & 168 deletions src/credential/presentation/04-send-authorization-response.ts

This file was deleted.

69 changes: 0 additions & 69 deletions src/credential/presentation/06-evaluate-dcql-query.ts

This file was deleted.

78 changes: 78 additions & 0 deletions src/credential/presentation/06-fetch-presentation-definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { PresentationDefinition, RequestObject } from "./types";
import { RelyingPartyEntityConfiguration } from "../../trust/types";
import { hasStatusOrThrow } from "../../utils/misc";

export type FetchPresentationDefinition = (
requestObject: RequestObject,
context?: {
appFetch?: GlobalFetch["fetch"];
},
rpConf?: RelyingPartyEntityConfiguration["payload"]["metadata"]
) => Promise<{
presentationDefinition: PresentationDefinition;
}>;

/**
* Retrieves a PresentationDefinition based on the given parameters.
*
* The method attempts the following strategies in order:
* 1. Checks if `presentation_definition` is directly available in the request object.
* 2. Fetches the `presentation_definition` from the URI provided in the relying party configuration.
* 3. Uses a pre-configured `presentation_definition` from the relying party configuration if the `scope` is present in the request object.
*
* If none of the above conditions are met, the function throws an error indicating the definition could not be found.
*
* @param {RequestObject} requestObject - The request object containing the presentation definition or references to it.
* @param {RelyingPartyEntityConfiguration["payload"]["metadata"]} [rpConf] - Optional relying party configuration.
* @param {Object} [context] - Optional context for providing a custom fetch implementation.
* @param {GlobalFetch["fetch"]} [context.appFetch] - Custom fetch function, defaults to global `fetch`.
* @returns {Promise<{ presentationDefinition: PresentationDefinition }>} - Resolves with the presentation definition.
* @throws {Error} - Throws if the presentation definition cannot be found or fetched.
*/
export const fetchPresentDefinition: FetchPresentationDefinition = async (
requestObject,
{ appFetch = fetch } = {},
rpConf
) => {
// Check if `presentation_definition` is directly available in the request object
if (requestObject.presentation_definition) {
return {
presentationDefinition: requestObject.presentation_definition,
};
}

// Check if `presentation_definition_uri` is provided in the relying party configuration
if (rpConf?.wallet_relying_party?.presentation_definition_uri) {
try {
// Fetch the presentation definition from the provided URI
const presentationDefinition = await appFetch(
rpConf?.wallet_relying_party.presentation_definition_uri,
{
method: "GET",
}
)
.then(hasStatusOrThrow(200))
.then((raw) => raw.json())
.then((json) => PresentationDefinition.parse(json));

return {
presentationDefinition,
};
} catch (error) {
throw new Error(`Failed to fetch presentation definition: ${error}`);
}
}

// Check if `scope` is present in the request object and a pre-configured presentation definition exists
if (
requestObject.scope &&
rpConf?.wallet_relying_party?.presentation_definition
) {
return {
presentationDefinition:
rpConf.wallet_relying_party.presentation_definition,
};
}

throw new Error("Presentation definition not found");
};
Loading

0 comments on commit aed712b

Please sign in to comment.