Skip to content

Commit

Permalink
Time and timestamp utility refactoring (#593)
Browse files Browse the repository at this point in the history
1. Exported all time utilities for downstream external usage
2. Renamed `getCurrentTimeInHighPrecision()` -> `getCurrentTimestamp()` for dev friendliness
3. Removed redundant `createDateString()`
4. Grouped all time utilities into a class
5. Replaced all direct usage of temporal library
  • Loading branch information
thehenrytsai authored Nov 2, 2023
1 parent 7d9caec commit d79901a
Show file tree
Hide file tree
Showing 31 changed files with 225 additions and 225 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export { Protocols } from './utils/protocols.js';
export { ProtocolsConfigure, ProtocolsConfigureOptions } from './interfaces/protocols-configure.js';
export { ProtocolsQuery, ProtocolsQueryOptions } from './interfaces/protocols-query.js';
export { Records } from './utils/records.js';
export { createTimestamp } from './utils/time.js';
export { RecordsDelete, RecordsDeleteOptions } from './interfaces/records-delete.js';
export { RecordsRead, RecordsReadOptions } from './interfaces/records-read.js';
export { Secp256k1 } from './utils/secp256k1.js';
export { Signer } from './types/signer.js';
export { SortOrder } from './types/message-types.js';
export { Time } from './utils/time.js';

// store interfaces
export { DataStoreLevel } from './store/data-store-level.js';
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/events-get.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Signer } from '../types/signer.js';
import type { EventsGetDescriptor, EventsGetMessage } from '../types/event-types.js';

import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';

export type EventsGetOptions = {
watermark?: string;
Expand All @@ -16,7 +16,7 @@ export class EventsGet extends Message<EventsGetMessage> {
public static async parse(message: EventsGetMessage): Promise<EventsGet> {
Message.validateJsonSchema(message);
await validateMessageSignatureIntegrity(message.authorization.signature, message.descriptor);
validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.messageTimestamp);

return new EventsGet(message);
}
Expand All @@ -25,7 +25,7 @@ export class EventsGet extends Message<EventsGetMessage> {
const descriptor: EventsGetDescriptor = {
interface : DwnInterfaceName.Events,
method : DwnMethodName.Get,
messageTimestamp : options.messageTimestamp ?? getCurrentTimeInHighPrecision(),
messageTimestamp : options.messageTimestamp ?? Time.getCurrentTimestamp(),
};

if (options.watermark) {
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/messages-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { Signer } from '../types/signer.js';
import type { MessagesGetDescriptor, MessagesGetMessage } from '../types/messages-types.js';

import { Cid } from '../utils/cid.js';
import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../index.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';

export type MessagesGetOptions = {
messageCids: string[];
Expand All @@ -19,7 +19,7 @@ export class MessagesGet extends Message<MessagesGetMessage> {
this.validateMessageCids(message.descriptor.messageCids);

await validateMessageSignatureIntegrity(message.authorization.signature, message.descriptor);
validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.messageTimestamp);

return new MessagesGet(message);
}
Expand All @@ -29,7 +29,7 @@ export class MessagesGet extends Message<MessagesGetMessage> {
interface : DwnInterfaceName.Messages,
method : DwnMethodName.Get,
messageCids : options.messageCids,
messageTimestamp : options?.messageTimestamp ?? getCurrentTimeInHighPrecision(),
messageTimestamp : options?.messageTimestamp ?? Time.getCurrentTimestamp(),
};

const authorization = await Message.createAuthorization(descriptor, options.signer);
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/permissions-grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { DelegatedGrantMessage, PermissionConditions, PermissionScope, Reco
import type { PermissionsGrantDescriptor, PermissionsGrantMessage } from '../types/permissions-types.js';

import { removeUndefinedProperties } from '../utils/object.js';
import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';
import { normalizeProtocolUrl, normalizeSchemaUrl } from '../utils/url.js';

export type PermissionsGrantOptions = {
Expand Down Expand Up @@ -39,8 +39,8 @@ export class PermissionsGrant extends Message<PermissionsGrantMessage> {
public static async parse(message: PermissionsGrantMessage): Promise<PermissionsGrant> {
await validateMessageSignatureIntegrity(message.authorization.signature, message.descriptor);
PermissionsGrant.validateScope(message);
validateTimestamp(message.descriptor.messageTimestamp);
validateTimestamp(message.descriptor.dateExpires);
Time.validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.dateExpires);

return new PermissionsGrant(message);
}
Expand All @@ -53,7 +53,7 @@ export class PermissionsGrant extends Message<PermissionsGrantMessage> {
const descriptor: PermissionsGrantDescriptor = {
interface : DwnInterfaceName.Permissions,
method : DwnMethodName.Grant,
messageTimestamp : options.messageTimestamp ?? getCurrentTimeInHighPrecision(),
messageTimestamp : options.messageTimestamp ?? Time.getCurrentTimestamp(),
dateExpires : options.dateExpires,
description : options.description,
grantedTo : options.grantedTo,
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/permissions-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { PermissionConditions, PermissionScope } from '../types/permissions
import type { PermissionsRequestDescriptor, PermissionsRequestMessage } from '../types/permissions-types.js';

import { removeUndefinedProperties } from '../utils/object.js';
import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';

export type PermissionsRequestOptions = {
messageTimestamp?: string;
Expand All @@ -22,7 +22,7 @@ export class PermissionsRequest extends Message<PermissionsRequestMessage> {

public static async parse(message: PermissionsRequestMessage): Promise<PermissionsRequest> {
await validateMessageSignatureIntegrity(message.authorization.signature, message.descriptor);
validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.messageTimestamp);

return new PermissionsRequest(message);
}
Expand All @@ -31,7 +31,7 @@ export class PermissionsRequest extends Message<PermissionsRequestMessage> {
const descriptor: PermissionsRequestDescriptor = {
interface : DwnInterfaceName.Permissions,
method : DwnMethodName.Request,
messageTimestamp : options.messageTimestamp ?? getCurrentTimeInHighPrecision(),
messageTimestamp : options.messageTimestamp ?? Time.getCurrentTimestamp(),
description : options.description,
grantedTo : options.grantedTo,
grantedBy : options.grantedBy,
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/permissions-revoke.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Signer } from '../types/signer.js';
import type { PermissionsGrantMessage, PermissionsRevokeDescriptor, PermissionsRevokeMessage } from '../types/permissions-types.js';

import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';

export type PermissionsRevokeOptions = {
messageTimestamp?: string;
Expand All @@ -15,7 +15,7 @@ export type PermissionsRevokeOptions = {
export class PermissionsRevoke extends Message<PermissionsRevokeMessage> {
public static async parse(message: PermissionsRevokeMessage): Promise<PermissionsRevoke> {
await validateMessageSignatureIntegrity(message.authorization.signature, message.descriptor);
validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.messageTimestamp);

return new PermissionsRevoke(message);
}
Expand All @@ -24,7 +24,7 @@ export class PermissionsRevoke extends Message<PermissionsRevokeMessage> {
const descriptor: PermissionsRevokeDescriptor = {
interface : DwnInterfaceName.Permissions,
method : DwnMethodName.Revoke,
messageTimestamp : options.messageTimestamp ?? getCurrentTimeInHighPrecision(),
messageTimestamp : options.messageTimestamp ?? Time.getCurrentTimestamp(),
permissionsGrantId : options.permissionsGrantId,
};

Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/protocols-configure.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Signer } from '../types/signer.js';
import type { ProtocolDefinition, ProtocolRuleSet, ProtocolsConfigureDescriptor, ProtocolsConfigureMessage } from '../types/protocols-types.js';

import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../index.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';
import { normalizeProtocolUrl, normalizeSchemaUrl, validateProtocolUrlNormalized, validateSchemaUrlNormalized } from '../utils/url.js';

export type ProtocolsConfigureOptions = {
Expand All @@ -22,7 +22,7 @@ export class ProtocolsConfigure extends Message<ProtocolsConfigureMessage> {
Message.validateJsonSchema(message);
ProtocolsConfigure.validateProtocolDefinition(message.descriptor.definition);
await validateMessageSignatureIntegrity(message.authorization.signature, message.descriptor);
validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.messageTimestamp);

return new ProtocolsConfigure(message);
}
Expand All @@ -31,7 +31,7 @@ export class ProtocolsConfigure extends Message<ProtocolsConfigureMessage> {
const descriptor: ProtocolsConfigureDescriptor = {
interface : DwnInterfaceName.Protocols,
method : DwnMethodName.Configure,
messageTimestamp : options.messageTimestamp ?? getCurrentTimeInHighPrecision(),
messageTimestamp : options.messageTimestamp ?? Time.getCurrentTimestamp(),
definition : ProtocolsConfigure.normalizeDefinition(options.definition)
};

Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/protocols-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type { ProtocolsQueryDescriptor, ProtocolsQueryFilter, ProtocolsQueryMess

import { GrantAuthorization } from '../core/grant-authorization.js';
import { removeUndefinedProperties } from '../utils/object.js';
import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';
import { normalizeProtocolUrl, validateProtocolUrlNormalized } from '../utils/url.js';

import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
Expand All @@ -29,7 +29,7 @@ export class ProtocolsQuery extends Message<ProtocolsQueryMessage> {
if (message.descriptor.filter !== undefined) {
validateProtocolUrlNormalized(message.descriptor.filter.protocol);
}
validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.messageTimestamp);

return new ProtocolsQuery(message);
}
Expand All @@ -38,7 +38,7 @@ export class ProtocolsQuery extends Message<ProtocolsQueryMessage> {
const descriptor: ProtocolsQueryDescriptor = {
interface : DwnInterfaceName.Protocols,
method : DwnMethodName.Query,
messageTimestamp : options.messageTimestamp ?? getCurrentTimeInHighPrecision(),
messageTimestamp : options.messageTimestamp ?? Time.getCurrentTimestamp(),
filter : ProtocolsQuery.normalizeFilter(options.filter),
};

Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/records-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import type { RecordsDeleteDescriptor, RecordsDeleteMessage } from '../types/rec
import { Message } from '../core/message.js';

import { ProtocolAuthorization } from '../core/protocol-authorization.js';
import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../index.js';
import { DwnInterfaceName, DwnMethodName } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';

export type RecordsDeleteOptions = {
recordId: string;
Expand All @@ -22,7 +22,7 @@ export class RecordsDelete extends Message<RecordsDeleteMessage> {

public static async parse(message: RecordsDeleteMessage): Promise<RecordsDelete> {
await validateMessageSignatureIntegrity(message.authorization.signature, message.descriptor);
validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.messageTimestamp);

const recordsDelete = new RecordsDelete(message);
return recordsDelete;
Expand All @@ -35,7 +35,7 @@ export class RecordsDelete extends Message<RecordsDeleteMessage> {
*/
public static async create(options: RecordsDeleteOptions): Promise<RecordsDelete> {
const recordId = options.recordId;
const currentTime = getCurrentTimeInHighPrecision();
const currentTime = Time.getCurrentTimestamp();

const descriptor: RecordsDeleteDescriptor = {
interface : DwnInterfaceName.Records,
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/records-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type { RecordsFilter, RecordsQueryDescriptor, RecordsQueryMessage } from
import { Message } from '../core/message.js';
import { Records } from '../utils/records.js';
import { removeUndefinedProperties } from '../utils/object.js';
import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
import { DwnInterfaceName, DwnMethodName } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';
import { validateProtocolUrlNormalized, validateSchemaUrlNormalized } from '../utils/url.js';

export enum DateSort {
Expand Down Expand Up @@ -49,7 +49,7 @@ export class RecordsQuery extends Message<RecordsQueryMessage> {
if (message.descriptor.filter.schema !== undefined) {
validateSchemaUrlNormalized(message.descriptor.filter.schema);
}
validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.messageTimestamp);

return new RecordsQuery(message);
}
Expand All @@ -58,7 +58,7 @@ export class RecordsQuery extends Message<RecordsQueryMessage> {
const descriptor: RecordsQueryDescriptor = {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Query,
messageTimestamp : options.messageTimestamp ?? getCurrentTimeInHighPrecision(),
messageTimestamp : options.messageTimestamp ?? Time.getCurrentTimestamp(),
filter : Records.normalizeFilter(options.filter),
dateSort : options.dateSort,
pagination : options.pagination,
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/records-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { ProtocolAuthorization } from '../core/protocol-authorization.js';
import { Records } from '../utils/records.js';
import { RecordsGrantAuthorization } from '../core/records-grant-authorization.js';
import { removeUndefinedProperties } from '../utils/object.js';
import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../index.js';
import { DwnInterfaceName, DwnMethodName } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';

export type RecordsReadOptions = {
filter: RecordsFilter;
Expand All @@ -31,7 +31,7 @@ export class RecordsRead extends Message<RecordsReadMessage> {
if (message.authorization !== undefined) {
await validateMessageSignatureIntegrity(message.authorization.signature, message.descriptor);
}
validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.messageTimestamp);

const recordsRead = new RecordsRead(message);
return recordsRead;
Expand All @@ -46,7 +46,7 @@ export class RecordsRead extends Message<RecordsReadMessage> {
*/
public static async create(options: RecordsReadOptions): Promise<RecordsRead> {
const { filter, signer, permissionsGrantId, protocolRole } = options;
const currentTime = getCurrentTimeInHighPrecision();
const currentTime = Time.getCurrentTimestamp();

const descriptor: RecordsReadDescriptor = {
interface : DwnInterfaceName.Records,
Expand Down
12 changes: 6 additions & 6 deletions src/interfaces/records-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import { ProtocolAuthorization } from '../core/protocol-authorization.js';
import { RecordsGrantAuthorization } from '../core/records-grant-authorization.js';
import { removeUndefinedProperties } from '../utils/object.js';
import { Secp256k1 } from '../utils/secp256k1.js';
import { Time } from '../utils/time.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
import { DwnInterfaceName, DwnMethodName } from '../core/message.js';
import { getCurrentTimeInHighPrecision, validateTimestamp } from '../utils/time.js';
import { normalizeProtocolUrl, normalizeSchemaUrl, validateProtocolUrlNormalized, validateSchemaUrlNormalized } from '../utils/url.js';

export type RecordsWriteOptions = {
Expand Down Expand Up @@ -273,7 +273,7 @@ export class RecordsWrite {
const dataCid = options.dataCid ?? await Cid.computeDagPbCidFromBytes(options.data!);
const dataSize = options.dataSize ?? options.data!.length;

const currentTime = getCurrentTimeInHighPrecision();
const currentTime = Time.getCurrentTimestamp();

const descriptor: RecordsWriteDescriptor = {
interface : DwnInterfaceName.Records,
Expand Down Expand Up @@ -356,7 +356,7 @@ export class RecordsWrite {
*/
public static async createFrom(options: CreateFromOptions): Promise<RecordsWrite> {
const sourceMessage = options.recordsWriteMessage;
const currentTime = getCurrentTimeInHighPrecision();
const currentTime = Time.getCurrentTimestamp();

// inherit published value from parent if neither published nor datePublished is specified
const published = options.published ?? (options.datePublished ? true : sourceMessage.descriptor.published);
Expand Down Expand Up @@ -634,10 +634,10 @@ export class RecordsWrite {
validateSchemaUrlNormalized(this.message.descriptor.schema);
}

validateTimestamp(this.message.descriptor.messageTimestamp);
validateTimestamp(this.message.descriptor.dateCreated);
Time.validateTimestamp(this.message.descriptor.messageTimestamp);
Time.validateTimestamp(this.message.descriptor.dateCreated);
if (this.message.descriptor.datePublished) {
validateTimestamp(this.message.descriptor.datePublished);
Time.validateTimestamp(this.message.descriptor.datePublished);
}
}

Expand Down
Loading

0 comments on commit d79901a

Please sign in to comment.