Skip to content

Commit

Permalink
fix using delegate grant
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Aug 29, 2024
1 parent eb8508c commit 74440d7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
30 changes: 19 additions & 11 deletions packages/api/src/dwn-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,20 +663,22 @@ export class DwnApi {
* Extract the `author` DID from the record entry since records may be signed by the
* tenant owner or any other entity.
*/
author : getRecordAuthor(entry),
author : getRecordAuthor(entry),
/**
* Set the `connectedDid` to currently connected DID so that subsequent calls to
* {@link Record} instance methods, such as `record.update()` are executed on the
* local DWN even if the record was returned by a query of a remote DWN.
*/
connectedDid : this.connectedDid,
connectedDid : this.connectedDid,
/**
* If the record was returned by a query of a remote DWN, set the `remoteOrigin` to
* the DID of the DWN that returned the record. The `remoteOrigin` property will be used
* to determine which DWN to send subsequent read requests to in the event the data
* payload exceeds the threshold for being returned with queries.
*/
remoteOrigin : request.from,
remoteOrigin : request.from,
cachedPermissions : this.cachedPermissionsApi,
delegateDid : this.delegateDid,
...entry as DwnMessage[DwnInterface.RecordsWrite]
};
const record = new Record(this.agent, recordOptions);
Expand Down Expand Up @@ -737,20 +739,22 @@ export class DwnApi {
* Extract the `author` DID from the record since records may be signed by the
* tenant owner or any other entity.
*/
author : getRecordAuthor(responseRecord),
author : getRecordAuthor(responseRecord),
/**
* Set the `connectedDid` to currently connected DID so that subsequent calls to
* {@link Record} instance methods, such as `record.update()` are executed on the
* local DWN even if the record was read from a remote DWN.
*/
connectedDid : this.connectedDid,
connectedDid : this.connectedDid,
/**
* If the record was returned by reading from a remote DWN, set the `remoteOrigin` to
* the DID of the DWN that returned the record. The `remoteOrigin` property will be used
* to determine which DWN to send subsequent read requests to in the event the data
* payload must be read again (e.g., if the data stream is consumed).
*/
remoteOrigin : request.from,
remoteOrigin : request.from,
cachedPermissions : this.cachedPermissionsApi,
delegateDid : this.delegateDid,
...responseRecord,
};

Expand Down Expand Up @@ -786,8 +790,10 @@ export class DwnApi {
* The handler to process the subscription events.
*/
subscriptionHandler: SubscriptionUtil.recordSubscriptionHandler({
agent : this.agent,
connectedDid : this.connectedDid,
agent : this.agent,
connectedDid : this.connectedDid,
delegateDid : this.delegateDid,
cachedPermissions : this.cachedPermissionsApi,
request
})
};
Expand Down Expand Up @@ -855,14 +861,16 @@ export class DwnApi {
* Assume the author is the connected DID since the record was just written to the
* local DWN.
*/
author : this.connectedDid,
author : this.connectedDid,
/**
* Set the `connectedDid` to currently connected DID so that subsequent calls to
* {@link Record} instance methods, such as `record.update()` are executed on the
* local DWN.
*/
connectedDid : this.connectedDid,
encodedData : dataBlob,
connectedDid : this.connectedDid,
encodedData : dataBlob,
cachedPermissions : this.cachedPermissionsApi,
delegateDid : this.delegateDid,
...responseMessage,
};

Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export type RecordOptions = DwnMessage[DwnInterface.RecordsWrite | DwnInterface.
/** The optional DID that will sign the records on behalf of the connectedDid */
delegateDid?: string;

/** cached permission API for fast grant lookup */
cachedPermissions?: CachedPermissions;

/** The data of the record, either as a Base64 URL encoded string or a Blob. */
encodedData?: string | Blob;

Expand Down
12 changes: 9 additions & 3 deletions packages/api/src/subscription-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DwnRecordSubscriptionHandler, getRecordAuthor, Web5Agent } from '@web5/agent';
import { CachedPermissions, DwnRecordSubscriptionHandler, getRecordAuthor, Web5Agent } from '@web5/agent';
import { RecordsSubscribeRequest } from './dwn-api.js';
import { Record } from './record.js';

Expand All @@ -9,9 +9,11 @@ export class SubscriptionUtil {
/**
* Creates a record subscription handler that can be used to process incoming {Record} messages.
*/
static recordSubscriptionHandler({ agent, connectedDid, request }:{
static recordSubscriptionHandler({ agent, connectedDid, request, delegateDid, cachedPermissions }:{
agent: Web5Agent;
connectedDid: string;
delegateDid?: string;
cachedPermissions: CachedPermissions;
request: RecordsSubscribeRequest;
}): DwnRecordSubscriptionHandler {
const { subscriptionHandler, from: remoteOrigin } = request;
Expand All @@ -26,7 +28,11 @@ export class SubscriptionUtil {
initialWrite
};

const record = new Record(agent, { ...message, ...recordOptions });
const record = new Record(agent, {
...message, ...recordOptions,
cachedPermissions : cachedPermissions,
delegateDid : delegateDid,
});
subscriptionHandler(record);
};
}
Expand Down

0 comments on commit 74440d7

Please sign in to comment.