registry_wasm.mx raw
1 //go:build wasm
2 //:build wasm
3
4 package registry
5
6 import (
7 "git.smesh.lol/smesh/web/common/jsbridge/callback"
8 "unsafe"
9 )
10
11 // --- Identity state ---
12
13 //:wasmimport bridge registry_set_seckey
14 func wasmSetSeckey(ptr *byte, len int32, cap int32)
15
16 //:wasmimport bridge registry_seckey
17 func wasmSeckey(outPtr *int32, outLen *int32)
18
19 //:wasmimport bridge registry_set_pubkey
20 func wasmSetPubkey(ptr *byte, len int32, cap int32)
21
22 //:wasmimport bridge registry_pubkey
23 func wasmPubkey(outPtr *int32, outLen *int32)
24
25 //:wasmimport bridge registry_set_has_key
26 func wasmSetHasKey(v int32)
27
28 //:wasmimport bridge registry_has_key
29 func wasmHasKey() (n int32)
30
31 func SetSeckey(hex string) {
32 wasmSetSeckey(unsafe.StringData(hex), int32(len(hex)), int32(len(hex)))
33 }
34
35 func Seckey() (s string) {
36 var rPtr, rLen int32
37 wasmSeckey(&rPtr, &rLen)
38 if rLen <= 0 {
39 return ""
40 }
41 return unsafe.String((*byte)(unsafe.Pointer(uintptr(rPtr))), rLen)
42 }
43
44 func SetPubkey(pub string) {
45 wasmSetPubkey(unsafe.StringData(pub), int32(len(pub)), int32(len(pub)))
46 }
47
48 func Pubkey() (s string) {
49 var rPtr, rLen int32
50 wasmPubkey(&rPtr, &rLen)
51 if rLen <= 0 {
52 return ""
53 }
54 return unsafe.String((*byte)(unsafe.Pointer(uintptr(rPtr))), rLen)
55 }
56
57 func SetHasKey(v bool) {
58 if v {
59 wasmSetHasKey(1)
60 } else {
61 wasmSetHasKey(0)
62 }
63 }
64
65 func HasKey() (ok bool) { return wasmHasKey() != 0 }
66
67 // --- Register extension hooks (simple signatures use callback table) ---
68
69 //:wasmimport bridge registry_on_marmot_init
70 func wasmOnMarmotInit(cbID int32)
71
72 //:wasmimport bridge registry_on_marmot_send
73 func wasmOnMarmotSend(cbID int32)
74
75 //:wasmimport bridge registry_on_marmot_subscribe
76 func wasmOnMarmotSubscribe(cbID int32)
77
78 //:wasmimport bridge registry_on_marmot_publish_kp
79 func wasmOnMarmotPublishKP(cbID int32)
80
81 //:wasmimport bridge registry_on_marmot_list_groups
82 func wasmOnMarmotListGroups(cbID int32)
83
84 //:wasmimport bridge registry_on_save_dm_record
85 func wasmOnSaveDMRecord(cbID int32)
86
87 //:wasmimport bridge registry_on_broadcast_to_clients
88 func wasmOnBroadcastToClients(cbID int32)
89
90 //:wasmimport bridge registry_on_send_to_client
91 func wasmOnSendToClient(cbID int32)
92
93 func OnMarmotInit(fn func(string)) { wasmOnMarmotInit(callback.RegisterS(fn)) }
94 func OnMarmotSend(fn func(string, string)) { wasmOnMarmotSend(callback.RegisterSS(fn)) }
95 func OnMarmotSubscribe(fn func()) { wasmOnMarmotSubscribe(callback.Register0(fn)) }
96 func OnMarmotPublishKP(fn func(string)) { wasmOnMarmotPublishKP(callback.RegisterS(fn)) }
97 func OnMarmotListGroups(fn func(string)) { wasmOnMarmotListGroups(callback.RegisterS(fn)) }
98 func OnSaveDMRecord(fn func(string)) { wasmOnSaveDMRecord(callback.RegisterS(fn)) }
99 func OnBroadcastToClients(fn func(string)) { wasmOnBroadcastToClients(callback.RegisterS(fn)) }
100 func OnSendToClient(fn func(string, string)) { wasmOnSendToClient(callback.RegisterSS(fn)) }
101
102 // --- Hooks with nested response callbacks (custom wasmexport) ---
103
104 //:wasmimport bridge registry_on_encrypt_nip04
105 func wasmOnEncryptNip04()
106
107 //:wasmimport bridge registry_on_encrypt_nip17
108 func wasmOnEncryptNip17()
109
110 //:wasmimport bridge registry_on_decrypt_dm
111 func wasmOnDecryptDM()
112
113 //:wasmimport bridge registry_hook_respond_s
114 func wasmHookRespondS(reqID int32, ptr *byte, len int32, cap int32)
115
116 //:wasmimport bridge registry_hook_respond_ss
117 func wasmHookRespondSS(reqID int32, p1 *byte, l1 int32, c1 int32, p2 *byte, l2 int32, c2 int32)
118
119 var hookEncryptNip04 func(string, string, func(string))
120
121 func OnEncryptNip04(fn func(string, string, func(string))) {
122 hookEncryptNip04 = fn
123 wasmOnEncryptNip04()
124 }
125
126 //:wasmexport __hook_encrypt_nip04
127 func dispatchEncryptNip04(reqID int32, pkPtr *byte, pkLen int32, ctPtr *byte, ctLen int32) {
128 if hookEncryptNip04 == nil {
129 return
130 }
131 pk := ptrToString(pkPtr, pkLen)
132 ct := ptrToString(ctPtr, ctLen)
133 rid := reqID
134 hookEncryptNip04(pk, ct, func(result string) {
135 wasmHookRespondS(rid, unsafe.StringData(result), int32(len(result)), int32(len(result)))
136 })
137 }
138
139 var hookEncryptNip17 func(string, string, func(string, string))
140
141 func OnEncryptNip17(fn func(string, string, func(string, string))) {
142 hookEncryptNip17 = fn
143 wasmOnEncryptNip17()
144 }
145
146 //:wasmexport __hook_encrypt_nip17
147 func dispatchEncryptNip17(reqID int32, pkPtr *byte, pkLen int32, ctPtr *byte, ctLen int32) {
148 if hookEncryptNip17 == nil {
149 return
150 }
151 pk := ptrToString(pkPtr, pkLen)
152 ct := ptrToString(ctPtr, ctLen)
153 rid := reqID
154 hookEncryptNip17(pk, ct, func(a, b string) {
155 wasmHookRespondSS(rid,
156 unsafe.StringData(a), int32(len(a)), int32(len(a)),
157 unsafe.StringData(b), int32(len(b)), int32(len(b)),
158 )
159 })
160 }
161
162 var hookDecryptDM func(string, func(string))
163
164 func OnDecryptDM(fn func(string, func(string))) {
165 hookDecryptDM = fn
166 wasmOnDecryptDM()
167 }
168
169 //:wasmexport __hook_decrypt_dm
170 func dispatchDecryptDM(reqID int32, evPtr *byte, evLen int32) {
171 if hookDecryptDM == nil {
172 return
173 }
174 ev := ptrToString(evPtr, evLen)
175 rid := reqID
176 hookDecryptDM(ev, func(result string) {
177 wasmHookRespondS(rid, unsafe.StringData(result), int32(len(result)), int32(len(result)))
178 })
179 }
180
181 // --- Call extension hooks ---
182
183 //:wasmimport bridge registry_encrypt_nip04
184 func wasmEncryptNip04(pkPtr *byte, pkLen int32, pkCap int32, ctPtr *byte, ctLen int32, ctCap int32, cbID int32)
185
186 //:wasmimport bridge registry_encrypt_nip17
187 func wasmEncryptNip17(pkPtr *byte, pkLen int32, pkCap int32, ctPtr *byte, ctLen int32, ctCap int32, cbID int32)
188
189 //:wasmimport bridge registry_decrypt_dm
190 func wasmDecryptDM(ptr *byte, len int32, cap int32, cbID int32)
191
192 //:wasmimport bridge registry_make_dm_record
193 func wasmMakeDMRecord(peerPtr *byte, peerLen int32, peerCap int32, fromPtr *byte, fromLen int32, fromCap int32, contentPtr *byte, contentLen int32, contentCap int32, ts int64, protoPtr *byte, protoLen int32, protoCap int32, eidPtr *byte, eidLen int32, eidCap int32, outPtr *int32, outLen *int32)
194
195 //:wasmimport bridge registry_marmot_init
196 func wasmMarmotInit(ptr *byte, len int32, cap int32)
197
198 //:wasmimport bridge registry_marmot_send
199 func wasmMarmotSendMsg(rPtr *byte, rLen int32, rCap int32, cPtr *byte, cLen int32, cCap int32)
200
201 //:wasmimport bridge registry_marmot_subscribe
202 func wasmMarmotSubscribeCall()
203
204 //:wasmimport bridge registry_marmot_publish_kp
205 func wasmMarmotPublishKP(ptr *byte, len int32, cap int32)
206
207 //:wasmimport bridge registry_marmot_list_groups
208 func wasmMarmotListGroupsCall(ptr *byte, len int32, cap int32)
209
210 //:wasmimport bridge registry_save_dm_record
211 func wasmSaveDMRecord(ptr *byte, len int32, cap int32)
212
213 //:wasmimport bridge registry_broadcast_to_clients
214 func wasmBroadcastToClients(ptr *byte, len int32, cap int32)
215
216 //:wasmimport bridge registry_send_to_client
217 func wasmSendToClientCall(idPtr *byte, idLen int32, idCap int32, msgPtr *byte, msgLen int32, msgCap int32)
218
219 //:wasmimport bridge registry_has_hook
220 func wasmHasHook(ptr *byte, len int32, cap int32) (n int32)
221
222 //:wasmimport bridge registry_load_module
223 func wasmLoadModule(ptr *byte, len int32, cap int32, cbID int32)
224
225 func EncryptNip04(pubkey, content string, cb func(string)) {
226 wasmEncryptNip04(
227 unsafe.StringData(pubkey), int32(len(pubkey)), int32(len(pubkey)),
228 unsafe.StringData(content), int32(len(content)), int32(len(content)),
229 callback.RegisterSOnce(cb),
230 )
231 }
232
233 func EncryptNip17(pubkey, content string, cb func(string, string)) {
234 wasmEncryptNip17(
235 unsafe.StringData(pubkey), int32(len(pubkey)), int32(len(pubkey)),
236 unsafe.StringData(content), int32(len(content)), int32(len(content)),
237 callback.RegisterSSOnce(cb),
238 )
239 }
240
241 func DecryptDM(evJSON string, cb func(string)) {
242 wasmDecryptDM(unsafe.StringData(evJSON), int32(len(evJSON)), int32(len(evJSON)), callback.RegisterSOnce(cb))
243 }
244
245 func MakeDMRecord(peer, from, content string, ts int64, proto, eid string) (s string) {
246 var rPtr, rLen int32
247 wasmMakeDMRecord(
248 unsafe.StringData(peer), int32(len(peer)), int32(len(peer)),
249 unsafe.StringData(from), int32(len(from)), int32(len(from)),
250 unsafe.StringData(content), int32(len(content)), int32(len(content)),
251 ts,
252 unsafe.StringData(proto), int32(len(proto)), int32(len(proto)),
253 unsafe.StringData(eid), int32(len(eid)), int32(len(eid)),
254 &rPtr, &rLen,
255 )
256 if rLen <= 0 {
257 return ""
258 }
259 return unsafe.String((*byte)(unsafe.Pointer(uintptr(rPtr))), rLen)
260 }
261
262 func MarmotInit(relayURLsJSON string) {
263 wasmMarmotInit(unsafe.StringData(relayURLsJSON), int32(len(relayURLsJSON)), int32(len(relayURLsJSON)))
264 }
265
266 func MarmotSend(recipient, content string) {
267 wasmMarmotSendMsg(
268 unsafe.StringData(recipient), int32(len(recipient)), int32(len(recipient)),
269 unsafe.StringData(content), int32(len(content)), int32(len(content)),
270 )
271 }
272
273 func MarmotSubscribe() {
274 wasmMarmotSubscribeCall()
275 }
276
277 func MarmotPublishKP(relayURLsJSON string) {
278 wasmMarmotPublishKP(unsafe.StringData(relayURLsJSON), int32(len(relayURLsJSON)), int32(len(relayURLsJSON)))
279 }
280
281 func MarmotListGroups(clientID string) {
282 wasmMarmotListGroupsCall(unsafe.StringData(clientID), int32(len(clientID)), int32(len(clientID)))
283 }
284
285 func SaveDMRecord(dmJSON string) {
286 wasmSaveDMRecord(unsafe.StringData(dmJSON), int32(len(dmJSON)), int32(len(dmJSON)))
287 }
288
289 func BroadcastToClients(msg string) {
290 wasmBroadcastToClients(unsafe.StringData(msg), int32(len(msg)), int32(len(msg)))
291 }
292
293 func SendToClient(clientID, msg string) {
294 wasmSendToClientCall(
295 unsafe.StringData(clientID), int32(len(clientID)), int32(len(clientID)),
296 unsafe.StringData(msg), int32(len(msg)), int32(len(msg)),
297 )
298 }
299
300 func HasHook(name string) (ok bool) {
301 return wasmHasHook(unsafe.StringData(name), int32(len(name)), int32(len(name))) != 0
302 }
303
304 func LoadModule(name string, cb func()) {
305 wasmLoadModule(unsafe.StringData(name), int32(len(name)), int32(len(name)), callback.Register0Once(cb))
306 }
307
308 func ptrToString(ptr *byte, length int32) (s string) {
309 if ptr == nil || length <= 0 {
310 return ""
311 }
312 return unsafe.String(ptr, length)
313 }
314