-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Trustroots/nrcommon
Nrcommon
- Loading branch information
Showing
7 changed files
with
84 additions
and
341 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@trustroots/nrcommon", | ||
"version": "0.0.1", | ||
"exports": "./mod.ts", | ||
"imports": { | ||
"zod": "npm:zod@^3.23.8" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { z } from "zod"; | ||
export { z }; |
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 |
---|---|---|
@@ -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>; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.