Skip to content

Commit

Permalink
lint and sync upon agent recover
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Sep 6, 2024
1 parent 6b51057 commit f6776ec
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './local-key-manager.js';
export * from './permissions-api.js';
export * from './rpc-client.js';
export * from './store-data.js';
export * from './store-data-protocols.js';
export * from './store-did.js';
export * from './store-identity.js';
export * from './store-key.js';
Expand Down
36 changes: 18 additions & 18 deletions packages/agent/src/sync-engine-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export class SyncEngineLevel implements SyncEngine {
protocol,
cached : true
});

permissionGrantId = messagesReadGrant.grant.id;
granteeDid = delegateDid;
} catch(error:any) {
Expand All @@ -316,11 +316,11 @@ export class SyncEngineLevel implements SyncEngine {
}

const { reply: { status, subscription } } = await this.agent.processDwnRequest({
author: did,
target: did,
messageType: DwnInterface.MessagesSubscribe,
author : did,
target : did,
messageType : DwnInterface.MessagesSubscribe,
granteeDid,
messageParams: {
messageParams : {
permissionGrantId
},
subscriptionHandler: async (event) => {
Expand Down Expand Up @@ -371,12 +371,12 @@ export class SyncEngineLevel implements SyncEngine {
protocol,
cached : true
});

permissionGrantId = messagesReadGrant.grant.id;
granteeDid = delegateDid;
} catch(error:any) {
console.error('SyncEngineLevel: pull - Error fetching MessagesRead permission grant for delegate DID', error);
return
return;
}
}

Expand All @@ -391,9 +391,9 @@ export class SyncEngineLevel implements SyncEngine {
permissionGrantId
}
});

let reply: MessagesReadReply;

try {
reply = await this.agent.rpc.sendDwnRequest({
dwnUrl,
Expand All @@ -413,7 +413,7 @@ export class SyncEngineLevel implements SyncEngine {
if (processStatus.code === 202 || processStatus.code === 204 || processStatus.code === 409) {
await this.addMessage(did, messageCid);
}
}
};
let permissionGrantId: string | undefined;
let granteeDid: string | undefined;
if (delegateDid) {
Expand All @@ -425,7 +425,7 @@ export class SyncEngineLevel implements SyncEngine {
protocol,
cached : true
});

permissionGrantId = messagesReadGrant.grant.id;
granteeDid = delegateDid;
} catch(error:any) {
Expand All @@ -434,21 +434,21 @@ export class SyncEngineLevel implements SyncEngine {
}
}
const messagesSubscribeMessage = await this.agent.sendDwnRequest({
store: false,
author: did,
target: did,
messageType: DwnInterface.MessagesSubscribe,
store : false,
author : did,
target : did,
messageType : DwnInterface.MessagesSubscribe,
granteeDid,
messageParams: {
messageParams : {
permissionGrantId,
},
subscriptionHandler
});

const { status: sendStatus, subscription } = await this.agent.rpc.sendDwnRequest({
dwnUrl,
targetDid: did,
message: messagesSubscribeMessage.message,
targetDid : did,
message : messagesSubscribeMessage.message,
subscriptionHandler
});

Expand Down
22 changes: 20 additions & 2 deletions packages/api/src/web5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
} from '@web5/agent';

import { Web5UserAgent } from '@web5/user-agent';
import { DwnRegistrar, WalletConnect } from '@web5/agent';
import { DwnRegistrar, IdentityProtocolDefinition, JwkProtocolDefinition, WalletConnect } from '@web5/agent';

import { DidApi } from './did-api.js';
import { DwnApi } from './dwn-api.js';
Expand Down Expand Up @@ -280,10 +280,28 @@ export class Web5 {
const serviceEndpointNodes = techPreview?.dwnEndpoints ?? didCreateOptions?.dwnEndpoints ?? ['https://dwn.tbddev.org/beta'];

// Initialize, if necessary, and start the agent.
if (await userAgent.firstLaunch()) {
const firstLaunch = await userAgent.firstLaunch();
if (firstLaunch) {
recoveryPhrase = await userAgent.initialize({ password, recoveryPhrase, dwnEndpoints: serviceEndpointNodes });
}
await userAgent.start({ password });
if (firstLaunch && sync !== 'off') {

// register only the identity-specific protocols for agent sync
await userAgent.sync.registerIdentity({
did : userAgent.agentDid.uri,
options : {
protocols: [
IdentityProtocolDefinition.protocol,
JwkProtocolDefinition.protocol
]
}
});

// attempt to pull the latest messages from the DWN so that the identities can be in sync
await userAgent.sync.sync('pull');
}

// Attempt to retrieve the connected Identity if it exists.
const connectedIdentity: BearerIdentity = await userAgent.identity.connectedIdentity();
let identity: BearerIdentity;
Expand Down

0 comments on commit f6776ec

Please sign in to comment.