holosphere 1.1.21 → 1.3.0-alpha0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/global.js CHANGED
@@ -730,11 +730,70 @@ export async function deleteAllGlobal(holoInstance, tableName, password = null)
730
730
  }
731
731
  }
732
732
 
733
+ /**
734
+ * Subscribe to real-time changes in a global table.
735
+ * @param {HoloSphere} holoInstance - The HoloSphere instance.
736
+ * @param {string} tableName - The table name to subscribe to.
737
+ * @param {string|null} key - Specific key to subscribe to, or null for all keys.
738
+ * @param {function} callback - Callback for data changes.
739
+ * @param {object} [options] - Subscription options.
740
+ * @param {boolean} [options.realtimeOnly] - Only fire for new changes.
741
+ * @returns {Promise<{ unsubscribe: () => void }>}
742
+ */
743
+ export async function subscribeGlobal(holoInstance, tableName, key, callback, options = {}) {
744
+ const dataPath = holoInstance.gun.get(holoInstance.appname).get(tableName);
745
+ let active = true;
746
+
747
+ if (key) {
748
+ // Subscribe to a specific key
749
+ dataPath.get(key).on(async (data) => {
750
+ if (!active || !data) return;
751
+ try {
752
+ const parsed = await holoInstance.parse(data);
753
+ if (parsed) callback(parsed, key);
754
+ } catch (e) {
755
+ console.warn('[subscribeGlobal] Error parsing data:', e);
756
+ }
757
+ });
758
+ } else {
759
+ // Subscribe to all keys in the table
760
+ dataPath.map().on(async (data, k) => {
761
+ if (!active || !data || k === '_') return;
762
+ try {
763
+ const parsed = await holoInstance.parse(data);
764
+ if (parsed) callback(parsed, k);
765
+ } catch (e) {
766
+ console.warn('[subscribeGlobal] Error parsing data:', e);
767
+ }
768
+ });
769
+ }
770
+
771
+ return {
772
+ unsubscribe: () => {
773
+ active = false;
774
+ if (key) {
775
+ dataPath.get(key).off();
776
+ } else {
777
+ dataPath.off();
778
+ }
779
+ },
780
+ stop: () => {
781
+ active = false;
782
+ if (key) {
783
+ dataPath.get(key).off();
784
+ } else {
785
+ dataPath.off();
786
+ }
787
+ }
788
+ };
789
+ }
790
+
733
791
  // Export all global operations as default
734
792
  export default {
735
793
  putGlobal,
736
794
  getGlobal,
737
795
  getAllGlobal,
738
796
  deleteGlobal,
739
- deleteAllGlobal
740
- };
797
+ deleteAllGlobal,
798
+ subscribeGlobal
799
+ };
@@ -0,0 +1,321 @@
1
+ /**
2
+ * GunDB-based federation handshake protocol for HoloSphere v1.3
3
+ * Replaces Nostr NIP-44 encrypted DMs with GunDB DM channels.
4
+ * Same protocol payloads (JSON with type: federation_request/response/update),
5
+ * different transport layer.
6
+ */
7
+
8
+ /**
9
+ * Generate a unique message ID
10
+ */
11
+ function generateMessageId() {
12
+ return Date.now().toString(36) + '_' + Math.random().toString(36).slice(2, 10);
13
+ }
14
+
15
+ /**
16
+ * Get the DM path for a recipient in GunDB
17
+ */
18
+ function getDMPath(holosphere, recipientPubKey) {
19
+ return holosphere.gun.get(holosphere.appname).get('_dm').get(recipientPubKey);
20
+ }
21
+
22
+ /**
23
+ * Write a DM to a recipient's channel
24
+ */
25
+ async function sendDM(holosphere, recipientPubKey, message) {
26
+ const msgId = generateMessageId();
27
+ const payload = JSON.stringify({
28
+ ...message,
29
+ id: msgId,
30
+ timestamp: Date.now()
31
+ });
32
+
33
+ return new Promise((resolve) => {
34
+ getDMPath(holosphere, recipientPubKey).get(msgId).put(payload, (ack) => {
35
+ if (ack.err) {
36
+ console.warn('[handshake] Failed to send DM:', ack.err);
37
+ resolve({ success: false, error: ack.err });
38
+ } else {
39
+ resolve({ success: true, id: msgId });
40
+ }
41
+ });
42
+ });
43
+ }
44
+
45
+ /**
46
+ * Subscribe to federation DMs for a public key.
47
+ * Listens for incoming federation requests, responses, and updates.
48
+ *
49
+ * @param {object} holosphere - The HoloSphere instance
50
+ * @param {string|Uint8Array} privateKey - The user's private key (for identity)
51
+ * @param {string} publicKey - The user's public key
52
+ * @param {object} handlers - Event handlers
53
+ * @param {function} handlers.onRequest - Called when a federation request is received
54
+ * @param {function} handlers.onResponse - Called when a federation response is received
55
+ * @param {function} handlers.onUpdate - Called when a federation update is received
56
+ * @param {function} handlers.onUpdateResponse - Called when an update response is received
57
+ * @returns {function} Unsubscribe function
58
+ */
59
+ export function subscribeToFederationDMs(holosphere, privateKey, publicKey, handlers) {
60
+ const dmPath = getDMPath(holosphere, publicKey);
61
+ let active = true;
62
+ const processedMessages = new Set();
63
+
64
+ dmPath.map().on((data, key) => {
65
+ if (!active || !data || key === '_') return;
66
+
67
+ // Avoid processing the same message twice
68
+ if (processedMessages.has(key)) return;
69
+ processedMessages.add(key);
70
+
71
+ try {
72
+ const message = typeof data === 'string' ? JSON.parse(data) : data;
73
+ const senderPubKey = message.senderPubKey || message.sender || '';
74
+
75
+ switch (message.type) {
76
+ case 'federation_request':
77
+ if (handlers.onRequest) {
78
+ handlers.onRequest(message, senderPubKey);
79
+ }
80
+ break;
81
+ case 'federation_response':
82
+ if (handlers.onResponse) {
83
+ handlers.onResponse(message, senderPubKey);
84
+ }
85
+ break;
86
+ case 'federation_update':
87
+ if (handlers.onUpdate) {
88
+ handlers.onUpdate(message, senderPubKey);
89
+ }
90
+ break;
91
+ case 'federation_update_response':
92
+ if (handlers.onUpdateResponse) {
93
+ handlers.onUpdateResponse(message, senderPubKey);
94
+ }
95
+ break;
96
+ default:
97
+ console.log('[handshake] Unknown DM type:', message.type);
98
+ }
99
+ } catch (e) {
100
+ // Skip unparseable messages
101
+ }
102
+ });
103
+
104
+ // Return unsubscribe function
105
+ return () => {
106
+ active = false;
107
+ dmPath.off();
108
+ };
109
+ }
110
+
111
+ /**
112
+ * Initiate a federation handshake with a partner.
113
+ *
114
+ * @param {object} holosphere - The HoloSphere instance
115
+ * @param {string|Uint8Array} privateKey - The initiator's private key
116
+ * @param {object} params - Handshake parameters
117
+ * @param {string} params.partnerPubKey - The partner's public key
118
+ * @param {string} params.holonId - The initiator's holon ID
119
+ * @param {string} params.holonName - The initiator's holon name
120
+ * @param {object} [params.lensConfig] - Lens configuration to share
121
+ * @param {string} [params.message] - Optional message
122
+ * @returns {Promise<{ success: boolean, requestId?: string }>}
123
+ */
124
+ export async function initiateFederationHandshake(holosphere, privateKey, params) {
125
+ const {
126
+ partnerPubKey,
127
+ holonId,
128
+ holonName,
129
+ lensConfig = {},
130
+ message = ''
131
+ } = params;
132
+
133
+ const senderPubKey = holosphere.client?.publicKey || '';
134
+
135
+ const request = {
136
+ type: 'federation_request',
137
+ senderPubKey,
138
+ senderHolonId: holonId,
139
+ senderHolonName: holonName,
140
+ lensConfig,
141
+ message,
142
+ status: 'pending'
143
+ };
144
+
145
+ const result = await sendDM(holosphere, partnerPubKey, request);
146
+ if (result.success) {
147
+ console.log('[handshake] Federation request sent to:', partnerPubKey?.slice(0, 8));
148
+ }
149
+ return { success: result.success, requestId: result.id };
150
+ }
151
+
152
+ /**
153
+ * Accept a federation request.
154
+ *
155
+ * @param {object} holosphere - The HoloSphere instance
156
+ * @param {string|Uint8Array} privateKey - The responder's private key
157
+ * @param {object} params - Accept parameters
158
+ * @param {string} params.requesterPubKey - The requester's public key
159
+ * @param {string} params.holonId - The responder's holon ID
160
+ * @param {string} params.holonName - The responder's holon name
161
+ * @param {object} [params.lensConfig] - Lens configuration
162
+ * @param {string} [params.requestId] - Original request ID
163
+ * @returns {Promise<{ success: boolean }>}
164
+ */
165
+ export async function acceptFederationRequest(holosphere, privateKey, params) {
166
+ const {
167
+ requesterPubKey,
168
+ holonId,
169
+ holonName,
170
+ lensConfig = {},
171
+ requestId
172
+ } = params;
173
+
174
+ const senderPubKey = holosphere.client?.publicKey || '';
175
+
176
+ const response = {
177
+ type: 'federation_response',
178
+ senderPubKey,
179
+ responderHolonId: holonId,
180
+ responderHolonName: holonName,
181
+ lensConfig,
182
+ status: 'accepted',
183
+ requestId
184
+ };
185
+
186
+ // Add the requester as an allowed author
187
+ if (requesterPubKey) {
188
+ holosphere.addAllowedAuthor(requesterPubKey);
189
+ }
190
+
191
+ return sendDM(holosphere, requesterPubKey, response);
192
+ }
193
+
194
+ /**
195
+ * Reject a federation request.
196
+ */
197
+ export async function rejectFederationRequest(holosphere, privateKey, params) {
198
+ const { requesterPubKey, holonId, reason = '', requestId } = params;
199
+ const senderPubKey = holosphere.client?.publicKey || '';
200
+
201
+ const response = {
202
+ type: 'federation_response',
203
+ senderPubKey,
204
+ responderHolonId: holonId,
205
+ status: 'rejected',
206
+ reason,
207
+ requestId
208
+ };
209
+
210
+ return sendDM(holosphere, requesterPubKey, response);
211
+ }
212
+
213
+ /**
214
+ * Process a received federation response.
215
+ * Creates the federation relationship on the initiator's side.
216
+ *
217
+ * @param {object} holosphere - The HoloSphere instance
218
+ * @param {object} response - The response object
219
+ * @param {string} senderPubKey - The responder's public key
220
+ * @param {object} options - Processing options
221
+ * @param {string} options.holonId - The initiator's holon ID
222
+ * @param {string[]} [options.inboundLenses] - Lenses to accept inbound data for
223
+ * @returns {Promise<{ success: boolean }>}
224
+ */
225
+ export async function processFederationResponse(holosphere, response, senderPubKey, options = {}) {
226
+ const { holonId, inboundLenses = [] } = options;
227
+
228
+ if (response.status !== 'accepted') {
229
+ return { success: false, reason: 'not_accepted' };
230
+ }
231
+
232
+ try {
233
+ // Add the responder as an allowed author
234
+ if (senderPubKey) {
235
+ holosphere.addAllowedAuthor(senderPubKey);
236
+ }
237
+
238
+ // Store the responder's holon ID for federation lookup
239
+ const responderHolonId = response.responderHolonId || senderPubKey;
240
+
241
+ console.log('[handshake] Processing accepted federation from:', responderHolonId?.slice(0, 8));
242
+ return { success: true, responderHolonId };
243
+ } catch (error) {
244
+ console.error('[handshake] Error processing federation response:', error);
245
+ return { success: false, error: error.message };
246
+ }
247
+ }
248
+
249
+ /**
250
+ * Request a federation update (e.g., change shared lenses).
251
+ *
252
+ * @param {object} holosphere - The HoloSphere instance
253
+ * @param {string|Uint8Array} privateKey - The requester's private key
254
+ * @param {object} params - Update parameters
255
+ * @param {string} params.partnerPubKey - The partner's public key
256
+ * @param {string} params.holonId - The requester's holon ID
257
+ * @param {string} params.holonName - The requester's holon name
258
+ * @param {object} params.newLensConfig - The new lens configuration
259
+ * @param {string} [params.message] - Optional message
260
+ * @returns {Promise<{ success: boolean }>}
261
+ */
262
+ export async function requestFederationUpdate(holosphere, privateKey, params) {
263
+ const {
264
+ partnerPubKey,
265
+ holonId,
266
+ holonName,
267
+ newLensConfig = {},
268
+ message = ''
269
+ } = params;
270
+
271
+ const senderPubKey = holosphere.client?.publicKey || '';
272
+
273
+ const update = {
274
+ type: 'federation_update',
275
+ senderPubKey,
276
+ senderHolonId: holonId,
277
+ senderHolonName: holonName,
278
+ newLensConfig,
279
+ message
280
+ };
281
+
282
+ return sendDM(holosphere, partnerPubKey, update);
283
+ }
284
+
285
+ /**
286
+ * Accept a federation update request.
287
+ */
288
+ export async function acceptFederationUpdate(holosphere, privateKey, params) {
289
+ const { requesterPubKey, holonId, newLensConfig = {}, updateId } = params;
290
+ const senderPubKey = holosphere.client?.publicKey || '';
291
+
292
+ const response = {
293
+ type: 'federation_update_response',
294
+ senderPubKey,
295
+ responderHolonId: holonId,
296
+ status: 'accepted',
297
+ newLensConfig,
298
+ updateId
299
+ };
300
+
301
+ return sendDM(holosphere, requesterPubKey, response);
302
+ }
303
+
304
+ /**
305
+ * Reject a federation update request.
306
+ */
307
+ export async function rejectFederationUpdate(holosphere, privateKey, params) {
308
+ const { requesterPubKey, holonId, reason = '', updateId } = params;
309
+ const senderPubKey = holosphere.client?.publicKey || '';
310
+
311
+ const response = {
312
+ type: 'federation_update_response',
313
+ senderPubKey,
314
+ responderHolonId: holonId,
315
+ status: 'rejected',
316
+ reason,
317
+ updateId
318
+ };
319
+
320
+ return sendDM(holosphere, requesterPubKey, response);
321
+ }