shogun-core 6.9.7 → 6.9.9

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 (34) hide show
  1. package/README.md +64 -191
  2. package/dist/browser/shogun-core.js +10966 -14092
  3. package/dist/browser/shogun-core.js.map +1 -1
  4. package/dist/src/gundb/crypto.js +0 -14
  5. package/dist/src/gundb/db.js +14 -13
  6. package/dist/src/gundb/index.js +0 -2
  7. package/dist/src/index.js +2 -4
  8. package/dist/src/managers/AuthManager.js +1 -3
  9. package/dist/src/managers/CoreInitializer.js +7 -126
  10. package/dist/src/plugins/nostr/nostrSigner.js +22 -31
  11. package/dist/src/plugins/web3/web3Connector.js +13 -38
  12. package/dist/src/plugins/web3/web3Signer.js +15 -22
  13. package/dist/src/plugins/webauthn/webauthn.js +7 -5
  14. package/dist/src/plugins/webauthn/webauthnPlugin.js +22 -20
  15. package/dist/src/plugins/webauthn/webauthnSigner.js +3 -3
  16. package/dist/src/utils/seedPhrase.js +36 -10
  17. package/dist/tsconfig.tsbuildinfo +1 -1
  18. package/dist/types/src/core.d.ts +2 -4
  19. package/dist/types/src/gundb/db.d.ts +1 -9
  20. package/dist/types/src/gundb/index.d.ts +0 -2
  21. package/dist/types/src/index.d.ts +2 -4
  22. package/dist/types/src/interfaces/shogun.d.ts +1 -5
  23. package/dist/types/src/managers/CoreInitializer.d.ts +0 -15
  24. package/dist/types/src/plugins/nostr/nostrSigner.d.ts +0 -1
  25. package/dist/types/src/plugins/web3/web3Connector.d.ts +2 -1
  26. package/dist/types/src/plugins/webauthn/webauthnSigner.d.ts +0 -1
  27. package/dist/types/src/utils/seedPhrase.d.ts +11 -3
  28. package/package.json +3 -4
  29. package/dist/src/gundb/db-holster.js +0 -1337
  30. package/dist/src/gundb/rxjs-holster.js +0 -383
  31. package/dist/src/gundb/rxjs.js +0 -506
  32. package/dist/types/src/gundb/db-holster.d.ts +0 -283
  33. package/dist/types/src/gundb/rxjs-holster.d.ts +0 -84
  34. package/dist/types/src/gundb/rxjs.d.ts +0 -110
package/README.md CHANGED
@@ -6,18 +6,13 @@
6
6
 
7
7
  ## Overview
8
8
 
9
- Shogun Core is a comprehensive SDK for building decentralized applications (dApps) that simplifies authentication, wallet management, and decentralized data storage. It combines GunDB's or Holster's peer-to-peer networking with modern authentication standards and blockchain integration.
10
-
11
- **Now supports both Gun and Holster!** You can use either database backend - Gun for maximum compatibility or Holster for modern ES modules and improved performance.
9
+ Shogun Core is a comprehensive SDK for building decentralized applications (dApps) that simplifies authentication, wallet management, and decentralized data storage. It utilizes GunDB's peer-to-peer networking with modern authentication standards and blockchain integration.
12
10
 
13
11
  ## Features
14
12
 
15
13
  - 🔐 **Multiple Authentication Methods**: Password, WebAuthn (biometrics), Web3 (MetaMask), Nostr, Challenge (Server-Signed)
16
- - 🌐 **Decentralized Storage**: Built on GunDB or Holster for peer-to-peer data synchronization
17
- - 🔄 **Dual Database Support**: Use Gun (maximum compatibility) or Holster (modern ES modules, improved performance)
14
+ - 🌐 **Decentralized Storage**: Built on GunDB for peer-to-peer data synchronization
18
15
  - 🔌 **Plugin System**: Extensible architecture with built-in plugins
19
-
20
- - 📱 **Reactive Programming**: RxJS integration for real-time data streams
21
16
  - 🛡️ **Security**: End-to-end encryption and secure key management
22
17
  - 🎯 **TypeScript**: Full TypeScript support with comprehensive type definitions
23
18
 
@@ -31,7 +26,7 @@ yarn add shogun-core
31
26
 
32
27
  ## Quick Start
33
28
 
34
- ### Using Gun (Default)
29
+ ### Basic Initialization
35
30
 
36
31
  ```typescript
37
32
  import { ShogunCore } from 'shogun-core';
@@ -61,120 +56,93 @@ const shogun = new ShogunCore({
61
56
  });
62
57
  ```
63
58
 
64
- ### Using Holster (Alternative)
65
59
 
66
- ```typescript
67
- import { ShogunCore } from 'shogun-core';
68
- import Holster from '@mblaney/holster';
69
60
 
70
- // Create Holster instance first
71
- const holster = Holster({
72
- peers: ['ws://localhost:8765'],
73
- });
61
+ ## Database API Reference
74
62
 
75
- // Initialize Shogun Core with the Holster instance
76
- const shogun = new ShogunCore({
77
- holsterInstance: holster, // Use Holster instead of Gun
63
+ The `shogun.db` instance provides a high-level API for interacting with the decentralized database.
78
64
 
79
- // Enable authentication plugins (same as Gun)
80
- web3: { enabled: true },
81
- webauthn: {
82
- enabled: true,
83
- rpName: 'My Awesome App',
84
- rpId: window.location.hostname,
85
- },
86
- nostr: { enabled: true },
87
- });
65
+ ### 1. Connection & Session Management
88
66
 
89
- // All other APIs work the same way!
90
- ```
67
+ - `db.isLoggedIn(): boolean`: Returns `true` if a user is currently authenticated.
68
+ - `db.getCurrentUser(): { pub: string; user?: any } | null`: Returns the current user's public key and instance.
69
+ - `db.getUserPub(): string | null`: Returns the current user's public key.
70
+ - `db.onAuth(callback: (user: any) => void): () => void`: Listens for authentication changes. Returns an unsubscribe function.
71
+ - `db.restoreSession(): Promise<RestoreResult>`: Attempts to restore a previous session from `sessionStorage`.
91
72
 
92
- ## Basic Database Operations
73
+ ### 2. Promise-based Advanced Utilities (Firegun API)
74
+ *Note: These methods provide full Promise support and auto-retries.*
93
75
 
94
76
  ```typescript
95
77
  const db = shogun.db;
96
78
 
97
- // Store data using Gun chaining (Traditional Method)
98
- await db.get('users').get('alice').get('profile').put({
99
- name: 'Alice Smith',
100
- email: 'alice@example.com',
101
- });
79
+ // Fetch data with auto-retry and 5s timeout
80
+ const data = await db.Get('public/posts/123');
102
81
 
103
- // Read data
104
- const profile = await db.get('users').get('alice').get('profile').once().then();
82
+ // Save data (supports deep object merging)
83
+ await db.Put('public/settings', { theme: 'dark' });
105
84
 
106
- // Update specific fields
107
- await db
108
- .get('users')
109
- .get('alice')
110
- .get('profile')
111
- .get('name')
112
- .put('Alice Johnson');
85
+ // Insert into a collection with a random key
86
+ await db.Set('public/logs', { event: 'login', time: Date.now() });
113
87
 
114
- // Iterate over collections with .map()
115
- db.get('users').map((user, userId) => {
116
- console.log(`User ${userId}:`, user);
117
- });
118
- ```
88
+ // Recursive load of nested nodes
89
+ const fullData = await db.Load('public/complex-node');
119
90
 
120
- ### Promise-based Advanced Utilities (Firegun API)
91
+ // "Delete" a node (Tombstoning)
92
+ await db.Del('public/temp-data');
121
93
 
122
- Shogun Core includes built-in Promise wrappers, timeouts, and user-space utilities directly in the `DataBase` class:
94
+ // Deeply nullify all keys in a node
95
+ await db.purge('public/old-config');
96
+ ```
123
97
 
98
+ ### 3. Real-time Subscriptions
124
99
  ```typescript
125
- const db = shogun.db;
100
+ // Listen to changes with an identifier for easy unsubscription
101
+ db.On('public/feed', (data) => console.log('Update:', data), 'myListener');
126
102
 
127
- // Promise-based Root GET with auto-retry
128
- const data = await db.Get('public/posts/123');
103
+ // Stop listening
104
+ db.Off('myListener');
129
105
 
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
106
+ // One-time fetch of initial state + future changes
107
+ db.Listen('public/status', (status) => console.log('Status:', status));
108
+ ```
149
109
 
150
- // Read from current user's graph
151
- const userSettings = await db.userGet('settings');
110
+ ### 4. User-Space Operations
111
+ These methods automatically prefix the path with `~pubkey/` of the logged-in user.
152
112
 
153
- // Write to current user's graph
154
- await db.userPut('profile', { name: 'Alice' });
113
+ - `db.userGet(path: string)`: Read from current user's graph.
114
+ - `db.userPut(path: string, data: any)`: Write to current user's graph.
115
+ - `db.userDel(path: string)`: Delete node from user's graph.
116
+ - `db.userLoad(path: string)`: Recursively load user-space data.
155
117
 
156
- // Delete user node (Tombstoning)
157
- await db.userDel('old-post');
118
+ ### 5. Advanced Features
119
+ - **Content Addressing**: `db.addContentAdressing('#key', data)` hashes data using SHA-256 for immutable storage.
120
+ - **Security**: `db.generatePublicCert()` creates a public certificate for P2P interactions.
121
+ - **Cleanup**: `db.aggressiveAuthCleanup()` forcefully clears all local auth state.
158
122
 
159
- // Purge (nullify all keys in a node)
160
- await db.purge('~pubkey/public/posts/123');
161
- ```
123
+ ## Authentication API
162
124
 
163
- ## Authentication Methods
125
+ Shogun Core provides a unified authentication interface. Plugins (Web3, WebAuthn, etc.) extend this system.
164
126
 
165
- ### 1. Traditional Authentication
127
+ ### Core Methods
166
128
 
167
129
  ```typescript
168
- // Sign up
169
- const signUpResult = await shogun.signUp('username', 'password');
170
- if (signUpResult.success) {
171
- console.log('User created:', signUpResult.username);
172
- }
173
-
174
- // Login
175
- const loginResult = await shogun.login('username', 'password');
176
- if (loginResult.success) {
177
- console.log('Logged in as:', loginResult.username);
130
+ // 1. Traditional Signup/Login
131
+ await shogun.signUp('alice', 'Password123!');
132
+ await shogun.login('alice', 'Password123!');
133
+
134
+ // 2. Pair-based Authentication
135
+ const pair = await shogun.db.crypto.createPair();
136
+ await shogun.loginWithPair('alice', pair);
137
+
138
+ // 3. Mnemonic Seed Authentication
139
+ const mnemonic = 'word1 word2 ...';
140
+ await shogun.loginWithSeed('alice', mnemonic);
141
+
142
+ // 4. Session Check & Logout
143
+ if (shogun.isLoggedIn()) {
144
+ console.log('User Pub:', shogun.db.getUserPub());
145
+ shogun.logout();
178
146
  }
179
147
  ```
180
148
 
@@ -345,41 +313,7 @@ console.log('Chat Pub:', chatPair.pub);
345
313
  </html>
346
314
  ```
347
315
 
348
- ### With Holster (ES Modules)
349
316
 
350
- ```html
351
- <!DOCTYPE html>
352
- <html>
353
- <head>
354
- <title>Shogun Core with Holster</title>
355
- </head>
356
- <body>
357
- <h1>My dApp</h1>
358
- <script type="module">
359
- import Holster from 'https://cdn.jsdelivr.net/npm/@mblaney/holster/build/holster.js';
360
- import { ShogunCore } from 'https://cdn.jsdelivr.net/npm/shogun-core/dist/src/index.js';
361
-
362
- // Create Holster instance
363
- const holster = Holster({
364
- peers: ['ws://localhost:8765'],
365
- });
366
-
367
- // Initialize Shogun Core
368
- const shogunCore = new ShogunCore({
369
- holsterInstance: holster,
370
- web3: { enabled: true },
371
- webauthn: {
372
- enabled: true,
373
- rpName: 'My Browser dApp',
374
- rpId: window.location.hostname,
375
- },
376
- });
377
-
378
- console.log('Shogun Core initialized with Holster!', shogunCore);
379
- </script>
380
- </body>
381
- </html>
382
- ```
383
317
 
384
318
  ## Event System
385
319
 
@@ -404,36 +338,13 @@ shogun.on('error', (error) => {
404
338
  });
405
339
  ```
406
340
 
407
- ## Database Backend: Gun vs Holster
408
-
409
- Shogun Core supports both Gun and Holster as database backends. Choose based on your needs:
410
341
 
411
- ### Gun (Default)
412
-
413
- - **Pros**: Maximum compatibility, large ecosystem, battle-tested
414
- - **Cons**: Older codebase, CommonJS modules
415
- - **Best for**: Existing projects, maximum compatibility
416
-
417
- ### Holster
418
-
419
- - **Pros**: Modern ES modules, improved performance, cleaner API
420
- - **Cons**: Newer project, smaller ecosystem
421
- - **Best for**: New projects, modern build systems, performance-critical apps
422
-
423
- **Note**: The API is identical regardless of which backend you choose. Shogun Core handles all the differences internally.
424
-
425
- ### Differences to be aware of:
426
-
427
- - **Pair-based authentication**: Currently only supported with Gun (username/password works with both)
428
- - **Event system**: Gun has native events, Holster uses polling (handled automatically)
429
- - **Chaining API**: Gun uses `.get().get()`, Holster uses `.get().next()` (handled automatically via proxy)
430
342
 
431
343
  ## Configuration Options
432
344
 
433
345
  ```typescript
434
346
  interface ShogunCoreConfig {
435
- gunInstance?: IGunInstance<any>; // Optional: existing Gun instance (required if holsterInstance not provided)
436
- holsterInstance?: any; // Optional: existing Holster instance (required if gunInstance not provided)
347
+ gunInstance?: IGunInstance<any>; // Optional: existing Gun instance
437
348
 
438
349
  // Plugin configurations
439
350
  webauthn?: {
@@ -470,45 +381,7 @@ interface ShogunCoreConfig {
470
381
  }
471
382
  ```
472
383
 
473
- **Note**:
474
-
475
- - Either `gunInstance` or `holsterInstance` must be provided (but not both)
476
-
477
- ## Migration Guide
478
-
479
- ### From Gun to Holster
480
-
481
- If you're currently using Gun and want to switch to Holster:
482
-
483
- 1. **Install Holster**:
484
-
485
- ```bash
486
- npm install @mblaney/holster
487
- # or
488
- yarn add @mblaney/holster
489
- ```
490
-
491
- 2. **Update your initialization**:
492
-
493
- ```typescript
494
- // Before (Gun)
495
- import Gun from "gun";
496
- const gun = Gun({ peers: [...] });
497
- const shogun = new ShogunCore({ gunInstance: gun });
498
-
499
- // After (Holster)
500
- import Holster from "@mblaney/holster";
501
- const holster = Holster({ peers: [...] });
502
- const shogun = new ShogunCore({ holsterInstance: holster });
503
- ```
504
-
505
- 3. **That's it!** All other code remains the same. The API is identical.
506
-
507
- ### Limitations when using Holster
508
384
 
509
- - Pair-based authentication (`loginWithPair()`) is not yet supported - use username/password instead
510
- - Some advanced Gun features may not be available
511
- - Event system uses polling instead of native events (handled automatically)
512
385
 
513
386
  ## Testing
514
387