From 74100329c385143c4121b9f0d8806fe6e19843e8 Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Tue, 30 Apr 2024 14:18:33 -0300 Subject: [PATCH 1/2] DCKM-385: network resolver for proof requests --- packages/core/src/network-resolver.test.ts | 67 ++++++++++++++++++++++ packages/core/src/network-resolver.ts | 37 ++++++++++-- packages/data-store/src/types.ts | 1 + 3 files changed, 101 insertions(+), 4 deletions(-) diff --git a/packages/core/src/network-resolver.test.ts b/packages/core/src/network-resolver.test.ts index b66e5c29..6d4bea0f 100644 --- a/packages/core/src/network-resolver.test.ts +++ b/packages/core/src/network-resolver.test.ts @@ -3,6 +3,7 @@ import { accountResolver, credentialResolver, dockDocumentNetworkResolver, + proofRequestResolver, resolveApiNetwork, } from './network-resolver'; @@ -139,4 +140,70 @@ describe('Wallet', () => { expect(result).toBe('mainnet'); }); }); + + describe('proofRequestResolver', () => { + it('should detect a testnet proof request', async () => { + let result = await dockDocumentNetworkResolver({ + document: { + qr: 'https://creds-staging.dock.io/proof/4970377a-6283-46d8-b95e-db99b011e48c', + id: '4970377a-6283-46d8-b95e-db99b011e48c', + response_url: + 'https://api-staging.dock.io/proof-requests/4970377a-6283-46d8-b95e-db99b011e48c/send-presentation', + type: 'proof-request', + }, + dataStore: wallet.dataStore, + }); + + expect(result.networkId).toBe('testnet'); + expect(result.resolver).toBe(proofRequestResolver); + + result = await dockDocumentNetworkResolver({ + document: { + qr: 'https://creds-testnet.dock.io/proof/4970377a-6283-46d8-b95e-db99b011e48c', + id: '4970377a-6283-46d8-b95e-db99b011e48c', + response_url: + 'https://api-testnet.dock.io/proof-requests/4970377a-6283-46d8-b95e-db99b011e48c/send-presentation', + type: 'proof-request', + }, + dataStore: wallet.dataStore, + }); + + expect(result.networkId).toBe('testnet'); + + }); + + it('should detect a mainnet proof request', async () => { + const result = await dockDocumentNetworkResolver({ + document: { + qr: 'https://creds.dock.io/proof/4970377a-6283-46d8-b95e-db99b011e48c', + id: '4970377a-6283-46d8-b95e-db99b011e48c', + response_url: + 'https://api-staging.dock.io/proof-requests/4970377a-6283-46d8-b95e-db99b011e48c/send-presentation', + type: 'proof-request', + }, + dataStore: wallet.dataStore, + }); + + expect(result.networkId).toBe('mainnet'); + expect(result.resolver).toBe(proofRequestResolver); + }); + + it('should fallback to current network', async () => { + await wallet.setNetwork('mainnet'); + + const result = await dockDocumentNetworkResolver({ + document: { + qr: 'https://something.dock.io/proof/4970377a-6283-46d8-b95e-db99b011e48c', + id: '4970377a-6283-46d8-b95e-db99b011e48c', + response_url: + 'https://something.dock.io/proof-requests/4970377a-6283-46d8-b95e-db99b011e48c/send-presentation', + type: 'proof-request', + }, + dataStore: wallet.dataStore, + }); + + expect(result.networkId).toBe('mainnet'); + expect(result.isFallback).toBe(true); + }); + }); }); diff --git a/packages/core/src/network-resolver.ts b/packages/core/src/network-resolver.ts index 96a77466..49bce0c9 100644 --- a/packages/core/src/network-resolver.ts +++ b/packages/core/src/network-resolver.ts @@ -15,10 +15,10 @@ export const dockDocumentNetworkResolver: DocumentNetworkResolver = async ({ }: DocumentResolverProps): Promise => { let networkId; let isFallback = false; - + let currentResolver; for (const resolver of resolvers) { networkId = await resolver({document, dataStore}); - + currentResolver = resolver; if (networkId) { break; } @@ -32,6 +32,7 @@ export const dockDocumentNetworkResolver: DocumentNetworkResolver = async ({ return { networkId: networkId, isFallback, + resolver: currentResolver, }; }; @@ -86,6 +87,27 @@ export async function credentialResolver({ }); } +export async function proofRequestResolver({ + document, + dataStore, +}: DocumentResolverProps): Promise { + if (!document) { + return null; + } + + const isProofRequest = + document.type === 'proof-request' && document.qr; + + if (document.type !== 'proof-request') { + return null; + } + + return resolveApiNetwork({ + url: document.qr, + dataStore, + }); +} + export async function didResolver({ document, dataStore, @@ -102,7 +124,9 @@ export async function accountResolver({ return null; } - const isAddress = Array.isArray(document.type) ? document.type.includes('Address') : document.type === 'Address'; + const isAddress = Array.isArray(document.type) + ? document.type.includes('Address') + : document.type === 'Address'; if (!isAddress) { return null; @@ -125,4 +149,9 @@ export async function accountResolver({ return network?.id; } -const resolvers = [credentialResolver, accountResolver, didResolver]; +const resolvers = [ + credentialResolver, + accountResolver, + didResolver, + proofRequestResolver, +]; diff --git a/packages/data-store/src/types.ts b/packages/data-store/src/types.ts index 340d82b7..b6b9dfec 100644 --- a/packages/data-store/src/types.ts +++ b/packages/data-store/src/types.ts @@ -12,6 +12,7 @@ export type DocumentNetworkResolver = ( export type DocumentResolverResult = { networkId: string; isFallback: boolean; + resolver?: Function; }; export type Network = { From 1c48eab6564d7411cd3f0572ca99fa29273f6c50 Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Tue, 30 Apr 2024 14:19:52 -0300 Subject: [PATCH 2/2] DCKM-385: network resolver for proof requests --- packages/core/src/network-resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/network-resolver.ts b/packages/core/src/network-resolver.ts index 49bce0c9..6d70d788 100644 --- a/packages/core/src/network-resolver.ts +++ b/packages/core/src/network-resolver.ts @@ -98,7 +98,7 @@ export async function proofRequestResolver({ const isProofRequest = document.type === 'proof-request' && document.qr; - if (document.type !== 'proof-request') { + if (!isProofRequest) { return null; }