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.
- package/README.md +64 -191
- package/dist/browser/shogun-core.js +10966 -14092
- package/dist/browser/shogun-core.js.map +1 -1
- package/dist/src/gundb/crypto.js +0 -14
- package/dist/src/gundb/db.js +14 -13
- package/dist/src/gundb/index.js +0 -2
- package/dist/src/index.js +2 -4
- package/dist/src/managers/AuthManager.js +1 -3
- package/dist/src/managers/CoreInitializer.js +7 -126
- package/dist/src/plugins/nostr/nostrSigner.js +22 -31
- package/dist/src/plugins/web3/web3Connector.js +13 -38
- package/dist/src/plugins/web3/web3Signer.js +15 -22
- package/dist/src/plugins/webauthn/webauthn.js +7 -5
- package/dist/src/plugins/webauthn/webauthnPlugin.js +22 -20
- package/dist/src/plugins/webauthn/webauthnSigner.js +3 -3
- package/dist/src/utils/seedPhrase.js +36 -10
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/src/core.d.ts +2 -4
- package/dist/types/src/gundb/db.d.ts +1 -9
- package/dist/types/src/gundb/index.d.ts +0 -2
- package/dist/types/src/index.d.ts +2 -4
- package/dist/types/src/interfaces/shogun.d.ts +1 -5
- package/dist/types/src/managers/CoreInitializer.d.ts +0 -15
- package/dist/types/src/plugins/nostr/nostrSigner.d.ts +0 -1
- package/dist/types/src/plugins/web3/web3Connector.d.ts +2 -1
- package/dist/types/src/plugins/webauthn/webauthnSigner.d.ts +0 -1
- package/dist/types/src/utils/seedPhrase.d.ts +11 -3
- package/package.json +3 -4
- package/dist/src/gundb/db-holster.js +0 -1337
- package/dist/src/gundb/rxjs-holster.js +0 -383
- package/dist/src/gundb/rxjs.js +0 -506
- package/dist/types/src/gundb/db-holster.d.ts +0 -283
- package/dist/types/src/gundb/rxjs-holster.d.ts +0 -84
- 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
|
|
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
|
|
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
|
-
###
|
|
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
|
-
|
|
71
|
-
const holster = Holster({
|
|
72
|
-
peers: ['ws://localhost:8765'],
|
|
73
|
-
});
|
|
61
|
+
## Database API Reference
|
|
74
62
|
|
|
75
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
98
|
-
await db.
|
|
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
|
-
//
|
|
104
|
-
|
|
82
|
+
// Save data (supports deep object merging)
|
|
83
|
+
await db.Put('public/settings', { theme: 'dark' });
|
|
105
84
|
|
|
106
|
-
//
|
|
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
|
-
//
|
|
115
|
-
db.
|
|
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
|
-
|
|
91
|
+
// "Delete" a node (Tombstoning)
|
|
92
|
+
await db.Del('public/temp-data');
|
|
121
93
|
|
|
122
|
-
|
|
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
|
-
|
|
100
|
+
// Listen to changes with an identifier for easy unsubscription
|
|
101
|
+
db.On('public/feed', (data) => console.log('Update:', data), 'myListener');
|
|
126
102
|
|
|
127
|
-
//
|
|
128
|
-
|
|
103
|
+
// Stop listening
|
|
104
|
+
db.Off('myListener');
|
|
129
105
|
|
|
130
|
-
//
|
|
131
|
-
|
|
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
|
-
|
|
151
|
-
|
|
110
|
+
### 4. User-Space Operations
|
|
111
|
+
These methods automatically prefix the path with `~pubkey/` of the logged-in user.
|
|
152
112
|
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
157
|
-
|
|
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
|
-
|
|
160
|
-
await db.purge('~pubkey/public/posts/123');
|
|
161
|
-
```
|
|
123
|
+
## Authentication API
|
|
162
124
|
|
|
163
|
-
|
|
125
|
+
Shogun Core provides a unified authentication interface. Plugins (Web3, WebAuthn, etc.) extend this system.
|
|
164
126
|
|
|
165
|
-
###
|
|
127
|
+
### Core Methods
|
|
166
128
|
|
|
167
129
|
```typescript
|
|
168
|
-
//
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
|
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
|
|