Skip to content

Commit

Permalink
Refactor identity-agent and add docs
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Mar 15, 2024
1 parent 140712c commit 90b6048
Show file tree
Hide file tree
Showing 12 changed files with 1,057 additions and 934 deletions.
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
},
"devDependencies": {
"@playwright/test": "1.40.1",
"@tbd54566975/dwn-sdk-js": "0.2.18",
"@types/chai": "4.3.6",
"@types/eslint": "8.44.2",
"@types/mocha": "10.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/web5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class Web5 {

// Initialize, if necessary, and start the agent.
if (await userAgent.firstLaunch()) {
await userAgent.initialize({ password, recoveryPhrase });
recoveryPhrase = await userAgent.initialize({ password, recoveryPhrase });
}
await userAgent.start({ password });

Expand Down
95 changes: 83 additions & 12 deletions packages/api/tests/web5.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ describe('Web5', () => {
await testHarness.closeStorage();
});

describe('connect()', () => {
it('accepts an externally created DID', async () => {
const testIdentity = await testHarness.createIdentity({
name : 'Test',
testDwnUrls : ['https://dwn.example.com']
});

// Call connect() with the custom agent.
const { web5, did } = await Web5.connect({
agent : testHarness.agent,
connectedDid : testIdentity.did.uri
});

expect(did).to.exist;
expect(web5).to.exist;
});
});

describe('constructor', () => {
it('instantiates Web5 API with provided Web5Agent and connectedDid', async () => {
// Create a new Identity.
Expand All @@ -41,23 +59,73 @@ describe('Web5', () => {
expect(web5).to.have.property('dwn');
expect(web5).to.have.property('vc');
});

it('supports a single agent with multiple Web5 instances and different DIDs', async () => {
// Create two identities, each of which is stored in a new tenant.
const careerIdentity = await testHarness.agent.identity.create({
metadata : { name: 'Social' },
didMethod : 'jwk'
});
const socialIdentity = await testHarness.agent.identity.create({
metadata : { name: 'Social' },
didMethod : 'jwk'
});

// Instantiate a Web5 instance with the "Career" Identity, write a record, and verify the result.
const web5Career = new Web5({ agent: testHarness.agent, connectedDid: careerIdentity.did.uri });
expect(web5Career).to.exist;

// Instantiate a Web5 instance with the "Social" Identity, write a record, and verify the result.
const web5Social = new Web5({ agent: testHarness.agent, connectedDid: socialIdentity.did.uri });
expect(web5Social).to.exist;
});
});

describe('connect()', () => {
it('accepts an externally created DID', async () => {
const testIdentity = await testHarness.createIdentity({
name : 'Test',
testDwnUrls : ['https://dwn.example.com']
describe('scenarios', () => {
it('writes records with multiple identities under management', async () => {
// First launch and initialization.
await testHarness.agent.initialize({ password: 'test' });

// Start the Agent, which will decrypt and load the Agent's DID from the vault.
await testHarness.agent.start({ password: 'test' });

// Create two identities, each of which is stored in a new tenant.
const careerIdentity = await testHarness.agent.identity.create({
metadata : { name: 'Social' },
didMethod : 'jwk'
});
const socialIdentity = await testHarness.agent.identity.create({
metadata : { name: 'Social' },
didMethod : 'jwk'
});

// Call connect() with the custom agent.
const { web5, did } = await Web5.connect({
agent : testHarness.agent,
connectedDid : testIdentity.did.uri
// Instantiate a Web5 instance with the "Career" Identity, write a record, and verify the result.
const web5Career = new Web5({ agent: testHarness.agent, connectedDid: careerIdentity.did.uri });
const careerResult = await web5Career.dwn.records.write({
data : 'Hello, world!',
message : {
schema : 'foo/bar',
dataFormat : 'text/plain'
}
});
expect(careerResult.status.code).to.equal(202);
expect(careerResult.record).to.exist;
expect(careerResult.record?.author).to.equal(careerIdentity.did.uri);
expect(await careerResult.record?.data.text()).to.equal('Hello, world!');

expect(did).to.exist;
expect(web5).to.exist;
// Instantiate a Web5 instance with the "Social" Identity, write a record, and verify the result.
const web5Social = new Web5({ agent: testHarness.agent, connectedDid: socialIdentity.did.uri });
const socialResult = await web5Social.dwn.records.write({
data : 'Hello, everyone!',
message : {
schema : 'foo/bar',
dataFormat : 'text/plain'
}
});
expect(socialResult.status.code).to.equal(202);
expect(socialResult.record).to.exist;
expect(socialResult.record?.author).to.equal(socialIdentity.did.uri);
expect(await socialResult.record?.data.text()).to.equal('Hello, everyone!');
});
});
});
Expand All @@ -69,10 +137,13 @@ describe('Web5', () => {
keyDerivationWorkFactor : 1,
store : new MemoryStore<string, string>()
});
const { web5 } = await Web5.connect({ agentVault });
const { web5, recoveryPhrase } = await Web5.connect({ agentVault });

expect(web5).to.exist;
expect(web5.agent).to.be.instanceOf(Web5UserAgent);
// Verify recovery phrase is a 12-word string.
expect(recoveryPhrase).to.be.a('string');
expect(recoveryPhrase.split(' ')).to.have.lengthOf(12);
});
});
});
5 changes: 4 additions & 1 deletion packages/identity-agent/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"preLaunchTask": "build tests",
"skipFiles": [
"<node_internals>/**"
]
],
"env": {
"TEST_DWN_URL": "http://localhost:3000"
}
}
]
}
Loading

0 comments on commit 90b6048

Please sign in to comment.