From 919502165ed3b37752746178ba29b1d10d5917a9 Mon Sep 17 00:00:00 2001 From: Johnson Tinuoye Date: Mon, 2 Oct 2023 10:50:28 +0100 Subject: [PATCH 1/6] feat: refactor unnecessary implementations --- packages/core/src/did-provider.ts | 2 +- packages/react-native/lib/accountsHooks.js | 2 +- .../lib/credentials/credentialHooks.js | 2 +- packages/react-native/lib/didHooks.js | 75 ++++--------------- packages/react-native/lib/index.js | 21 ++---- 5 files changed, 24 insertions(+), 78 deletions(-) diff --git a/packages/core/src/did-provider.ts b/packages/core/src/did-provider.ts index e4a747a5..339ab8b3 100644 --- a/packages/core/src/did-provider.ts +++ b/packages/core/src/did-provider.ts @@ -83,7 +83,7 @@ export async function createDIDock({wallet, address, name}) { await wallet.add(dockDIDResolution); } -const createDIDKeyDocument = async ( +export const createDIDKeyDocument = async ( keypairDoc: any, didDocParams: any = {}, ) => { diff --git a/packages/react-native/lib/accountsHooks.js b/packages/react-native/lib/accountsHooks.js index 0fb00e79..886bb3ab 100644 --- a/packages/react-native/lib/accountsHooks.js +++ b/packages/react-native/lib/accountsHooks.js @@ -2,7 +2,7 @@ import {getAccount, useWallet} from './index'; import {useMemo} from 'react'; export function useAccounts() { - const {documents, wallet} = useWallet({syncDocs: true}); + const {documents, wallet} = useWallet(); const accounts = useMemo(() => { if (Array.isArray(documents)) { return documents diff --git a/packages/react-native/lib/credentials/credentialHooks.js b/packages/react-native/lib/credentials/credentialHooks.js index 3523c622..d6eeac4a 100644 --- a/packages/react-native/lib/credentials/credentialHooks.js +++ b/packages/react-native/lib/credentials/credentialHooks.js @@ -66,7 +66,7 @@ async function getCredentialValidityStatus(credential) { } export function useCredentialUtils() { - const {documents, wallet} = useWallet({syncDocs: true}); + const {documents, wallet} = useWallet(); const credentials = useMemo(() => { if (Array.isArray(documents)) { diff --git a/packages/react-native/lib/didHooks.js b/packages/react-native/lib/didHooks.js index c083b599..efd1291d 100644 --- a/packages/react-native/lib/didHooks.js +++ b/packages/react-native/lib/didHooks.js @@ -1,64 +1,15 @@ import {useCallback, useMemo} from 'react'; import {didServiceRPC} from '@docknetwork/wallet-sdk-wasm/src/services/dids'; -import {createDIDProvider} from '@docknetwork/wallet-sdk-core/src/did-provider'; +import {createDIDKeyDocument, createDIDProvider} from '@docknetwork/wallet-sdk-core/src/did-provider'; import {useWallet} from './index'; -// TODO: there is no point in using hooks here -// we could just use a regular function and createDIDKeypairDocument looks redundant -export function useDIDUtils() { - const createDIDKeypairDocument = useCallback(async keypairParams => { - const {type, derivePath} = keypairParams; - return didServiceRPC.generateKeyDoc({ - type, - derivePath, - controller: keypairParams.controller, - }); - }, []); - - const createDIDKeyDocument = useCallback( - async (keypairDoc, didDocParams = {}) => { - const keypairDocCorrelations = Array.isArray(keypairDoc.correlation) - ? keypairDoc.correlation - : []; - - const {didDocument} = await didServiceRPC.keypairToDIDKeyDocument({ - keypairDoc: keypairDoc, - }); - - const didDocumentResolution = await didServiceRPC.getDIDResolution({ - didDocumentCustomProp: didDocParams, - didDocument, - }); - - didDocumentResolution.correlation.push(keypairDoc.id); - - keypairDocCorrelations.push(didDocumentResolution.id); - - return { - didDocumentResolution, - keypairDoc, - }; - }, - [], - ); - - return useMemo(() => { - return { - createDIDKeypairDocument, - createDIDKeyDocument, - }; - }, [createDIDKeypairDocument, createDIDKeyDocument]); -} - export function useDIDManagement() { - const {wallet, documents} = useWallet({syncDocs: true}); + const { wallet, documents } = useWallet(); const didProvider = useMemo(() => { return createDIDProvider({ wallet, }); }, [wallet]); - const {createDIDKeypairDocument, createDIDKeyDocument} = useDIDUtils(); - const didList = useMemo(() => { if (Array.isArray(documents)) { return documents.filter(doc => { @@ -69,31 +20,31 @@ export function useDIDManagement() { }, [documents]); const didMethodKey = useCallback( - async ({derivePath, type, name}) => { - const keydoc = await createDIDKeypairDocument({ - derivePath, + async ({ derivePath, type, name }) => { + const keydoc = await didServiceRPC.generateKeyDoc({ type, + derivePath, }); - const {didDocumentResolution} = await createDIDKeyDocument(keydoc, { + const { didDocumentResolution } = await createDIDKeyDocument(keydoc, { name, }); await wallet.add(keydoc); await wallet.add(didDocumentResolution); }, - [createDIDKeyDocument, createDIDKeypairDocument, wallet], + [wallet], ); const createDID = useCallback( async didParams => { - const {derivePath, type = 'ed25519', name, didType, address} = didParams; + const { derivePath, type = 'ed25519', name, didType, address } = didParams; switch (didType) { case 'diddock': - return didProvider.createDIDock({address, name}); + return didProvider.createDIDock({ address, name }); case 'didkey': // TODO: use the same approach as we have for DIDDock // move didMethodKey to didProvider, and stop using react hooks for it // so that it can be used with non-react apps later on (e.g: flutter, nodejs) - return didMethodKey({derivePath, name, type}); + return didMethodKey({ derivePath, name, type }); default: throw Error('Invalid DID type'); } @@ -102,7 +53,7 @@ export function useDIDManagement() { ); const editDID = useCallback( async didParams => { - const {id, name} = didParams; + const { id, name } = didParams; if (typeof id === 'string' && id.length > 0) { const docs = await wallet.query({ id, @@ -122,7 +73,7 @@ export function useDIDManagement() { ); const deleteDID = useCallback( async didParams => { - const {id} = didParams; + const { id } = didParams; if (typeof id === 'string' && id.length > 0) { return await wallet.remove(id); } else { @@ -133,7 +84,7 @@ export function useDIDManagement() { ); const exportDID = useCallback( - async ({id, password}) => { + async ({ id, password }) => { const existingDoc = await wallet.getDocumentById(id); if (existingDoc) { const allCorrelationDocuments = ( diff --git a/packages/react-native/lib/index.js b/packages/react-native/lib/index.js index cc1345e2..21461511 100644 --- a/packages/react-native/lib/index.js +++ b/packages/react-native/lib/index.js @@ -86,7 +86,7 @@ export function getAccount(address, documents): AccountDetails { } export function useAccount(address) { - const {documents, wallet} = useWallet({syncDocs: true}); + const {documents, wallet} = useWallet(); const account = getAccount(address, documents); return { @@ -97,7 +97,7 @@ export function useAccount(address) { }; } -export function useWallet({syncDocs = true} = {}) { +export function useWallet() { return useContext(WalletSDKContext); } @@ -108,9 +108,7 @@ export function _useWalletController() { const [firstFetch, setFirstFetch] = useState(); const [networkId, setNetworkId] = useState(); - const testMode = useMemo(() => { - return networkId === wallet?.dataStore?.testNetworkId; - }, [wallet, networkId]); + const testMode = networkId === wallet?.dataStore?.testNetworkId; const toggleTestMode = async () => { await wallet.setNetwork( @@ -134,8 +132,7 @@ export function _useWalletController() { wallet.getAllDocuments().then(setDocuments); }, [documents, wallet, firstFetch]); - const refetch = useCallback( - async ({fetchBalances} = {}) => { + const refetch = async ({fetchBalances} = {fetchBalances: true}) => { try { const allDocs = await wallet.query({}); if (fetchBalances) { @@ -152,9 +149,7 @@ export function _useWalletController() { } catch (err) { console.error(err); } - }, - [wallet, setDocuments], - ); + } useEffect(() => { if (!wallet) { @@ -182,10 +177,10 @@ export function _useWalletController() { }); }, [status, wallet, refetch]); - const createWallet = useCallback(async () => { + const createWallet = async () => { const newWallet = await getOrCreateWallet(); setWallet(newWallet); - }, [setWallet]); + }; return { wallet, @@ -194,7 +189,7 @@ export function _useWalletController() { testMode, documents, status, - refetch: () => refetch({fetchBalances: true}), + refetch, }; } From a152c92fbb40f7b7234e157672af5def6a0fb1d7 Mon Sep 17 00:00:00 2001 From: Johnson Tinuoye Date: Mon, 2 Oct 2023 12:24:37 +0100 Subject: [PATCH 2/6] refactor did management hook --- packages/core/src/did-provider.ts | 69 ++++++++++++++-- packages/react-native/lib/didHooks.js | 113 ++++---------------------- 2 files changed, 81 insertions(+), 101 deletions(-) diff --git a/packages/core/src/did-provider.ts b/packages/core/src/did-provider.ts index 339ab8b3..e621a139 100644 --- a/packages/core/src/did-provider.ts +++ b/packages/core/src/did-provider.ts @@ -83,6 +83,51 @@ export async function createDIDock({wallet, address, name}) { await wallet.add(dockDIDResolution); } +async function editDID({wallet, id, name}){ + if (typeof id === 'string' && id.length > 0) { + const docs = await wallet.query({ + id, + }); + if (docs.length === 1) { + const doc = docs[0]; + await wallet.update({ + ...doc, + name, + }); + } + } else { + throw new Error('Document ID is not set'); + } +}; + +async function deleteDID({wallet, id}){ + if (typeof id === 'string' && id.length > 0) { + return await wallet.remove(id); + } else { + throw Error('Document ID is not set'); + } +} + +async function exportDID({wallet, id, password }){ + const existingDoc = await wallet.getDocumentById(id); + if (existingDoc) { + const allCorrelationDocuments = ( + await wallet.resolveCorrelations(id) + ).filter(doc => doc && doc?.id !== existingDoc.id); + const documentsToExport = [existingDoc, ...allCorrelationDocuments]; + + if (allCorrelationDocuments.length >= 1) { + return wallet.exportDocuments({ + documents: documentsToExport, + password, + }); + } + throw new Error('DID KeyPair not found'); + } + + throw new Error('DID Document not found'); +} + export const createDIDKeyDocument = async ( keypairDoc: any, didDocParams: any = {}, @@ -110,11 +155,11 @@ export const createDIDKeyDocument = async ( }; }; -export async function createDIKey({wallet, name}) { +export async function createDIDKey({wallet, name, derivePath=undefined, type=undefined}) { assert(!!wallet, 'wallet is required'); assert(!!name, 'name is required'); - const keyDoc = await didServiceRPC.generateKeyDoc({}); + const keyDoc = await didServiceRPC.generateKeyDoc({derivePath, type}); const {didDocumentResolution} = await createDIDKeyDocument(keyDoc, { name, @@ -156,7 +201,7 @@ export async function ensureDID({wallet}) { assert(!!wallet, 'wallet is required'); const dids = await getAll({wallet}); if (dids.length === 0) { - return createDIKey({wallet, name: 'Default DID'}); + return createDIDKey({wallet, name: 'Default DID'}); } } @@ -166,7 +211,10 @@ export interface IDIDProvider { password: string; }): Promise; createDIDock(params: {address: string; name: string}): Promise; - createDIDKey(params: {name: string}): Promise; + createDIDKey(params: {name: string, derivePath?:string, type?: string}): Promise; + editDID(params: {id: string; name: string}): Promise; + deleteDID(params: {id: string;}): Promise; + exportDID(params: {id: string; password: string}): Promise; getAll(): Promise; getDIDKeyPairs(): Promise; ensureDID(): Promise; @@ -180,8 +228,17 @@ export function createDIDProvider({wallet}): IDIDProvider { async createDIDock({address, name}) { return createDIDock({wallet, address, name}); }, - async createDIDKey({name}) { - return createDIKey({wallet, name}); + async createDIDKey({name, derivePath, type}) { + return createDIDKey({wallet, name, derivePath, type}); + }, + async editDID(params) { + return editDID({wallet, ...params}); + }, + async deleteDID(params) { + return deleteDID({wallet, ...params}); + }, + async exportDID(params) { + return exportDID({wallet, ...params}); }, async getAll() { return getAll({wallet}); diff --git a/packages/react-native/lib/didHooks.js b/packages/react-native/lib/didHooks.js index efd1291d..85354a12 100644 --- a/packages/react-native/lib/didHooks.js +++ b/packages/react-native/lib/didHooks.js @@ -1,7 +1,7 @@ -import {useCallback, useMemo} from 'react'; -import {didServiceRPC} from '@docknetwork/wallet-sdk-wasm/src/services/dids'; -import {createDIDKeyDocument, createDIDProvider} from '@docknetwork/wallet-sdk-core/src/did-provider'; -import {useWallet} from './index'; +import { useCallback, useMemo } from 'react'; +import { didServiceRPC } from '@docknetwork/wallet-sdk-wasm/src/services/dids'; +import { createDIDKeyDocument, createDIDProvider } from '@docknetwork/wallet-sdk-core/src/did-provider'; +import { useWallet } from './index'; export function useDIDManagement() { const { wallet, documents } = useWallet(); @@ -19,102 +19,25 @@ export function useDIDManagement() { return []; }, [documents]); - const didMethodKey = useCallback( - async ({ derivePath, type, name }) => { - const keydoc = await didServiceRPC.generateKeyDoc({ - type, - derivePath, - }); - const { didDocumentResolution } = await createDIDKeyDocument(keydoc, { - name, - }); - await wallet.add(keydoc); - await wallet.add(didDocumentResolution); - }, - [wallet], - ); - - const createDID = useCallback( - async didParams => { - const { derivePath, type = 'ed25519', name, didType, address } = didParams; - switch (didType) { - case 'diddock': - return didProvider.createDIDock({ address, name }); - case 'didkey': - // TODO: use the same approach as we have for DIDDock - // move didMethodKey to didProvider, and stop using react hooks for it - // so that it can be used with non-react apps later on (e.g: flutter, nodejs) - return didMethodKey({ derivePath, name, type }); - default: - throw Error('Invalid DID type'); - } - }, - [didProvider, didMethodKey], - ); - const editDID = useCallback( - async didParams => { - const { id, name } = didParams; - if (typeof id === 'string' && id.length > 0) { - const docs = await wallet.query({ - id, - }); - if (docs.length === 1) { - const doc = docs[0]; - await wallet.update({ - ...doc, - name, - }); - } - } else { - throw new Error('Document ID is not set'); - } - }, - [wallet], - ); - const deleteDID = useCallback( - async didParams => { - const { id } = didParams; - if (typeof id === 'string' && id.length > 0) { - return await wallet.remove(id); - } else { - throw Error('Document ID is not set'); - } - }, - [wallet], - ); - - const exportDID = useCallback( - async ({ id, password }) => { - const existingDoc = await wallet.getDocumentById(id); - if (existingDoc) { - const allCorrelationDocuments = ( - await wallet.resolveCorrelations(id) - ).filter(doc => doc && doc?.id !== existingDoc.id); - const documentsToExport = [existingDoc, ...allCorrelationDocuments]; - - if (allCorrelationDocuments.length >= 1) { - return wallet.exportDocuments({ - documents: documentsToExport, - password, - }); - } - throw new Error('DID KeyPair not found'); - } - - throw new Error('DID Document not found'); - }, - [wallet], - ); + const createDID = async didParams => { + const { derivePath, type = 'ed25519', name, didType, address } = didParams; + switch (didType) { + case 'diddock': + return didProvider.createDIDock({ address, name }); + case 'didkey': + return didProvider.createDidKey({ derivePath, name, type }); + default: + throw Error('Invalid DID type'); + } + }; return { - // I've moved this method to the did-provider.ts file - // Can be used as an example on how to remove code from hooks importDID: didProvider.importDID, didProvider, createDID, - editDID, - deleteDID, + editDID: didProvider.editDID, + deleteDID: didProvider.deleteDID, didList, - exportDID, + exportDID: didProvider.exportDID, }; } From fdc5bfac517ce04eb836d0868dfed2c1d4d50237 Mon Sep 17 00:00:00 2001 From: Johnson Tinuoye Date: Mon, 2 Oct 2023 13:00:28 +0100 Subject: [PATCH 3/6] feat: refactor presentation --- packages/react-native/lib/accountsHooks.js | 10 ++++------ .../lib/credentialPresentHooks.js | 19 ------------------- packages/react-native/lib/index.js | 2 -- 3 files changed, 4 insertions(+), 27 deletions(-) delete mode 100644 packages/react-native/lib/credentialPresentHooks.js diff --git a/packages/react-native/lib/accountsHooks.js b/packages/react-native/lib/accountsHooks.js index 886bb3ab..3d456113 100644 --- a/packages/react-native/lib/accountsHooks.js +++ b/packages/react-native/lib/accountsHooks.js @@ -2,7 +2,7 @@ import {getAccount, useWallet} from './index'; import {useMemo} from 'react'; export function useAccounts() { - const {documents, wallet} = useWallet(); + const { documents, wallet } = useWallet(); const accounts = useMemo(() => { if (Array.isArray(documents)) { return documents @@ -21,9 +21,7 @@ export function useAccounts() { return []; }, [documents, wallet?.accounts]); - return useMemo(() => { - return { - accounts, - }; - }, [accounts]); + return { + accounts, + }; } diff --git a/packages/react-native/lib/credentialPresentHooks.js b/packages/react-native/lib/credentialPresentHooks.js deleted file mode 100644 index cf767347..00000000 --- a/packages/react-native/lib/credentialPresentHooks.js +++ /dev/null @@ -1,19 +0,0 @@ -import {useCallback, useMemo} from 'react'; -import {credentialServiceRPC} from '@docknetwork/wallet-sdk-wasm/src/services/credential'; - -export function usePresentation() { - const presentCredentials = useCallback( - async ({credentials, keyDoc, challenge, id, domain}) => { - return credentialServiceRPC.createPresentation({ - credentials, - keyDoc, - challenge, - id, - domain, - }); - }, - [], - ); - - return useMemo(() => ({presentCredentials}), [presentCredentials]); -} diff --git a/packages/react-native/lib/index.js b/packages/react-native/lib/index.js index 21461511..23315725 100644 --- a/packages/react-native/lib/index.js +++ b/packages/react-native/lib/index.js @@ -19,7 +19,6 @@ import {AccountDetails} from '@docknetwork/wallet-sdk-wasm/src/modules/account'; import {DocumentType} from '@docknetwork/wallet-sdk-wasm/src/types'; import './rn-rpc-server'; import {useDIDManagement, useDIDUtils} from './didHooks'; -import {usePresentation} from './credentialPresentHooks'; import {useAccounts} from './accountsHooks'; import { useCredentialUtils, @@ -42,7 +41,6 @@ export const WalletSDKContext = React.createContext({ }); setStorage(AsyncStorage); -export {usePresentation}; export {useAccounts}; export {useDIDManagement, useDIDUtils}; export { From 8e0adfa1b440012224da48f254ed1620ea7cc036 Mon Sep 17 00:00:00 2001 From: Johnson Tinuoye Date: Mon, 2 Oct 2023 15:21:12 +0100 Subject: [PATCH 4/6] feat: refactor unnecessary useCallbacks --- .../lib/credentials/credentialHooks.js | 59 ++++++++----------- packages/react-native/lib/index.js | 4 +- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/packages/react-native/lib/credentials/credentialHooks.js b/packages/react-native/lib/credentials/credentialHooks.js index d6eeac4a..7bf0b5bd 100644 --- a/packages/react-native/lib/credentials/credentialHooks.js +++ b/packages/react-native/lib/credentials/credentialHooks.js @@ -66,7 +66,7 @@ async function getCredentialValidityStatus(credential) { } export function useCredentialUtils() { - const {documents, wallet} = useWallet(); + const { documents, wallet } = useWallet(); const credentials = useMemo(() => { if (Array.isArray(documents)) { @@ -82,39 +82,31 @@ export function useCredentialUtils() { return []; }, [documents]); - const doesCredentialExist = useCallback((allCredentials, credentialToAdd) => { + const doesCredentialExist = (allCredentials, credentialToAdd) => { return !!allCredentials.find(item => item.id === credentialToAdd.id); - }, []); + }; - const saveCredential = useCallback( - async jsonData => { - validateCredential(jsonData); - if (doesCredentialExist(credentials, jsonData)) { - throw new Error('This credential already exists in the wallet'); - } - await wallet.addDocument(jsonData); - }, - [credentials, doesCredentialExist, wallet], - ); - const deleteCredential = useCallback( - async credentialId => { - assert( - typeof credentialId === 'string' && credentialId.length > 0, - 'Credential ID is not set', - ); - return await wallet.remove(credentialId); - }, - [wallet], - ); - - return useMemo(() => { - return { - credentials, - doesCredentialExist, - saveCredential, - deleteCredential, - }; - }, [credentials, doesCredentialExist, saveCredential, deleteCredential]); + const saveCredential = async jsonData => { + validateCredential(jsonData); + if (doesCredentialExist(credentials, jsonData)) { + throw new Error('This credential already exists in the wallet'); + } + await wallet.addDocument(jsonData); + }; + const deleteCredential = async credentialId => { + assert( + typeof credentialId === 'string' && credentialId.length > 0, + 'Credential ID is not set', + ); + return await wallet.remove(credentialId); + }; + + return { + credentials, + doesCredentialExist, + saveCredential, + deleteCredential, + }; } export function isInThePast(date) { const today = new Date(); @@ -130,7 +122,7 @@ export async function getCredentialStatus(credential) { if (hasExpired) { return CREDENTIAL_STATUS.EXPIRED; } - const {verified, error} = await getCredentialValidityStatus(credential); + const { verified, error } = await getCredentialValidityStatus(credential); if ( typeof error === 'string' && @@ -144,6 +136,7 @@ export async function getCredentialStatus(credential) { return CREDENTIAL_STATUS.INVALID; } +//TODO: Implement a caching mechanism that is attached to credentials context instead of global export let cachedCredentialStatus = {}; export function useGetCredentialStatus({credential}) { diff --git a/packages/react-native/lib/index.js b/packages/react-native/lib/index.js index 23315725..9e032a5e 100644 --- a/packages/react-native/lib/index.js +++ b/packages/react-native/lib/index.js @@ -200,12 +200,12 @@ export function WalletSDKProvider({onError, customUri, children, onReady}) { const {createWallet} = controller; - const handleReady = useCallback(async () => { + const handleReady = async () => { await createWallet(); if (onReady) { onReady(); } - }, [onReady, createWallet]); + }; const eventHandler: WebviewEventHandler = useMemo( () => From 9dcf4313295eecbdf2eb87838270f9717a7ed89f Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Wed, 11 Oct 2023 18:27:27 -0300 Subject: [PATCH 5/6] DCKA-2041: message provider, bug fixes --- packages/core/src/message-provider.ts | 26 ++++++++---- .../src/entities/document/create-document.ts | 7 +++- .../src/entities/document/helpers.ts | 8 +++- packages/react-native/lib/wallet.ts | 41 +++++++++++++++++++ 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/packages/core/src/message-provider.ts b/packages/core/src/message-provider.ts index 9bf1441d..08698644 100644 --- a/packages/core/src/message-provider.ts +++ b/packages/core/src/message-provider.ts @@ -47,18 +47,30 @@ export function createMessageProvider({ } }, - async processDIDCommMessages() { + async processDIDCommMessages(limit = 1) { try { const messages = await wallet.getDocumentsByType(WalletDocumentTypes.DIDCommMessage); const keyPairDocs = await getKeyPairDocs(didProvider); - for (const { encryptedMessage } of messages) { + let count = 0; + for (const message of messages) { + if (count >= limit) { + return; + } try { - const decryptedMessage = await relayService.resolveDidcommMessage({ keyPairDocs, encryptedMessage }); - wallet.eventManager.emit('didcomm-message-decrypted', decryptedMessage); + if (!message.encryptedMessage) { + await wallet.removeDocument(message.id); + throw new Error(`Message with payload ${JSON.stringify(message)} is invalid`); + } + const decryptedMessage = await relayService.resolveDidcommMessage({ keyPairDocs, message: message.encryptedMessage }); + wallet.eventManager.emit('didcomm-message-decrypted', { + decryptedMessage, + messageId: message.id + }); // the wallet app will call markMessageAsRead after the message is processed } catch(err) { captureException(err); } + count++; } } catch (error) { captureException(error); @@ -90,12 +102,12 @@ export function createMessageProvider({ skipMessageResolution: true, }); - for (const { _id, encryptedMessage } of encryptedMessages) { + for (const message of encryptedMessages) { try { await wallet.addDocument({ - id: _id, + id: message._id, type: WalletDocumentTypes.DIDCommMessage, - encryptedMessage, + encryptedMessage: message, }); } catch(err) { // this message will be lost if it fails to be stored in the wallet diff --git a/packages/data-store/src/entities/document/create-document.ts b/packages/data-store/src/entities/document/create-document.ts index cd5bf05c..77d8b21e 100644 --- a/packages/data-store/src/entities/document/create-document.ts +++ b/packages/data-store/src/entities/document/create-document.ts @@ -31,7 +31,10 @@ export async function createDocument({ types: json.type, }); - const documentId = json.id || uuid(); + if (!json.id) { + json.id = uuid() + } + let networkId; if (json._networkId) { @@ -48,7 +51,7 @@ export async function createDocument({ const entity: DocumentEntity = { networkId, - id: documentId, + id: json.id, type: json.type, _typeRel, correlation: json.correlation || [], diff --git a/packages/data-store/src/entities/document/helpers.ts b/packages/data-store/src/entities/document/helpers.ts index 294c9b8f..b19fee0f 100644 --- a/packages/data-store/src/entities/document/helpers.ts +++ b/packages/data-store/src/entities/document/helpers.ts @@ -72,7 +72,13 @@ export function toWalletDocument(entity: DocumentEntity): WalletDocument { return entity; } - return JSON.parse(entity.data); + const result = JSON.parse(entity.data); + + if (!result.id) { + result.id = entity.id; + } + + return result; } /** diff --git a/packages/react-native/lib/wallet.ts b/packages/react-native/lib/wallet.ts index e10dff0b..a8c38e11 100644 --- a/packages/react-native/lib/wallet.ts +++ b/packages/react-native/lib/wallet.ts @@ -1,8 +1,12 @@ import {createWallet, IWallet} from '@docknetwork/wallet-sdk-core/src/wallet'; import {dockDocumentNetworkResolver} from '@docknetwork/wallet-sdk-core/src/network-resolver'; import {DataStoreConfigs} from '@docknetwork/wallet-sdk-data-store/src/types'; +import { createDIDProvider, IDIDProvider } from '@docknetwork/wallet-sdk-core/src/did-provider'; +import { createMessageProvider, IMessageProvider } from '@docknetwork/wallet-sdk-core/src/message-provider'; let wallet: IWallet; +let didProvider: IDIDProvider; +let messageProvider: IMessageProvider; export function getWallet() { if (!wallet) { @@ -12,10 +16,34 @@ export function getWallet() { return wallet; } +export function getMessageProvider() { + if (!messageProvider) { + throw new Error('Message provider not initialized'); + } + + return messageProvider; +} + +export function getDIDProvider() { + if (!didProvider) { + throw new Error('DID provider not initialized'); + } + + return didProvider; +} + export function setWallet(_wallet: IWallet) { wallet = _wallet; } +export function setDIDProvider(_didProvider: IDIDProvider) { + didProvider = _didProvider; +} + +export function setMessageProvider(_messageProvider: IMessageProvider) { + messageProvider = _messageProvider; +} + export async function getOrCreateWallet(params: DataStoreConfigs = {} as any) { if (!wallet) { await initializeWallet(params); @@ -35,5 +63,18 @@ export async function initializeWallet(params: DataStoreConfigs = {} as any) { setWallet(_wallet); + const _didProvider = createDIDProvider({ + wallet: _wallet, + }); + + setDIDProvider(_didProvider); + + const _messageProvider = createMessageProvider({ + wallet: _wallet, + didProvider: _didProvider, + }); + + setMessageProvider(_messageProvider); + return _wallet; } From c4d9ed943a1c50b5dc0ffa26a598152395b726f8 Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Thu, 12 Oct 2023 19:25:43 -0300 Subject: [PATCH 6/6] switch node connection to https --- packages/data-store/src/configs.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/data-store/src/configs.ts b/packages/data-store/src/configs.ts index 98ad6794..35c41148 100644 --- a/packages/data-store/src/configs.ts +++ b/packages/data-store/src/configs.ts @@ -11,7 +11,7 @@ export const DEFAULT_CONFIGS: DataStoreConfigs = { id: 'mainnet', credentialHostnames: ['creds.dock.io'], configs: { - substrateUrl: 'wss://mainnet-node.dock.io', + substrateUrl: 'https://mainnet-node.dock.io/h', addressPrefix: 22, }, }, @@ -20,7 +20,7 @@ export const DEFAULT_CONFIGS: DataStoreConfigs = { id: 'testnet', credentialHostnames: ['creds-testnet.dock.io', 'creds-staging.dock.io'], configs: { - substrateUrl: 'wss://knox-1.dock.io', + substrateUrl: 'https://knox-1.dock.io/h', addressPrefix: 21, }, },