Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade api and agent #500

Merged
merged 9 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/few-oranges-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@web5/identity-agent": patch
"@web5/proxy-agent": patch
"@web5/user-agent": patch
"@web5/agent": patch
"@web5/api": patch
---

Upgrade DWN SDK with newest features

- remove `Permissions` interface and replace permissions with a first-class protocol representing it
- adding `RecordsTags` functionality
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@changesets/cli": "^2.27.1",
"@npmcli/package-json": "5.0.0",
"@typescript-eslint/eslint-plugin": "6.4.0",
"@web5/dwn-server": "0.1.17",
"@web5/dwn-server": "0.2.1",
"eslint-plugin-mocha": "10.1.0",
"npkill": "0.11.3"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
"dependencies": {
"@noble/ciphers": "0.4.1",
"@scure/bip39": "1.2.2",
"@tbd54566975/dwn-sdk-js": "0.2.22",
"@tbd54566975/dwn-sdk-js": "0.3.1",
"@web5/common": "1.0.0",
"@web5/crypto": "1.0.0",
"@web5/dids": "1.0.0",
"@web5/dids": "1.0.1",
"abstract-level": "1.0.4",
"ed25519-keygen": "0.4.11",
"level": "8.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@
"@web5/agent": "0.3.1",
"@web5/common": "1.0.0",
"@web5/crypto": "1.0.0",
"@web5/dids": "1.0.0",
"@web5/dids": "1.0.1",
"@web5/user-agent": "0.3.1"
},
"devDependencies": {
"@playwright/test": "1.40.1",
"@tbd54566975/dwn-sdk-js": "0.2.22",
"@tbd54566975/dwn-sdk-js": "0.3.1",
"@types/chai": "4.3.6",
"@types/eslint": "8.44.2",
"@types/mocha": "10.0.1",
Expand Down
19 changes: 16 additions & 3 deletions packages/api/src/record.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Readable } from '@web5/common';

Check warning on line 1 in packages/api/src/record.ts

View workflow job for this annotation

GitHub Actions / tbdocs-reporter

extractor: typedoc:missing-docs

Record.tags (GetSignature) does not have any documentation.
import type {
Web5Agent,
DwnMessage,
Expand All @@ -9,7 +9,7 @@
} from '@web5/agent';

import { DwnInterface } from '@web5/agent';
import { Convert, NodeStream, removeUndefinedProperties, Stream } from '@web5/common';
import { Convert, isEmptyObject, NodeStream, removeUndefinedProperties, Stream } from '@web5/common';

import { dataToBlob, SendCache } from './utils.js';

Expand Down Expand Up @@ -105,6 +105,9 @@

/** The published status of the record. */
published?: DwnMessageDescriptor[DwnInterface.RecordsWrite]['published'];

/** The tags associated with the updated record */
tags?: DwnMessageDescriptor[DwnInterface.RecordsWrite]['tags'];
}

/**
Expand Down Expand Up @@ -247,6 +250,8 @@
/** Record's published status (true/false) */
get published() { return this._descriptor.published; }

get tags() { return this._descriptor.tags; }

/**
* Returns a copy of the raw `RecordsWriteMessage` that was used to create the current `Record` instance.
*/
Expand Down Expand Up @@ -540,7 +545,8 @@
published : this.published,
recipient : this.recipient,
recordId : this.id,
schema : this.schema
schema : this.schema,
tags : this.tags,
};
}

Expand Down Expand Up @@ -586,6 +592,13 @@
recordId : this._recordId
};

// NOTE: The original Record's tags are copied to the update message, so that the tags are not lost.
// However if a user passes new tags in the `RecordUpdateParams` object, they will overwrite the original tags.
// If the updated tag object is empty or set to null, we remove the tags property to avoid schema validation errors in the DWN SDK.
if (isEmptyObject(updateMessage.tags) || updateMessage.tags === null) {
delete updateMessage.tags;
}

let dataBlob: Blob;
if (data !== undefined) {
// If `data` is being updated then `dataCid` and `dataSize` must be undefined and the `data`
Expand All @@ -598,7 +611,7 @@

// Throw an error if an attempt is made to modify immutable properties.
// Note: `data` and `dateModified` have already been handled.
const mutableDescriptorProperties = new Set(['data', 'dataCid', 'dataSize', 'datePublished', 'messageTimestamp', 'published']);
const mutableDescriptorProperties = new Set(['data', 'dataCid', 'dataSize', 'datePublished', 'messageTimestamp', 'published', 'tags']);
Record.verifyPermittedMutation(Object.keys(params), mutableDescriptorProperties);

// If `published` is set to false, ensure that `datePublished` is undefined. Otherwise, DWN SDK's schema validation
Expand Down
30 changes: 28 additions & 2 deletions packages/api/tests/dwn-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ describe('DwnApi', () => {
const response = await dwnAlice.protocols.query({
from : bobDid.uri,
message : {
permissionsGrantId : 'bafyreiduimprbncdo2oruvjrvmfmwuyz4xx3d5biegqd2qntlryvuuosem',
filter : {
permissionGrantId : 'bafyreiduimprbncdo2oruvjrvmfmwuyz4xx3d5biegqd2qntlryvuuosem',
filter : {
protocol: 'https://doesnotexist.com/protocol'
}
}
Expand Down Expand Up @@ -238,6 +238,32 @@ describe('DwnApi', () => {
expect(await result.record?.data.text()).to.equal(dataString);
});

it('creates a record with tags', async () => {
const result = await dwnAlice.records.create({
data : 'some data',
message : {
schema : 'foo/bar',
dataFormat : 'text/plain',
tags : {
foo : 'bar',
count : 2,
bool : true
}
}
});

expect(result.status.code).to.equal(202);
expect(result.status.detail).to.equal('Accepted');
expect(result.record).to.exist;
expect(result.record?.tags).to.exist;
expect(result.record?.tags).to.deep.equal({
foo : 'bar',
count : 2,
bool : true
});

});

it('creates a record with JSON data', async () => {
const dataJson = { hello: 'world!'};
const result = await dwnAlice.records.create({
Expand Down
88 changes: 88 additions & 0 deletions packages/api/tests/record.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,94 @@ describe('Record', () => {
expect(error.message).to.include('is an immutable property. Its value cannot be changed.');
}
});

it('should override tags on update', async () => {
// create a record with tags
const { status, record } = await dwnAlice.records.write({
data : 'Hello, world!',
message : {
schema : 'foo/bar',
dataFormat : 'text/plain',
tags : {
tag1 : 'value1',
tag2 : 'value2'
}
}
});

expect(status.code).to.equal(202);
expect(record).to.not.be.undefined;
expect(await record.data.text()).to.equal('Hello, world!');
expect(record.tags).to.deep.equal({ tag1: 'value1', tag2: 'value2'});

// if you do not pass any tags they remain unchanged
const updateResultWithoutTags = await record!.update({
data: 'hi',
});

expect(updateResultWithoutTags.status.code).to.equal(202);
expect(record.tags).to.deep.equal({ tag1: 'value1', tag2: 'value2'}); // unchanged
expect(await record.data.text()).to.equal('hi');

// if you modify the tags they override the existing tags
const updateResultWithTags = await record!.update({
tags: {
tag1 : 'value3',
tag3 : 'value4'
}
});

expect(updateResultWithTags.status.code).to.equal(202);
expect(record.tags).to.deep.equal({ tag1: 'value3', tag3: 'value4'}); // changed to updated tags
expect(await record.data.text()).to.equal('hi');
});

it('should remove tags on update if tags are set to an empty object or null', async () => {
// create a record with tags
const { status, record } = await dwnAlice.records.write({
data : 'Hello, world!',
message : {
schema : 'foo/bar',
dataFormat : 'text/plain',
tags : {
tag1 : 'value1',
tag2 : 'value2'
}
}
});

expect(status.code).to.equal(202);
expect(record).to.not.be.undefined;
expect(await record.data.text()).to.equal('Hello, world!');
expect(record.tags).to.deep.equal({ tag1: 'value1', tag2: 'value2'});

// if you use an empty tags object it removes the tags
const updateResultWithEmptyTags = await record!.update({
tags: {}
});

expect(updateResultWithEmptyTags.status.code).to.equal(202);
expect(record.tags).to.not.exist; // removed

// add tags to the record again
const updateResultWithTags = await record!.update({
tags: {
tag1 : 'value3',
tag3 : 'value4'
}
});

expect(updateResultWithTags.status.code).to.equal(202);
expect(record.tags).to.deep.equal({ tag1: 'value3', tag3: 'value4'}); // added tags

// if you use null it removes the tags
const updateResultWithNullTags = await record!.update({
tags: null
});

expect(updateResultWithNullTags.status.code).to.equal(202);
expect(record.tags).to.not.exist; // removed
});
});

describe('store()', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-env/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ version: "3.98"
services:
dwn-server:
container_name: dwn-server
image: ghcr.io/tbd54566975/dwn-server:dwn-sdk-0.2.22
image: ghcr.io/tbd54566975/dwn-server:dwn-sdk-0.3.1
ports:
- "3000:3000"
2 changes: 1 addition & 1 deletion packages/identity-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@web5/agent": "0.3.1",
"@web5/common": "1.0.0",
"@web5/crypto": "1.0.0",
"@web5/dids": "1.0.0"
"@web5/dids": "1.0.1"
},
"devDependencies": {
"@playwright/test": "1.40.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/proxy-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@web5/agent": "0.3.1",
"@web5/common": "1.0.0",
"@web5/crypto": "1.0.0",
"@web5/dids": "1.0.0"
"@web5/dids": "1.0.1"
},
"devDependencies": {
"@playwright/test": "1.40.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/user-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@web5/agent": "0.3.1",
"@web5/common": "1.0.0",
"@web5/crypto": "1.0.0",
"@web5/dids": "1.0.0"
"@web5/dids": "1.0.1"
},
"devDependencies": {
"@playwright/test": "1.40.1",
Expand Down
Loading
Loading