-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnip-01.ts
172 lines (138 loc) · 4.21 KB
/
nip-01.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @file NIP-01: Basic protocol flow implementation
* @module nips/nip-01
*/
import { getLogger } from '../utils/logger.js';
import type { NostrWSMessage, NostrEvent } from '../types/messages.js';
import { MESSAGE_TYPES } from '../types/messages.js';
const logger = getLogger('NIP-01');
interface NostrEventValidationResult {
valid: boolean;
error?: string;
}
/**
* Validates a message according to NIP-01 specifications
*/
export function validateMessage(message: NostrWSMessage): boolean {
if (!message || !Array.isArray(message)) {
logger.debug('Invalid message format');
return false;
}
if (!message[0] || !(message[0] in MESSAGE_TYPES)) {
logger.debug(`Invalid message type: ${message[0]}`);
return false;
}
if (!message[1] || typeof message[1] !== 'object') {
logger.debug('Invalid message data');
return false;
}
// Message type specific validation
if (message[0] === MESSAGE_TYPES.EVENT) {
if (!validateEvent(message[1] as NostrEvent).valid) {
return false;
}
}
if (message[0] === MESSAGE_TYPES.REQ) {
if (!validateReqMessage(message)) {
return false;
}
}
if (message[0] === MESSAGE_TYPES.CLOSE) {
if (!validateCloseMessage(message)) {
return false;
}
}
return true;
}
/**
* Creates an EVENT message
*/
export function createEventMessage(event: NostrEvent): NostrWSMessage {
return [MESSAGE_TYPES.EVENT, event];
}
/**
* Creates a REQ message
*/
export function createReqMessage(subscriptionId: string, filters: Array<Record<string, unknown>>): NostrWSMessage {
return [MESSAGE_TYPES.REQ, {
subscription_id: subscriptionId,
filters
}];
}
/**
* Creates a CLOSE message
*/
export function createCloseMessage(subscriptionId: string): NostrWSMessage {
return [MESSAGE_TYPES.CLOSE, {
subscription_id: subscriptionId
}];
}
/**
* Creates a NOTICE message
*/
export function createNoticeMessage(message: string): NostrWSMessage {
return [MESSAGE_TYPES.NOTICE, {
message
}];
}
/**
* Validates a Nostr event according to NIP-01
*/
export function validateEvent(event: NostrEvent): NostrEventValidationResult {
if (typeof event !== 'object' || !event) {
return { valid: false, error: 'Event must be an object' };
}
const typedEvent = event;
if (!typedEvent.id || typeof typedEvent.id !== 'string') {
return { valid: false, error: 'Event must have a string id' };
}
if (!typedEvent.pubkey || typeof typedEvent.pubkey !== 'string') {
return { valid: false, error: 'Event must have a string pubkey' };
}
if (!typedEvent.created_at || typeof typedEvent.created_at !== 'number') {
return { valid: false, error: 'Event must have a number created_at' };
}
if (!typedEvent.kind || typeof typedEvent.kind !== 'number') {
return { valid: false, error: 'Event must have a number kind' };
}
if (!Array.isArray(typedEvent.tags)) {
return { valid: false, error: 'Event must have an array of tags' };
}
if (!typedEvent.content || typeof typedEvent.content !== 'string') {
return { valid: false, error: 'Event must have a string content' };
}
if (!typedEvent.sig || typeof typedEvent.sig !== 'string') {
return { valid: false, error: 'Event must have a string sig' };
}
// Validate tag structure
for (const tag of typedEvent.tags) {
if (!Array.isArray(tag) || tag.length === 0) {
return { valid: false, error: 'Each tag must be a non-empty array' };
}
if (typeof tag[0] !== 'string') {
return { valid: false, error: 'Tag identifier must be a string' };
}
}
return { valid: true };
}
// Private helper functions
function validateReqMessage(message: NostrWSMessage): boolean {
const { subscription_id, filters } = message[1] as any;
if (!subscription_id || typeof subscription_id !== 'string') {
logger.debug('Invalid subscription_id');
return false;
}
if (!Array.isArray(filters)) {
logger.debug('Invalid filters format');
return false;
}
return true;
}
function validateCloseMessage(message: NostrWSMessage): boolean {
const { subscription_id } = message[1] as any;
if (!subscription_id || typeof subscription_id !== 'string') {
logger.debug('Invalid subscription_id');
return false;
}
return true;
}