Skip to content

Commit

Permalink
reorganize (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlowaterNostr authored Jun 12, 2024
1 parent 6e135c4 commit 81034be
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 85 deletions.
4 changes: 1 addition & 3 deletions deploy/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ const relay = await run({
default_policy: {
allowed_kinds: "all",
},
default_information: {
auth_required: false,
},
auth_required: true,
});
if (relay instanceof Error) {
console.error(relay);
Expand Down
46 changes: 25 additions & 21 deletions main.tsx → main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export type Relay = {
[Symbol.asyncDispose]: () => Promise<void>;
};

const ENV_relayed_pubkey = "relayed_pubkey";
export const ENV_relayed_pubkey = "relayed_pubkey";

export async function run(args: {
port?: number;
Expand Down Expand Up @@ -107,7 +107,7 @@ export async function run(args: {
const write_replaceable_event = write_replaceable_event_sqlite(db);
const write_regular_event = write_regular_event_sqlite(db);
const get_event_count = get_event_count_sqlite(db);
const get_events_by_filter = get_events_by_filter_sqlite(db);
const get_events_by_filter = get_events_by_filter_sqlite(db, args._debug || false);

// Administrator Keys
if (args.admin == undefined) {
Expand Down Expand Up @@ -169,23 +169,7 @@ export async function run(args: {
},
root_handler({
...args,
is_member: ((admin: PublicKey) => async (pubkey: string) => {
const key = PublicKey.FromString(pubkey);
if (key instanceof Error) {
return key;
}
// admin is always a member
if (key.hex == admin.hex) {
return true;
}
const policy = await policyStore.resolvePolicyByKind(NostrKind.TEXT_NOTE);
if (policy instanceof Error) {
return policy;
}
const policyAllow = policy.allow.has(pubkey);
const policyBlock = policy.block.has(pubkey);
return policyAllow && !policyBlock;
})(args.admin),
is_member: is_member({ admin: args.admin, policyStore }),
// deletion
delete_event: delete_event_sqlite(db),
delete_events_from_pubkey: async () => {
Expand Down Expand Up @@ -483,7 +467,27 @@ async function verifyToken(event: NostrEvent, relayInformationStore: RelayInform
}
}

// export const kv = await Deno.openKv("./test-kv");
const is_member = (args: {
admin: PublicKey;
policyStore: PolicyStore;
}): func_IsMember =>
async (pubkey: string) => {
const { admin, policyStore } = args;
const key = PublicKey.FromString(pubkey);
if (key instanceof Error) {
return key;
}
if (key.hex == admin.hex) {
return true;
}
const policy = await policyStore.resolvePolicyByKind(NostrKind.TEXT_NOTE);
if (policy instanceof Error) {
return policy;
}
const policyAllow = policy.allow.has(pubkey);
const policyBlock = policy.block.has(pubkey);
return policyAllow && !policyBlock;
};

const graphiql = `
<!--
Expand Down Expand Up @@ -599,7 +603,7 @@ const graphiql = `
</body>
</html>`;

export function atobSafe(data) {
export function atobSafe(data: string) {
try {
return atob(data);
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ test: fmt
--trace-leaks --unstable-kv \
--allow-read=queries,test.sqlite,relayed.db,relayed.db-journal \
--allow-net --allow-write --allow-ffi \
--allow-env=DENO_DEPLOYMENT_ID,DENO_DIR,HOME \
--allow-env=DENO_DEPLOYMENT_ID,DENO_DIR,HOME,relayed_pubkey \
--coverage \
test.ts
tests/*.ts

cov:
deno coverage coverage --html
Expand Down
6 changes: 4 additions & 2 deletions resolvers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ CREATE TABLE IF NOT exists events_v2 (
`;

export const get_events_by_filter_sqlite =
(db: DB): func_GetEventsByFilter => async (filter: NostrFilter) => {
(db: DB, log: boolean): func_GetEventsByFilter => async (filter: NostrFilter) => {
let sql = `SELECT json(event) as event FROM events_v1 WHERE true`;
const params = [] as any[];

Expand All @@ -83,7 +83,9 @@ export const get_events_by_filter_sqlite =
sql += ` ORDER BY created_at DESC LIMIT :limit `;
params.push(filter.limit || 200);

console.log(sql, "\n", params, "\n", filter);
if (log) {
console.log(sql, "\n", params, "\n", filter);
}
let results: [string][];
try {
results = db.query<[string]>(sql, params);
Expand Down
2 changes: 1 addition & 1 deletion resolvers/nip11.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { software, supported_nips } from "../main.tsx";
import { software, supported_nips } from "../main.ts";
import { PublicKey } from "../nostr.ts/key.ts";

export type RelayInfomationBase = {
Expand Down
2 changes: 1 addition & 1 deletion resolvers/policy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DB } from "https://deno.land/x/[email protected]/mod.ts";
import { DefaultPolicy } from "../main.tsx";
import { DefaultPolicy } from "../main.ts";
import { EventRelayMembers, Kind_V2 } from "../events.ts";
import { parseJSON } from "../nostr.ts/_helper.ts";
import { PublicKey } from "../nostr.ts/key.ts";
Expand Down
2 changes: 1 addition & 1 deletion routes/landing.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { software, supported_nips } from "../main.tsx";
import { software, supported_nips } from "../main.ts";
import { RelayInformation } from "../resolvers/nip11.ts";

export default function Landing(information?: RelayInformation) {
Expand Down
105 changes: 52 additions & 53 deletions test.ts → tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// deno-lint-ignore-file no-empty
import { Relay, run, software, supported_nips } from "./main.tsx";
import { ENV_relayed_pubkey, Relay, run, software, supported_nips } from "../main.ts";
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
import { assertIsError, assertNotInstanceOf } from "https://deno.land/[email protected]/assert/mod.ts";
import { fail } from "https://deno.land/[email protected]/assert/fail.ts";

import * as client_test from "./nostr.ts/relay-single-test.ts";
import { ChannelCreation, ChannelEdition } from "./events.ts";
import { Kind_V2 } from "./events.ts";
import * as client_test from "../nostr.ts/relay-single-test.ts";
import { ChannelCreation, ChannelEdition } from "../events.ts";
import { Kind_V2 } from "../events.ts";
import {
InMemoryAccountContext,
NostrKind,
RelayResponse_Event,
sign_event_v2,
Signer,
} from "./nostr.ts/nostr.ts";
import { prepareNormalNostrEvent } from "./nostr.ts/event.ts";
import { RelayRejectedEvent, SingleRelayConnection, SubscriptionStream } from "./nostr.ts/relay-single.ts";
import { PrivateKey } from "./nostr.ts/key.ts";
} from "../nostr.ts/nostr.ts";
import { prepareNormalNostrEvent } from "../nostr.ts/event.ts";
import { RelayRejectedEvent, SingleRelayConnection, SubscriptionStream } from "../nostr.ts/relay-single.ts";
import { PrivateKey, PublicKey } from "../nostr.ts/key.ts";
import { sleep } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts";

const test_kv = async () => {
Expand Down Expand Up @@ -84,41 +84,6 @@ Deno.test({
const event_got_2 = await client.getEvent(event_sent.id);
assertEquals(event_got_2, event_sent);
}
// {
// await relay.set_policy({
// kind: NostrKind.CONTACTS,
// read: true,
// write: true,
// });
// const event_1 = await randomEvent(ctx, NostrKind.CONTACTS, "1");
// const event_2 = await randomEvent(ctx, NostrKind.CONTACTS, "2");
// const event_3 = await randomEvent(ctx, NostrKind.CONTACTS, "3");

// const err_1 = await client.sendEvent(event_1);
// if (err_1 instanceof Error) fail(err_1.message);

// const err_2 = await client.sendEvent(event_2);
// if (err_2 instanceof Error) fail(err_2.message);

// const err_3 = await client.sendEvent(event_3);
// if (err_3 instanceof Error) fail(err_3.message);

// const stream = await client.newSub("get kind 3", {
// kinds: [NostrKind.CONTACTS],
// }) as SubscriptionStream;

// const events: NostrEvent[] = [];

// for await (const msg of stream.chan) {
// if (msg.type == "EVENT") {
// events.push(msg.event);
// } else if (msg.type == "EOSE") {
// await stream.chan.close();
// }
// }

// assertEquals(events.length, 3);
// }
{
const ctx1 = InMemoryAccountContext.Generate();
const event_1 = await randomEvent(ctx1, NostrKind.TEXT_NOTE, "test:main 1");
Expand Down Expand Up @@ -281,6 +246,50 @@ Deno.test({
name: "NIP-11: Relay Information Document",
// ignore: true,
fn: async (t) => {
await t.step("get relay information", async (t) => {
await t.step("admin from the argument", async () => {
await using relay = await run({
default_policy: {
allowed_kinds: "none",
},
default_information: {
name: "Nostr Relay",
},
auth_required: false,
admin: test_ctx.publicKey,
kv: await test_kv(),
}) as Relay;
const information = await relay.get_relay_information();
assertEquals(information, {
name: "Nostr Relay",
pubkey: test_ctx.publicKey,
software,
supported_nips,
});
});
await t.step("admin from the env var", async () => {
const key = PrivateKey.Generate();
Deno.env.set(ENV_relayed_pubkey, key.toPublicKey().hex);
await using relay = await run({
default_policy: {
allowed_kinds: "none",
},
default_information: {
name: "Nostr Relay",
},
auth_required: false,
kv: await test_kv(),
}) as Relay;
const information = await relay.get_relay_information();
assertEquals(information, {
name: "Nostr Relay",
pubkey: key.toPublicKey(),
software,
supported_nips,
});
});
});

await using relay = await run({
default_policy: {
allowed_kinds: "none",
Expand All @@ -294,16 +303,6 @@ Deno.test({
// system_key: PrivateKey.Generate(),
}) as Relay;

await t.step("get relay information", async () => {
const information = await relay.get_relay_information();
assertEquals(information, {
name: "Nostr Relay",
pubkey: test_ctx.publicKey,
software,
supported_nips,
});
});

await t.step("set relay information", async () => {
await relay.set_relay_information({
name: "Nostr Relay2",
Expand Down
2 changes: 1 addition & 1 deletion ws.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// deno-lint-ignore-file
import { func_ResolvePolicyByKind } from "./resolvers/policy.ts";
import { atobSafe, DefaultPolicy } from "./main.tsx";
import { atobSafe, DefaultPolicy } from "./main.ts";
import { func_WriteRegularEvent, func_WriteReplaceableEvent } from "./resolvers/event.ts";

import { func_GetEventsByFilter } from "./resolvers/event.ts";
Expand Down

0 comments on commit 81034be

Please sign in to comment.