add unixTimestamp()
This commit is contained in:
parent
289dbcd523
commit
303fa2ff4b
@ -2,6 +2,7 @@ import { ProtocolError } from "../error"
|
||||
import { Filters, SubscriptionId } from "."
|
||||
import { RawEvent, SignedEvent } from "../event"
|
||||
import WebSocket from "ws"
|
||||
import { unixTimestamp } from "../util"
|
||||
|
||||
/**
|
||||
* 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),
|
||||
["#e"]: filters.eventTags?.map((e) => e.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:
|
||||
filters.since !== undefined
|
||||
? Math.floor(filters.since.getTime() / 1000)
|
||||
: undefined,
|
||||
filters.since !== undefined ? unixTimestamp(filters.since) : undefined,
|
||||
until:
|
||||
filters.until !== undefined
|
||||
? Math.floor(filters.until.getTime() / 1000)
|
||||
: undefined,
|
||||
filters.until !== undefined ? unixTimestamp(filters.until) : undefined,
|
||||
limit: filters.limit,
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { ProtocolError } from "./error"
|
||||
import * as secp from "@noble/secp256k1"
|
||||
import { PublicKey, PrivateKey } from "./keypair"
|
||||
import { unixTimestamp } from "./util"
|
||||
|
||||
// TODO This file is missing proper documentation
|
||||
// TODO Add remaining event types
|
||||
@ -76,8 +77,8 @@ export class EventId {
|
||||
const serializedTags = `[${tags
|
||||
.map((tag) => `[${tag.map((v) => `"${v}"`).join(",")}]`)
|
||||
.join(",")}]`
|
||||
const serialized = `[0,"${event.pubkey}",${Math.floor(
|
||||
event.createdAt.getTime() / 1000
|
||||
const serialized = `[0,"${event.pubkey}",${unixTimestamp(
|
||||
event.createdAt
|
||||
)},${event.kind},${serializedTags},"${content}"]`
|
||||
const hash = await secp.utils.sha256(
|
||||
Uint8Array.from(charCodes(serialized))
|
||||
@ -153,7 +154,7 @@ export class SignedEvent {
|
||||
return {
|
||||
id: id.toString(),
|
||||
pubkey: event.pubkey.toString(),
|
||||
created_at: Math.floor(event.createdAt.getTime() / 1000),
|
||||
created_at: unixTimestamp(event.createdAt),
|
||||
kind: event.kind,
|
||||
tags,
|
||||
content,
|
||||
|
6
packages/nostr/src/util.ts
Normal file
6
packages/nostr/src/util.ts
Normal file
@ -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…
x
Reference in New Issue
Block a user