Skip to content

Commit

Permalink
Merge branch 'okstatus-feat' of https://github.com/Toheeb-Ojuolape/we…
Browse files Browse the repository at this point in the history
…b5-js into okstatus-feat
  • Loading branch information
Toheeb-Ojuolape committed Oct 24, 2024
2 parents e91082f + 47286d7 commit d99ddc9
Show file tree
Hide file tree
Showing 62 changed files with 2,101 additions and 214 deletions.
8 changes: 0 additions & 8 deletions .changeset/itchy-mayflies-camp.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changeset/nine-moose-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web5/api": minor
---

Identity information exclusively stored using Agent's DID as a tenant
5 changes: 5 additions & 0 deletions .changeset/old-buckets-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web5/api": patch
---

Allow records to be updated without storing.
8 changes: 0 additions & 8 deletions .changeset/rude-socks-pay.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changeset/slimy-mayflies-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web5/api": patch
---

Ensure protocolRole is maintained between query/read and subscribe/read.
5 changes: 5 additions & 0 deletions .changeset/smooth-weeks-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web5/api": patch
---

Added parameter for app display name for dynamic rendering in the wallet during web5 connect flow
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@changesets/cli": "^2.27.5",
"@npmcli/package-json": "5.0.0",
"@typescript-eslint/eslint-plugin": "7.9.0",
"@web5/dwn-server": "0.4.10",
"@web5/dwn-server": "0.6.0",
"audit-ci": "^7.0.1",
"eslint-plugin-mocha": "10.4.3",
"globals": "^13.24.0",
Expand Down Expand Up @@ -60,4 +60,4 @@
"rollup@>=4.0.0 <4.22.4": ">=4.22.4"
}
}
}
}
33 changes: 33 additions & 0 deletions packages/agent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
# @web5/agent

## 0.8.1

### Patch Changes

- [#961](https://github.com/TBD54566975/web5-js/pull/961) [`ca89770`](https://github.com/TBD54566975/web5-js/commit/ca89770943c538cfe62a3cfabcced483ef768e36) Thanks [@LiranCohen](https://github.com/LiranCohen)! - Fix error where `dwn-store` records were not being updated when marked as such.

## 0.8.0

### Minor Changes

- [#914](https://github.com/TBD54566975/web5-js/pull/914) [`bd1cb00`](https://github.com/TBD54566975/web5-js/commit/bd1cb00616029b0d18687b597e90d3b7c4dbeae1) Thanks [@LiranCohen](https://github.com/LiranCohen)! - Ability to Update a DID

- [#911](https://github.com/TBD54566975/web5-js/pull/911) [`c92159c`](https://github.com/TBD54566975/web5-js/commit/c92159c32a4f45aae73eb16199185af95a4b55e3) Thanks [@LiranCohen](https://github.com/LiranCohen)! - Simplify IdentityApi to be agent-focused and storing both the DID and IdentityMetadata under the agent's tenant.

### Patch Changes

- [#953](https://github.com/TBD54566975/web5-js/pull/953) [`3f39bf1`](https://github.com/TBD54566975/web5-js/commit/3f39bf1d14cf835a2959b5386a82ff7228d31e6c) Thanks [@LiranCohen](https://github.com/LiranCohen)! - Add ability to update DWN Endpoints

- [#945](https://github.com/TBD54566975/web5-js/pull/945) [`101f463`](https://github.com/TBD54566975/web5-js/commit/101f463eae4fa54383a8ffd28292755076ce50f4) Thanks [@thehenrytsai](https://github.com/thehenrytsai)! - Added parameter for app display name for dynamic rendering in the wallet during web5 connect flow

- [#958](https://github.com/TBD54566975/web5-js/pull/958) [`7072331`](https://github.com/TBD54566975/web5-js/commit/70723312d677c3f0aac960688613b45160528f90) Thanks [@LiranCohen](https://github.com/LiranCohen)! - Prevent SyncEngine from stopping completely during a sync failure, next interval will try again.

- [#954](https://github.com/TBD54566975/web5-js/pull/954) [`5120f6f`](https://github.com/TBD54566975/web5-js/commit/5120f6fd88e4883e1b8614b513f1aa826de7ce4c) Thanks [@LiranCohen](https://github.com/LiranCohen)! - Add `getProtocolRole` util

- [#910](https://github.com/TBD54566975/web5-js/pull/910) [`d3b4728`](https://github.com/TBD54566975/web5-js/commit/d3b4728fb2ad8a15fd81d6db1865bd7b8c762561) Thanks [@LiranCohen](https://github.com/LiranCohen)! - Enable EventEmitterStream

- [#956](https://github.com/TBD54566975/web5-js/pull/956) [`e7f5dfe`](https://github.com/TBD54566975/web5-js/commit/e7f5dfec5fc4cff686340a935442342a9383aa34) Thanks [@LiranCohen](https://github.com/LiranCohen)! - Add ability to update the Identity Metadata name field.

- Updated dependencies [[`bd1cb00`](https://github.com/TBD54566975/web5-js/commit/bd1cb00616029b0d18687b597e90d3b7c4dbeae1), [`3f39bf1`](https://github.com/TBD54566975/web5-js/commit/3f39bf1d14cf835a2959b5386a82ff7228d31e6c), [`bfa0417`](https://github.com/TBD54566975/web5-js/commit/bfa0417a2e9fc1300c5e604bea19b75ab1c73645)]:
- @web5/dids@1.2.0
- @web5/common@1.1.0
- @web5/crypto@1.0.6

## 0.7.4

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/agent/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web5/agent",
"version": "0.7.4",
"version": "0.8.1",
"type": "module",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down Expand Up @@ -71,7 +71,7 @@
"dependencies": {
"@noble/ciphers": "0.5.3",
"@scure/bip39": "1.2.2",
"@tbd54566975/dwn-sdk-js": "0.4.7",
"@tbd54566975/dwn-sdk-js": "0.5.1",
"@web5/common": "workspace:*",
"@web5/crypto": "workspace:*",
"@web5/dids": "workspace:*",
Expand Down Expand Up @@ -110,4 +110,4 @@
"sinon": "18.0.0",
"typescript": "5.1.6"
}
}
}
29 changes: 26 additions & 3 deletions packages/agent/src/agent-did-resolver-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DidResolutionResult, DidResolverCache, DidResolverCacheLevel, DidResolverCacheLevelParams } from '@web5/dids';
import { Web5PlatformAgent } from './types/agent.js';
import { logger } from '@web5/common';


/**
Expand Down Expand Up @@ -47,11 +48,33 @@ export class AgentDidResolverCache extends DidResolverCacheLevel implements DidR
const cachedResult = JSON.parse(str);
if (!this._resolving.has(did) && Date.now() >= cachedResult.ttlMillis) {
this._resolving.set(did, true);
if (this.agent.agentDid.uri === did || 'undefined' !== typeof await this.agent.identity.get({ didUri: did })) {

// if a DID is stored in the DID Store, then we don't want to evict it from the cache until we have a successful resolution
// upon a successful resolution, we will update both the storage and the cache with the newly resolved Document.
const storedDid = await this.agent.did.get({ didUri: did, tenant: this.agent.agentDid.uri });
if ('undefined' !== typeof storedDid) {
try {
const result = await this.agent.did.resolve(did);
if (!result.didResolutionMetadata.error) {
this.set(did, result);

// if the resolution was successful, update the stored DID with the new Document
if (!result.didResolutionMetadata.error && result.didDocument) {

const portableDid = {
...storedDid,
document : result.didDocument,
metadata : result.didDocumentMetadata,
};

try {
// this will throw an error if the DID is not managed by the agent, or there is no difference between the stored and resolved DID
// We don't publish the DID in this case, as it was received by the resolver.
await this.agent.did.update({ portableDid, tenant: this.agent.agentDid.uri, publish: false });
} catch(error: any) {
// if the error is not due to no changes detected, log the error
if (error.message && !error.message.includes('No changes detected, update aborted')) {
logger.error(`Error updating DID: ${error.message}`);
}
}
}
} finally {
this._resolving.delete(did);
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/src/bearer-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class BearerIdentity {
public async export(): Promise<PortableIdentity> {
return {
portableDid : await this.did.export(),
metadata : this.metadata
metadata : { ...this.metadata },
};
}
}
13 changes: 10 additions & 3 deletions packages/agent/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from './oidc.js';
import { pollWithTtl } from './utils.js';

import { Convert } from '@web5/common';
import { Convert, logger } from '@web5/common';
import { CryptoUtils } from '@web5/crypto';
import { DidJwk } from '@web5/dids';
import { DwnInterfaceName, DwnMethodName } from '@tbd54566975/dwn-sdk-js';
Expand All @@ -17,6 +17,7 @@ import { DwnInterfaceName, DwnMethodName } from '@tbd54566975/dwn-sdk-js';
* a did from a provider.
*/
async function initClient({
displayName,
connectServerUrl,
walletUri,
permissionRequests,
Expand Down Expand Up @@ -44,10 +45,12 @@ async function initClient({
const request = await Oidc.createAuthRequest({
client_id : clientDid.uri,
scope : 'openid did:jwk',
redirect_uri : callbackEndpoint,
// custom properties:
// code_challenge : codeChallengeBase64Url,
// code_challenge_method : 'S256',
permissionRequests : permissionRequests,
redirect_uri : callbackEndpoint,
displayName,
});

// Sign the Request Object using the Client DID's signing key.
Expand Down Expand Up @@ -91,6 +94,7 @@ async function initClient({

// a deeplink to a web5 compatible wallet. if the wallet scans this link it should receive
// a route to its web5 connect provider flow and the params of where to fetch the auth request.
logger.log(`Wallet URI: ${walletUri}`);
const generatedWalletUri = new URL(walletUri);
generatedWalletUri.searchParams.set('request_uri', parData.request_uri);
generatedWalletUri.searchParams.set(
Expand Down Expand Up @@ -133,7 +137,10 @@ async function initClient({
* a did from a provider.
*/
export type WalletConnectOptions = {
/** The URL of the intermediary server which relays messages between the client and provider */
/** The user friendly name of the client/app to be displayed when prompting end-user with permission requests. */
displayName: string;

/** The URL of the intermediary server which relays messages between the client and provider. */
connectServerUrl: string;

/**
Expand Down
55 changes: 54 additions & 1 deletion packages/agent/src/did-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import type {
DidResolverCache,
} from '@web5/dids';

import { BearerDid, Did, UniversalResolver } from '@web5/dids';
import { BearerDid, Did, DidDht, UniversalResolver } from '@web5/dids';

import type { AgentDataStore } from './store-data.js';
import type { AgentKeyManager } from './types/key-manager.js';
import type { ResponseStatus, Web5PlatformAgent } from './types/agent.js';

import { InMemoryDidStore } from './store-did.js';
import { AgentDidResolverCache } from './agent-did-resolver-cache.js';
import { canonicalize } from '@web5/crypto';

export enum DidInterface {
Create = 'Create',
Expand Down Expand Up @@ -256,6 +257,58 @@ export class AgentDidApi<TKeyManager extends AgentKeyManager = AgentKeyManager>
return verificationMethod;
}

public async update({ tenant, portableDid, publish = true }: {
tenant?: string;
portableDid: PortableDid;
publish?: boolean;
}): Promise<BearerDid> {

// Check if the DID exists in the store.
const existingDid = await this.get({ didUri: portableDid.uri, tenant: tenant ?? portableDid.uri });
if (!existingDid) {
throw new Error(`AgentDidApi: Could not update, DID not found: ${portableDid.uri}`);
}

// If the document has not changed, abort the update.
if (canonicalize(portableDid.document) === canonicalize(existingDid.document)) {
throw new Error('AgentDidApi: No changes detected, update aborted');
}

// If private keys are present in the PortableDid, import the key material into the Agent's key
// manager. Validate that the key material for every verification method in the DID document is
// present in the key manager. If no keys are present, this will fail.
// NOTE: We currently do not delete the previous keys from the document.
// TODO: Add support for deleting the keys no longer present in the document.
const bearerDid = await BearerDid.import({ keyManager: this.agent.keyManager, portableDid });

// Only the DID URI, document, and metadata are stored in the Agent's DID store.
const { uri, document, metadata } = bearerDid;
const portableDidWithoutKeys: PortableDid = { uri, document, metadata };

// pre-populate the resolution cache with the document and metadata
await this.cache.set(uri, { didDocument: document, didResolutionMetadata: { }, didDocumentMetadata: metadata });

await this._store.set({
id : uri,
data : portableDidWithoutKeys,
agent : this.agent,
tenant : tenant ?? uri,
updateExisting : true,
useCache : true
});

if (publish) {
const parsedDid = Did.parse(uri);
// currently only supporting DHT as a publishable method.
// TODO: abstract this into the didMethod class so that other publishable methods can be supported.
if (parsedDid && parsedDid.method === 'dht') {
await DidDht.publish({ did: bearerDid });
}
}

return bearerDid;
}

public async import({ portableDid, tenant }: {
portableDid: PortableDid;
tenant?: string;
Expand Down
Loading

0 comments on commit d99ddc9

Please sign in to comment.