shogun-core 6.9.4 → 6.9.6

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.
Files changed (41) hide show
  1. package/README.md +135 -117
  2. package/dist/browser/shogun-core.js +53280 -118158
  3. package/dist/browser/shogun-core.js.map +1 -1
  4. package/dist/src/examples/auth-test.js +0 -5
  5. package/dist/src/gundb/db-holster.js +46 -0
  6. package/dist/src/gundb/db.js +634 -3
  7. package/dist/src/gundb/derive.js +14 -20
  8. package/dist/src/gundb/rxjs-holster.js +23 -7
  9. package/dist/src/gundb/rxjs.js +45 -22
  10. package/dist/src/interfaces/shogun.js +0 -2
  11. package/dist/src/managers/CoreInitializer.js +37 -45
  12. package/dist/src/plugins/index.js +0 -4
  13. package/dist/src/plugins/web3/web3ConnectorPlugin.js +0 -1
  14. package/dist/src/plugins/web3/web3Signer.js +0 -1
  15. package/dist/src/plugins/webauthn/webauthn.js +28 -10
  16. package/dist/src/plugins/webauthn/webauthnSigner.js +5 -26
  17. package/dist/src/utils/deepEqual.js +48 -0
  18. package/dist/tsconfig.tsbuildinfo +1 -1
  19. package/dist/types/src/gundb/db.d.ts +156 -1
  20. package/dist/types/src/gundb/types.d.ts +11 -9
  21. package/dist/types/src/interfaces/events.d.ts +2 -2
  22. package/dist/types/src/interfaces/shogun.d.ts +1 -9
  23. package/dist/types/src/managers/CoreInitializer.d.ts +7 -0
  24. package/dist/types/src/plugins/index.d.ts +0 -5
  25. package/dist/types/src/plugins/web3/web3Signer.d.ts +0 -1
  26. package/dist/types/src/utils/deepEqual.d.ts +10 -0
  27. package/package.json +3 -11
  28. package/dist/src/examples/zkproof-credentials-example.js +0 -357
  29. package/dist/src/examples/zkproof-example.js +0 -357
  30. package/dist/src/plugins/zkproof/index.js +0 -46
  31. package/dist/src/plugins/zkproof/types.js +0 -1
  32. package/dist/src/plugins/zkproof/zkCredentials.js +0 -287
  33. package/dist/src/plugins/zkproof/zkProofConnector.js +0 -267
  34. package/dist/src/plugins/zkproof/zkProofPlugin.js +0 -405
  35. package/dist/types/src/examples/zkproof-credentials-example.d.ts +0 -12
  36. package/dist/types/src/examples/zkproof-example.d.ts +0 -11
  37. package/dist/types/src/plugins/zkproof/index.d.ts +0 -48
  38. package/dist/types/src/plugins/zkproof/types.d.ts +0 -123
  39. package/dist/types/src/plugins/zkproof/zkCredentials.d.ts +0 -112
  40. package/dist/types/src/plugins/zkproof/zkProofConnector.d.ts +0 -46
  41. package/dist/types/src/plugins/zkproof/zkProofPlugin.d.ts +0 -76
package/README.md CHANGED
@@ -12,7 +12,7 @@ Shogun Core is a comprehensive SDK for building decentralized applications (dApp
12
12
 
13
13
  ## Features
14
14
 
15
- - 🔐 **Multiple Authentication Methods**: Password, WebAuthn (biometrics), Web3 (MetaMask), Nostr, ZK-Proof (anonymous), Challenge (Server-Signed)
15
+ - 🔐 **Multiple Authentication Methods**: Password, WebAuthn (biometrics), Web3 (MetaMask), Nostr, Challenge (Server-Signed)
16
16
  - 🌐 **Decentralized Storage**: Built on GunDB or Holster for peer-to-peer data synchronization
17
17
  - 🔄 **Dual Database Support**: Use Gun (maximum compatibility) or Holster (modern ES modules, improved performance)
18
18
  - 🔌 **Plugin System**: Extensible architecture with built-in plugins
@@ -34,62 +34,56 @@ yarn add shogun-core
34
34
  ### Using Gun (Default)
35
35
 
36
36
  ```typescript
37
- import { ShogunCore } from "shogun-core";
38
- import Gun from "gun";
37
+ import { ShogunCore } from 'shogun-core';
38
+ import Gun from 'gun';
39
39
 
40
40
  // Create Gun instance first
41
- const gun = Gun({
42
- peers: ['https://gun-manhattan.herokuapp.com/gun']
41
+ const gun = Gun({
42
+ peers: ['https://gun-manhattan.herokuapp.com/gun'],
43
43
  });
44
44
 
45
45
  // Initialize Shogun Core with the Gun instance
46
46
  const shogun = new ShogunCore({
47
47
  gunInstance: gun,
48
-
48
+
49
49
  // Enable authentication plugins
50
50
  web3: { enabled: true },
51
- webauthn: {
51
+ webauthn: {
52
52
  enabled: true,
53
- rpName: "My Awesome App",
53
+ rpName: 'My Awesome App',
54
54
  rpId: window.location.hostname,
55
55
  },
56
56
  nostr: { enabled: true },
57
- zkproof: {
57
+ zkproof: {
58
58
  enabled: true,
59
- defaultGroupId: "my-app-users",
59
+ defaultGroupId: 'my-app-users',
60
60
  },
61
61
  });
62
-
63
-
64
62
  ```
65
63
 
66
64
  ### Using Holster (Alternative)
67
65
 
68
66
  ```typescript
69
- import { ShogunCore } from "shogun-core";
70
- import Holster from "@mblaney/holster";
67
+ import { ShogunCore } from 'shogun-core';
68
+ import Holster from '@mblaney/holster';
71
69
 
72
70
  // Create Holster instance first
73
- const holster = Holster({
74
- peers: ['ws://localhost:8765']
71
+ const holster = Holster({
72
+ peers: ['ws://localhost:8765'],
75
73
  });
76
74
 
77
75
  // Initialize Shogun Core with the Holster instance
78
76
  const shogun = new ShogunCore({
79
- holsterInstance: holster, // Use Holster instead of Gun
80
-
77
+ holsterInstance: holster, // Use Holster instead of Gun
78
+
81
79
  // Enable authentication plugins (same as Gun)
82
80
  web3: { enabled: true },
83
- webauthn: {
81
+ webauthn: {
84
82
  enabled: true,
85
- rpName: "My Awesome App",
83
+ rpName: 'My Awesome App',
86
84
  rpId: window.location.hostname,
87
85
  },
88
86
  nostr: { enabled: true },
89
- zkproof: {
90
- enabled: true,
91
- defaultGroupId: "my-app-users",
92
- },
93
87
  });
94
88
 
95
89
  // All other APIs work the same way!
@@ -100,17 +94,22 @@ const shogun = new ShogunCore({
100
94
  ```typescript
101
95
  const db = shogun.db;
102
96
 
103
- // Store data using Gun chaining
97
+ // Store data using Gun chaining (Traditional Method)
104
98
  await db.get('users').get('alice').get('profile').put({
105
99
  name: 'Alice Smith',
106
- email: 'alice@example.com'
100
+ email: 'alice@example.com',
107
101
  });
108
102
 
109
103
  // Read data
110
104
  const profile = await db.get('users').get('alice').get('profile').once().then();
111
105
 
112
106
  // Update specific fields
113
- await db.get('users').get('alice').get('profile').get('name').put('Alice Johnson');
107
+ await db
108
+ .get('users')
109
+ .get('alice')
110
+ .get('profile')
111
+ .get('name')
112
+ .put('Alice Johnson');
114
113
 
115
114
  // Iterate over collections with .map()
116
115
  db.get('users').map((user, userId) => {
@@ -118,45 +117,88 @@ db.get('users').map((user, userId) => {
118
117
  });
119
118
  ```
120
119
 
120
+ ### Promise-based Advanced Utilities (Firegun API)
121
+
122
+ Shogun Core includes built-in Promise wrappers, timeouts, and user-space utilities directly in the `DataBase` class:
123
+
124
+ ```typescript
125
+ const db = shogun.db;
126
+
127
+ // Promise-based Root GET with auto-retry
128
+ const data = await db.Get('public/posts/123');
129
+
130
+ // Promise-based Root PUT (supports deep object merging)
131
+ await db.Put('public/settings', { theme: 'dark' }, true);
132
+
133
+ // Listen to node changes with easy unsubscribe
134
+ await db.On(
135
+ 'public/feed',
136
+ (data) => console.log('Feed updated:', data),
137
+ 'myFeedListener',
138
+ );
139
+ await db.Off('myFeedListener'); // Cleanly remove the listener
140
+
141
+ // Content Addressed Storage (CAS)
142
+ // Hashes the data automatically using SHA-256 and saves it immutably
143
+ const ack = await db.addContentAdressing('#immutable-posts', {
144
+ text: 'Hello Web3!',
145
+ });
146
+
147
+ // --- User Space Operations ---
148
+ // These automatically prefix the path with `~pubkey/` of the logged-in user
149
+
150
+ // Read from current user's graph
151
+ const userSettings = await db.userGet('settings');
152
+
153
+ // Write to current user's graph
154
+ await db.userPut('profile', { name: 'Alice' });
155
+
156
+ // Delete user node (Tombstoning)
157
+ await db.userDel('old-post');
158
+
159
+ // Purge (nullify all keys in a node)
160
+ await db.purge('~pubkey/public/posts/123');
161
+ ```
162
+
121
163
  ## Authentication Methods
122
164
 
123
165
  ### 1. Traditional Authentication
124
166
 
125
167
  ```typescript
126
168
  // Sign up
127
- const signUpResult = await shogun.signUp("username", "password");
169
+ const signUpResult = await shogun.signUp('username', 'password');
128
170
  if (signUpResult.success) {
129
- console.log("User created:", signUpResult.username);
171
+ console.log('User created:', signUpResult.username);
130
172
  }
131
173
 
132
174
  // Login
133
- const loginResult = await shogun.login("username", "password");
175
+ const loginResult = await shogun.login('username', 'password');
134
176
  if (loginResult.success) {
135
- console.log("Logged in as:", loginResult.username);
177
+ console.log('Logged in as:', loginResult.username);
136
178
  }
137
179
  ```
138
180
 
139
181
  ### 2. Web3 Plugin API
140
182
 
141
183
  ```typescript
142
- const web3Plugin = shogun.getPlugin("web3");
184
+ const web3Plugin = shogun.getPlugin('web3');
143
185
 
144
186
  if (web3Plugin && web3Plugin.isAvailable()) {
145
187
  const connectionResult = await web3Plugin.connectMetaMask();
146
-
188
+
147
189
  if (connectionResult.success) {
148
190
  const address = connectionResult.address!;
149
-
191
+
150
192
  // Login with Web3 wallet
151
193
  const loginResult = await web3Plugin.login(address);
152
194
  if (loginResult.success) {
153
- console.log("Web3 login successful");
195
+ console.log('Web3 login successful');
154
196
  }
155
-
197
+
156
198
  // Register new user
157
199
  const signUpResult = await web3Plugin.signUp(address);
158
200
  if (signUpResult.success) {
159
- console.log("Web3 registration successful");
201
+ console.log('Web3 registration successful');
160
202
  }
161
203
  }
162
204
  }
@@ -165,28 +207,28 @@ if (web3Plugin && web3Plugin.isAvailable()) {
165
207
  ### 3. WebAuthn Plugin API
166
208
 
167
209
  ```typescript
168
- const webauthnPlugin = shogun.getPlugin("webauthn");
210
+ const webauthnPlugin = shogun.getPlugin('webauthn');
169
211
 
170
212
  if (webauthnPlugin && webauthnPlugin.isSupported()) {
171
213
  // Register with seed phrase for multi-device support
172
- const signUpResult = await webauthnPlugin.signUp("username", {
173
- generateSeedPhrase: true
214
+ const signUpResult = await webauthnPlugin.signUp('username', {
215
+ generateSeedPhrase: true,
174
216
  });
175
-
217
+
176
218
  if (signUpResult.success && signUpResult.seedPhrase) {
177
- console.log("🔑 SAVE THESE 12 WORDS:", signUpResult.seedPhrase);
219
+ console.log('🔑 SAVE THESE 12 WORDS:', signUpResult.seedPhrase);
178
220
  }
179
221
 
180
222
  // Import account on another device
181
223
  const importResult = await webauthnPlugin.importFromSeed(
182
- "username",
183
- "word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12"
224
+ 'username',
225
+ 'word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12',
184
226
  );
185
-
227
+
186
228
  // Authenticate
187
- const loginResult = await webauthnPlugin.login("username");
229
+ const loginResult = await webauthnPlugin.login('username');
188
230
  if (loginResult.success) {
189
- console.log("WebAuthn authentication successful");
231
+ console.log('WebAuthn authentication successful');
190
232
  }
191
233
  }
192
234
  ```
@@ -194,64 +236,41 @@ if (webauthnPlugin && webauthnPlugin.isSupported()) {
194
236
  ### 4. Nostr Plugin API
195
237
 
196
238
  ```typescript
197
- const nostrPlugin = shogun.getPlugin("nostr");
239
+ const nostrPlugin = shogun.getPlugin('nostr');
198
240
 
199
241
  if (nostrPlugin && nostrPlugin.isAvailable()) {
200
242
  const connectionResult = await nostrPlugin.connectNostrWallet();
201
-
243
+
202
244
  if (connectionResult.success) {
203
245
  const address = connectionResult.address!;
204
-
246
+
205
247
  const loginResult = await nostrPlugin.login(address);
206
248
  if (loginResult.success) {
207
- console.log("Nostr login successful");
249
+ console.log('Nostr login successful');
208
250
  }
209
-
251
+
210
252
  const signUpResult = await nostrPlugin.signUp(address);
211
253
  if (signUpResult.success) {
212
- console.log("Nostr registration successful");
254
+ console.log('Nostr registration successful');
213
255
  }
214
256
  }
215
257
  }
216
258
  ```
217
259
 
218
- ### 5. ZK-Proof Plugin API
219
-
220
- ```typescript
221
- const zkPlugin = shogun.getPlugin("zkproof");
222
-
223
- if (zkPlugin && zkPlugin.isAvailable()) {
224
- // Sign up with ZK-Proof (creates anonymous identity)
225
- const signUpResult = await zkPlugin.signUp();
226
-
227
- if (signUpResult.success && signUpResult.seedPhrase) {
228
- console.log("🔑 SAVE THIS TRAPDOOR:", signUpResult.seedPhrase);
229
- }
230
-
231
- // Login with trapdoor (anonymous authentication)
232
- const loginResult = await zkPlugin.login(trapdoor);
233
- if (loginResult.success) {
234
- console.log("ZK-Proof login successful (anonymous)");
235
- }
236
- }
237
- ```
238
-
239
-
240
-
241
260
  ### 7. Challenge Auth Plugin API
242
261
 
243
262
  The `Challenge` plugin allows for server-signed challenges to verify user authenticity without exposing private keys directly, useful for certain server-side integrations.
244
263
 
245
264
  ```typescript
246
- const challengePlugin = shogun.getPlugin("challenge");
265
+ const challengePlugin = shogun.getPlugin('challenge');
247
266
 
248
267
  if (challengePlugin) {
249
268
  // Initiate a challenge
250
269
  const challenge = await challengePlugin.createChallenge();
251
-
270
+
252
271
  // User signs the challenge
253
272
  const signature = await user.sign(challenge);
254
-
273
+
255
274
  // Verify the signature
256
275
  const isValid = await challengePlugin.verify(challenge, signature);
257
276
  }
@@ -262,29 +281,29 @@ if (challengePlugin) {
262
281
  Shogun Core supports BIP39 mnemonics and Hierarchical Deterministic (HD) key derivation, allowing you to generate multiple purpose-specific keys from a single seed.
263
282
 
264
283
  ```typescript
265
- import {
266
- generateSeedPhrase,
267
- validateSeedPhrase,
284
+ import {
285
+ generateSeedPhrase,
286
+ validateSeedPhrase,
268
287
  seedToKeyPair,
269
- deriveChildKey
270
- } from "shogun-core";
288
+ deriveChildKey,
289
+ } from 'shogun-core';
271
290
 
272
291
  // 1. Generate a new 12-word mnemonic
273
292
  const mnemonic = generateSeedPhrase();
274
- console.log("Secret Mnemonic:", mnemonic);
293
+ console.log('Secret Mnemonic:', mnemonic);
275
294
 
276
295
  // 2. Validate a mnemonic
277
296
  const isValid = validateSeedPhrase(mnemonic);
278
297
 
279
298
  // 3. Convert mnemonic to a master SEA pair
280
- const masterPair = await seedToKeyPair(mnemonic, "my-username");
299
+ const masterPair = await seedToKeyPair(mnemonic, 'my-username');
281
300
 
282
301
  // 4. Derive child keys for specific purposes (HD Wallet style)
283
- const chatPair = await deriveChildKey(masterPair, "messaging");
284
- const walletPair = await deriveChildKey(masterPair, "payment");
302
+ const chatPair = await deriveChildKey(masterPair, 'messaging');
303
+ const walletPair = await deriveChildKey(masterPair, 'payment');
285
304
 
286
- console.log("Master Pub:", masterPair.pub);
287
- console.log("Chat Pub:", chatPair.pub);
305
+ console.log('Master Pub:', masterPair.pub);
306
+ console.log('Chat Pub:', chatPair.pub);
288
307
  ```
289
308
 
290
309
  ## Browser Usage (CDN)
@@ -305,8 +324,8 @@ console.log("Chat Pub:", chatPair.pub);
305
324
 
306
325
  <script>
307
326
  // Create Gun instance first
308
- const gun = Gun({
309
- peers: ["https://gun-manhattan.herokuapp.com/gun"]
327
+ const gun = Gun({
328
+ peers: ['https://gun-manhattan.herokuapp.com/gun'],
310
329
  });
311
330
 
312
331
  // Initialize Shogun Core
@@ -315,12 +334,12 @@ console.log("Chat Pub:", chatPair.pub);
315
334
  web3: { enabled: true },
316
335
  webauthn: {
317
336
  enabled: true,
318
- rpName: "My Browser dApp",
337
+ rpName: 'My Browser dApp',
319
338
  rpId: window.location.hostname,
320
339
  },
321
340
  });
322
341
 
323
- console.log("Shogun Core initialized in browser!", shogunCore);
342
+ console.log('Shogun Core initialized in browser!', shogunCore);
324
343
  </script>
325
344
  </body>
326
345
  </html>
@@ -341,8 +360,8 @@ console.log("Chat Pub:", chatPair.pub);
341
360
  import { ShogunCore } from 'https://cdn.jsdelivr.net/npm/shogun-core/dist/src/index.js';
342
361
 
343
362
  // Create Holster instance
344
- const holster = Holster({
345
- peers: ['ws://localhost:8765']
363
+ const holster = Holster({
364
+ peers: ['ws://localhost:8765'],
346
365
  });
347
366
 
348
367
  // Initialize Shogun Core
@@ -351,12 +370,12 @@ console.log("Chat Pub:", chatPair.pub);
351
370
  web3: { enabled: true },
352
371
  webauthn: {
353
372
  enabled: true,
354
- rpName: "My Browser dApp",
373
+ rpName: 'My Browser dApp',
355
374
  rpId: window.location.hostname,
356
375
  },
357
376
  });
358
377
 
359
- console.log("Shogun Core initialized with Holster!", shogunCore);
378
+ console.log('Shogun Core initialized with Holster!', shogunCore);
360
379
  </script>
361
380
  </body>
362
381
  </html>
@@ -366,22 +385,22 @@ console.log("Chat Pub:", chatPair.pub);
366
385
 
367
386
  ```typescript
368
387
  // Listen for authentication events
369
- shogun.on("auth:login", (data) => {
370
- console.log("User logged in:", data.username);
371
- console.log("Authentication method:", data.method);
388
+ shogun.on('auth:login', (data) => {
389
+ console.log('User logged in:', data.username);
390
+ console.log('Authentication method:', data.method);
372
391
  });
373
392
 
374
- shogun.on("auth:logout", () => {
375
- console.log("User logged out");
393
+ shogun.on('auth:logout', () => {
394
+ console.log('User logged out');
376
395
  });
377
396
 
378
- shogun.on("auth:signup", (data) => {
379
- console.log("New user signed up:", data.username);
397
+ shogun.on('auth:signup', (data) => {
398
+ console.log('New user signed up:', data.username);
380
399
  });
381
400
 
382
401
  // Listen for errors
383
- shogun.on("error", (error) => {
384
- console.error("Shogun error:", error.message);
402
+ shogun.on('error', (error) => {
403
+ console.error('Shogun error:', error.message);
385
404
  });
386
405
  ```
387
406
 
@@ -390,11 +409,13 @@ shogun.on("error", (error) => {
390
409
  Shogun Core supports both Gun and Holster as database backends. Choose based on your needs:
391
410
 
392
411
  ### Gun (Default)
412
+
393
413
  - **Pros**: Maximum compatibility, large ecosystem, battle-tested
394
414
  - **Cons**: Older codebase, CommonJS modules
395
415
  - **Best for**: Existing projects, maximum compatibility
396
416
 
397
417
  ### Holster
418
+
398
419
  - **Pros**: Modern ES modules, improved performance, cleaner API
399
420
  - **Cons**: Newer project, smaller ecosystem
400
421
  - **Best for**: New projects, modern build systems, performance-critical apps
@@ -402,6 +423,7 @@ Shogun Core supports both Gun and Holster as database backends. Choose based on
402
423
  **Note**: The API is identical regardless of which backend you choose. Shogun Core handles all the differences internally.
403
424
 
404
425
  ### Differences to be aware of:
426
+
405
427
  - **Pair-based authentication**: Currently only supported with Gun (username/password works with both)
406
428
  - **Event system**: Gun has native events, Holster uses polling (handled automatically)
407
429
  - **Chaining API**: Gun uses `.get().get()`, Holster uses `.get().next()` (handled automatically via proxy)
@@ -412,7 +434,7 @@ Shogun Core supports both Gun and Holster as database backends. Choose based on
412
434
  interface ShogunCoreConfig {
413
435
  gunInstance?: IGunInstance<any>; // Optional: existing Gun instance (required if holsterInstance not provided)
414
436
  holsterInstance?: any; // Optional: existing Holster instance (required if gunInstance not provided)
415
-
437
+
416
438
  // Plugin configurations
417
439
  webauthn?: {
418
440
  enabled?: boolean;
@@ -428,13 +450,6 @@ interface ShogunCoreConfig {
428
450
  enabled?: boolean;
429
451
  };
430
452
 
431
- zkproof?: {
432
- enabled?: boolean;
433
- defaultGroupId?: string;
434
- deterministic?: boolean;
435
- minEntropy?: number;
436
- };
437
-
438
453
  postAuth?: {
439
454
  enabled?: boolean;
440
455
  };
@@ -455,7 +470,8 @@ interface ShogunCoreConfig {
455
470
  }
456
471
  ```
457
472
 
458
- **Note**:
473
+ **Note**:
474
+
459
475
  - Either `gunInstance` or `holsterInstance` must be provided (but not both)
460
476
 
461
477
  ## Migration Guide
@@ -465,6 +481,7 @@ interface ShogunCoreConfig {
465
481
  If you're currently using Gun and want to switch to Holster:
466
482
 
467
483
  1. **Install Holster**:
484
+
468
485
  ```bash
469
486
  npm install @mblaney/holster
470
487
  # or
@@ -472,6 +489,7 @@ If you're currently using Gun and want to switch to Holster:
472
489
  ```
473
490
 
474
491
  2. **Update your initialization**:
492
+
475
493
  ```typescript
476
494
  // Before (Gun)
477
495
  import Gun from "gun";
@@ -519,4 +537,4 @@ yarn test src/__tests__/plugins
519
537
 
520
538
  ## License
521
539
 
522
- This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
540
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.