Skip to content

Commit

Permalink
Merge pull request #168 from docknetwork/integrate-DCKA-2041-android-…
Browse files Browse the repository at this point in the history
…credential-distribution-not-working-when-app-is-running-on-background
  • Loading branch information
maycon-mello authored Oct 11, 2023
2 parents 560bcaa + 9dcf431 commit ac0f31c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
26 changes: 19 additions & 7 deletions packages/core/src/message-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions packages/data-store/src/entities/document/create-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -48,7 +51,7 @@ export async function createDocument({

const entity: DocumentEntity = {
networkId,
id: documentId,
id: json.id,
type: json.type,
_typeRel,
correlation: json.correlation || [],
Expand Down
8 changes: 7 additions & 1 deletion packages/data-store/src/entities/document/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions packages/react-native/lib/wallet.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
Expand All @@ -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;
}

0 comments on commit ac0f31c

Please sign in to comment.