add unixTimestamp()
This commit is contained in:
parent
289dbcd523
commit
303fa2ff4b
|
@ -2,6 +2,7 @@ import { ProtocolError } from "../error"
|
||||||
import { Filters, SubscriptionId } from "."
|
import { Filters, SubscriptionId } from "."
|
||||||
import { RawEvent, SignedEvent } from "../event"
|
import { RawEvent, SignedEvent } from "../event"
|
||||||
import WebSocket from "ws"
|
import WebSocket from "ws"
|
||||||
|
import { unixTimestamp } from "../util"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The connection to a relay. This is the lowest layer of the nostr protocol.
|
* The connection to a relay. This is the lowest layer of the nostr protocol.
|
||||||
|
@ -241,15 +242,10 @@ function serializeFilters(filters: Filters): RawFilters {
|
||||||
kinds: filters.kinds?.map((kind) => kind),
|
kinds: filters.kinds?.map((kind) => kind),
|
||||||
["#e"]: filters.eventTags?.map((e) => e.toString()),
|
["#e"]: filters.eventTags?.map((e) => e.toString()),
|
||||||
["#p"]: filters.pubkeyTags?.map((p) => p.toString()),
|
["#p"]: filters.pubkeyTags?.map((p) => p.toString()),
|
||||||
// TODO The Math.floor has been repeated too many times at this point, have a unix timestamp function in event.ts
|
|
||||||
since:
|
since:
|
||||||
filters.since !== undefined
|
filters.since !== undefined ? unixTimestamp(filters.since) : undefined,
|
||||||
? Math.floor(filters.since.getTime() / 1000)
|
|
||||||
: undefined,
|
|
||||||
until:
|
until:
|
||||||
filters.until !== undefined
|
filters.until !== undefined ? unixTimestamp(filters.until) : undefined,
|
||||||
? Math.floor(filters.until.getTime() / 1000)
|
|
||||||
: undefined,
|
|
||||||
limit: filters.limit,
|
limit: filters.limit,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { ProtocolError } from "./error"
|
import { ProtocolError } from "./error"
|
||||||
import * as secp from "@noble/secp256k1"
|
import * as secp from "@noble/secp256k1"
|
||||||
import { PublicKey, PrivateKey } from "./keypair"
|
import { PublicKey, PrivateKey } from "./keypair"
|
||||||
|
import { unixTimestamp } from "./util"
|
||||||
|
|
||||||
// TODO This file is missing proper documentation
|
// TODO This file is missing proper documentation
|
||||||
// TODO Add remaining event types
|
// TODO Add remaining event types
|
||||||
|
@ -76,8 +77,8 @@ export class EventId {
|
||||||
const serializedTags = `[${tags
|
const serializedTags = `[${tags
|
||||||
.map((tag) => `[${tag.map((v) => `"${v}"`).join(",")}]`)
|
.map((tag) => `[${tag.map((v) => `"${v}"`).join(",")}]`)
|
||||||
.join(",")}]`
|
.join(",")}]`
|
||||||
const serialized = `[0,"${event.pubkey}",${Math.floor(
|
const serialized = `[0,"${event.pubkey}",${unixTimestamp(
|
||||||
event.createdAt.getTime() / 1000
|
event.createdAt
|
||||||
)},${event.kind},${serializedTags},"${content}"]`
|
)},${event.kind},${serializedTags},"${content}"]`
|
||||||
const hash = await secp.utils.sha256(
|
const hash = await secp.utils.sha256(
|
||||||
Uint8Array.from(charCodes(serialized))
|
Uint8Array.from(charCodes(serialized))
|
||||||
|
@ -153,7 +154,7 @@ export class SignedEvent {
|
||||||
return {
|
return {
|
||||||
id: id.toString(),
|
id: id.toString(),
|
||||||
pubkey: event.pubkey.toString(),
|
pubkey: event.pubkey.toString(),
|
||||||
created_at: Math.floor(event.createdAt.getTime() / 1000),
|
created_at: unixTimestamp(event.createdAt),
|
||||||
kind: event.kind,
|
kind: event.kind,
|
||||||
tags,
|
tags,
|
||||||
content,
|
content,
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
/**
|
||||||
|
* Calculate the unix timestamp (seconds since epoch) of the `Date`.
|
||||||
|
*/
|
||||||
|
export function unixTimestamp(date: Date): number {
|
||||||
|
return Math.floor(date.getTime() / 1000)
|
||||||
|
}
|
Loading…
Reference in New Issue