Skip to content

Commit

Permalink
Use cliffy to invoke repost.
Browse files Browse the repository at this point in the history
  • Loading branch information
chmac committed Aug 9, 2024
1 parent ec683cb commit f0d8021
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
29 changes: 29 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { cliffy, nostrTools } from "./deps.ts";
import { repost } from "./validation/repost.ts";

function getOrCreatePrivateKey(maybePrivateKeyNsec?: string) {
if (typeof maybePrivateKeyNsec === "string") {
const decoded = nostrTools.nip19.decode(maybePrivateKeyNsec);
if (decoded.type !== "nsec") {
throw new Error("#5jLJ2W Invalid nsec");
}
return decoded.data;
}

const key = nostrTools.generateSecretKey();
const nsec = nostrTools.nip19.nsecEncode(key);
console.log(`#2yrJza Using random nsec ${nsec}`);
return key;
}

await new cliffy.Command()
.globalEnv(
"PRIVATE_KEY_NSEC=<value:string>",
"Specify the private key in nsec format"
)
.action((options) => {
const privateKey = getOrCreatePrivateKey(options.privateKeyNsec);

repost(privateKey);
})
.parse(Deno.args);
54 changes: 24 additions & 30 deletions validation/repost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { nostrToolsRelay } from "../deps.ts";
const { Relay } = nostrToolsRelay;

import { nostrTools } from "../deps.ts";
const { finalizeEvent, generateSecretKey, getPublicKey } = nostrTools;
const { finalizeEvent } = nostrTools;
type Event = nostrTools.Event;
type EventTemplate = nostrTools.EventTemplate;
type VerifiedEvent = nostrTools.VerifiedEvent;
Expand All @@ -14,39 +14,12 @@ import {
MAP_NOTE_REPOST_KIND,
} from "../common/constants.ts";

// should be fixed, not generated new every time
let SECRET_KEY = generateSecretKey();
let PUBLIC_KEY = getPublicKey(SECRET_KEY);

const RELAY = DEV_RELAYS[0];

console.log(`connecting to ${RELAY}…`);
const relay = await Relay.connect(RELAY);
console.log(`connected to ${relay.url}`);

async function handleEvent(event: Event) {
if (!validateEvent(event)) {
console.info(`Discarding event…`);
return;
}
const repostedEvent = generateRepostedEvent(event);
await publishEvent(repostedEvent);
}

const sub = relay.subscribe(
[
{
kinds: [MAP_NOTE_KIND],
},
],
{
onevent: handleEvent,
oneose() {
sub.close();
},
}
);

function validateEvent(event: Event) {
if (!event.kind == MAP_NOTE_KIND) {
return false;
Expand All @@ -58,7 +31,7 @@ async function publishEvent(event: VerifiedEvent) {
await relay.publish(event);
}

function generateRepostedEvent(originalEvent: Event) {
function generateRepostedEvent(originalEvent: Event, privateKey: Uint8Array) {
const derivedTags = deriveTags(originalEvent);
const derivedContent = deriveContent(originalEvent);
const dTag = ["d", `${originalEvent.pubkey}:${originalEvent.id}`];
Expand All @@ -70,7 +43,7 @@ function generateRepostedEvent(originalEvent: Event) {
tags: [eTag, dTag, ...derivedTags],
content: derivedContent,
};
const signedEvent = finalizeEvent(eventTemplate, SECRET_KEY);
const signedEvent = finalizeEvent(eventTemplate, privateKey);
return signedEvent;
}

Expand All @@ -81,3 +54,24 @@ function deriveTags(event: Event): Tags {
function deriveContent(event: Event): string {
return event.content;
}

export async function repost(privateKey: Uint8Array) {
const sub = relay.subscribe(
[
{
kinds: [MAP_NOTE_KIND],
since: Math.floor(Date.now() / 1e3),
},
],
{
onevent: (event) => {
if (!validateEvent(event)) {
console.info(`Discarding event…`);
return;
}
const repostedEvent = generateRepostedEvent(event, privateKey);
publishEvent(repostedEvent);
},
}
);
}

0 comments on commit f0d8021

Please sign in to comment.