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

Sync fixes #103

Merged
merged 8 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 19 additions & 19 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions packages/dids/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tbd54566975/dids",
"version": "0.1.5",
"version": "0.1.6",
"description": "TBD DIDs library",
"type": "module",
"main": "./dist/cjs/main.cjs",
Expand Down Expand Up @@ -80,7 +80,7 @@
"dependencies": {
"@decentralized-identity/ion-tools": "1.0.7",
"@tbd54566975/crypto": "0.1.4",
"@tbd54566975/dwn-sdk-js": "0.0.32",
"@tbd54566975/dwn-sdk-js": "0.0.33",
"cross-fetch": "3.1.5"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/web5-agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tbd54566975/web5-agent",
"version": "0.1.4",
"version": "0.1.5",
"description": "Web5 Agent",
"type": "module",
"main": "./dist/cjs/main.cjs",
Expand Down Expand Up @@ -76,7 +76,7 @@
},
"dependencies": {
"readable-stream": "4.4.0",
"@tbd54566975/dwn-sdk-js": "0.0.32"
"@tbd54566975/dwn-sdk-js": "0.0.33"
},
"devDependencies": {
"@types/chai": "4.3.0",
Expand Down Expand Up @@ -109,5 +109,5 @@
},
"overrides": {
"socket.io-parser@>4.0.4 <4.2.3": "4.2.3"
}
}
}
4 changes: 2 additions & 2 deletions packages/web5-proxy-agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tbd54566975/web5-proxy-agent",
"version": "0.1.4",
"version": "0.1.5",
"description": "Web5 Proxy Agent",
"type": "module",
"main": "./dist/cjs/main.cjs",
Expand Down Expand Up @@ -75,7 +75,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@tbd54566975/web5-agent": "0.1.4"
"@tbd54566975/web5-agent": "0.1.5"
},
"devDependencies": {
"@types/chai": "4.3.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/web5-user-agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tbd54566975/web5-user-agent",
"version": "0.1.5",
"version": "0.1.6",
"description": "Web5 User Agent",
"type": "module",
"main": "./dist/cjs/main.cjs",
Expand Down Expand Up @@ -76,9 +76,9 @@
},
"dependencies": {
"@decentralized-identity/ion-tools": "1.0.7",
"@tbd54566975/dids": "0.1.5",
"@tbd54566975/dwn-sdk-js": "0.0.32",
"@tbd54566975/web5-agent": "0.1.4",
"@tbd54566975/dids": "0.1.6",
"@tbd54566975/dwn-sdk-js": "0.0.33",
"@tbd54566975/web5-agent": "0.1.5",
"abstract-level": "1.0.3",
"cross-fetch": "3.1.5",
"flat": "5.0.2",
Expand Down Expand Up @@ -119,5 +119,5 @@
},
"overrides": {
"socket.io-parser@>4.0.4 <4.2.3": "4.2.3"
}
}
}
26 changes: 20 additions & 6 deletions packages/web5-user-agent/src/sync-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ export class SyncApi implements SyncManager {
}

const dwnMessage = await this.#getDwnMessage(did, messageCid);
if (!dwnMessage) {
delOps.push({ type: 'del', key: key });
await this.setWatermark(did, dwnUrl, 'push', watermark);
await this.#addMessage(did, messageCid);

continue;
}

try {
const reply = await this.#dwnRpcClient.sendDwnRequest({
Expand Down Expand Up @@ -339,13 +346,16 @@ export class SyncApi implements SyncManager {
const result: MessagesGetReply = await this.#dwn.processMessage(author, messagesGet.toJSON());
const [ messageEntry ] = result.messages;

// absence of a messageEntry or message within messageEntry can happen because updating a Record actually creates another
// RecordsWrite with the same recordId. only the first and most recent RecordsWrite messages are kept for a given
// recordId. any in between are outright nuked from everywhere.
if (!messageEntry) {
throw new Error('TODO: figure out error message');
return undefined;
}

let { message } = messageEntry;
if (!message) {
throw new Error('TODO: message not found');
return undefined;
}

let dwnMessage: DwnMessage = { message };
Expand All @@ -367,12 +377,16 @@ export class SyncApi implements SyncManager {

const reply = await this.#dwn.processMessage(author, recordsRead.toJSON()) as RecordsReadReply;

if (reply.status.code >= 400) {
const { status: { code, detail } } = reply;
throw new Error(`(${code}) Failed to read data associated with record ${message['recordId']}. ${detail}}`);
} else {
// if the data no longer exists (aka 404), it's likely that a `RecordsDelete` took place.
// `RecordsDelete` keeps a `RecordsWrite` and just deletes the associated data, effectively acting as a "tombstone".
// We still need to _push_ this tombstone so that the `RecordsDelete` can be processed successfully.
// if 200, return the data. if 4xx ignore for the reason explained, if >= 5xx throw error
if (reply.status.code === 200) {
const dataBytes = await DataStream.toBytes(reply.record.data);
dwnMessage.data = new Blob([dataBytes]);
} else if (reply.status.code >= 500) {
const { status: { code, detail } } = reply;
throw new Error(`(${code}) Failed to read data associated with record ${message['recordId']}. ${detail}}`);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions packages/web5/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tbd54566975/web5",
"version": "0.7.5",
"version": "0.7.6",
"description": "SDK for accessing the features and capabilities of Web5",
"type": "module",
"main": "./dist/cjs/main.cjs",
Expand Down Expand Up @@ -81,11 +81,11 @@
"dependencies": {
"@decentralized-identity/ion-tools": "1.0.7",
"@tbd54566975/crypto": "0.1.4",
"@tbd54566975/dids": "0.1.5",
"@tbd54566975/dwn-sdk-js": "0.0.32",
"@tbd54566975/web5-agent": "0.1.4",
"@tbd54566975/web5-proxy-agent": "0.1.4",
"@tbd54566975/web5-user-agent": "0.1.5",
"@tbd54566975/dids": "0.1.6",
"@tbd54566975/dwn-sdk-js": "0.0.33",
"@tbd54566975/web5-agent": "0.1.5",
"@tbd54566975/web5-proxy-agent": "0.1.5",
"@tbd54566975/web5-user-agent": "0.1.6",
"level": "8.0.0",
"ms": "2.1.3",
"readable-web-to-node-stream": "3.0.2"
Expand Down Expand Up @@ -122,5 +122,5 @@
},
"overrides": {
"socket.io-parser@>4.0.4 <4.2.3": "4.2.3"
}
}
}
12 changes: 8 additions & 4 deletions packages/web5/tests/record.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ describe('Record', () => {
keyEncryptionInputs : [
{
derivationScheme : KeyDerivationScheme.Protocols,
publicKey : encryptionPublicKeyJwk as DwnPublicKeyJwk
publicKey : encryptionPublicKeyJwk as DwnPublicKeyJwk,
publicKeyId : recordEncryptionKeyId
},
{
derivationScheme : KeyDerivationScheme.Schemas,
publicKey : encryptionPublicKeyJwk as DwnPublicKeyJwk
publicKey : encryptionPublicKeyJwk as DwnPublicKeyJwk,
publicKeyId : recordEncryptionKeyId
},
]
};
Expand Down Expand Up @@ -908,11 +910,13 @@ describe('Record', () => {
keyEncryptionInputs : [
{
derivationScheme : KeyDerivationScheme.Protocols,
publicKey : encryptionPublicKeyJwk as DwnPublicKeyJwk
publicKey : encryptionPublicKeyJwk as DwnPublicKeyJwk,
publicKeyId : recordEncryptionKeyId
},
{
derivationScheme : KeyDerivationScheme.Schemas,
publicKey : encryptionPublicKeyJwk as DwnPublicKeyJwk
publicKey : encryptionPublicKeyJwk as DwnPublicKeyJwk,
publicKeyId : recordEncryptionKeyId
},
]
};
Expand Down