diff --git a/graphql-schema.ts b/graphql-schema.ts index eed2fdd..eefe92f 100644 --- a/graphql-schema.ts +++ b/graphql-schema.ts @@ -45,7 +45,7 @@ export const typeDefs = gql` type RelayInformation { name: String description: String - pubkey: PublicKey! + pubkey: PublicKey contact: String supported_nips: [Int!] software: String diff --git a/main.tsx b/main.tsx index 8badb5d..a3ec9cd 100644 --- a/main.tsx +++ b/main.tsx @@ -231,15 +231,24 @@ export const supported_nips = [1, 2]; export const software = "https://github.com/BlowaterNostr/relayed"; const landing_handler = async (args: { relayInformationStore: RelayInformationStore }) => { + const storeInformation = await args.relayInformationStore.resolveRelayInformation(); + if(storeInformation instanceof Error) { + return new Response(render(Error404()), { status: 404 }); + } const resp = new Response( - render(Landing(await args.relayInformationStore.resolveRelayInformation()), { status: 200 }), + render(Landing(storeInformation), { status: 200 }), ); resp.headers.set("content-type", "html"); return resp; }; const information_handler = async (args: { relayInformationStore: RelayInformationStore }) => { - const resp = new Response(JSON.stringify(await args.relayInformationStore.resolveRelayInformation()), { + const storeInformation = await args.relayInformationStore.resolveRelayInformation(); + if(storeInformation instanceof Error) { + return new Response(render(Error404()), { status: 404 }); + } + const information = { ...storeInformation, pubkey: storeInformation.pubkey.bech32()} + const resp = new Response(JSON.stringify(information), { status: 200, }); resp.headers.set("content-type", "application/json; charset=utf-8"); @@ -258,7 +267,11 @@ async function verifyToken(event: NostrEvent, relayInformationStore: RelayInform if (pubkey instanceof Error) { throw new Error("pubkey not valid"); } - if (pubkey.hex !== (await relayInformationStore.resolveRelayInformation()).pubkey.hex) { + const storeInformation = await relayInformationStore.resolveRelayInformation(); + if(storeInformation instanceof Error) { + throw new Error("store pubkey not valid"); + } + if (pubkey.hex !== storeInformation.pubkey.hex) { throw new Error("not admin"); } return { diff --git a/queries/getRelayInformation.gql b/queries/getRelayInformation.gql index b7fd978..38cf3b3 100644 --- a/queries/getRelayInformation.gql +++ b/queries/getRelayInformation.gql @@ -4,7 +4,10 @@ query getRelayInformation { contact description icon - pubkey + pubkey { + hex + bech32 + } software supported_nips version diff --git a/queries/setRelayInformation.gql b/queries/setRelayInformation.gql index f24960f..bb9b03a 100644 --- a/queries/setRelayInformation.gql +++ b/queries/setRelayInformation.gql @@ -10,7 +10,10 @@ mutation setRelayInformation($pubkey: String, $contact: String, $name: String, $ contact description icon - pubkey + pubkey { + hex + bech32 + } software supported_nips version diff --git a/resolvers/nip11.ts b/resolvers/nip11.ts index fd75112..0f6e176 100644 --- a/resolvers/nip11.ts +++ b/resolvers/nip11.ts @@ -1,5 +1,13 @@ import { PublicKey } from "../_libs.ts"; +export type RelayInformationSave = { + name?: string; + description?: string; + pubkey?: string; + contact?: string; + icon?: string; +} + export type RelayInformation = { name?: string; description?: string; @@ -31,38 +39,33 @@ export class RelayInformationStore { this.default_information = default_information; } - resolveRelayInformation = async (): Promise => { - const get_relay_information = (await this.kv.get(["relay_information"])).value; - // if pubkey is set in default_information, it will be used as the pubkey - if (get_relay_information && this.default_information.pubkey) { - get_relay_information.pubkey = this.default_information.pubkey; + resolveRelayInformation = async (): Promise => { + const store_information_i = (await this.kv.get(["relay_information"])).value; + if(!store_information_i) { + return { ...this.default_information, ...not_modifiable_information }; + } + if (!store_information_i.pubkey) { + return { ...this.default_information, ...{ name: store_information_i.name, contact: store_information_i.contact, description: store_information_i.description, icon: store_information_i.icon }, ...not_modifiable_information }; + } + const get_relay_information_pubkey = PublicKey.FromString(store_information_i.pubkey); + if (get_relay_information_pubkey instanceof Error) { + return get_relay_information_pubkey } - return { ...this.default_information, ...get_relay_information, ...not_modifiable_information }; + const store_infomation = { + ...store_information_i, + pubkey: get_relay_information_pubkey, + } + return { ...this.default_information, ...store_infomation, ...not_modifiable_information }; }; - set_relay_information = async ( - args: { - name?: string; - description?: string; - pubkey?: string; - contact?: string; - icon?: string; - }, - ) => { + set_relay_information = async (args: RelayInformationSave) => { const old_information = await this.resolveRelayInformation(); - const new_information = { - ...old_information, - name: args.name, - description: args.description, - contact: args.contact, - icon: args.icon, - }; - if (args.pubkey) { - const pubkey = PublicKey.FromString(args.pubkey); - if (pubkey instanceof Error) { - throw new Error("Invalid pubkey"); - } - new_information.pubkey = pubkey; + if (old_information instanceof Error) { + return old_information; + } + const new_information = {...old_information, ...args}; + if (new_information.pubkey instanceof PublicKey) { + new_information.pubkey = new_information.pubkey.hex } await this.kv.set(["relay_information"], new_information); return { ...new_information, ...not_modifiable_information }; diff --git a/routes/landing.tsx b/routes/landing.tsx index 7aef46f..2e7bb81 100644 --- a/routes/landing.tsx +++ b/routes/landing.tsx @@ -18,7 +18,7 @@ export default function Landing(information?: RelayInformation) { Description: {information?.description}
  • - Pubkey: {information?.pubkey} + Pubkey: {information?.pubkey.bech32()}
  • Contact: {information?.contact}