Skip to content

Commit

Permalink
Improve semantics of Record class
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Dec 9, 2023
1 parent 23d86b7 commit 33f666a
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 273 deletions.
19 changes: 8 additions & 11 deletions packages/api/src/dwn-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,6 @@ export class DwnApi {
createFrom: async (request: RecordsCreateFromRequest): Promise<RecordsWriteResponse> => {
const { author: inheritedAuthor, ...inheritedProperties } = request.record.toJSON();

// Remove target from inherited properties since target is being explicitly defined in method parameters.
delete inheritedProperties.target;

// If `data` is being updated then `dataCid` and `dataSize` must not be present.
if (request.data !== undefined) {
delete inheritedProperties.dataCid;
Expand Down Expand Up @@ -372,11 +369,11 @@ export class DwnApi {
*/
author : RecordsWrite.getAuthor(entry),
/**
* Set the `target` DID to currently connected DID so that subsequent calls to
* 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.
*/
target : 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
Expand Down Expand Up @@ -432,11 +429,11 @@ export class DwnApi {
*/
author : RecordsWrite.getAuthor(responseRecord),
/**
* Set the `target` DID to currently connected DID so that subsequent calls to
* 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.
*/
target : 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
Expand Down Expand Up @@ -489,14 +486,14 @@ export class DwnApi {
* Assume the author is the connected DID since the record was just written to the
* local DWN.
*/
author : this.connectedDid,
encodedData : dataBlob,
author : this.connectedDid,
/**
* Set the `target` DID to currently connected DID so that subsequent calls to
* 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.
*/
target : this.connectedDid,
connectedDid : this.connectedDid,
encodedData : dataBlob,
...responseMessage,
};

Expand Down
53 changes: 27 additions & 26 deletions packages/api/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { dataToBlob } from './utils.js';
*/
export type RecordOptions = RecordsWriteMessage & {
author: string;
target: string;
connectedDid: string;
encodedData?: string | Blob;
data?: Readable | ReadableStream;
remoteOrigin?: string;
Expand All @@ -37,7 +37,6 @@ export type RecordModel = RecordsWriteDescriptor
& {
author: string;
recordId?: string;
target: string;
}

/**
Expand Down Expand Up @@ -68,29 +67,29 @@ export type RecordUpdateOptions = {
* @beta
*/
export class Record implements RecordModel {
// mutable properties

/** Record's author DID */
author: string;

/** Record's target DID */
target: string;

// Record instance metadata.
private _agent: Web5Agent;
private _connectedDid: string;
private _encodedData?: Blob;
private _readableStream?: Readable;
private _remoteOrigin?: string;

// Private variables for DWN `RecordsWrite` message properties.
private _author: string;
private _attestation?: RecordsWriteMessage['attestation'];
private _contextId?: string;
private _descriptor: RecordsWriteDescriptor;
private _encodedData?: Blob;
private _encryption?: RecordsWriteMessage['encryption'];
private _readableStream?: Readable;
private _recordId: string;
private _remoteOrigin?: string;

// Immutable DWN Record properties.
// Getters for immutable DWN Record properties.

/** Record's signatures attestation */
get attestation(): RecordsWriteMessage['attestation'] { return this._attestation; }

/** DID that signed the record. */
get author(): string { return this._author; }

/** Record's context ID */
get contextId() { return this._contextId; }

Expand Down Expand Up @@ -127,7 +126,7 @@ export class Record implements RecordModel {
/** Record's schema */
get schema() { return this._descriptor.schema; }

// Mutable DWN Record properties.
// Getters for mutable DWN Record properties.

/** Record's CID */
get dataCid() { return this._descriptor.dataCid; }
Expand All @@ -150,10 +149,13 @@ export class Record implements RecordModel {
constructor(agent: Web5Agent, options: RecordOptions) {
this._agent = agent;

/** Store the target and author DIDs that were used to create the message to use for subsequent
* updates, reads, etc. */
this.author = options.author;
this.target = options.target;
/** Store the author DID that originally signed the message as a convenience for developers, so
* that they don't have to decode the signer's DID from the JWS. */
this._author = options.author;

/** Store the currently `connectedDid` so that subsequent message signing is done with the
* connected DID's keys and DWN requests target the connected DID's DWN. */
this._connectedDid = options.connectedDid;

/** If the record was queried or read from a remote DWN, the `remoteOrigin` DID will be
* defined. This value is used to send subsequent read requests to the same remote DWN in the
Expand Down Expand Up @@ -271,7 +273,7 @@ export class Record implements RecordModel {
// A. ...a remote DWN if the record was originally queried from a remote DWN.
await self.readRecordData({ target: self._remoteOrigin, isRemote: true }) :
// B. ...a local DWN if the record was originally queried from the local DWN.
await self.readRecordData({ target: self.target, isRemote: false });
await self.readRecordData({ target: self._connectedDid, isRemote: false });
}

if (!self._readableStream) {
Expand Down Expand Up @@ -305,7 +307,7 @@ export class Record implements RecordModel {
async send(target: string): Promise<ResponseStatus> {
const { reply: { status } } = await this._agent.sendDwnRequest({
messageType : DwnInterfaceName.Records + DwnMethodName.Write,
author : this.author,
author : this._connectedDid,
dataStream : await this.data.blob(),
target : target,
messageOptions : this.toJSON(),
Expand Down Expand Up @@ -338,8 +340,7 @@ export class Record implements RecordModel {
published : this.published,
recipient : this.recipient,
recordId : this.id,
schema : this.schema,
target : this.target,
schema : this.schema
};
}

Expand Down Expand Up @@ -415,11 +416,11 @@ export class Record implements RecordModel {
};

const agentResponse = await this._agent.processDwnRequest({
author : this.author,
author : this._connectedDid,
dataStream : dataBlob,
messageOptions,
messageType : DwnInterfaceName.Records + DwnMethodName.Write,
target : this.target,
target : this._connectedDid,
});

const { message, reply: { status } } = agentResponse;
Expand Down Expand Up @@ -456,7 +457,7 @@ export class Record implements RecordModel {
*/
private async readRecordData({ target, isRemote }: { target: string, isRemote: boolean }) {
const readRequest = {
author : this.author,
author : this._connectedDid,
messageOptions : { filter: { recordId: this.id } },
messageType : DwnInterfaceName.Records + DwnMethodName.Read,
target,
Expand Down
Loading

0 comments on commit 33f666a

Please sign in to comment.