Skip to content

Commit

Permalink
Add SyncManager to identity and proxy agent packages
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Sep 1, 2023
1 parent edfb874 commit 979fb16
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 44 deletions.
93 changes: 60 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/agent/src/test-managed-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import { DwnManager } from './dwn-manager.js';
import { KeyManager } from './key-manager.js';
import { Web5RpcClient } from './rpc-client.js';
import { AppDataVault } from './app-data-store.js';
import { SyncManagerLevel } from './sync-manager.js';
import { cryptoToPortableKeyPair } from './utils.js';
import { DidStoreDwn, DidStoreMemory } from './store-managed-did.js';
import { IdentityManager, ManagedIdentity } from './identity-manager.js';
import { IdentityStoreDwn, IdentityStoreMemory } from './store-managed-identity.js';
import { KeyStoreDwn, KeyStoreMemory, PrivateKeyStoreDwn, PrivateKeyStoreMemory } from './store-managed-key.js';
import { SyncManagerLevel } from './sync-manager.js';

type CreateMethodOptions = {
agentClass: new (options: any) => Web5ManagedAgent
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/tests/dwn-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
RecordsRead,
EventsGetReply,
EventsGetMessage,
MessagesGetReply,
RecordsReadReply,
RecordsQueryReply,
UnionMessageReply,
MessagesGetMessage,
RecordsQueryMessage,
RecordsWriteMessage,
RecordsDeleteMessage,
MessagesGetReply,
ProtocolsConfigureMessage,
} from '@tbd54566975/dwn-sdk-js';

Expand Down
1 change: 0 additions & 1 deletion packages/agent/tests/sync-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ describe('SyncManagerLevel', () => {
describe('with Web5ManagedAgent', () => {
let alice: ManagedIdentity;
let aliceDid: PortableDid;
// let syncManager: SyncManagerLevel;
let testAgent: TestManagedAgent;

before(async () => {
Expand Down
16 changes: 12 additions & 4 deletions packages/agent/tests/utils/test-agent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Level } from 'level';
import { Dwn } from '@tbd54566975/dwn-sdk-js';
import { DidIonMethod, DidKeyMethod, DidResolver } from '@web5/dids';
import { MessageStoreLevel, DataStoreLevel, EventLogLevel } from '@tbd54566975/dwn-sdk-js/stores';
Expand Down Expand Up @@ -44,6 +45,7 @@ type TestAgentOptions = {
dwnDataStore: DataStoreLevel;
dwnEventLog: EventLogLevel;
dwnMessageStore: MessageStoreLevel;
syncStore: Level;
}

export class TestAgent implements Web5ManagedAgent {
Expand All @@ -58,12 +60,13 @@ export class TestAgent implements Web5ManagedAgent {
syncManager: SyncManager;

/**
* DWN-related properties.
* Store-related properties.
*/
dwn: Dwn;
dwnDataStore: DataStoreLevel;
dwnEventLog: EventLogLevel;
dwnMessageStore: MessageStoreLevel;
syncStore: Level;

constructor(options: TestAgentOptions) {
this.appData = options.appData;
Expand All @@ -87,19 +90,22 @@ export class TestAgent implements Web5ManagedAgent {
this.dwnDataStore = options.dwnDataStore;
this.dwnEventLog = options.dwnEventLog;
this.dwnMessageStore = options.dwnMessageStore;
this.syncStore = options.syncStore;
}

async clearStorage(): Promise<void> {
this.agentDid = undefined;
await this.dwnDataStore.clear();
await this.dwnEventLog.clear();
await this.dwnMessageStore.clear();
await this.syncStore.clear();
}

async closeStorage(): Promise<void> {
await this.dwnDataStore.close();
await this.dwnEventLog.close();
await this.dwnMessageStore.close();
await this.syncStore.close();
}

static async create(options: CreateMethodOptions = {}): Promise<TestAgent> {
Expand Down Expand Up @@ -142,8 +148,9 @@ export class TestAgent implements Web5ManagedAgent {
// Instantiate an RPC Client.
const rpcClient = new Web5RpcClient();

// Instantiate a SyncManager.
const syncManager = new SyncManagerLevel({ dataPath: testDataPath('SYNC_STORE') });
// Instantiate a custom SyncManager and LevelDB-backed store.
const syncStore = new Level(testDataPath('SYNC_STORE'));
const syncManager = new SyncManagerLevel({ db: syncStore });

return new TestAgent({
appData,
Expand All @@ -157,7 +164,8 @@ export class TestAgent implements Web5ManagedAgent {
identityManager,
keyManager,
rpcClient,
syncManager
syncManager,
syncStore
});
}

Expand Down
20 changes: 18 additions & 2 deletions packages/identity-agent/src/identity-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
VcResponse,
DidResponse,
DwnResponse,
SyncManager,
AppDataStore,
SendVcRequest,
SendDidRequest,
Expand All @@ -27,6 +28,7 @@ import {
Web5RpcClient,
IdentityManager,
IdentityStoreDwn,
SyncManagerLevel,
PrivateKeyStoreDwn,
cryptoToPortableKeyPair,
} from '@web5/agent';
Expand All @@ -40,6 +42,7 @@ export type IdentityAgentOptions = {
identityManager: IdentityManager;
keyManager: KeyManager;
rpcClient: DwnRpc;
syncManager: SyncManager;
}

export class IdentityAgent implements Web5ManagedAgent {
Expand All @@ -51,6 +54,7 @@ export class IdentityAgent implements Web5ManagedAgent {
identityManager: IdentityManager;
keyManager: KeyManager;
rpcClient: DwnRpc;
syncManager: SyncManager;

constructor(options: IdentityAgentOptions) {
this.agentDid = options.agentDid;
Expand All @@ -61,16 +65,21 @@ export class IdentityAgent implements Web5ManagedAgent {
this.identityManager = options.identityManager;
this.keyManager = options.keyManager;
this.rpcClient = options.rpcClient;
this.syncManager = options.syncManager;

// Set this agent to be the default agent.
this.didManager.agent = this;
this.dwnManager.agent = this;
this.identityManager.agent = this;
this.keyManager.agent = this;
this.syncManager.agent = this;
}

static async create(options: Partial<IdentityAgentOptions> = {}): Promise<IdentityAgent> {
let { agentDid, appData, didManager, didResolver, dwnManager, identityManager, keyManager, rpcClient } = options;
let {
agentDid, appData, didManager, didResolver, dwnManager,
identityManager, keyManager, rpcClient, syncManager
} = options;

if (agentDid === undefined) {
// An Agent DID was not specified, so set to empty string.
Expand Down Expand Up @@ -140,6 +149,12 @@ export class IdentityAgent implements Web5ManagedAgent {
rpcClient = new Web5RpcClient();
}

if (syncManager === undefined) {
// A custom SyncManager implementation was not specified, so
// instantiate a LevelDB-backed default.
syncManager = new SyncManagerLevel();
}

// Instantiate the Identity Agent.
const agent = new IdentityAgent({
agentDid,
Expand All @@ -149,7 +164,8 @@ export class IdentityAgent implements Web5ManagedAgent {
dwnManager,
identityManager,
keyManager,
rpcClient
rpcClient,
syncManager
});

return agent;
Expand Down
Loading

0 comments on commit 979fb16

Please sign in to comment.