Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions integration-tests/cheqd-credentials.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IWallet } from '@docknetwork/wallet-sdk-core/lib/types';
import { createVerificationController } from '@docknetwork/wallet-sdk-core/src/verification-controller';
import { CheqdCredentialNonZKP, CheqdCredentialZKP } from './data/credentials/cheqd-credentials';
import { closeWallet, createNewWallet, getCredentialProvider, getWallet } from './helpers';
import { addCredentialIfNotExists, closeWallet, createNewWallet, getCredentialProvider, getWallet } from './helpers';
import { ProofTemplateIds, createProofRequest } from './helpers/certs-helpers';

describe('Cheq integration tests', () => {
Expand All @@ -12,7 +12,7 @@ describe('Cheq integration tests', () => {
it('should verify a non ZKP cheqd credential', async () => {
const wallet: IWallet = await getWallet();

getCredentialProvider().addCredential(CheqdCredentialNonZKP);
await addCredentialIfNotExists(CheqdCredentialNonZKP);

const proofRequest = await createProofRequest(
ProofTemplateIds.ANY_CREDENTIAL,
Expand Down
7 changes: 4 additions & 3 deletions integration-tests/credentials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
UniversityDegreeCredentialBBS,
} from './data/credentials';
import {
addCredentialIfNotExists,
cleanup,
closeWallet,
getCredentialProvider,
Expand All @@ -29,7 +30,7 @@

it('expect to import credentials', async () => {
for (const credentialJSON of allCredentials) {
await getCredentialProvider().addCredential(credentialJSON);
await addCredentialIfNotExists(credentialJSON);
const credential = await getCredentialProvider().getById(
credentialJSON.id,
);
Expand All @@ -46,21 +47,21 @@
`${credentialUrl}?p=${btoa(password)}`,
);

await getCredentialProvider().addCredential(credential);
await addCredentialIfNotExists(credential);

const result: any = await getCredentialProvider().isValid(credential);

expect(result.status).toBe('verified');
});

it('should get status of bbs revokable credential - cheqd issuer', async () => {
await getCredentialProvider().addCredential(CheqdRevocationCredential);
await addCredentialIfNotExists(CheqdRevocationCredential);

const result: any = await getCredentialProvider().isValid(
CheqdRevocationCredential,
);

expect(result.status).toBe('verified');

Check failure on line 64 in integration-tests/credentials.test.ts

View workflow job for this annotation

GitHub Actions / Integration Test Report

integration-tests/credentials.test.ts ► Credentials › credential status ► should get status of bbs revokable credential - cheqd issuer

Failed test found in: reports/jest-junit.xml Error: Error: expect(received).toBe(expected) // Object.is equality
Raw output
Error: expect(received).toBe(expected) // Object.is equality

Expected: "verified"
Received: "invalid"
    at Object.<anonymous> (/home/runner/work/wallet-sdk/wallet-sdk/integration-tests/credentials.test.ts:64:29)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
});
});

Expand Down
25 changes: 21 additions & 4 deletions integration-tests/helpers/wallet-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ export function getCredentialProvider(): ICredentialProvider {
return credentialProvider;
}

export async function addCredentialIfNotExists(credential: any) {
try {
return await credentialProvider.addCredential(credential);
} catch (err) {
if (!err.message?.includes('already exists')) throw err;
}
}

export async function setNetwork(networkId) {
return Promise.resolve(wallet.setNetwork(networkId));
}
Expand Down Expand Up @@ -122,15 +130,24 @@ export async function getDocumentsByType(type) {
return wallet.getDocumentsByType(type);
}

export async function closeWallet(wallet?: IWallet) {
if (!wallet) {
wallet = await getWallet();
export async function closeWallet(walletToClose?: IWallet) {
if (!walletToClose) {
walletToClose = await getWallet();
}

if (messageProvider) {
messageProvider.stop();
}

if (walletToClose.networkCheckInterval) {
clearInterval(walletToClose.networkCheckInterval);
walletToClose.networkCheckInterval = undefined;
}

return new Promise(res => {
setTimeout(async () => {
try {
wallet.dataStore.db.destroy();
walletToClose.dataStore.db.destroy();
await blockchainService.disconnect();
} catch (err) {
console.error(err);
Expand Down
5 changes: 3 additions & 2 deletions integration-tests/sd-jwt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
UniversityDegreeCredentialBBS,
} from './data/credentials';
import {
addCredentialIfNotExists,
cleanup,
closeWallet,
getCredentialProvider,
Expand All @@ -26,8 +27,8 @@ describe('SD JWT Credentials', () => {

wallet = await getWallet();

const result = await getCredentialProvider().addCredential(jwt);
credentialId = result.id;
const result = await addCredentialIfNotExists(jwt);
credentialId = result?.id;
});

it('expect to import SD-JWT credential', async () => {
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/switch-wallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
addCredentialIfNotExists,
closeWallet,
getCredentialProvider,
getDocumentsByType,
getWallet,
setNetwork,
Expand All @@ -17,7 +17,7 @@ describe('Switch wallet', () => {

it('expect to maintain separate document stores when switching between networks', async () => {
await setNetwork('testnet');
await getCredentialProvider().addCredential(BasicCredential);
await addCredentialIfNotExists(BasicCredential);

const testnetCredentials = await getDocumentsByType('VerifiableCredential');
expect(testnetCredentials.length).toBe(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {IWallet} from '@docknetwork/wallet-sdk-core/lib/types';
import {
addCredentialIfNotExists,
closeWallet,
getCredentialProvider,
getWallet,
Expand All @@ -12,7 +13,7 @@ describe('BBS+ revocation', () => {
it('should verify a revokable bbs+ credential', async () => {
const wallet: IWallet = await getWallet();

getCredentialProvider().addCredential(bbsPlusRevocationCredential);
await addCredentialIfNotExists(bbsPlusRevocationCredential);

const proofRequest = await createProofRequest(
ProofTemplateIds.ANY_CREDENTIAL,
Expand Down Expand Up @@ -59,7 +60,7 @@ describe('BBS+ revocation', () => {
it('should verify a revokable bbs+ credential with an updated witness', async () => {
const wallet: IWallet = await getWallet();

getCredentialProvider().addCredential(credentialWithUpdatedWitness);
await addCredentialIfNotExists(credentialWithUpdatedWitness);

const proofRequest = await createProofRequest(
ProofTemplateIds.ANY_CREDENTIAL,
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/verification-flow/cheqd-revocation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {IWallet} from '@docknetwork/wallet-sdk-core/lib/types';
import {
addCredentialIfNotExists,
closeWallet,
getCredentialProvider,
getWallet,
Expand All @@ -12,7 +13,7 @@ describe('BBS+ revocation cheqd', () => {
it('should verify a revokable bbs+ credential issued on cheqd', async () => {
const wallet: IWallet = await getWallet();

getCredentialProvider().addCredential(cheqdRevocationCredential);
await addCredentialIfNotExists(cheqdRevocationCredential);

const proofRequest = await createProofRequest(
ProofTemplateIds.ANY_CREDENTIAL,
Expand Down
12 changes: 4 additions & 8 deletions integration-tests/verification-flow/range-proofs.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {IWallet} from '@docknetwork/wallet-sdk-core/lib/types';
import {
addCredentialIfNotExists,
closeWallet,
getCredentialProvider,
getWallet,
} from '../helpers/wallet-helpers';
import {createVerificationController} from '@docknetwork/wallet-sdk-core/src/verification-controller';
Expand Down Expand Up @@ -29,7 +29,7 @@ describe('Range proofs verification', () => {
`${credentialUrl}?p=${btoa(password)}`,
);

getCredentialProvider().addCredential(credential);
await addCredentialIfNotExists(credential);

await controller.start({
template: proofRequest.qr,
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('Range proofs verification', () => {
`${credentialUrl}?p=${btoa(password)}`,
);

getCredentialProvider().addCredential(credential);
await addCredentialIfNotExists(credential);

await controller.start({
template: proofRequest.qr,
Expand Down Expand Up @@ -135,11 +135,7 @@ describe('Range proofs verification', () => {
`${credentialUrl}?p=${btoa(password)}`,
);

try {
await getCredentialProvider().addCredential(credential);
} catch(err) {
console.error('Credential already added');
}
await addCredentialIfNotExists(credential);

// pexToBounds should skip issuanceDate
// There is an SDK limitation that prevents us from sharing the actual issuanceDate
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/verification-flow/vpi-verification.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {IWallet} from '@docknetwork/wallet-sdk-core/lib/types';
import {
addCredentialIfNotExists,
closeWallet,
getCredentialProvider,
getWallet,
Expand Down Expand Up @@ -75,7 +76,7 @@ describe('VPI verification', () => {
it('should verify a vpi credential', async () => {
const wallet: IWallet = await getWallet();

getCredentialProvider().addCredential(credential);
await addCredentialIfNotExists(credential);

const proofRequest = await createProofRequest(
ProofTemplateIds.ANY_CREDENTIAL,
Expand Down
33 changes: 29 additions & 4 deletions packages/core/src/message-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,30 @@ export function createMessageProvider({
}

let listenerIntervalId = null;
let processMessageTimeoutId = null;
let stopped = false;

const processMessageInterval = 3000;

async function processMessageRecurrentJob() {
try {
await processDIDCommMessages();
} finally {
setTimeout(processMessageRecurrentJob, processMessageInterval);
if (!stopped) {
processMessageTimeoutId = setTimeout(processMessageRecurrentJob, processMessageInterval);
}
}
}

function stop() {
stopped = true;
if (listenerIntervalId) {
clearInterval(listenerIntervalId);
listenerIntervalId = null;
}
if (processMessageTimeoutId) {
clearTimeout(processMessageTimeoutId);
processMessageTimeoutId = null;
}
}

Expand Down Expand Up @@ -333,12 +349,20 @@ export function createMessageProvider({
*/
startAutoFetch(timeout = 2000) {
clearInterval(listenerIntervalId);
stopped = false;
listenerIntervalId = setInterval(async () => {
await fetchMessages();
await processDIDCommMessages();
try {
await fetchMessages();
await processDIDCommMessages();
} catch (err) {
logger.debug(`Auto-fetch error: ${err.message}`);
}
}, timeout);

return () => clearInterval(listenerIntervalId);
return () => {
clearInterval(listenerIntervalId);
listenerIntervalId = null;
};
},
/**
* Clears all cached messages from the wallet
Expand Down Expand Up @@ -410,5 +434,6 @@ export function createMessageProvider({
* console.log('Message marked as read');
*/
markMessageAsRead,
stop,
} as any;
}
5 changes: 5 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@ export interface IMessageProvider {
* @returns {Promise<void>}
*/
processMessageRecurrentJob: () => Promise<void>;

/**
* Stops all message fetching and processing timers
*/
stop: () => void;
}

/**
Expand Down
Loading
Loading