Skip to content

Commit

Permalink
Merge pull request #256 from docknetwork/DCKM-385-quotient-wallet-res…
Browse files Browse the repository at this point in the history
…trict-with-a-warning-to-scan-the-verification-of-biometric-flow-if-you-are-on-mainnet

DCKM-385: network resolver for proof requests
  • Loading branch information
maycon-mello authored Apr 30, 2024
2 parents d9246b4 + 1c48eab commit 973be74
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
67 changes: 67 additions & 0 deletions packages/core/src/network-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
accountResolver,
credentialResolver,
dockDocumentNetworkResolver,
proofRequestResolver,
resolveApiNetwork,
} from './network-resolver';

Expand Down Expand Up @@ -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);
});
});
});
37 changes: 33 additions & 4 deletions packages/core/src/network-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export const dockDocumentNetworkResolver: DocumentNetworkResolver = async ({
}: DocumentResolverProps): Promise<DocumentResolverResult> => {
let networkId;
let isFallback = false;

let currentResolver;
for (const resolver of resolvers) {
networkId = await resolver({document, dataStore});

currentResolver = resolver;
if (networkId) {
break;
}
Expand All @@ -32,6 +32,7 @@ export const dockDocumentNetworkResolver: DocumentNetworkResolver = async ({
return {
networkId: networkId,
isFallback,
resolver: currentResolver,
};
};

Expand Down Expand Up @@ -86,6 +87,27 @@ export async function credentialResolver({
});
}

export async function proofRequestResolver({
document,
dataStore,
}: DocumentResolverProps): Promise<ResolverResult> {
if (!document) {
return null;
}

const isProofRequest =
document.type === 'proof-request' && document.qr;

if (!isProofRequest) {
return null;
}

return resolveApiNetwork({
url: document.qr,
dataStore,
});
}

export async function didResolver({
document,
dataStore,
Expand All @@ -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;
Expand All @@ -125,4 +149,9 @@ export async function accountResolver({
return network?.id;
}

const resolvers = [credentialResolver, accountResolver, didResolver];
const resolvers = [
credentialResolver,
accountResolver,
didResolver,
proofRequestResolver,
];
1 change: 1 addition & 0 deletions packages/data-store/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type DocumentNetworkResolver = (
export type DocumentResolverResult = {
networkId: string;
isFallback: boolean;
resolver?: Function;
};

export type Network = {
Expand Down

0 comments on commit 973be74

Please sign in to comment.