Skip to content

Commit

Permalink
Merge branch 'master' into feat/read-ecosystem-data-from-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayteekay authored Mar 20, 2024
2 parents 397e572 + 3ef8a8f commit 7ef92d2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 48 deletions.
11 changes: 7 additions & 4 deletions packages/core/src/credential-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export async function addCredential({wallet, credential}) {
});
}

syncCredentialStatus({ wallet, credentialIds: [credential.id] });

return response;
}

Expand Down Expand Up @@ -203,10 +205,11 @@ async function syncCredentialStatus({ wallet, credentialIds, forceFetch }: SyncC
continue;
}

statusDoc.status = CredentialStatus.Pending;
statusDoc.updatedAt = new Date().toISOString();

await wallet.updateDocument(statusDoc);
if (!statusDoc.status) {
statusDoc.status = CredentialStatus.Pending;
statusDoc.updatedAt = new Date().toISOString();
await wallet.updateDocument(statusDoc);
}

if (!isApiConnected) {
await dockService.ensureDockReady();
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ export async function createWallet(
return result;
});
},
removeDocument: (id: string) => {
removeDocument: async (id: string) => {
const document = await wallet.getDocumentById(id);

return removeDocument({
dataStore,
id,
}).then(result => {
eventEmitter.emit(WalletEvents.documentRemoved, result);
eventEmitter.emit(WalletEvents.documentRemoved, document);
return result;
});
},
Expand Down
30 changes: 12 additions & 18 deletions packages/react-native/lib/credentials/credentialHooks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {useMemo, useCallback, useState, useEffect} from 'react';
import {useDocument, useWallet} from '../index';
import {useDocument, useDocuments} from '../index';
import assert from 'assert';
import axios from 'axios';
import { getCredentialProvider } from '../wallet';
import { getCredentialProvider, getWallet } from '../wallet';

export const sortByIssuanceDate = (a, b) =>
getCredentialTimestamp(b) - getCredentialTimestamp(a);
Expand Down Expand Up @@ -34,20 +33,14 @@ export function waitFor(condition, timeout) {
}

export function useCredentialUtils() {
const { documents, wallet } = useWallet();
const {documents, loading} = useDocuments({
type: 'VerifiableCredential',
});

const credentials = useMemo(() => {
if (Array.isArray(documents)) {
return documents
.filter(doc => {
return (
doc.type === 'VerifiableCredential' ||
doc.type?.includes('VerifiableCredential')
);
})
.sort(sortByIssuanceDate);
}
return [];
return documents
.filter(doc => !!doc.id)
.sort(sortByIssuanceDate);
}, [documents]);

const doesCredentialExist = useCallback((allCredentials, credentialToAdd) => {
Expand All @@ -60,18 +53,19 @@ export function useCredentialUtils() {
typeof credentialId === 'string' && credentialId.length > 0,
'Credential ID is not set',
);
return await wallet.remove(credentialId);
return await getWallet().remove(credentialId);
},
[wallet],
[],
);

return useMemo(() => {
return {
credentials,
doesCredentialExist,
deleteCredential,
loading,
};
}, [credentials, doesCredentialExist, deleteCredential]);
}, [credentials, doesCredentialExist, deleteCredential, loading]);
}

export function useCredentialStatus({ credential }: any) {
Expand Down
87 changes: 63 additions & 24 deletions packages/react-native/lib/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ import WebView from 'react-native-webview';
import {WebviewEventHandler} from './message-handler';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {AccountDetails} from '@docknetwork/wallet-sdk-wasm/src/modules/account';
import {DocumentType, WalletDocument} from '@docknetwork/wallet-sdk-wasm/src/types';
import {
DocumentType,
WalletDocument,
} from '@docknetwork/wallet-sdk-wasm/src/types';
import './rn-rpc-server';
import {useDIDManagement} from './didHooks';
import {useAccounts} from './accountsHooks';
import {
useCredentialUtils,
useCredentialStatus,
} from './credentials/credentialHooks';
import {getOrCreateWallet} from './wallet';
import {getOrCreateWallet, getWallet} from './wallet';
import debounce from 'lodash.debounce';
import {setV1LocalStorage} from '@docknetwork/wallet-sdk-data-store/src/migration/migration1/v1-data-store';
import {IWallet} from '@docknetwork/wallet-sdk-core/src/types';
Expand All @@ -51,10 +54,7 @@ export const WalletSDKContext = React.createContext<WalletSDKContextProps>({
setStorage(AsyncStorage);
export {useAccounts};
export {useDIDManagement};
export {
useCredentialUtils,
useCredentialStatus,
};
export {useCredentialUtils, useCredentialStatus};
export function getStorage() {
return AsyncStorage;
}
Expand Down Expand Up @@ -103,34 +103,73 @@ export function useAccount(address) {
};
}

const useEventListener = (eventManager, eventNames, listener) => {
useEffect(() => {
eventNames.forEach(eventName => eventManager.on(eventName, listener));
return () =>
eventNames.forEach(eventName =>
eventManager.removeListener(eventName, listener),
);
}, [eventManager, eventNames, listener]);
};

const events = [
WalletEvents.documentAdded,
WalletEvents.documentRemoved,
WalletEvents.documentUpdated,
];

export function useDocument(id) {
const {wallet} = useWallet();
const [document, setDocument] = useState(null);

useEffect(() => {
console.log('useDocument', id, wallet);
async function refetchDocument(updatedDoc) {
const refetchDocument = useCallback(
async updatedDoc => {
if (updatedDoc.id !== id) return;
const _doc = await wallet.getDocumentById(id);
setDocument(_doc);
}
const doc = await getWallet().getDocumentById(id);
setDocument(doc);
},
[id],
);

setDocument(wallet.getDocumentById(id))
useEffect(() => {
getWallet().getDocumentById(id).then(setDocument);
}, [id]);

wallet.eventManager.on(WalletEvents.documentAdded, refetchDocument);
wallet.eventManager.on(WalletEvents.documentRemoved, refetchDocument);
wallet.eventManager.on(WalletEvents.documentUpdated, refetchDocument);
useEventListener(getWallet().eventManager, events, refetchDocument);

return () => {
wallet.eventManager.removeListener(WalletEvents.documentAdded, refetchDocument);
wallet.eventManager.removeListener(WalletEvents.documentRemoved, refetchDocument);
wallet.eventManager.removeListener(WalletEvents.documentUpdated, refetchDocument);
}
}, [id, wallet]);

return document;
}

export function useDocuments({type}) {
const [documents, setDocuments] = useState([]);
const [loading, setLoading] = useState(true);

const fetchDocuments = useCallback(
async (updatedDoc, forceFetch = false) => {
if (
forceFetch ||
updatedDoc?.type === type ||
updatedDoc?.type?.includes(type)
) {
const docs = await getWallet().getDocumentsByType(type);
setDocuments(docs);
setLoading(false);
}
},
[type],
);

useEffect(() => {
fetchDocuments(null, true);
}, [fetchDocuments, setLoading]);

useEventListener(getWallet().eventManager, events, fetchDocuments);

return {
documents,
loading,
};
}

export function useWallet() {
return useContext(WalletSDKContext);
Expand Down

0 comments on commit 7ef92d2

Please sign in to comment.