Skip to content

Commit

Permalink
refine schemas and implement into auditor
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Jan 7, 2025
1 parent 32e4db9 commit f0b3685
Show file tree
Hide file tree
Showing 39 changed files with 1,133 additions and 238 deletions.
7 changes: 5 additions & 2 deletions libraries/auditor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"@jspm/core": "^2.1.0",
"@nostrwatch/websocket": "workspace:^",
"ajv": "^8.17.1",
"browserify-zlib": "^0.2.0",
"buffer": "^6.0.3",
Expand All @@ -35,8 +36,10 @@
"tseep": "^1.3.1",
"url": "^0.11.4",
"util": "^0.12.5",
"ws": "^8.18.0",
"@nostrwatch/websocket": "workspace:^"
"ws": "^8.18.0"
},
"peerDependencies": {
"@nostrwatch/schemata": "workspace:^"
},
"devDependencies": {
"@apidevtools/json-schema-ref-parser": "^11.7.2",
Expand Down
2 changes: 1 addition & 1 deletion libraries/auditor/src/base/Sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventEmitter } from "tseep";

import { Ingestor } from "#base/Ingestor.js";
import Logger from "#base/Logger.js";
import type { WebSocketWrapper as WebSocket } from '#base/WebSocketWrapper.js';
import type { WebSocketWrapper as WebSocket } from '@nostrwatch/websocket';

import { Nip01ClientMessageGenerator } from "#src/nips/Nip01/utils/generators.js";
import type { Note, RelayEventMessage } from "#src/nips/Nip01/interfaces/index.js";
Expand Down
157 changes: 0 additions & 157 deletions libraries/auditor/src/base/WebSocketWrapper.ts

This file was deleted.

21 changes: 15 additions & 6 deletions libraries/auditor/src/nips/Nip01/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ import type {
ClientCloseMessage
} from './interfaces/index.js';

import {
noteSchema,
relayClosedSchema,
relayEoseSchema,
relayEventSchema,
relayNoticeSchema,
relayOkSchema
} from '@nostrwatch/schemata';

//nip01
export class Nip01 extends Suite implements ISuite {

Expand All @@ -27,15 +36,15 @@ export class Nip01 extends Suite implements ISuite {
}

public readonly messageValidators: Record<string, SchemaValidator<any>> = {
'EVENT': new SchemaValidator<RelayEventMessage>(schemata.RelayEventMessage),
'CLOSED': new SchemaValidator<RelayClosedMessage>(schemata.RelayClosedMessage),
'NOTICE': new SchemaValidator<RelayNoticeMessage>(schemata.RelayNoticeMessage),
'OK': new SchemaValidator<RelayOkMessage>(schemata.RelayOkMessage),
'EOSE': new SchemaValidator<RelayEoseMessage>(schemata.RelayEoseMessage)
'EVENT': new SchemaValidator<RelayEventMessage>(relayEventSchema),
'CLOSED': new SchemaValidator<RelayClosedMessage>(relayClosedSchema),
'NOTICE': new SchemaValidator<RelayNoticeMessage>(relayNoticeSchema),
'OK': new SchemaValidator<RelayOkMessage>(relayOkSchema),
'EOSE': new SchemaValidator<RelayEoseMessage>(relayEoseSchema)
};

public readonly jsonValidators: Record<string, SchemaValidator<any>> = {
'EVENT': new SchemaValidator<Note>(schemata.Note),
'EVENT': new SchemaValidator<Note>(noteSchema),
};

private subId: string = 'test';
Expand Down
2 changes: 1 addition & 1 deletion libraries/auditor/src/nips/Nip11/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { WebSocketWrapper as WebSocket } from '@nostrwatch/websocket';
import { Suite } from '#base/Suite.js';
import type { ISuite } from '#base/Suite.js';

import nip11Schema from './schemata/nip11.schema.json' with { type: "json" };
import { nip11Schema } from '@nostrwatch/schemata';
import { SchemaValidator } from '#src/base/SchemaValidator.js';

//nip01
Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,7 @@
{
"type": "array",
"items": [
{
"const": "AUTH"
},
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^[a-f0-9]{64}$",
"description": "32-byte lowercase hex-encoded sha256 hash of the serialized event data"
},
"pubkey": {
"type": "string",
"pattern": "^[a-f0-9]{64}$",
"description": "32-byte lowercase hex-encoded public key of the event creator"
},
"created_at": {
"type": "integer",
"minimum": 1000000000,
"maximum": 9999999999,
"description": "Unix timestamp in seconds (10-digit timestamp, not in milliseconds)"
},
"kind": {
"type": "integer",
"minimum": 0,
"maximum": 65535,
"description": "Integer between 0 and 65535"
},
"tags": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "string",
"description": "Arbitrary string within the tag array"
},
"minItems": 1
},
"description": "Array of arrays, each containing arbitrary strings as tags"
},
"content": {
"type": "string",
"description": "Arbitrary string content of the event"
},
"sig": {
"type": "string",
"pattern": "^[a-f0-9]{128}$",
"description": "64-byte lowercase hex-encoded signature of the sha256 hash of the serialized event data (same as 'id' field)"
}
},
"required": [
"id",
"pubkey",
"created_at",
"kind",
"tags",
"content",
"sig"
],
"additionalProperties": false
}
],
"minItems": 2,
"maxItems": 2
}
---
type: "array"
items:
- const: "AUTH"
- $ref: "@/note.yaml"
minItems: "2"
maxItems: "2"
1 change: 1 addition & 0 deletions libraries/schemata
Submodule schemata added at 3b4b8b
17 changes: 17 additions & 0 deletions libraries/schemata-js-ajv/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@nostrwatch/schemata-js-ajv",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@nostrwatch/schemata": "workspace:^",
"ajv": "^8.17.1",
"nostr-tools": "^2.10.4"
}
}
29 changes: 29 additions & 0 deletions libraries/schemata-js-ajv/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Ajv, { type ErrorObject } from 'ajv';
import * as NostrSchemata from '@nostrwatch/schemata'
import { type NostrEvent } from 'nostr-tools'

const ajv = new Ajv();

export type ValidatorResult = {
valid: boolean;
warnings: string[];
errors: ErrorObject[];
}

export const validate = (note: NostrEvent): ValidatorResult | undefined => {
const { kind } = note;
const result: ValidatorResult = { valid: false, errors: [], warnings: [] }
const schema = NostrSchemata?.[`kind${kind}Schema`];
if(!schema) {
result.warnings.push('No schema found for kind');
return result;
}
const validate = ajv.compile(schema);
const valid = validate(note);
if(valid) {
result.valid = true;
return result;
}
result.errors = validate.errors ?? [];
return result;
}
20 changes: 20 additions & 0 deletions libraries/schemata-old/nip01/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@nostrwatch/nip01-schema",
"version": "0.0.1",
"main": "dist/index.js",
"license": "MIT",
"dependencies": {
"json-loader": "0.5.7",
"ts-loader": "9.5.1",
"typescript": "5.5.4",
"webpack": "5.93.0",
"webpack-cli": "5.1.4",
"webpack-merge": "6.0.1",
"webpack-node-externals": "3.0.0",
"yaml-convert": "1.0.1"
},
"scripts": {
"prebuild": "mkdir -p dist && yaml-convert --pretty true --input src/schema.yaml --output src/schema.json && cp src/schema.json dist/schema.json",
"build": "tsc && webpack --mode development --stats verbose"
}
}
2 changes: 2 additions & 0 deletions libraries/schemata-old/nip01/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const schema = require('./schema.json')
module.exports = schema
Loading

0 comments on commit f0b3685

Please sign in to comment.