Skip to content

Commit

Permalink
mocked connect flow without sync
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Aug 6, 2024
1 parent 6b29f22 commit ded4c68
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 54 deletions.
2 changes: 0 additions & 2 deletions packages/api/src/dwn-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,6 @@ export class DwnApi {
}
});

console.log('got grant', delegatedGrant.recordId);

// set the required delegated grant and grantee DID for the read operation
agentRequest.messageParams.delegatedGrant = delegatedGrant;
agentRequest.granteeDid = this.impersonatorDid;
Expand Down
25 changes: 0 additions & 25 deletions packages/api/src/web5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,31 +356,6 @@ export class Web5 {

throw new Error(`Failed to process delegated grant: ${reply.status.detail}`);
}

// in lieu of sync being enabled, manually add the grants to the connectedDID's local DWN tenant
const { reply: connectedReply } = await userAgent.processDwnRequest({
author : connectedDid,
target : connectedDid,
messageType : DwnInterface.RecordsWrite,
rawMessage : grantMessage,
dataStream : new Blob([ data ])
});

if (connectedReply.status.code !== 202) {
// delete DID and Identity if grant processing fails
try {
await userAgent.did.delete({ didUri: connectedDid });
} catch(error: any) {
console.error(`Failed to delete DID: ${error.message}`);
}

try {
await userAgent.identity.delete({ didUri: connectedDid });
} catch(error: any) {
console.error(`Failed to delete Identity: ${error.message}`);
}
throw new Error(`Failed to process delegated grant for connectedDID: ${connectedReply.status.detail}`);
}
}

} catch (error:any) {
Expand Down
76 changes: 49 additions & 27 deletions packages/api/tests/web5.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Web5UserAgent } from '@web5/user-agent';
import { AgentIdentityApi, DwnInterface, DwnProtocolDefinition, DwnRegistrar, HdIdentityVault, PlatformAgentTestHarness } from '@web5/agent';

import { Web5 } from '../src/web5.js';
import { DwnInterfaceName, DwnMethodName, Time } from '@tbd54566975/dwn-sdk-js';
import { DwnInterfaceName, DwnMethodName, Jws, Time } from '@tbd54566975/dwn-sdk-js';
import { testDwnUrl } from './utils/test-config.js';

describe('Web5', () => {
Expand All @@ -27,6 +27,7 @@ describe('Web5', () => {
});

after(async () => {
sinon.restore();
await testHarness.clearStorage();
await testHarness.closeStorage();
});
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('Web5', () => {
}
};

const { reply: protocolConfigReply } = await testHarness.agent.dwn.processRequest({
const { reply: protocolConfigReply, message: protocolConfigureMessage } = await testHarness.agent.dwn.processRequest({
author : alice.did.uri,
target : alice.did.uri,
messageType : DwnInterface.ProtocolsConfigure,
Expand Down Expand Up @@ -154,6 +155,33 @@ describe('Web5', () => {
expect(did).to.equal(alice.did.uri);
expect(impersonatorDid).to.equal(app.uri);

// in lieu of sync, we will process the grants and protocol definition on the local connected agent
const { reply: localProtocolReply } = await web5.agent.processDwnRequest({
author : alice.did.uri,
target : alice.did.uri,
messageType : DwnInterface.ProtocolsConfigure,
rawMessage : protocolConfigureMessage,
});
expect(localProtocolReply.status.code).to.equal(202);

const { reply: grantWriteLocalReply } = await web5.agent.processDwnRequest({
author : alice.did.uri,
target : alice.did.uri,
messageType : DwnInterface.RecordsWrite,
rawMessage : writeGrant.recordsWrite.message,
dataStream : new Blob([ writeGrant.permissionGrantBytes ])
});
expect(grantWriteLocalReply.status.code).to.equal(202);

const { reply: grantReadLocalReply } = await web5.agent.processDwnRequest({
author : alice.did.uri,
target : alice.did.uri,
messageType : DwnInterface.RecordsWrite,
rawMessage : readGrant.recordsWrite.message,
dataStream : new Blob([ readGrant.permissionGrantBytes ])
});
expect(grantReadLocalReply.status.code).to.equal(202);

// use the grant to write a record
const writeResult = await web5.dwn.records.write({
data : 'Hello, world!',
Expand All @@ -162,31 +190,26 @@ describe('Web5', () => {
protocolPath : 'foo',
}
});
console.log('write record local reply', writeResult.status);
expect(writeResult.status.code).to.equal(202);

// // use the result to write to the testHarness
// const { reply: writeReply } = await testHarness.agent.dwn.processRequest({
// author: alice.did.uri,
// target: alice.did.uri,
// messageType: DwnInterface.RecordsWrite,
// rawMessage: writeResult.record.rawMessage as RecordsWriteMessage,
// });
// console.log('write record reply', writeReply.status);
// expect(writeReply.status.code).to.equal(202);

// // use the read grant to read the record
// const readResult = await web5.dwn.records.read({
// protocol: protocol.protocol,
// message: {
// filter: { recordId: writeResult.record.id }
// }
// });
// console.log('read record local reply', readResult.status);
// expect(readResult.status.code).to.equal(200);
// expect(readResult.record).to.exist;

}).timeout(20000); // long timeout for dht resolution
expect(writeResult.record).to.exist;
// test that the logical author is the connected DID and the signer is the impersonator DID
expect(writeResult.record.author).to.equal(did);
const writeSigner = Jws.getSignerDid(writeResult.record.authorization.signature.signatures[0]);
expect(writeSigner).to.equal(impersonatorDid);

const readResult = await web5.dwn.records.read({
protocol : protocol.protocol,
message : {
filter: { recordId: writeResult.record.id }
}
});
expect(readResult.status.code).to.equal(200);
expect(readResult.record).to.exist;
// test that the logical author is the connected DID and the signer is the impersonator DID
expect(readResult.record.author).to.equal(did);
const readSigner = Jws.getSignerDid(readResult.record.authorization.signature.signatures[0]);
expect(readSigner).to.equal(impersonatorDid);
});
});

describe('constructor', () => {
Expand Down Expand Up @@ -489,6 +512,5 @@ describe('Web5', () => {
expect(registerStub.callCount, 'registerTenant called').to.equal(2); // called twice, once for Agent DID once for Identity DID
});
});

});
});

0 comments on commit ded4c68

Please sign in to comment.