166 lines
4.2 KiB
JavaScript
166 lines
4.2 KiB
JavaScript
// event.ts
|
|
import { schnorr as schnorr2 } from "@noble/curves/secp256k1";
|
|
import { sha256 } from "@noble/hashes/sha256";
|
|
import { bytesToHex as bytesToHex2 } from "@noble/hashes/utils";
|
|
|
|
// keys.ts
|
|
import { schnorr } from "@noble/curves/secp256k1";
|
|
import { bytesToHex } from "@noble/hashes/utils";
|
|
function getPublicKey(privateKey) {
|
|
return bytesToHex(schnorr.getPublicKey(privateKey));
|
|
}
|
|
|
|
// utils.ts
|
|
var utf8Decoder = new TextDecoder("utf-8");
|
|
var utf8Encoder = new TextEncoder();
|
|
|
|
// event.ts
|
|
var verifiedSymbol = Symbol("verified");
|
|
function finishEvent(t, privateKey) {
|
|
const event = t;
|
|
event.pubkey = getPublicKey(privateKey);
|
|
event.id = getEventHash(event);
|
|
event.sig = getSignature(event, privateKey);
|
|
event[verifiedSymbol] = true;
|
|
return event;
|
|
}
|
|
function serializeEvent(evt) {
|
|
if (!validateEvent(evt))
|
|
throw new Error("can't serialize event with wrong or missing properties");
|
|
return JSON.stringify([0, evt.pubkey, evt.created_at, evt.kind, evt.tags, evt.content]);
|
|
}
|
|
function getEventHash(event) {
|
|
let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)));
|
|
return bytesToHex2(eventHash);
|
|
}
|
|
var isRecord = (obj) => obj instanceof Object;
|
|
function validateEvent(event) {
|
|
if (!isRecord(event))
|
|
return false;
|
|
if (typeof event.kind !== "number")
|
|
return false;
|
|
if (typeof event.content !== "string")
|
|
return false;
|
|
if (typeof event.created_at !== "number")
|
|
return false;
|
|
if (typeof event.pubkey !== "string")
|
|
return false;
|
|
if (!event.pubkey.match(/^[a-f0-9]{64}$/))
|
|
return false;
|
|
if (!Array.isArray(event.tags))
|
|
return false;
|
|
for (let i = 0; i < event.tags.length; i++) {
|
|
let tag = event.tags[i];
|
|
if (!Array.isArray(tag))
|
|
return false;
|
|
for (let j = 0; j < tag.length; j++) {
|
|
if (typeof tag[j] === "object")
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
function getSignature(event, key) {
|
|
return bytesToHex2(schnorr2.sign(getEventHash(event), key));
|
|
}
|
|
|
|
// nip28.ts
|
|
var channelCreateEvent = (t, privateKey) => {
|
|
let content;
|
|
if (typeof t.content === "object") {
|
|
content = JSON.stringify(t.content);
|
|
} else if (typeof t.content === "string") {
|
|
content = t.content;
|
|
} else {
|
|
return void 0;
|
|
}
|
|
return finishEvent(
|
|
{
|
|
kind: 40 /* ChannelCreation */,
|
|
tags: [...t.tags ?? []],
|
|
content,
|
|
created_at: t.created_at
|
|
},
|
|
privateKey
|
|
);
|
|
};
|
|
var channelMetadataEvent = (t, privateKey) => {
|
|
let content;
|
|
if (typeof t.content === "object") {
|
|
content = JSON.stringify(t.content);
|
|
} else if (typeof t.content === "string") {
|
|
content = t.content;
|
|
} else {
|
|
return void 0;
|
|
}
|
|
return finishEvent(
|
|
{
|
|
kind: 41 /* ChannelMetadata */,
|
|
tags: [["e", t.channel_create_event_id], ...t.tags ?? []],
|
|
content,
|
|
created_at: t.created_at
|
|
},
|
|
privateKey
|
|
);
|
|
};
|
|
var channelMessageEvent = (t, privateKey) => {
|
|
const tags = [["e", t.channel_create_event_id, t.relay_url, "root"]];
|
|
if (t.reply_to_channel_message_event_id) {
|
|
tags.push(["e", t.reply_to_channel_message_event_id, t.relay_url, "reply"]);
|
|
}
|
|
return finishEvent(
|
|
{
|
|
kind: 42 /* ChannelMessage */,
|
|
tags: [...tags, ...t.tags ?? []],
|
|
content: t.content,
|
|
created_at: t.created_at
|
|
},
|
|
privateKey
|
|
);
|
|
};
|
|
var channelHideMessageEvent = (t, privateKey) => {
|
|
let content;
|
|
if (typeof t.content === "object") {
|
|
content = JSON.stringify(t.content);
|
|
} else if (typeof t.content === "string") {
|
|
content = t.content;
|
|
} else {
|
|
return void 0;
|
|
}
|
|
return finishEvent(
|
|
{
|
|
kind: 43 /* ChannelHideMessage */,
|
|
tags: [["e", t.channel_message_event_id], ...t.tags ?? []],
|
|
content,
|
|
created_at: t.created_at
|
|
},
|
|
privateKey
|
|
);
|
|
};
|
|
var channelMuteUserEvent = (t, privateKey) => {
|
|
let content;
|
|
if (typeof t.content === "object") {
|
|
content = JSON.stringify(t.content);
|
|
} else if (typeof t.content === "string") {
|
|
content = t.content;
|
|
} else {
|
|
return void 0;
|
|
}
|
|
return finishEvent(
|
|
{
|
|
kind: 44 /* ChannelMuteUser */,
|
|
tags: [["p", t.pubkey_to_mute], ...t.tags ?? []],
|
|
content,
|
|
created_at: t.created_at
|
|
},
|
|
privateKey
|
|
);
|
|
};
|
|
export {
|
|
channelCreateEvent,
|
|
channelHideMessageEvent,
|
|
channelMessageEvent,
|
|
channelMetadataEvent,
|
|
channelMuteUserEvent
|
|
};
|