-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseArcadeRelay.ts
152 lines (135 loc) · 4.05 KB
/
useArcadeRelay.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
import { SetStateAction, useContext, useEffect, useRef, useState } from 'react'
import {
createNewAccount,
formatEvent,
getEventHash,
NostrEvent,
NostrEventToSerialize,
NostrEventToSign,
NostrKind,
signEvent,
} from '../nostr'
import { addEvent } from '../store'
import { createDemoChannelEvent } from '../demo/createDemoChannelEvent'
import { ArcadeContext } from '../context'
export type UseArcadeRelayState = {
isPaused: boolean
ready: boolean
}
export type UseArcadeRelayActions = {
createDemoChannel: () => void
initialSubscribe: () => void
sendChannelMessage: (channelId: string, message: string) => void
setPause: React.Dispatch<SetStateAction<boolean>>
}
type UseArcadeRelayFunction = () => [UseArcadeRelayState, UseArcadeRelayActions]
const subId = Math.random().toString().slice(2)
export const useArcadeRelay: UseArcadeRelayFunction = () => {
const context = useContext(ArcadeContext)
const [isPaused, setPause] = useState<boolean>(false)
const [ready, setReady] = useState<boolean>(false)
const ws = useRef<WebSocket | null>(null)
useEffect(() => {
if (!context) {
throw new Error('Missing Arcade context')
}
// ws.current = new WebSocket('wss://relay.damus.io')
ws.current = new WebSocket('wss://relay.arcade.city')
ws.current.onopen = () => {
console.log('ws opened')
setReady(true)
}
ws.current.onclose = () => {
console.log('ws closed')
setReady(false)
}
const wsCurrent = ws.current
context.ws = ws.current
return () => {
wsCurrent.close()
}
}, [context])
useEffect(() => {
if (!ws.current) return
ws.current.onmessage = (e) => {
if (isPaused) return
const message = JSON.parse(e.data)
const event = message[2]
addEvent(event)
}
}, [isPaused])
const initialSubscribe = () => {
if (!ws.current || ws.current.readyState !== WebSocket.OPEN) {
console.log('Not ready.')
setReady(false)
return
}
ws.current.send(
JSON.stringify(['REQ', subId, { kinds: [NostrKind.channelcreate, NostrKind.channelmessage] }]),
)
}
const createDemoChannel = async () => {
if (!ws.current || ws.current.readyState !== WebSocket.OPEN) {
console.log('Not ready.')
setReady(false)
return
}
const event = await createDemoChannelEvent()
const formattedEvent = formatEvent(event)
ws.current.send(formattedEvent)
console.log('Sent')
}
const sendChannelMessage = async (channelId: string, message: string) => {
if (!ws.current || ws.current.readyState !== WebSocket.OPEN) {
console.log('Not ready.')
setReady(false)
return
}
const event = await createMessageEvent(channelId, message)
const formattedEvent = formatEvent(event)
console.log('trying to send:', formattedEvent)
ws.current.send(formattedEvent)
}
const state: UseArcadeRelayState = { isPaused, ready }
const actions: UseArcadeRelayActions = { createDemoChannel, initialSubscribe, sendChannelMessage, setPause }
context.actions = actions
return [state, actions]
}
export const normalizeEvent = (event: any) =>
({
id: event.nid,
pubkey: event.pubkey,
created_at: event.created_at,
kind: event.kind,
tags: event.tags ?? [],
content: event.content,
sig: event.sig,
} as NostrEvent)
const createMessageEvent = async (channelId: string, message: string) => {
const { pubkey, privkey } = createNewAccount()
const date = new Date()
const dateTimeInSeconds = Math.floor(date.getTime() / 1000)
const nostrEventToSerialize: NostrEventToSerialize = {
created_at: dateTimeInSeconds,
kind: NostrKind.channelmessage,
tags: [],
content: JSON.stringify({
channelId,
text: message,
type: 'text',
}),
pubkey,
}
const id = getEventHash(nostrEventToSerialize)
const nostrEventToSign: NostrEventToSign = {
...nostrEventToSerialize,
id,
}
const sig = await signEvent(nostrEventToSign, privkey)
const nostrEvent: NostrEvent = {
...nostrEventToSerialize,
id,
sig,
}
return nostrEvent
}