You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
1.0 KiB
TypeScript
29 lines
1.0 KiB
TypeScript
import { v4 as uuid } from 'uuid';
|
|
|
|
export interface MessageParams {
|
|
createAtNetworkTimestamp: number;
|
|
identifier?: string;
|
|
}
|
|
|
|
export abstract class Message {
|
|
/**
|
|
* This is the network timestamp when this message was created (and so, potentially signed).
|
|
* This must be used as the envelope timestamp, as other devices are going to use it to verify messages.
|
|
* There is also the stored_at/effectiveTimestamp which we get back once we sent a message to the recipient's swarm, but that's not included here.
|
|
*/
|
|
public readonly createAtNetworkTimestamp: number;
|
|
public readonly identifier: string;
|
|
|
|
constructor({ createAtNetworkTimestamp, identifier }: MessageParams) {
|
|
this.createAtNetworkTimestamp = createAtNetworkTimestamp;
|
|
if (identifier && identifier.length === 0) {
|
|
throw new Error('Cannot set empty identifier');
|
|
}
|
|
|
|
if (!createAtNetworkTimestamp || createAtNetworkTimestamp <= 0) {
|
|
throw new Error('Cannot set undefined createAtNetworkTimestamp or <=0');
|
|
}
|
|
this.identifier = identifier || uuid();
|
|
}
|
|
}
|