Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nrcommon #37

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nrapp/redux/eventsSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ID_SEPARATOR } from "@/constants";
import { Event } from "@/typesTEMPORARY";
import { Event } from "@/../nrcommon/mod";
import {
createEntityAdapter,
createSlice,
Expand Down
21 changes: 0 additions & 21 deletions nrapp/typesTEMPORARY.ts

This file was deleted.

8 changes: 8 additions & 0 deletions nrcommon/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@trustroots/nrcommon",
"version": "0.0.1",
"exports": "./mod.ts",
"imports": {
"zod": "npm:zod@^3.23.8"
}
}
2 changes: 2 additions & 0 deletions nrcommon/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { z } from "zod";
export { z };
73 changes: 73 additions & 0 deletions nrcommon/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { z } from "./deps.ts"

import { version as PACKAGE_VERSION } from "./deno.json" with { type: "json" };
export const CONTENT_MINIMUM_LENGTH = 3;
export const CONTENT_MAXIMUM_LENGTH = 300;

function isHex(s: string) {
return s.split("").every((c) => "0123456789abcdef".split("").includes(c));
}

function isPlusCode(code: string) {
const re =
/(^|\s)([23456789C][23456789CFGHJMPQRV][23456789CFGHJMPQRVWX]{6}\+[23456789CFGHJMPQRVWX]{2,7})(\s|$)/i;
return re.test(code);
}

export const eventSchema = z
.object({
id: z.string().length(32),
pubkey: z.string().length(32),
kind: z.number(),
created_at: z.number(),
tags: z.string().array().array(),
content: z.string(),
sig: z.string(),
})
.strict();

export type Event = z.infer<typeof eventSchema>;

function hasOpenLocationCode(tags: string[][]): boolean {
const namespaces = tags
.filter((tag) => tag[0] === "L")
.map((tag) => tag.slice(1))
.flat();
const hasOpenLocationCodeNamespace = namespaces.includes("open-location-code");
if (!hasOpenLocationCodeNamespace) return false;

const plusCodeTags = tags.filter(
(tag) => tag.length > 3 && tag[0] === "l" && tag[2] === "open-location-code"
);
if (plusCodeTags.length === 0) return false;

const plusCodes = plusCodeTags.map((plusCodeTag) => plusCodeTag[1]);
const validPlusCodes = plusCodes.every(isPlusCode);

if (!validPlusCodes) return false;

return true;
}

function hasVersion(tags: string[][]): boolean {
const versionTags = tags.filter((tag) => tag[0] === "kind30398_version");
if (versionTags.length !== 1) return false;
const versionTag = versionTags[0];
if (versionTag.length !== 2) return false;
const version = versionTag[1];
if (version !== PACKAGE_VERSION) return false
return true
}

export const kind30398EventSchema = eventSchema.extend({
kind: z.literal(30398),
tags: z
.string()
.array()
.array()
.refine(hasOpenLocationCode, { message: "no valid open-location-code label" })
.refine(hasVersion, { message: "no valid kind30398_version" }),
content: z.string().max(CONTENT_MAXIMUM_LENGTH, `content is above max length of ${CONTENT_MAXIMUM_LENGTH}`).min(CONTENT_MINIMUM_LENGTH, `content is below min length of ${CONTENT_MINIMUM_LENGTH}`)
});

export type Kind30398Event = z.infer<typeof kind30398EventSchema>;
193 changes: 0 additions & 193 deletions nrserver/repost.ts

This file was deleted.

Loading