-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fd7017
commit 3017bd2
Showing
6 changed files
with
137 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,21 @@ import { SubscriptionMap, ws_handler } from "./ws.ts"; | |
import { render } from "https://esm.sh/[email protected]"; | ||
import { RootResolver } from "./resolvers/root.ts"; | ||
import * as gql from "https://esm.sh/[email protected]"; | ||
import { Policy } from "./resolvers/policy.ts"; | ||
import { func_GetRelayMembers, Policy } from "./resolvers/policy.ts"; | ||
import { func_ResolvePolicyByKind } from "./resolvers/policy.ts"; | ||
import { NostrEvent, NostrKind, parseJSON, PublicKey, verify_event_v2, verifyEvent } from "./_libs.ts"; | ||
import { | ||
NostrEvent, | ||
NostrKind, | ||
parseJSON, | ||
PrivateKey, | ||
PublicKey, | ||
verify_event_v2, | ||
verifyEvent, | ||
} from "./_libs.ts"; | ||
import { PolicyStore } from "./resolvers/policy.ts"; | ||
import { Policies } from "./resolvers/policy.ts"; | ||
import { | ||
event_v1_schema_sqlite, | ||
event_schema_sqlite, | ||
func_DeleteEventsFromPubkey, | ||
func_GetDeletedEventIDs, | ||
func_GetEventCount, | ||
|
@@ -44,6 +52,7 @@ import { | |
} from "./channel.ts"; | ||
import { func_GetChannelByID } from "./channel.ts"; | ||
import { DB } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { get_relay_members } from "./resolvers/policy.ts"; | ||
|
||
const schema = gql.buildSchema(gql.print(typeDefs)); | ||
|
||
|
@@ -86,24 +95,30 @@ export async function run(args: { | |
admin?: PublicKey; | ||
default_policy: DefaultPolicy; | ||
default_information?: RelayInformationStringify; | ||
system_key: string | PrivateKey; | ||
kv?: Deno.Kv; | ||
_debug?: boolean; | ||
}): Promise<Error | Relay> { | ||
const isDenoDeploy = Deno.env.get("DENO_DEPLOYMENT_ID") !== undefined; | ||
|
||
//------------------ | ||
// argument checking | ||
// Deno KV | ||
let kv = args.kv; | ||
if (kv == undefined) { | ||
kv = await Deno.openKv(); | ||
} | ||
|
||
// SQLite Database | ||
let db: DB | undefined; | ||
let get_channel_by_id: func_GetChannelByID; | ||
if (!isDenoDeploy) { | ||
db = new DB("relayed.db"); | ||
db.execute(`${sqlite_schema}${event_v1_schema_sqlite}`); | ||
db.execute(`${sqlite_schema}${event_schema_sqlite}`); | ||
get_channel_by_id = get_channel_by_id_sqlite(db); | ||
} | ||
|
||
// Administrator Keys | ||
let admin_pubkey: string | undefined | PublicKey | Error = args.default_information?.pubkey; | ||
if (admin_pubkey == undefined) { | ||
const env_pubkey = Deno.env.get(ENV_relayed_pubkey); | ||
|
@@ -120,6 +135,17 @@ export async function run(args: { | |
return admin_pubkey; | ||
} | ||
|
||
// Relay Key | ||
let system_key: string | PrivateKey | Error = args.system_key; | ||
if (typeof system_key == "string") { | ||
system_key = PrivateKey.FromString(system_key); | ||
if (system_key instanceof Error) { | ||
return system_key; | ||
} | ||
} | ||
// argument checking done | ||
//----------------------- | ||
|
||
const { default_policy } = args; | ||
/////////////// | ||
const connections = new Map<WebSocket, SubscriptionMap>(); | ||
|
@@ -129,7 +155,13 @@ export async function run(args: { | |
}); | ||
|
||
const get_all_policies = Policies(kv); | ||
const policyStore = new PolicyStore(default_policy, kv, await get_all_policies()); | ||
const policyStore = new PolicyStore({ | ||
default_policy, | ||
kv, | ||
system_account: system_key, | ||
initial_policies: await get_all_policies(), | ||
db, | ||
}); | ||
const relayInformationStore = new RelayInformationStore( | ||
kv, | ||
{ | ||
|
@@ -168,6 +200,7 @@ export async function run(args: { | |
write_replaceable_event: eventStore.write_replaceable_event, | ||
policyStore, | ||
relayInformationStore, | ||
get_relay_members: get_relay_members(db), | ||
kv, | ||
db: db, | ||
// get_channel_by_name: get_channel_by_name(db) | ||
|
@@ -224,6 +257,7 @@ const root_handler = ( | |
policyStore: PolicyStore; | ||
relayInformationStore: RelayInformationStore; | ||
// get_channel_by_name: func_GetChannelByName; | ||
get_relay_members: func_GetRelayMembers; | ||
kv: Deno.Kv; | ||
db?: DB; | ||
_debug: boolean; | ||
|
@@ -323,6 +357,7 @@ const graphql_handler = ( | |
delete_event: func_DeleteEvent; | ||
delete_events_from_pubkey: func_DeleteEventsFromPubkey; | ||
get_deleted_event_ids: func_GetDeletedEventIDs; | ||
get_relay_members: func_GetRelayMembers; | ||
}, | ||
) => | ||
async (req: Request) => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { NostrKind, PublicKey } from "../_libs.ts"; | ||
import { DB } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { NostrAccountContext, NostrKind, parseJSON, PrivateKey, PublicKey, sign_event_v2 } from "../_libs.ts"; | ||
import { DefaultPolicy } from "../main.tsx"; | ||
import { Event_V2, EventRelayMembers, Kind_V2 } from "../events.ts"; | ||
|
||
export const Policies = (kv: Deno.Kv) => | ||
async function () { | ||
|
@@ -25,19 +27,23 @@ export class PolicyStore { | |
policies = new Map<NostrKind, Policy>(); | ||
|
||
constructor( | ||
private default_policy: DefaultPolicy, | ||
private kv: Deno.Kv, | ||
initial_policies: Policy[], | ||
private readonly args: { | ||
default_policy: DefaultPolicy; | ||
kv: Deno.Kv; | ||
system_account: PrivateKey; | ||
initial_policies: Policy[]; | ||
db?: DB; | ||
}, | ||
) { | ||
for (const policy of initial_policies) { | ||
for (const policy of args.initial_policies) { | ||
this.policies.set(policy.kind, policy); | ||
} | ||
} | ||
|
||
resolvePolicyByKind = async (kind: NostrKind): Promise<Policy> => { | ||
const policy = this.policies.get(kind); | ||
if (policy == undefined) { | ||
const default_policy = this.default_policy; | ||
const default_policy = this.args.default_policy; | ||
let allow_this_kind: boolean; | ||
if (default_policy.allowed_kinds == "all") { | ||
allow_this_kind = true; | ||
|
@@ -90,7 +96,61 @@ export class PolicyStore { | |
policy.block = blocks; | ||
} | ||
this.policies.set(args.kind, policy); | ||
await this.kv.set(["policy", args.kind], policy); | ||
await this.args.kv.set(["policy", args.kind], policy); | ||
|
||
{ | ||
// WIP | ||
// generate new relay member list | ||
if (this.args.db) { | ||
if (args.kind == NostrKind.TEXT_NOTE) { | ||
const event = await sign_event_v2(this.args.system_account, { | ||
pubkey: this.args.system_account.toPublicKey().hex, | ||
kind: Kind_V2.RelayMember, | ||
members: Array.from(policy.allow), | ||
created_at: Date.now(), | ||
}); | ||
// warn: could throw | ||
try { | ||
this.args.db.query( | ||
`INSERT INTO events_v2 (id, pubkey, kind, event) VALUES (?, ?, ?, ?);`, | ||
[event.id, event.pubkey, event.kind, JSON.stringify(event)], | ||
); | ||
} catch (e) { | ||
console.log(event); | ||
console.error(e); | ||
} | ||
} | ||
} else { | ||
console.log("Feature Not Supported in this environment: Relay Members"); | ||
} | ||
} | ||
return policy; | ||
}; | ||
} | ||
|
||
export type func_GetRelayMembers = () => Promise<undefined | EventRelayMembers | Error>; | ||
export const get_relay_members = (db?: DB): func_GetRelayMembers => async () => { | ||
if (!db) { | ||
return new Error("get_relay_members is not supported"); | ||
} | ||
const rows = db.query<[string]>( | ||
"select event from events_v2 where kind = (?)", | ||
[Kind_V2.RelayMember], | ||
); | ||
|
||
const events = [] as EventRelayMembers[]; | ||
for (const row of rows) { | ||
const relay_member_event = parseJSON<EventRelayMembers>(row[0]); | ||
if (relay_member_event instanceof Error) { | ||
return relay_member_event; | ||
} | ||
events.push(relay_member_event); | ||
} | ||
if (events.length == 0) { | ||
return; | ||
} | ||
|
||
const event = events.sort((e1, e2) => e1.created_at - e2.created_at)[0]; | ||
console.log(event); | ||
return event; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters