Skip to content

Commit

Permalink
Improve test coverage for DwnApi and Record. Added missing functional…
Browse files Browse the repository at this point in the history
…ity to ION DwnConfiguration.

Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Aug 21, 2023
1 parent 7439b5a commit 53d193c
Show file tree
Hide file tree
Showing 16 changed files with 1,577 additions and 1,357 deletions.
33 changes: 30 additions & 3 deletions packages/agent/src/test-managed-agent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { KeyValueStore } from '@web5/common';
import type { DidResolutionResult, DidResolverCache } from '@web5/dids';
import type { DidResolutionResult, DidResolverCache, PortableDid } from '@web5/dids';

import { Jose } from '@web5/crypto';
import { Dwn } from '@tbd54566975/dwn-sdk-js';
import { DidIonMethod, DidKeyMethod, DidResolver, DidResolverCacheLevel } from '@web5/dids';
import { LevelStore, MemoryStore } from '@web5/common';
import { DidIonMethod, DidKeyMethod, DidResolver, DidResolverCacheLevel } from '@web5/dids';
import { MessageStoreLevel, DataStoreLevel, EventLogLevel } from '@tbd54566975/dwn-sdk-js/stores';

import type { Web5ManagedAgent } from './types/agent.js';
Expand All @@ -16,8 +16,8 @@ import { KeyManager } from './key-manager.js';
import { Web5RpcClient } from './rpc-client.js';
import { AppDataVault } from './app-data-store.js';
import { cryptoToPortableKeyPair } from './utils.js';
import { IdentityManager } from './identity-manager.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';

Expand Down Expand Up @@ -167,6 +167,33 @@ export class TestManagedAgent {
this.agent.agentDid = agentDid.did;
}

public async createIdentity(options: {
keyAlgorithm?: 'Ed25519' | 'secp256k1';
testDwnUrls: string[]
}): Promise<{ did: PortableDid, identity: ManagedIdentity }> {
// Default to generating Ed25519 keys.
const { keyAlgorithm, testDwnUrls } = options;

const didOptions = await DidIonMethod.generateDwnOptions({
signingKeyAlgorithm : keyAlgorithm,
serviceEndpointNodes : testDwnUrls
});

// Create a PortableDid.
const did = await DidIonMethod.create({
anchor: false,
...didOptions
});

// Create a ManagedIdentity.
const identity: ManagedIdentity = {
did : did.did,
name : 'Test'
};

return { did, identity };
}

private static useDiskStorage(options: { agent?: Web5ManagedAgent, testDataLocation: string }) {
const { agent, testDataLocation } = options;
const testDataPath = (path: string) => `${testDataLocation}/${path}`;
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 @@ -275,7 +275,7 @@ describe('DwnManager', () => {
await testAgent.createAgentDid();

const services = [{
id : 'dwn',
id : '#dwn',
type : 'DecentralizedWebNode',
serviceEndpoint : {
encryptionKeys : ['#dwn-enc'],
Expand Down
19 changes: 13 additions & 6 deletions packages/api/src/dwn-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Web5Agent } from '@web5/agent';
import type { DwnResponse, Web5Agent } from '@web5/agent';
import type {
UnionMessageReply,
RecordsReadOptions,
Expand Down Expand Up @@ -139,15 +139,22 @@ export class DwnApi {
* TODO: Document method.
*/
query: async (request: ProtocolsQueryRequest): Promise<ProtocolsQueryResponse> => {
const agentResponse = await this.agent.processDwnRequest({
const agentRequest = {
author : this.connectedDid,
messageOptions : request.message,
messageType : DwnInterfaceName.Protocols + DwnMethodName.Query,
target : this.connectedDid
});
target : request.from || this.connectedDid
};

const { reply: { entries, status } } = agentResponse;
// const protocols = entries as ProtocolsQueryReplyEntry[];
let agentResponse: DwnResponse;

if (request.from) {
agentResponse = await this.agent.sendDwnRequest(agentRequest);
} else {
agentResponse = await this.agent.processDwnRequest(agentRequest);
}

const { reply: { entries = [], status } } = agentResponse;

const protocols = entries.map((entry: ProtocolsQueryReplyEntry) => {
const metadata = { author: this.connectedDid, };
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export class Protocol {

async send(target: string) {
const { reply } = await this._agent.sendDwnRequest({
messageType : 'ProtocolsConfigure',
author : this._metadata.author,
messageCid : this._metadata.messageCid,
messageType : 'ProtocolsConfigure',
target : target,
messageCid : this._metadata.messageCid
});

return { status: reply.status };
Expand Down
44 changes: 1 addition & 43 deletions packages/api/src/tech-preview.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,4 @@
import type { DidIonCreateOptions, DidIonKeySet, DidService } from '@web5/dids';

import { DidIonMethod, utils as didUtils } from '@web5/dids';

/**
* Generates two key pairs used for authorization and encryption purposes
* when interfacing with DWNs. The IDs of these keys are referenced in the
* service object that includes the dwnUrls provided.
*/
export async function generateDwnConfiguration(options: {
dwnUrls: string[]
}): Promise<DidIonCreateOptions> {
const { dwnUrls } = options;

const signingKeyPair = await DidIonMethod.generateJwkKeyPair({
keyAlgorithm : 'Ed25519',
keyId : '#dwn-sig'
});

const encryptionKeyPair = await DidIonMethod.generateJwkKeyPair({
keyAlgorithm : 'Ed25519',
keyId : '#dwn-enc'
});

const keySet: DidIonKeySet = {
verificationMethodKeys: [
{ ...signingKeyPair, relationships: ['authentication'] },
{ ...encryptionKeyPair, relationships: ['keyAgreement'] }
]
};

const services: DidService[] = [{
'id' : '#dwn',
'type' : 'DecentralizedWebNode',
'serviceEndpoint' : {
'nodes' : dwnUrls,
'signingKeys' : ['#dwn-sig'],
'encryptionKeys' : ['#dwn-enc']
}
}];

return { keySet, services };
}
import * as didUtils from '@web5/dids/utils';

/**
* Dynamically selects up to 2 DWN endpoints that are provided
Expand Down
7 changes: 4 additions & 3 deletions packages/api/src/web5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Web5UserAgent } from '@web5/user-agent';
import { VcApi } from './vc-api.js';
import { DwnApi } from './dwn-api.js';
import { DidApi } from './did-api.js';
import { generateDwnConfiguration, getTechPreviewDwnEndpoints } from './tech-preview.js';
import { getTechPreviewDwnEndpoints } from './tech-preview.js';
import { DidIonMethod } from '@web5/dids';

/**
* Override defaults configured during the technical preview phase.
Expand Down Expand Up @@ -87,8 +88,8 @@ export class Web5 {

// If an existing identity is not found found, create a new one.
if (identities.length === 0) {
const dwnUrls = techPreview?.dwnEndpoints ?? await getTechPreviewDwnEndpoints();
const didOptions = await generateDwnConfiguration({ dwnUrls });
const serviceEndpointNodes = techPreview?.dwnEndpoints ?? await getTechPreviewDwnEndpoints();
const didOptions = await DidIonMethod.generateDwnOptions({ serviceEndpointNodes });
await userAgent.identityManager.create({
name : 'Default',
didMethod : 'ion',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TestManagedAgent } from '@web5/agent';
import { DidApi } from '../src/did-api.js';
import { TestUserAgent } from './utils/test-user-agent.js';

describe('web5.did', () => {
describe('DidApi', () => {
let did: DidApi;
let testAgent: TestManagedAgent;

Expand Down
Loading

0 comments on commit 53d193c

Please sign in to comment.