shogun-core 3.3.7 β 4.0.0
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 +1378 -1221
- package/dist/browser/shogun-core.js +78074 -45286
- package/dist/browser/shogun-core.js.map +1 -1
- package/dist/core.js +2 -3
- package/dist/examples/simple-api-test.js +90 -65
- package/dist/examples/zkproof-credentials-example.js +218 -0
- package/dist/examples/zkproof-example.js +206 -0
- package/dist/gundb/api.js +111 -467
- package/dist/index.js +10 -1
- package/dist/interfaces/shogun.js +2 -2
- package/dist/managers/AuthManager.js +0 -2
- package/dist/managers/CoreInitializer.js +9 -12
- package/dist/plugins/index.js +9 -21
- package/dist/plugins/nostr/nostrConnectorPlugin.js +2 -2
- package/dist/plugins/webauthn/webauthn.js +20 -7
- package/dist/plugins/webauthn/webauthnPlugin.js +101 -17
- package/dist/plugins/zkproof/index.js +53 -0
- package/dist/plugins/zkproof/zkCredentials.js +213 -0
- package/dist/plugins/zkproof/zkProofConnector.js +198 -0
- package/dist/plugins/zkproof/zkProofPlugin.js +272 -0
- package/dist/types/core.d.ts +1 -1
- package/dist/types/examples/simple-api-test.d.ts +6 -1
- package/dist/types/examples/zkproof-credentials-example.d.ts +12 -0
- package/dist/types/examples/zkproof-example.d.ts +11 -0
- package/dist/types/gundb/api.d.ts +77 -165
- package/dist/types/gundb/types.d.ts +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/interfaces/events.d.ts +3 -3
- package/dist/types/interfaces/shogun.d.ts +9 -24
- package/dist/types/plugins/index.d.ts +5 -3
- package/dist/types/plugins/webauthn/types.d.ts +22 -1
- package/dist/types/plugins/webauthn/webauthn.d.ts +1 -1
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +23 -2
- package/dist/types/plugins/zkproof/index.d.ts +48 -0
- package/dist/types/plugins/zkproof/types.d.ts +123 -0
- package/dist/types/plugins/zkproof/zkCredentials.d.ts +112 -0
- package/dist/types/plugins/zkproof/zkProofConnector.d.ts +46 -0
- package/dist/types/plugins/zkproof/zkProofPlugin.d.ts +76 -0
- package/dist/types/utils/seedPhrase.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +2 -2
- package/dist/utils/seedPhrase.js +97 -0
- package/dist/utils/validation.js +3 -1
- package/package.json +14 -8
- package/dist/examples/api-test.js +0 -273
- package/dist/migration-test.js +0 -96
- package/dist/plugins/oauth/index.js +0 -8
- package/dist/plugins/oauth/oauthConnector.js +0 -759
- package/dist/plugins/oauth/oauthPlugin.js +0 -400
- package/dist/types/examples/api-test.d.ts +0 -12
- package/dist/types/migration-test.d.ts +0 -16
- package/dist/types/plugins/oauth/index.d.ts +0 -3
- package/dist/types/plugins/oauth/oauthConnector.d.ts +0 -110
- package/dist/types/plugins/oauth/oauthPlugin.d.ts +0 -91
- package/dist/types/plugins/oauth/types.d.ts +0 -114
- /package/dist/plugins/{oauth β zkproof}/types.js +0 -0
package/README.md
CHANGED
|
@@ -1,1221 +1,1378 @@
|
|
|
1
|
-
# Shogun Core π¦
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/shogun-core)
|
|
4
|
-
[](https://opensource.org/licenses/MIT)
|
|
5
|
-
[](https://www.typescriptlang.org/)
|
|
6
|
-
[](https://deepwiki.com/scobru/shogun-core)
|
|
7
|
-
|
|
8
|
-
## Overview
|
|
9
|
-
|
|
10
|
-
Shogun Core is a comprehensive SDK for building decentralized applications (dApps) that simplifies authentication, wallet management, and decentralized data storage. It combines GunDB's peer-to-peer networking with modern authentication standards and blockchain integration to provide a secure, user-friendly foundation for Web3 applications.
|
|
11
|
-
|
|
12
|
-
## Features
|
|
13
|
-
|
|
14
|
-
- π **Multiple Authentication Methods**: Traditional username/password, WebAuthn (biometrics), Web3 (MetaMask), Nostr, and
|
|
15
|
-
- π **Decentralized Storage**: Built on GunDB for peer-to-peer data synchronization
|
|
16
|
-
- π **Plugin System**: Extensible architecture with built-in plugins for various authentication methods
|
|
17
|
-
- π± **Reactive Programming**: RxJS integration for real-time data streams
|
|
18
|
-
- π‘οΈ **Security**: End-to-end encryption and secure key management
|
|
19
|
-
- π― **TypeScript**: Full TypeScript support with comprehensive type definitions
|
|
20
|
-
- π‘ **Event System**: Typed event system for monitoring authentication and data changes
|
|
21
|
-
- π **Password Recovery**: Secure password hint system with security questions
|
|
22
|
-
- β
**Type Consistency**: Unified return types across all authentication methods
|
|
23
|
-
- π **Simplified Architecture**: Focused on core functionality with reduced complexity
|
|
24
|
-
- β **Simple API**: Easy-to-use wrapper for common operations with minimal complexity
|
|
25
|
-
- π **Advanced Chaining**: Powerful chaining operations for complex nested data structures
|
|
26
|
-
- π€ **User Space Management**: Complete CRUD operations for user-specific data storage
|
|
27
|
-
- β‘ **Quick Start**: Rapid initialization with pre-configured setups
|
|
28
|
-
- ποΈ **Configuration Presets**: Pre-built configurations for common use cases
|
|
29
|
-
|
|
30
|
-
## Recent Updates (v2.0.1)
|
|
31
|
-
|
|
32
|
-
### π§ **Critical Bug Fixes**
|
|
33
|
-
|
|
34
|
-
- **π§ FIXED: Remove Operations**: Fixed critical bug in `remove()` and `removeUserData()` methods that was causing `TypeError: Cannot read properties of null (reading 'err')`
|
|
35
|
-
- **π§ IMPROVED: User Data Operations**: Rewrote all user data methods to use direct Gun user node for better reliability and error handling
|
|
36
|
-
- **π§ ENHANCED: Error Handling**: Added proper null checking and improved error logging throughout the user data operations
|
|
37
|
-
|
|
38
|
-
### β
**Major API Improvements & Simplification (v2.
|
|
39
|
-
|
|
40
|
-
- **β
|
|
41
|
-
-
|
|
42
|
-
-
|
|
43
|
-
-
|
|
44
|
-
-
|
|
45
|
-
- **β NEW:
|
|
46
|
-
- **β NEW:
|
|
47
|
-
-
|
|
48
|
-
-
|
|
49
|
-
- **
|
|
50
|
-
- **
|
|
51
|
-
- **Improved Maintainability**: Better organized codebase with clear separation
|
|
52
|
-
- **Better Performance**: Optimized operations with
|
|
53
|
-
|
|
54
|
-
### π§ **Bug Fixes & Improvements**
|
|
55
|
-
|
|
56
|
-
**Fixed Critical Remove Operations Bug**:
|
|
57
|
-
- Fixed `TypeError: Cannot read properties of null (reading 'err')` in `remove()` and `removeUserData()` methods
|
|
58
|
-
- Added proper null checking: `ack && ack.err` instead of just `ack.err`
|
|
59
|
-
- All user data operations now use direct Gun user node for better reliability
|
|
60
|
-
- Improved error handling and logging throughout user data operations
|
|
61
|
-
|
|
62
|
-
### β οΈ **BREAKING CHANGES**
|
|
63
|
-
|
|
64
|
-
- **π¨ REMOVED: Array Functions**: `putUserArray()`, `getUserArray()`, `addToUserArray()`, `removeFromUserArray()`, `updateInUserArray()` have been **REMOVED** due to GunDB compatibility issues
|
|
65
|
-
- **β οΈ DEPRECATED: Global Array Functions**: `putArray()`, `getArray()`, `addToArray()`, `removeFromArray()`, `updateInArray()` are deprecated and show warnings
|
|
66
|
-
- **β
MIGRATION**: Use **Collections** or **Direct GunDB Operations** instead (see examples below)
|
|
67
|
-
|
|
68
|
-
## Recent Updates (v1.7.0)
|
|
69
|
-
|
|
70
|
-
### β
**Type System Fixes**
|
|
71
|
-
|
|
72
|
-
- **Unified Return Types**: All authentication methods now use consistent `AuthResult` and `SignUpResult` types
|
|
73
|
-
- **Enhanced SignUpResult**: Extended to support
|
|
74
|
-
- **Type Safety**: Fixed TypeScript inconsistencies across all plugins
|
|
75
|
-
- **API Standardization**: All plugins implement unified `login()` and `signUp()` interfaces
|
|
76
|
-
|
|
77
|
-
## Installation
|
|
78
|
-
|
|
79
|
-
```bash
|
|
80
|
-
npm install shogun-core
|
|
81
|
-
# or
|
|
82
|
-
yarn add shogun-core
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
## Quick Start
|
|
86
|
-
|
|
87
|
-
### β **NEW: Simple API Setup (Recommended)**
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
import { quickStart, Gun } from "shogun-core";
|
|
91
|
-
|
|
92
|
-
// Create Gun instance
|
|
93
|
-
const gun = Gun({
|
|
94
|
-
peers: ['https://gun-manhattan.herokuapp.com/gun']
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// Quick start with simple API
|
|
98
|
-
const shogun = quickStart(gun, 'my-app');
|
|
99
|
-
await shogun.init();
|
|
100
|
-
|
|
101
|
-
// Use simplified API
|
|
102
|
-
const user = await shogun.api.signup('alice', 'password123');
|
|
103
|
-
if (user) {
|
|
104
|
-
console.log('User created:', user.username);
|
|
105
|
-
|
|
106
|
-
// User space operations
|
|
107
|
-
await shogun.api.updateProfile({
|
|
108
|
-
name: 'Alice',
|
|
109
|
-
email: 'alice@example.com'
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
await shogun.api.saveSettings({
|
|
113
|
-
theme: 'dark',
|
|
114
|
-
language: 'en'
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
// Create collections
|
|
118
|
-
await shogun.api.createCollection('todos', {
|
|
119
|
-
'1': { text: 'Learn Shogun Core', done: false },
|
|
120
|
-
'2': { text: 'Build dApp', done: false }
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
### Advanced Setup (Full Features)
|
|
126
|
-
|
|
127
|
-
```typescript
|
|
128
|
-
import { ShogunCore } from "shogun-core";
|
|
129
|
-
|
|
130
|
-
// Define your list of Gun peers
|
|
131
|
-
const relays = [
|
|
132
|
-
"wss://ruling-mastodon-improved.ngrok-free.app/gun",
|
|
133
|
-
"https://gun-manhattan.herokuapp.com/gun",
|
|
134
|
-
"https://peer.wallie.io/gun",
|
|
135
|
-
];
|
|
136
|
-
|
|
137
|
-
// Initialize Shogun Core with plugins
|
|
138
|
-
const shogun = new ShogunCore({
|
|
139
|
-
peers: relays,
|
|
140
|
-
scope: "my-awesome-app",
|
|
141
|
-
authToken: "YOUR_GUN_SUPER_PEER_SECRET", // Optional, for private peers
|
|
142
|
-
|
|
143
|
-
// Enable and configure Web3 (e.g., MetaMask) authentication
|
|
144
|
-
web3: {
|
|
145
|
-
enabled: true,
|
|
146
|
-
},
|
|
147
|
-
|
|
148
|
-
// Enable and configure WebAuthn (biometrics, security keys)
|
|
149
|
-
webauthn: {
|
|
150
|
-
enabled: true,
|
|
151
|
-
rpName: "My Awesome App", // Name of your application
|
|
152
|
-
rpId: window.location.hostname, // Relying party ID
|
|
153
|
-
},
|
|
154
|
-
|
|
155
|
-
// Enable and configure Nostr
|
|
156
|
-
nostr: {
|
|
157
|
-
enabled: true,
|
|
158
|
-
},
|
|
159
|
-
|
|
160
|
-
// Enable and configure
|
|
161
|
-
|
|
162
|
-
enabled: true,
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
//
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
//
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
//
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
//
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
//
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
//
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
//
|
|
495
|
-
const
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
//
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
//
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
): Promise<
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
//
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
): Promise<
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
): Promise<
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
-
|
|
1018
|
-
-
|
|
1019
|
-
-
|
|
1020
|
-
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
-
|
|
1031
|
-
-
|
|
1032
|
-
-
|
|
1033
|
-
-
|
|
1034
|
-
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
-
|
|
1179
|
-
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
- **
|
|
1191
|
-
- **
|
|
1192
|
-
- **
|
|
1193
|
-
- **
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1
|
+
# Shogun Core π¦
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/shogun-core)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://deepwiki.com/scobru/shogun-core)
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
Shogun Core is a comprehensive SDK for building decentralized applications (dApps) that simplifies authentication, wallet management, and decentralized data storage. It combines GunDB's peer-to-peer networking with modern authentication standards and blockchain integration to provide a secure, user-friendly foundation for Web3 applications.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- π **Multiple Authentication Methods**: Traditional username/password, WebAuthn (biometrics), Web3 (MetaMask), Nostr, and ZK-Proof (anonymous)
|
|
15
|
+
- π **Decentralized Storage**: Built on GunDB for peer-to-peer data synchronization
|
|
16
|
+
- π **Plugin System**: Extensible architecture with built-in plugins for various authentication methods
|
|
17
|
+
- π± **Reactive Programming**: RxJS integration for real-time data streams
|
|
18
|
+
- π‘οΈ **Security**: End-to-end encryption and secure key management
|
|
19
|
+
- π― **TypeScript**: Full TypeScript support with comprehensive type definitions
|
|
20
|
+
- π‘ **Event System**: Typed event system for monitoring authentication and data changes
|
|
21
|
+
- π **Password Recovery**: Secure password hint system with security questions
|
|
22
|
+
- β
**Type Consistency**: Unified return types across all authentication methods
|
|
23
|
+
- π **Simplified Architecture**: Focused on core functionality with reduced complexity
|
|
24
|
+
- β **Simple API**: Easy-to-use wrapper for common operations with minimal complexity
|
|
25
|
+
- π **Advanced Chaining**: Powerful chaining operations for complex nested data structures
|
|
26
|
+
- π€ **User Space Management**: Complete CRUD operations for user-specific data storage
|
|
27
|
+
- β‘ **Quick Start**: Rapid initialization with pre-configured setups
|
|
28
|
+
- ποΈ **Configuration Presets**: Pre-built configurations for common use cases
|
|
29
|
+
|
|
30
|
+
## Recent Updates (v2.0.1)
|
|
31
|
+
|
|
32
|
+
### π§ **Critical Bug Fixes**
|
|
33
|
+
|
|
34
|
+
- **π§ FIXED: Remove Operations**: Fixed critical bug in `remove()` and `removeUserData()` methods that was causing `TypeError: Cannot read properties of null (reading 'err')`
|
|
35
|
+
- **π§ IMPROVED: User Data Operations**: Rewrote all user data methods to use direct Gun user node for better reliability and error handling
|
|
36
|
+
- **π§ ENHANCED: Error Handling**: Added proper null checking and improved error logging throughout the user data operations
|
|
37
|
+
|
|
38
|
+
### β
**Major API Improvements & Simplification (v2.1.0)**
|
|
39
|
+
|
|
40
|
+
- **β STREAMLINED: Simple API Layer**: Simplified `SimpleGunAPI` to focus on high-level helpers only
|
|
41
|
+
- Direct database access via `api.database` for basic operations (get, put, auth, etc.)
|
|
42
|
+
- Helper methods for standardized data: profile, settings, preferences, collections
|
|
43
|
+
- Array/Object conversion utilities for GunDB
|
|
44
|
+
- Removed redundant wrapper methods to reduce complexity
|
|
45
|
+
- **β NEW: Quick Start Functions**: `quickStart()`, `autoQuickStart()` classes for rapid initialization
|
|
46
|
+
- **β NEW: Improved Type System**: Reduced `any` usage with better TypeScript types
|
|
47
|
+
- **β NEW: Configuration Presets**: Pre-built configurations for common use cases
|
|
48
|
+
- **β NEW: Advanced API Features**: Comprehensive plugin management, peer network control, advanced user management, and security systems
|
|
49
|
+
- **Enhanced Advanced Features**: Maintained and improved advanced plugin management, peer network management, and user tracking systems
|
|
50
|
+
- **Streamlined Event System**: Enhanced event system with better type safety and comprehensive event handling
|
|
51
|
+
- **Improved Maintainability**: Better organized codebase with clear separation of concerns
|
|
52
|
+
- **Better Performance**: Optimized operations with reduced abstraction layers
|
|
53
|
+
|
|
54
|
+
### π§ **Bug Fixes & Improvements**
|
|
55
|
+
|
|
56
|
+
**Fixed Critical Remove Operations Bug**:
|
|
57
|
+
- Fixed `TypeError: Cannot read properties of null (reading 'err')` in `remove()` and `removeUserData()` methods
|
|
58
|
+
- Added proper null checking: `ack && ack.err` instead of just `ack.err`
|
|
59
|
+
- All user data operations now use direct Gun user node for better reliability
|
|
60
|
+
- Improved error handling and logging throughout user data operations
|
|
61
|
+
|
|
62
|
+
### β οΈ **BREAKING CHANGES**
|
|
63
|
+
|
|
64
|
+
- **π¨ REMOVED: Array Functions**: `putUserArray()`, `getUserArray()`, `addToUserArray()`, `removeFromUserArray()`, `updateInUserArray()` have been **REMOVED** due to GunDB compatibility issues
|
|
65
|
+
- **β οΈ DEPRECATED: Global Array Functions**: `putArray()`, `getArray()`, `addToArray()`, `removeFromArray()`, `updateInArray()` are deprecated and show warnings
|
|
66
|
+
- **β
MIGRATION**: Use **Collections** or **Direct GunDB Operations** instead (see examples below)
|
|
67
|
+
|
|
68
|
+
## Recent Updates (v1.7.0)
|
|
69
|
+
|
|
70
|
+
### β
**Type System Fixes**
|
|
71
|
+
|
|
72
|
+
- **Unified Return Types**: All authentication methods now use consistent `AuthResult` and `SignUpResult` types
|
|
73
|
+
- **Enhanced SignUpResult**: Extended to support provider-specific authentication data
|
|
74
|
+
- **Type Safety**: Fixed TypeScript inconsistencies across all plugins
|
|
75
|
+
- **API Standardization**: All plugins implement unified `login()` and `signUp()` interfaces
|
|
76
|
+
|
|
77
|
+
## Installation
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
npm install shogun-core
|
|
81
|
+
# or
|
|
82
|
+
yarn add shogun-core
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Quick Start
|
|
86
|
+
|
|
87
|
+
### β **NEW: Simple API Setup (Recommended)**
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { quickStart, Gun } from "shogun-core";
|
|
91
|
+
|
|
92
|
+
// Create Gun instance
|
|
93
|
+
const gun = Gun({
|
|
94
|
+
peers: ['https://gun-manhattan.herokuapp.com/gun']
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Quick start with simple API
|
|
98
|
+
const shogun = quickStart(gun, 'my-app');
|
|
99
|
+
await shogun.init();
|
|
100
|
+
|
|
101
|
+
// Use simplified API
|
|
102
|
+
const user = await shogun.api.signup('alice', 'password123');
|
|
103
|
+
if (user) {
|
|
104
|
+
console.log('User created:', user.username);
|
|
105
|
+
|
|
106
|
+
// User space operations
|
|
107
|
+
await shogun.api.updateProfile({
|
|
108
|
+
name: 'Alice',
|
|
109
|
+
email: 'alice@example.com'
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
await shogun.api.saveSettings({
|
|
113
|
+
theme: 'dark',
|
|
114
|
+
language: 'en'
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Create collections
|
|
118
|
+
await shogun.api.createCollection('todos', {
|
|
119
|
+
'1': { text: 'Learn Shogun Core', done: false },
|
|
120
|
+
'2': { text: 'Build dApp', done: false }
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Advanced Setup (Full Features)
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { ShogunCore } from "shogun-core";
|
|
129
|
+
|
|
130
|
+
// Define your list of Gun peers
|
|
131
|
+
const relays = [
|
|
132
|
+
"wss://ruling-mastodon-improved.ngrok-free.app/gun",
|
|
133
|
+
"https://gun-manhattan.herokuapp.com/gun",
|
|
134
|
+
"https://peer.wallie.io/gun",
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
// Initialize Shogun Core with plugins
|
|
138
|
+
const shogun = new ShogunCore({
|
|
139
|
+
peers: relays,
|
|
140
|
+
scope: "my-awesome-app",
|
|
141
|
+
authToken: "YOUR_GUN_SUPER_PEER_SECRET", // Optional, for private peers
|
|
142
|
+
|
|
143
|
+
// Enable and configure Web3 (e.g., MetaMask) authentication
|
|
144
|
+
web3: {
|
|
145
|
+
enabled: true,
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
// Enable and configure WebAuthn (biometrics, security keys)
|
|
149
|
+
webauthn: {
|
|
150
|
+
enabled: true,
|
|
151
|
+
rpName: "My Awesome App", // Name of your application
|
|
152
|
+
rpId: window.location.hostname, // Relying party ID
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
// Enable and configure Nostr
|
|
156
|
+
nostr: {
|
|
157
|
+
enabled: true,
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
// Enable and configure ZK-Proof (anonymous authentication)
|
|
161
|
+
zkproof: {
|
|
162
|
+
enabled: true,
|
|
163
|
+
defaultGroupId: "my-app-users",
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
console.log("Shogun Core initialized!");
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## β **NEW: Simple API**
|
|
171
|
+
|
|
172
|
+
The Simple API provides an easy-to-use interface for common operations with minimal complexity. Perfect for beginners or when you need quick setup.
|
|
173
|
+
|
|
174
|
+
### Simple API Methods
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import { quickStart, Gun } from "shogun-core";
|
|
178
|
+
|
|
179
|
+
const gun = Gun({ peers: ['https://gun-manhattan.herokuapp.com/gun'] });
|
|
180
|
+
const shogun = quickStart(gun, 'my-app');
|
|
181
|
+
await shogun.init();
|
|
182
|
+
|
|
183
|
+
const api = shogun.api;
|
|
184
|
+
const db = api.database; // Access database directly for basic operations
|
|
185
|
+
|
|
186
|
+
// ===== BASIC OPERATIONS (use database) =====
|
|
187
|
+
|
|
188
|
+
// Authentication
|
|
189
|
+
const user = await db.signUp('username', 'password');
|
|
190
|
+
const loginResult = await db.login('username', 'password');
|
|
191
|
+
db.logout();
|
|
192
|
+
const isLoggedIn = db.isLoggedIn();
|
|
193
|
+
|
|
194
|
+
// Basic data operations
|
|
195
|
+
await db.put('path/to/data', { value: 'hello' });
|
|
196
|
+
const data = await db.getData('path/to/data');
|
|
197
|
+
await db.set('path/to/data', { value: 'updated' });
|
|
198
|
+
await db.remove('path/to/data');
|
|
199
|
+
|
|
200
|
+
// Gun node for advanced operations like .map()
|
|
201
|
+
const userNode = db.get('users');
|
|
202
|
+
userNode.map((user, userId) => console.log(`User ${userId}:`, user));
|
|
203
|
+
|
|
204
|
+
// ===== HELPER METHODS (use api) =====
|
|
205
|
+
|
|
206
|
+
// Profile management (standardized location)
|
|
207
|
+
await api.updateProfile({
|
|
208
|
+
name: 'John Doe',
|
|
209
|
+
email: 'john@example.com',
|
|
210
|
+
bio: 'Developer'
|
|
211
|
+
});
|
|
212
|
+
const profile = await api.getProfile();
|
|
213
|
+
|
|
214
|
+
// Settings and preferences (standardized locations)
|
|
215
|
+
await api.saveSettings({ language: 'en', notifications: true });
|
|
216
|
+
const settings = await api.getSettings();
|
|
217
|
+
|
|
218
|
+
await api.savePreferences({ theme: 'dark', fontSize: 14 });
|
|
219
|
+
const preferences = await api.getPreferences();
|
|
220
|
+
|
|
221
|
+
// Collections (standardized location for user collections)
|
|
222
|
+
await api.createCollection('todos', {
|
|
223
|
+
'1': { text: 'Learn Shogun Core', done: false },
|
|
224
|
+
'2': { text: 'Build dApp', done: false }
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
await api.addToCollection('todos', '3', {
|
|
228
|
+
text: 'Deploy to production',
|
|
229
|
+
done: false
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
const todos = await api.getCollection('todos');
|
|
233
|
+
await api.removeFromCollection('todos', '2');
|
|
234
|
+
|
|
235
|
+
// Array utilities for GunDB
|
|
236
|
+
const items = [
|
|
237
|
+
{ id: '1', name: 'Item 1' },
|
|
238
|
+
{ id: '2', name: 'Item 2' }
|
|
239
|
+
];
|
|
240
|
+
const indexed = api.arrayToIndexedObject(items); // Convert for GunDB storage
|
|
241
|
+
const restored = api.indexedObjectToArray(indexed); // Convert back
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## π **Advanced Gun Operations**
|
|
245
|
+
|
|
246
|
+
For advanced Gun.js operations, use the database instance directly via `api.database` or `shogun.database`:
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
const db = api.database; // or shogun.database
|
|
250
|
+
|
|
251
|
+
// Store nested data with Gun chaining
|
|
252
|
+
await db.get('users').get('alice').get('profile').put({
|
|
253
|
+
name: 'Alice Smith',
|
|
254
|
+
email: 'alice@example.com',
|
|
255
|
+
preferences: {
|
|
256
|
+
theme: 'dark',
|
|
257
|
+
language: 'en'
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// Read nested data
|
|
262
|
+
const profile = await db.get('users').get('alice').get('profile').once().then();
|
|
263
|
+
|
|
264
|
+
// Update specific fields
|
|
265
|
+
await db.get('users').get('alice').get('profile').get('preferences').get('theme').put('light');
|
|
266
|
+
|
|
267
|
+
// Iterate over collections with .map()
|
|
268
|
+
db.get('users').map((user, userId) => {
|
|
269
|
+
console.log(`User ${userId}:`, user);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
// Complex chaining for nested structures
|
|
273
|
+
await db.get('projects').get('my-app').get('tasks').get('1').put({
|
|
274
|
+
title: 'Implement authentication',
|
|
275
|
+
status: 'completed',
|
|
276
|
+
assignee: 'alice'
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// Access Gun instance directly if needed
|
|
280
|
+
const gunInstance = db.gun;
|
|
281
|
+
gunInstance.get('some-path').on((data) => {
|
|
282
|
+
console.log('Real-time data:', data);
|
|
283
|
+
});
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Chaining Examples
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
const db = shogun.database; // or api.database
|
|
290
|
+
|
|
291
|
+
// User management system
|
|
292
|
+
await db.get('users').get('alice').put({
|
|
293
|
+
profile: {
|
|
294
|
+
name: 'Alice',
|
|
295
|
+
email: 'alice@example.com'
|
|
296
|
+
},
|
|
297
|
+
settings: {
|
|
298
|
+
theme: 'dark',
|
|
299
|
+
notifications: true
|
|
300
|
+
},
|
|
301
|
+
posts: {
|
|
302
|
+
'1': { title: 'Hello World', content: 'My first post' },
|
|
303
|
+
'2': { title: 'GunDB is awesome', content: 'Learning decentralized storage' }
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
// Blog system
|
|
308
|
+
await db.get('blog').get('posts').get('2024-01-15').put({
|
|
309
|
+
title: 'Getting Started with Shogun Core',
|
|
310
|
+
author: 'alice',
|
|
311
|
+
content: 'Shogun Core makes decentralized apps easy...',
|
|
312
|
+
tags: ['tutorial', 'decentralized', 'web3'],
|
|
313
|
+
comments: {
|
|
314
|
+
'1': { author: 'bob', text: 'Great tutorial!' },
|
|
315
|
+
'2': { author: 'charlie', text: 'Very helpful, thanks!' }
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
// E-commerce system
|
|
320
|
+
await db.get('shop').get('products').get('laptop-001').put({
|
|
321
|
+
name: 'Gaming Laptop',
|
|
322
|
+
price: 1299.99,
|
|
323
|
+
stock: 15,
|
|
324
|
+
reviews: {
|
|
325
|
+
'1': { user: 'alice', rating: 5, comment: 'Amazing performance!' },
|
|
326
|
+
'2': { user: 'bob', rating: 4, comment: 'Good value for money' }
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// Read complex nested data
|
|
331
|
+
const product = await db.get('shop').get('products').get('laptop-001').once().then();
|
|
332
|
+
console.log('Product:', product.name);
|
|
333
|
+
console.log('Reviews:', product.reviews);
|
|
334
|
+
|
|
335
|
+
// Update nested data
|
|
336
|
+
await db.get('shop').get('products').get('laptop-001').get('stock').put(12);
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### Best Practices
|
|
340
|
+
|
|
341
|
+
1. **Use `api.database` or `shogun.database` for direct Gun operations**
|
|
342
|
+
2. **Use `api` helper methods for standardized data** - profile, settings, collections
|
|
343
|
+
3. **Keep paths descriptive** - Use meaningful path segments like `users/alice/profile`
|
|
344
|
+
4. **Handle errors appropriately** - Chaining operations can fail, always check results
|
|
345
|
+
5. **Use helper methods for conventions** - updateProfile(), saveSettings(), etc. provide standardized locations
|
|
346
|
+
|
|
347
|
+
## Plugin Authentication APIs
|
|
348
|
+
|
|
349
|
+
Shogun Core provides a unified plugin system for different authentication methods. Each plugin implements standardized `login()` and `signUp()` methods that return consistent `AuthResult` and `SignUpResult` objects.
|
|
350
|
+
|
|
351
|
+
### Core Types - β
**FIXED & UNIFIED**
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
// Authentication result interface - used by login methods
|
|
355
|
+
interface AuthResult {
|
|
356
|
+
success: boolean;
|
|
357
|
+
error?: string;
|
|
358
|
+
userPub?: string; // User's public key
|
|
359
|
+
username?: string; // Username or identifier
|
|
360
|
+
sessionToken?: string; // Session token if applicable
|
|
361
|
+
authMethod?: AuthMethod; // Authentication method used
|
|
362
|
+
sea?: {
|
|
363
|
+
// GunDB SEA pair for session persistence
|
|
364
|
+
pub: string;
|
|
365
|
+
priv: string;
|
|
366
|
+
epub: string;
|
|
367
|
+
epriv: string;
|
|
368
|
+
};
|
|
369
|
+
// External auth flow properties
|
|
370
|
+
redirectUrl?: string; // Redirect URL for external auth
|
|
371
|
+
pendingAuth?: boolean; // Indicates pending auth flow
|
|
372
|
+
message?: string; // Status message
|
|
373
|
+
provider?: string; // Provider name
|
|
374
|
+
isNewUser?: boolean; // True if this was a registration
|
|
375
|
+
user?: {
|
|
376
|
+
// User data
|
|
377
|
+
userPub?: string;
|
|
378
|
+
username?: string;
|
|
379
|
+
email?: string;
|
|
380
|
+
name?: string;
|
|
381
|
+
picture?: string;
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Sign up result interface - used by signUp methods β
ENHANCED
|
|
386
|
+
interface SignUpResult {
|
|
387
|
+
success: boolean;
|
|
388
|
+
userPub?: string;
|
|
389
|
+
username?: string;
|
|
390
|
+
pub?: string;
|
|
391
|
+
error?: string;
|
|
392
|
+
message?: string;
|
|
393
|
+
wallet?: any;
|
|
394
|
+
isNewUser?: boolean;
|
|
395
|
+
authMethod?: AuthMethod; // β
ADDED
|
|
396
|
+
sessionToken?: string; // β
ADDED
|
|
397
|
+
sea?: { pub: string; priv: string; epub: string; epriv: string }; // SEA pair for session persistence
|
|
398
|
+
// Multi-device support (WebAuthn and ZK-Proof)
|
|
399
|
+
seedPhrase?: string; // BIP39 mnemonic or trapdoor for account recovery
|
|
400
|
+
// External auth flow support
|
|
401
|
+
redirectUrl?: string;
|
|
402
|
+
pendingAuth?: boolean;
|
|
403
|
+
provider?: string;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Supported authentication methods
|
|
407
|
+
type AuthMethod =
|
|
408
|
+
| "password"
|
|
409
|
+
| "webauthn"
|
|
410
|
+
| "web3"
|
|
411
|
+
| "nostr"
|
|
412
|
+
| "zkproof"
|
|
413
|
+
| "pair";
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
### 1. Traditional Authentication
|
|
417
|
+
|
|
418
|
+
Direct username/password authentication using ShogunCore methods:
|
|
419
|
+
|
|
420
|
+
```typescript
|
|
421
|
+
// Sign up a new user - Returns SignUpResult β
|
|
422
|
+
const signUpResult: SignUpResult = await shogun.signUp("username", "password");
|
|
423
|
+
if (signUpResult.success) {
|
|
424
|
+
console.log("User created:", signUpResult.username);
|
|
425
|
+
console.log("Is new user:", signUpResult.isNewUser);
|
|
426
|
+
console.log("Auth method:", signUpResult.authMethod);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Login with username and password - Returns AuthResult β
|
|
430
|
+
const loginResult: AuthResult = await shogun.login("username", "password");
|
|
431
|
+
if (loginResult.success) {
|
|
432
|
+
console.log("Logged in as:", loginResult.username);
|
|
433
|
+
console.log("User public key:", loginResult.userPub);
|
|
434
|
+
}
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
### 2. Web3 Plugin API
|
|
438
|
+
|
|
439
|
+
Ethereum wallet authentication via MetaMask or other Web3 providers:
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
const web3Plugin = shogun.getPlugin<Web3ConnectorPlugin>("web3");
|
|
443
|
+
|
|
444
|
+
if (web3Plugin && web3Plugin.isAvailable()) {
|
|
445
|
+
// Connect to MetaMask
|
|
446
|
+
const connectionResult = await web3Plugin.connectMetaMask();
|
|
447
|
+
|
|
448
|
+
if (connectionResult.success) {
|
|
449
|
+
const address = connectionResult.address!;
|
|
450
|
+
|
|
451
|
+
// Login with Web3 wallet - Returns AuthResult β
|
|
452
|
+
const loginResult: AuthResult = await web3Plugin.login(address);
|
|
453
|
+
if (loginResult.success) {
|
|
454
|
+
console.log("Web3 login successful");
|
|
455
|
+
console.log("User public key:", loginResult.userPub);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// Register new user with Web3 wallet - Returns SignUpResult β
|
|
459
|
+
const signUpResult: SignUpResult = await web3Plugin.signUp(address);
|
|
460
|
+
if (signUpResult.success) {
|
|
461
|
+
console.log("Web3 registration successful");
|
|
462
|
+
console.log("Is new user:", signUpResult.isNewUser);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Plugin Interface - β
FIXED TYPES
|
|
468
|
+
interface Web3ConnectorPluginInterface {
|
|
469
|
+
// Authentication methods
|
|
470
|
+
login(address: string): Promise<AuthResult>; // β
CORRECT
|
|
471
|
+
signUp(address: string): Promise<SignUpResult>; // β
FIXED
|
|
472
|
+
|
|
473
|
+
// Connection methods
|
|
474
|
+
isAvailable(): boolean;
|
|
475
|
+
connectMetaMask(): Promise<ConnectionResult>;
|
|
476
|
+
getProvider(): Promise<ethers.JsonRpcProvider | ethers.BrowserProvider>;
|
|
477
|
+
getSigner(): Promise<ethers.Signer>;
|
|
478
|
+
|
|
479
|
+
// Credential management
|
|
480
|
+
generateCredentials(address: string): Promise<ISEAPair>;
|
|
481
|
+
generatePassword(signature: string): Promise<string>;
|
|
482
|
+
verifySignature(message: string, signature: string): Promise<string>;
|
|
483
|
+
}
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
### 3. WebAuthn Plugin API
|
|
487
|
+
|
|
488
|
+
Biometric and hardware key authentication with **multi-device support via seed phrase**:
|
|
489
|
+
|
|
490
|
+
```typescript
|
|
491
|
+
const webauthnPlugin = shogun.getPlugin<WebauthnPlugin>("webauthn");
|
|
492
|
+
|
|
493
|
+
if (webauthnPlugin && webauthnPlugin.isSupported()) {
|
|
494
|
+
// β NEW: Register with seed phrase for multi-device support
|
|
495
|
+
const signUpResult: SignUpResult = await webauthnPlugin.signUp("username", {
|
|
496
|
+
generateSeedPhrase: true // Generate BIP39 seed phrase (default: true)
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
if (signUpResult.success) {
|
|
500
|
+
console.log("WebAuthn registration successful");
|
|
501
|
+
console.log("User public key:", signUpResult.userPub);
|
|
502
|
+
|
|
503
|
+
// β οΈ CRITICAL: Display seed phrase to user for backup
|
|
504
|
+
if (signUpResult.seedPhrase) {
|
|
505
|
+
console.log("π SAVE THESE 12 WORDS:");
|
|
506
|
+
console.log(signUpResult.seedPhrase);
|
|
507
|
+
alert(`IMPORTANT: Write down these 12 words to access your account from other devices:\n\n${signUpResult.seedPhrase}`);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// β NEW: Import account on another device using seed phrase
|
|
512
|
+
const importResult = await webauthnPlugin.importFromSeed(
|
|
513
|
+
"username",
|
|
514
|
+
"word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12"
|
|
515
|
+
);
|
|
516
|
+
|
|
517
|
+
if (importResult.success) {
|
|
518
|
+
console.log("Account imported successfully!");
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Authenticate existing user - Returns AuthResult β
|
|
522
|
+
const loginResult: AuthResult = await webauthnPlugin.login("username");
|
|
523
|
+
if (loginResult.success) {
|
|
524
|
+
console.log("WebAuthn authentication successful");
|
|
525
|
+
console.log("Auth method:", loginResult.authMethod); // "webauthn"
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// Plugin Interface - β
FIXED TYPES
|
|
530
|
+
interface WebauthnPluginInterface {
|
|
531
|
+
// Authentication methods
|
|
532
|
+
login(username: string): Promise<AuthResult>; // β
CORRECT
|
|
533
|
+
signUp(username: string): Promise<SignUpResult>; // β
FIXED
|
|
534
|
+
|
|
535
|
+
// Capability checks
|
|
536
|
+
isSupported(): boolean;
|
|
537
|
+
|
|
538
|
+
// WebAuthn-specific methods
|
|
539
|
+
register(username: string, displayName?: string): Promise<WebAuthnCredential>;
|
|
540
|
+
authenticate(username?: string): Promise<WebAuthnCredential>;
|
|
541
|
+
generateCredentials(
|
|
542
|
+
username: string,
|
|
543
|
+
pair?: ISEAPair | null,
|
|
544
|
+
login?: boolean
|
|
545
|
+
): Promise<WebAuthnUniformCredentials>;
|
|
546
|
+
}
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
### 4. Nostr Plugin API
|
|
550
|
+
|
|
551
|
+
Bitcoin wallet and Nostr protocol authentication:
|
|
552
|
+
|
|
553
|
+
```typescript
|
|
554
|
+
const nostrPlugin = shogun.getPlugin<NostrConnectorPlugin>("nostr");
|
|
555
|
+
|
|
556
|
+
if (nostrPlugin && nostrPlugin.isAvailable()) {
|
|
557
|
+
// Connect to Nostr wallet (Bitcoin extension)
|
|
558
|
+
const connectionResult = await nostrPlugin.connectNostrWallet();
|
|
559
|
+
|
|
560
|
+
if (connectionResult.success) {
|
|
561
|
+
const address = connectionResult.address!;
|
|
562
|
+
|
|
563
|
+
// Login with Nostr/Bitcoin wallet - Returns AuthResult β
|
|
564
|
+
const loginResult: AuthResult = await nostrPlugin.login(address);
|
|
565
|
+
if (loginResult.success) {
|
|
566
|
+
console.log("Nostr login successful");
|
|
567
|
+
console.log("Auth method:", loginResult.authMethod); // "nostr"
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// Register with Nostr/Bitcoin wallet - Returns SignUpResult β
|
|
571
|
+
const signUpResult: SignUpResult = await nostrPlugin.signUp(address);
|
|
572
|
+
if (signUpResult.success) {
|
|
573
|
+
console.log("Nostr registration successful");
|
|
574
|
+
console.log("Is new user:", signUpResult.isNewUser);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// Plugin Interface - β
FIXED TYPES
|
|
580
|
+
interface NostrConnectorPluginInterface {
|
|
581
|
+
// Authentication methods
|
|
582
|
+
login(address: string): Promise<AuthResult>; // β
CORRECT
|
|
583
|
+
signUp(address: string): Promise<SignUpResult>; // β
FIXED
|
|
584
|
+
|
|
585
|
+
// Connection methods
|
|
586
|
+
isAvailable(): boolean;
|
|
587
|
+
connectBitcoinWallet(
|
|
588
|
+
type?: "alby" | "nostr" | "manual"
|
|
589
|
+
): Promise<ConnectionResult>;
|
|
590
|
+
connectNostrWallet(): Promise<ConnectionResult>;
|
|
591
|
+
|
|
592
|
+
// Credential and signature management
|
|
593
|
+
generateCredentials(
|
|
594
|
+
address: string,
|
|
595
|
+
signature: string,
|
|
596
|
+
message: string
|
|
597
|
+
): Promise<NostrConnectorCredentials>;
|
|
598
|
+
verifySignature(
|
|
599
|
+
message: string,
|
|
600
|
+
signature: string,
|
|
601
|
+
address: string
|
|
602
|
+
): Promise<boolean>;
|
|
603
|
+
generatePassword(signature: string): Promise<string>;
|
|
604
|
+
}
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
### 5. ZK-Proof Plugin API
|
|
608
|
+
|
|
609
|
+
Zero-Knowledge Proof authentication for **anonymous, privacy-preserving authentication**:
|
|
610
|
+
|
|
611
|
+
```typescript
|
|
612
|
+
const zkPlugin = shogun.getPlugin<ZkProofPlugin>("zkproof");
|
|
613
|
+
|
|
614
|
+
if (zkPlugin && zkPlugin.isAvailable()) {
|
|
615
|
+
// Sign up with ZK-Proof (creates anonymous identity)
|
|
616
|
+
const signUpResult: SignUpResult = await zkPlugin.signUp();
|
|
617
|
+
|
|
618
|
+
if (signUpResult.success) {
|
|
619
|
+
console.log("ZK-Proof registration successful");
|
|
620
|
+
console.log("Public commitment:", signUpResult.username);
|
|
621
|
+
|
|
622
|
+
// β οΈ CRITICAL: Display trapdoor to user for backup
|
|
623
|
+
if (signUpResult.seedPhrase) {
|
|
624
|
+
console.log("π SAVE THIS TRAPDOOR:");
|
|
625
|
+
console.log(signUpResult.seedPhrase);
|
|
626
|
+
alert(`IMPORTANT: Save this trapdoor to access your account:\n\n${signUpResult.seedPhrase}`);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// Login with trapdoor (anonymous authentication)
|
|
631
|
+
const loginResult: AuthResult = await zkPlugin.login(trapdoor);
|
|
632
|
+
if (loginResult.success) {
|
|
633
|
+
console.log("ZK-Proof login successful (anonymous)");
|
|
634
|
+
console.log("Auth method:", loginResult.authMethod); // "zkproof"
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// Plugin Interface
|
|
639
|
+
interface ZkProofPluginInterface {
|
|
640
|
+
// Authentication methods
|
|
641
|
+
login(trapdoor: string): Promise<AuthResult>;
|
|
642
|
+
signUp(seed?: string): Promise<SignUpResult>;
|
|
643
|
+
|
|
644
|
+
// ZK identity management
|
|
645
|
+
generateIdentity(seed?: string): Promise<ZkIdentityData>;
|
|
646
|
+
restoreIdentity(trapdoor: string): Promise<ZkIdentityData>;
|
|
647
|
+
generateCredentials(identityData: ZkIdentityData): Promise<ISEAPair>;
|
|
648
|
+
|
|
649
|
+
// ZK proof operations
|
|
650
|
+
generateProof(identityData: ZkIdentityData, options?: ZkProofGenerationOptions): Promise<any>;
|
|
651
|
+
verifyProof(proof: any, treeDepth?: number): Promise<ZkProofVerificationResult>;
|
|
652
|
+
|
|
653
|
+
// Group management
|
|
654
|
+
addToGroup(commitment: string, groupId?: string): void;
|
|
655
|
+
isAvailable(): boolean;
|
|
656
|
+
}
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
**Advanced: Verifiable Credentials**
|
|
660
|
+
|
|
661
|
+
ZK-Proof can also be used to prove attributes without revealing sensitive data:
|
|
662
|
+
|
|
663
|
+
```typescript
|
|
664
|
+
import { ZkCredentials, CredentialType } from "shogun-core";
|
|
665
|
+
|
|
666
|
+
const zkCreds = new ZkCredentials();
|
|
667
|
+
const identity = await zkPlugin.generateIdentity();
|
|
668
|
+
|
|
669
|
+
// Prove age without revealing birthdate
|
|
670
|
+
const ageProof = await zkCreds.proveAge(
|
|
671
|
+
identity,
|
|
672
|
+
new Date("1990-01-01"),
|
|
673
|
+
18
|
|
674
|
+
);
|
|
675
|
+
// β
Proves: "I am 18 or older"
|
|
676
|
+
// β Does NOT reveal: actual birthdate or exact age
|
|
677
|
+
|
|
678
|
+
// Prove citizenship without revealing country
|
|
679
|
+
const citizenshipProof = await zkCreds.proveCitizenship(
|
|
680
|
+
identity,
|
|
681
|
+
"Italy",
|
|
682
|
+
"EU"
|
|
683
|
+
);
|
|
684
|
+
// β
Proves: "I am an EU citizen"
|
|
685
|
+
// β Does NOT reveal: specific country or passport number
|
|
686
|
+
|
|
687
|
+
// Prove education without revealing institution
|
|
688
|
+
const eduProof = await zkCreds.proveEducation(
|
|
689
|
+
identity,
|
|
690
|
+
"Bachelor of Science",
|
|
691
|
+
"MIT",
|
|
692
|
+
2020
|
|
693
|
+
);
|
|
694
|
+
// β
Proves: "I have a Bachelor of Science degree"
|
|
695
|
+
// β Does NOT reveal: university name or grades
|
|
696
|
+
|
|
697
|
+
// Prove income without revealing exact amount
|
|
698
|
+
const incomeProof = await zkCreds.proveIncome(
|
|
699
|
+
identity,
|
|
700
|
+
75000,
|
|
701
|
+
50000,
|
|
702
|
+
"USD"
|
|
703
|
+
);
|
|
704
|
+
// β
Proves: "Income β₯ $50,000"
|
|
705
|
+
// β Does NOT reveal: exact salary or employer
|
|
706
|
+
```
|
|
707
|
+
|
|
708
|
+
**Use Cases:**
|
|
709
|
+
- Anonymous authentication
|
|
710
|
+
- Age verification (18+, 21+, etc.)
|
|
711
|
+
- Citizenship/residency proofs
|
|
712
|
+
- Education credentials
|
|
713
|
+
- Income verification for loans
|
|
714
|
+
- Employment status
|
|
715
|
+
- KYC compliance without revealing PII
|
|
716
|
+
- Anonymous voting and polls
|
|
717
|
+
|
|
718
|
+
See `src/examples/zkproof-credentials-example.ts` for complete examples.
|
|
719
|
+
|
|
720
|
+
### Comparison of Authentication Methods
|
|
721
|
+
|
|
722
|
+
| Feature | Password | WebAuthn | Web3 | Nostr | ZK-Proof |
|
|
723
|
+
|---------|----------|----------|------|-------|----------|
|
|
724
|
+
| **Anonymous** | β | β | β | β | β
|
|
|
725
|
+
| **Multi-device** | β
| β
(seed) | β
| β
| β
(trapdoor) |
|
|
726
|
+
| **Hardware-free** | β
| β | β | β | β
|
|
|
727
|
+
| **Privacy** | β οΈ | β οΈ | β οΈ | β οΈ | β
|
|
|
728
|
+
| **No wallet needed** | β
| β
| β | β | β
|
|
|
729
|
+
| **Verifiable credentials** | β | β | β | β | β
|
|
|
730
|
+
| **Group membership proofs** | β | β | β | β | β
|
|
|
731
|
+
| **Ease of use** | β
β
β
| β
β
| β
β
| β
β
| β
β
|
|
|
732
|
+
|
|
733
|
+
**Quick Setup:**
|
|
734
|
+
```bash
|
|
735
|
+
# Install dependencies
|
|
736
|
+
yarn add shogun-core
|
|
737
|
+
|
|
738
|
+
# For ZK-Proof circuit files (optional, for advanced proofs)
|
|
739
|
+
cd shogun-core
|
|
740
|
+
yarn setup:zkproof
|
|
741
|
+
|
|
742
|
+
# Run examples
|
|
743
|
+
yarn zkproof:example
|
|
744
|
+
yarn zkproof:credentials
|
|
745
|
+
```
|
|
746
|
+
|
|
747
|
+
## β Multi-Device Support with Seed Phrases
|
|
748
|
+
|
|
749
|
+
WebAuthn authentication now supports **multi-device access** through BIP39 seed phrases, solving the device-bound limitation of traditional WebAuthn.
|
|
750
|
+
|
|
751
|
+
### The Problem with Traditional WebAuthn
|
|
752
|
+
|
|
753
|
+
WebAuthn credentials are **device-specific** by design:
|
|
754
|
+
- Each device generates unique, non-exportable credentials
|
|
755
|
+
- Cannot transfer credentials between devices
|
|
756
|
+
- Changing devices means losing access to your account
|
|
757
|
+
|
|
758
|
+
### The Shogun Core Solution: Seed Phrases
|
|
759
|
+
|
|
760
|
+
When you register with WebAuthn, Shogun Core generates a **12-word BIP39 mnemonic** (seed phrase):
|
|
761
|
+
|
|
762
|
+
```typescript
|
|
763
|
+
const signUpResult = await webauthnPlugin.signUp("alice", {
|
|
764
|
+
generateSeedPhrase: true // Default: true
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
// β οΈ CRITICAL: User MUST save these words!
|
|
768
|
+
console.log("Your seed phrase:", signUpResult.seedPhrase);
|
|
769
|
+
// Example: "abandon ability able about above absent absorb abstract absurd abuse access accident"
|
|
770
|
+
```
|
|
771
|
+
|
|
772
|
+
### Benefits
|
|
773
|
+
|
|
774
|
+
β
**Same Account, Multiple Devices**: Access your account from any device
|
|
775
|
+
β
**Account Recovery**: Restore access if you lose your device
|
|
776
|
+
β
**Decentralized**: No need for password reset emails or centralized recovery
|
|
777
|
+
β
**Compatible**: Works with any BIP39-compatible wallet
|
|
778
|
+
β
**Secure**: 128-bit entropy, cryptographically secure
|
|
779
|
+
|
|
780
|
+
### Usage Examples
|
|
781
|
+
|
|
782
|
+
#### Registration on First Device (iPhone)
|
|
783
|
+
|
|
784
|
+
```typescript
|
|
785
|
+
const webauthnPlugin = shogun.getPlugin<WebauthnPlugin>("webauthn");
|
|
786
|
+
|
|
787
|
+
// Register with Face ID
|
|
788
|
+
const result = await webauthnPlugin.signUp("alice", {
|
|
789
|
+
generateSeedPhrase: true
|
|
790
|
+
});
|
|
791
|
+
|
|
792
|
+
if (result.success && result.seedPhrase) {
|
|
793
|
+
// Display to user with clear warning
|
|
794
|
+
showSeedPhraseBackupUI(result.seedPhrase);
|
|
795
|
+
// Example: "ability abandon about above absent absorb abstract absurd abuse access accident account"
|
|
796
|
+
}
|
|
797
|
+
```
|
|
798
|
+
|
|
799
|
+
#### Import on Second Device (Windows PC)
|
|
800
|
+
|
|
801
|
+
```typescript
|
|
802
|
+
// User enters their 12-word seed phrase
|
|
803
|
+
const seedPhrase = getUserInputSeedPhrase();
|
|
804
|
+
|
|
805
|
+
// Import account using seed phrase
|
|
806
|
+
const result = await webauthnPlugin.importFromSeed("alice", seedPhrase);
|
|
807
|
+
|
|
808
|
+
if (result.success) {
|
|
809
|
+
console.log("Account imported! You can now use Windows Hello.");
|
|
810
|
+
}
|
|
811
|
+
```
|
|
812
|
+
|
|
813
|
+
### User Interface Example
|
|
814
|
+
|
|
815
|
+
```tsx
|
|
816
|
+
// React component for seed phrase backup
|
|
817
|
+
function SeedPhraseBackup({ seedPhrase }: { seedPhrase: string }) {
|
|
818
|
+
const words = seedPhrase.split(' ');
|
|
819
|
+
|
|
820
|
+
return (
|
|
821
|
+
<div className="seed-phrase-backup">
|
|
822
|
+
<h2>π Save Your Recovery Phrase</h2>
|
|
823
|
+
<p><strong>Write down these 12 words in order</strong></p>
|
|
824
|
+
<p className="warning">
|
|
825
|
+
β οΈ Without these words, you cannot recover your account or access it from other devices!
|
|
826
|
+
</p>
|
|
827
|
+
|
|
828
|
+
<div className="word-grid">
|
|
829
|
+
{words.map((word, index) => (
|
|
830
|
+
<div key={index} className="word-item">
|
|
831
|
+
<span className="word-number">{index + 1}.</span>
|
|
832
|
+
<span className="word-text">{word}</span>
|
|
833
|
+
</div>
|
|
834
|
+
))}
|
|
835
|
+
</div>
|
|
836
|
+
|
|
837
|
+
<div className="actions">
|
|
838
|
+
<button onClick={() => downloadSeedPhrase(seedPhrase)}>
|
|
839
|
+
π₯ Download as Text File
|
|
840
|
+
</button>
|
|
841
|
+
<button onClick={() => printSeedPhrase(seedPhrase)}>
|
|
842
|
+
π¨οΈ Print on Paper
|
|
843
|
+
</button>
|
|
844
|
+
</div>
|
|
845
|
+
|
|
846
|
+
<label>
|
|
847
|
+
<input type="checkbox" required />
|
|
848
|
+
I have safely stored my 12-word recovery phrase
|
|
849
|
+
</label>
|
|
850
|
+
</div>
|
|
851
|
+
);
|
|
852
|
+
}
|
|
853
|
+
```
|
|
854
|
+
|
|
855
|
+
### Security Best Practices
|
|
856
|
+
|
|
857
|
+
1. **Never store seed phrases digitally** - Write them on paper
|
|
858
|
+
2. **Keep multiple backups** - Store in different secure locations
|
|
859
|
+
3. **Never share your seed phrase** - Anyone with it can access your account
|
|
860
|
+
4. **Verify before moving on** - Double-check you wrote it correctly
|
|
861
|
+
5. **Use steel backup** - For maximum durability (fire/water proof)
|
|
862
|
+
|
|
863
|
+
### Legacy Device-Bound Mode
|
|
864
|
+
|
|
865
|
+
If you don't need multi-device support, you can disable seed phrase generation:
|
|
866
|
+
|
|
867
|
+
```typescript
|
|
868
|
+
const result = await webauthnPlugin.signUp("alice", {
|
|
869
|
+
generateSeedPhrase: false // Device-bound only
|
|
870
|
+
});
|
|
871
|
+
// No seed phrase returned - traditional WebAuthn behavior
|
|
872
|
+
```
|
|
873
|
+
|
|
874
|
+
### Browser Usage (via CDN)
|
|
875
|
+
|
|
876
|
+
You can also use Shogun Core directly in the browser by including it from a CDN. This is ideal for static sites or lightweight applications.
|
|
877
|
+
|
|
878
|
+
```html
|
|
879
|
+
<!DOCTYPE html>
|
|
880
|
+
<html>
|
|
881
|
+
<head>
|
|
882
|
+
<title>Shogun Core in Browser</title>
|
|
883
|
+
</head>
|
|
884
|
+
<body>
|
|
885
|
+
<h1>My dApp</h1>
|
|
886
|
+
<!-- Required dependencies for Shogun Core -->
|
|
887
|
+
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
|
|
888
|
+
<script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>
|
|
889
|
+
|
|
890
|
+
<!-- Shogun Core library -->
|
|
891
|
+
<script src="https://cdn.jsdelivr.net/npm/shogun-core/dist/browser/shogun-core.js"></script>
|
|
892
|
+
|
|
893
|
+
<script>
|
|
894
|
+
// Access the global Shogun Core function
|
|
895
|
+
const shogunCore = window.SHOGUN_CORE({
|
|
896
|
+
peers: ["https://gun-manhattan.herokuapp.com/gun"],
|
|
897
|
+
scope: "my-browser-app",
|
|
898
|
+
web3: { enabled: true },
|
|
899
|
+
webauthn: {
|
|
900
|
+
enabled: true,
|
|
901
|
+
rpName: "My Browser dApp",
|
|
902
|
+
rpId: window.location.hostname,
|
|
903
|
+
},
|
|
904
|
+
});
|
|
905
|
+
|
|
906
|
+
console.log("Shogun Core initialized in browser!", shogunCore);
|
|
907
|
+
|
|
908
|
+
async function connectWallet() {
|
|
909
|
+
if (shogunCore.hasPlugin("web3")) {
|
|
910
|
+
const web3Plugin = shogunCore.getPlugin("web3");
|
|
911
|
+
try {
|
|
912
|
+
const provider = await web3Plugin.getProvider();
|
|
913
|
+
const signer = provider.getSigner();
|
|
914
|
+
const address = await signer.getAddress();
|
|
915
|
+
await web3Plugin.login(address);
|
|
916
|
+
console.log("Logged in with address:", address);
|
|
917
|
+
} catch (error) {
|
|
918
|
+
console.error("Web3 login failed:", error);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
</script>
|
|
923
|
+
</body>
|
|
924
|
+
</html>
|
|
925
|
+
```
|
|
926
|
+
|
|
927
|
+
## API Reference
|
|
928
|
+
|
|
929
|
+
### β **Simple API Methods**
|
|
930
|
+
|
|
931
|
+
#### Authentication
|
|
932
|
+
- `signup(username: string, password: string): Promise<UserInfo | null>` - Create new user account
|
|
933
|
+
- `login(username: string, password: string): Promise<UserInfo | null>` - Authenticate with username/password
|
|
934
|
+
- `logout(): void` - Logout current user
|
|
935
|
+
- `isLoggedIn(): boolean` - Check if user is authenticated
|
|
936
|
+
|
|
937
|
+
#### Data Operations
|
|
938
|
+
- `get<T>(path: string): Promise<T | null>` - Get data from path
|
|
939
|
+
- `put<T>(path: string, data: T): Promise<boolean>` - Store data at path
|
|
940
|
+
- `set<T>(path: string, data: T): Promise<boolean>` - Update data at path
|
|
941
|
+
- `remove(path: string): Promise<boolean>` - Remove data from path
|
|
942
|
+
|
|
943
|
+
#### Advanced Chaining Operations (NEW!)
|
|
944
|
+
- `node(path: string): GunNode` - Get Gun node for direct chaining (recommended)
|
|
945
|
+
- `chain(path: string): ChainingWrapper` - Get simplified chaining wrapper
|
|
946
|
+
- `getNode(path: string): GunNode` - Get Gun node for advanced operations like .map()
|
|
947
|
+
|
|
948
|
+
#### User Space Operations
|
|
949
|
+
- `putUserData<T>(path: string, data: T): Promise<boolean>` - Store user-specific data
|
|
950
|
+
- `getUserData<T>(path: string): Promise<T | null>` - Get user-specific data
|
|
951
|
+
- `setUserData<T>(path: string, data: T): Promise<boolean>` - Update user-specific data
|
|
952
|
+
- `removeUserData(path: string): Promise<boolean>` - Remove user-specific data
|
|
953
|
+
- `getAllUserData(): Promise<Record<string, unknown> | null>` - Get all user data
|
|
954
|
+
|
|
955
|
+
#### Profile & Settings
|
|
956
|
+
- `updateProfile(profileData: ProfileData): Promise<boolean>` - Update user profile
|
|
957
|
+
- `getProfile(): Promise<Record<string, unknown> | null>` - Get user profile
|
|
958
|
+
- `saveSettings(settings: Record<string, unknown>): Promise<boolean>` - Save user settings
|
|
959
|
+
- `getSettings(): Promise<Record<string, unknown> | null>` - Get user settings
|
|
960
|
+
- `savePreferences(preferences: Record<string, unknown>): Promise<boolean>` - Save user preferences
|
|
961
|
+
- `getPreferences(): Promise<Record<string, unknown> | null>` - Get user preferences
|
|
962
|
+
|
|
963
|
+
#### Collections
|
|
964
|
+
- `createCollection<T>(name: string, items: Record<string, T>): Promise<boolean>` - Create user collection
|
|
965
|
+
- `addToCollection<T>(name: string, itemId: string, item: T): Promise<boolean>` - Add item to collection
|
|
966
|
+
- `getCollection(name: string): Promise<Record<string, unknown> | null>` - Get collection
|
|
967
|
+
- `removeFromCollection(name: string, itemId: string): Promise<boolean>` - Remove item from collection
|
|
968
|
+
|
|
969
|
+
#### Utility Functions
|
|
970
|
+
- `arrayToIndexedObject<T>(arr: T[]): Record<string, T>` - Convert array to indexed object (helper)
|
|
971
|
+
- `indexedObjectToArray<T>(indexedObj: Record<string, T>): T[]` - Convert indexed object to array (helper)
|
|
972
|
+
|
|
973
|
+
#### β οΈ **REMOVED FUNCTIONS**
|
|
974
|
+
The following array functions have been **REMOVED** due to GunDB compatibility issues:
|
|
975
|
+
- `putUserArray()`, `getUserArray()`, `addToUserArray()`, `removeFromUserArray()`, `updateInUserArray()`
|
|
976
|
+
|
|
977
|
+
**Use collections or direct GunDB operations instead** (see examples above).
|
|
978
|
+
|
|
979
|
+
### Advanced API Methods
|
|
980
|
+
|
|
981
|
+
#### Core Authentication
|
|
982
|
+
- `login(username: string, password: string): Promise<AuthResult>` - Authenticate with username/password
|
|
983
|
+
- `loginWithPair(pair: ISEAPair): Promise<AuthResult>` - Authenticate directly with a GunDB SEA pair
|
|
984
|
+
- `signUp(username: string, password: string, email?: string, pair?: ISEAPair | null): Promise<SignUpResult>` - Create new user account
|
|
985
|
+
- `logout(): void` - Logout current user
|
|
986
|
+
- `isLoggedIn(): boolean` - Check if user is authenticated
|
|
987
|
+
- `setAuthMethod(method: AuthMethod): void` - Set authentication method
|
|
988
|
+
- `getAuthMethod(): AuthMethod | undefined` - Get current authentication method
|
|
989
|
+
- `saveCredentials(credentials: any): Promise<void>` - Save user credentials
|
|
990
|
+
|
|
991
|
+
#### Plugin Management
|
|
992
|
+
- `getPlugin<T>(name: string): T | undefined` - Get plugin by name
|
|
993
|
+
- `hasPlugin(name: string): boolean` - Check if plugin exists
|
|
994
|
+
- `register(plugin: ShogunPlugin): void` - Register custom plugin
|
|
995
|
+
- `unregister(pluginName: string): void` - Remove plugin
|
|
996
|
+
- `getPluginsInfo(): Array<{name: string; version: string; category?: PluginCategory; description?: string}>` - Get detailed plugin information
|
|
997
|
+
- `getPluginCount(): number` - Get total number of plugins
|
|
998
|
+
- `getPluginsInitializationStatus(): Record<string, {initialized: boolean; error?: string}>` - Check plugin initialization status
|
|
999
|
+
- `getPluginsByCategory(category: PluginCategory): ShogunPlugin[]` - Get plugins by category
|
|
1000
|
+
- `validatePluginSystem(): {...}` - Validate plugin system health
|
|
1001
|
+
- `reinitializeFailedPlugins(): {...}` - Reinitialize failed plugins
|
|
1002
|
+
- `checkPluginCompatibility(): {...}` - Check plugin compatibility
|
|
1003
|
+
- `getPluginSystemDebugInfo(): {...}` - Get comprehensive debug information
|
|
1004
|
+
|
|
1005
|
+
#### Peer Network Management (Database)
|
|
1006
|
+
- `addPeer(peer: string): void` - Add new peer to network
|
|
1007
|
+
- `removePeer(peer: string): void` - Remove peer from network
|
|
1008
|
+
- `getCurrentPeers(): string[]` - Get currently connected peers
|
|
1009
|
+
- `getAllConfiguredPeers(): string[]` - Get all configured peers
|
|
1010
|
+
- `getPeerInfo(): {[peer: string]: {connected: boolean; status: string}}` - Get detailed peer information
|
|
1011
|
+
- `reconnectToPeer(peer: string): void` - Reconnect to specific peer
|
|
1012
|
+
- `resetPeers(newPeers?: string[]): void` - Reset all peers and optionally add new ones
|
|
1013
|
+
|
|
1014
|
+
#### Advanced User Management (Database)
|
|
1015
|
+
- `getUserByAlias(alias: string): Promise<{...}>` - Get user by alias/username
|
|
1016
|
+
- `getUserDataByPub(userPub: string): Promise<{...}>` - Get user by public key
|
|
1017
|
+
- `getUserPubByEpub(epub: string): Promise<string | null>` - Get user public key by encryption key
|
|
1018
|
+
- `getUserAliasByPub(userPub: string): Promise<string | null>` - Get user alias by public key
|
|
1019
|
+
- `getAllRegisteredUsers(): Promise<Array<{...}>>` - Get all registered users
|
|
1020
|
+
- `updateUserLastSeen(userPub: string): Promise<void>` - Update user's last seen timestamp
|
|
1021
|
+
|
|
1022
|
+
#### Password Recovery & Security (Database)
|
|
1023
|
+
- `setPasswordHintWithSecurity(username: string, password: string, hint: string, securityQuestions: string[], securityAnswers: string[]): Promise<{success: boolean; error?: string}>` - Set up password recovery
|
|
1024
|
+
- `forgotPassword(username: string, securityAnswers: string[]): Promise<{success: boolean; hint?: string; error?: string}>` - Recover password
|
|
1025
|
+
|
|
1026
|
+
#### Error Handling & Debugging
|
|
1027
|
+
- `getRecentErrors(count?: number): ShogunError[]` - Get recent errors for debugging
|
|
1028
|
+
|
|
1029
|
+
#### Event Handling
|
|
1030
|
+
- `on<K extends keyof ShogunEventMap>(eventName: K, listener: Function): this` - Subscribe to typed events
|
|
1031
|
+
- `off<K extends keyof ShogunEventMap>(eventName: K, listener: Function): this` - Unsubscribe from events
|
|
1032
|
+
- `once<K extends keyof ShogunEventMap>(eventName: K, listener: Function): this` - Subscribe to one-time events
|
|
1033
|
+
- `emit<K extends keyof ShogunEventMap>(eventName: K, data?: ShogunEventMap[K]): boolean` - Emit custom events
|
|
1034
|
+
- `removeAllListeners(eventName?: string | symbol): this` - Remove all event listeners
|
|
1035
|
+
|
|
1036
|
+
### Configuration Options
|
|
1037
|
+
|
|
1038
|
+
```typescript
|
|
1039
|
+
interface ShogunCoreConfig {
|
|
1040
|
+
peers?: string[]; // GunDB peer URLs
|
|
1041
|
+
scope?: string; // Application scope
|
|
1042
|
+
authToken?: string; // GunDB super peer secret
|
|
1043
|
+
appToken?: string; // Application token
|
|
1044
|
+
|
|
1045
|
+
// Plugin configurations
|
|
1046
|
+
webauthn?: {
|
|
1047
|
+
enabled?: boolean;
|
|
1048
|
+
rpName?: string;
|
|
1049
|
+
rpId?: string;
|
|
1050
|
+
};
|
|
1051
|
+
|
|
1052
|
+
web3?: {
|
|
1053
|
+
enabled?: boolean;
|
|
1054
|
+
};
|
|
1055
|
+
|
|
1056
|
+
nostr?: {
|
|
1057
|
+
enabled?: boolean;
|
|
1058
|
+
};
|
|
1059
|
+
|
|
1060
|
+
zkproof?: {
|
|
1061
|
+
enabled?: boolean;
|
|
1062
|
+
defaultGroupId?: string;
|
|
1063
|
+
deterministic?: boolean;
|
|
1064
|
+
};
|
|
1065
|
+
|
|
1066
|
+
// Timeouts
|
|
1067
|
+
timeouts?: {
|
|
1068
|
+
login?: number;
|
|
1069
|
+
signup?: number;
|
|
1070
|
+
operation?: number;
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
```
|
|
1074
|
+
|
|
1075
|
+
## Event System
|
|
1076
|
+
|
|
1077
|
+
Shogun Core provides a comprehensive typed event system for monitoring authentication and data changes:
|
|
1078
|
+
|
|
1079
|
+
```typescript
|
|
1080
|
+
// Available events with their data types
|
|
1081
|
+
interface ShogunEventMap {
|
|
1082
|
+
"auth:login": AuthEventData; // User logged in
|
|
1083
|
+
"auth:logout": void; // User logged out
|
|
1084
|
+
"auth:signup": AuthEventData; // New user registered
|
|
1085
|
+
"plugin:registered": { name: string; version?: string; category?: string }; // Plugin registered
|
|
1086
|
+
"plugin:unregistered": { name: string }; // Plugin unregistered
|
|
1087
|
+
debug: { action: string; [key: string]: any }; // Debug information
|
|
1088
|
+
error: ErrorEventData; // Error occurred
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
// Listen for authentication events with full type safety
|
|
1092
|
+
shogun.on("auth:login", (data) => {
|
|
1093
|
+
console.log("User logged in:", data.username);
|
|
1094
|
+
console.log("Authentication method:", data.method);
|
|
1095
|
+
if (data.provider) {
|
|
1096
|
+
console.log("Provider:", data.provider);
|
|
1097
|
+
}
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
shogun.on("auth:logout", () => {
|
|
1101
|
+
console.log("User logged out");
|
|
1102
|
+
});
|
|
1103
|
+
|
|
1104
|
+
shogun.on("auth:signup", (data) => {
|
|
1105
|
+
console.log("New user signed up:", data.username);
|
|
1106
|
+
});
|
|
1107
|
+
|
|
1108
|
+
// Listen for errors
|
|
1109
|
+
shogun.on("error", (error) => {
|
|
1110
|
+
console.error("Shogun error:", error.message);
|
|
1111
|
+
});
|
|
1112
|
+
```
|
|
1113
|
+
|
|
1114
|
+
## Password Recovery System
|
|
1115
|
+
|
|
1116
|
+
Shogun Core includes a secure password recovery system using security questions:
|
|
1117
|
+
|
|
1118
|
+
```typescript
|
|
1119
|
+
// Set password hint with security questions
|
|
1120
|
+
await shogun.db.setPasswordHint(
|
|
1121
|
+
"username",
|
|
1122
|
+
"password",
|
|
1123
|
+
"My favorite color",
|
|
1124
|
+
["What is your favorite color?", "What was your first pet's name?"],
|
|
1125
|
+
["blue", "fluffy"]
|
|
1126
|
+
);
|
|
1127
|
+
|
|
1128
|
+
// Recover password using security answers
|
|
1129
|
+
const result = await shogun.db.forgotPassword("username", ["blue", "fluffy"]);
|
|
1130
|
+
|
|
1131
|
+
if (result.success) {
|
|
1132
|
+
console.log("Password hint:", result.hint);
|
|
1133
|
+
}
|
|
1134
|
+
```
|
|
1135
|
+
|
|
1136
|
+
Note: The cryptographic wallet derivation feature has been removed in v1.9.5 to simplify the architecture.
|
|
1137
|
+
|
|
1138
|
+
## Error Handling
|
|
1139
|
+
|
|
1140
|
+
Shogun Core includes comprehensive error handling with typed errors:
|
|
1141
|
+
|
|
1142
|
+
```typescript
|
|
1143
|
+
import { ShogunError, ErrorType } from "shogun-core";
|
|
1144
|
+
|
|
1145
|
+
try {
|
|
1146
|
+
await shogun.login("username", "password");
|
|
1147
|
+
} catch (error) {
|
|
1148
|
+
if (error instanceof ShogunError) {
|
|
1149
|
+
switch (error.type) {
|
|
1150
|
+
case ErrorType.AUTHENTICATION:
|
|
1151
|
+
console.error("Invalid credentials");
|
|
1152
|
+
break;
|
|
1153
|
+
case ErrorType.NETWORK:
|
|
1154
|
+
console.error("Network connection failed");
|
|
1155
|
+
break;
|
|
1156
|
+
default:
|
|
1157
|
+
console.error("Unknown error:", error.message);
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
```
|
|
1162
|
+
|
|
1163
|
+
## Contributing
|
|
1164
|
+
|
|
1165
|
+
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
1166
|
+
|
|
1167
|
+
## License
|
|
1168
|
+
|
|
1169
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
1170
|
+
|
|
1171
|
+
## SHIP Standards (Shogun Interface Proposals)
|
|
1172
|
+
|
|
1173
|
+
Shogun Core implements **SHIP standards** - modular, composable protocols for decentralized applications:
|
|
1174
|
+
|
|
1175
|
+
- **[SHIP-00](./ship/SHIP_00.md)**: Identity & Authentication Foundation
|
|
1176
|
+
- **[SHIP-01](./ship/SHIP_01.md)**: Decentralized Encrypted Messaging
|
|
1177
|
+
- **[SHIP-02](./ship/SHIP_02.md)**: Ethereum HD Wallet & Transaction Sending
|
|
1178
|
+
- **[SHIP-03](./ship/SHIP_03.md)**: Dual-Key Stealth Addresses (ERC-5564)
|
|
1179
|
+
- **[SHIP-04](./ship/SHIP_04.md)**: Multi-Modal Authentication (WebAuthn/Web3/Nostr/ZK-Proof)
|
|
1180
|
+
|
|
1181
|
+
See [ship/README.md](./ship/README.md) for complete SHIP documentation and examples.
|
|
1182
|
+
|
|
1183
|
+
## Advanced Features
|
|
1184
|
+
|
|
1185
|
+
For advanced use cases and comprehensive API coverage, see the [Advanced API Features](./API.md#advanced-api-features) section which includes:
|
|
1186
|
+
|
|
1187
|
+
- **Advanced Plugin Management**: Plugin health monitoring, compatibility checking, and system validation
|
|
1188
|
+
- **Peer Network Management**: Dynamic peer connection management and network monitoring
|
|
1189
|
+
- **Advanced User Management**: Comprehensive user lookup, tracking, and metadata management
|
|
1190
|
+
- **Password Recovery & Security**: Secure password hint system with security questions
|
|
1191
|
+
- **Error Handling & Debugging**: Advanced error tracking and debugging capabilities
|
|
1192
|
+
- **Event System**: Complete event handling reference with type safety
|
|
1193
|
+
- **Database Lifecycle**: Advanced database initialization and management
|
|
1194
|
+
|
|
1195
|
+
## Support
|
|
1196
|
+
|
|
1197
|
+
- π [Documentation](https://shogun-core-docs.vercel.app/)
|
|
1198
|
+
- π [Advanced API Reference](./API.md#advanced-api-features)
|
|
1199
|
+
- π¬ [Telegram Community](t.me/shogun_eco)
|
|
1200
|
+
- π [Issue Tracker](https://github.com/scobru/shogun-core/issues)
|
|
1201
|
+
|
|
1202
|
+
# SHOGUN CORE
|
|
1203
|
+
|
|
1204
|
+
Core library for Shogun Ecosystem
|
|
1205
|
+
|
|
1206
|
+
## Testing
|
|
1207
|
+
|
|
1208
|
+
This project includes a comprehensive test suite that covers all major functionality and has been recently updated to align with the current codebase structure.
|
|
1209
|
+
|
|
1210
|
+
### β
**Test Suite Status (Updated)**
|
|
1211
|
+
|
|
1212
|
+
- **β
All Tests Passing**: 696+ tests pass successfully
|
|
1213
|
+
- **β
Plugin System**: Complete plugin functionality testing
|
|
1214
|
+
- **β
Authentication Methods**: All auth methods (WebAuthn, Web3, Nostr, ZK-Proof) tested
|
|
1215
|
+
- **β
Simple API**: Full coverage of SimpleGunAPI functionality
|
|
1216
|
+
- **β
Error Handling**: Comprehensive error handling and edge case testing
|
|
1217
|
+
- **β
Browser Compatibility**: Cross-browser support validation
|
|
1218
|
+
- **β
Integration Tests**: End-to-end functionality testing
|
|
1219
|
+
|
|
1220
|
+
### Test Coverage
|
|
1221
|
+
|
|
1222
|
+
This project includes a comprehensive test suite that covers:
|
|
1223
|
+
|
|
1224
|
+
### Unit Tests
|
|
1225
|
+
|
|
1226
|
+
- **Validation Utils** (`src/__tests__/utils/validation.test.ts`)
|
|
1227
|
+
- Username validation
|
|
1228
|
+
- Email validation
|
|
1229
|
+
- Username generation from identity
|
|
1230
|
+
- Deterministic password generation
|
|
1231
|
+
|
|
1232
|
+
- **Error Handler** (`src/__tests__/utils/errorHandler.test.ts`)
|
|
1233
|
+
- Error creation and handling
|
|
1234
|
+
- Error statistics and logging
|
|
1235
|
+
- Retry logic
|
|
1236
|
+
- External logger integration
|
|
1237
|
+
|
|
1238
|
+
- **Event Emitter** (`src/__tests__/utils/eventEmitter.test.ts`)
|
|
1239
|
+
- Event registration and emission
|
|
1240
|
+
- Listener management
|
|
1241
|
+
- Error handling in listeners
|
|
1242
|
+
- Symbol events support
|
|
1243
|
+
|
|
1244
|
+
- **Storage** (`src/__tests__/storage/storage.test.ts`)
|
|
1245
|
+
- Memory and localStorage operations
|
|
1246
|
+
- Error handling
|
|
1247
|
+
- Test mode behavior
|
|
1248
|
+
- Data persistence
|
|
1249
|
+
|
|
1250
|
+
### Integration Tests
|
|
1251
|
+
|
|
1252
|
+
- **ShogunCore** (`src/__tests__/integration/shogunCore.test.ts`)
|
|
1253
|
+
- Plugin system validation
|
|
1254
|
+
- Authentication methods
|
|
1255
|
+
- Event system
|
|
1256
|
+
- Configuration handling
|
|
1257
|
+
- Error handling
|
|
1258
|
+
|
|
1259
|
+
### Browser Compatibility Tests
|
|
1260
|
+
|
|
1261
|
+
- **Compatibility** (`src/__tests__/browser/compatibility.test.ts`)
|
|
1262
|
+
- localStorage availability
|
|
1263
|
+
- Crypto API support
|
|
1264
|
+
- WebAuthn detection
|
|
1265
|
+
- Web3 provider detection
|
|
1266
|
+
- Event system compatibility
|
|
1267
|
+
- TextEncoder/TextDecoder support
|
|
1268
|
+
- Fetch API compatibility
|
|
1269
|
+
- URL API compatibility
|
|
1270
|
+
- Performance API compatibility
|
|
1271
|
+
- Console API compatibility
|
|
1272
|
+
|
|
1273
|
+
## Running Tests
|
|
1274
|
+
|
|
1275
|
+
You can eseguire i test sia dalla root del monorepo sia entrando nella cartella `shogun-core`.
|
|
1276
|
+
|
|
1277
|
+
Inside `shogun-core/` directory:
|
|
1278
|
+
|
|
1279
|
+
```bash
|
|
1280
|
+
# Install deps
|
|
1281
|
+
yarn install
|
|
1282
|
+
|
|
1283
|
+
# Tutti i test (una sola esecuzione) con coverage
|
|
1284
|
+
yarn test:ci
|
|
1285
|
+
|
|
1286
|
+
# Watch mode
|
|
1287
|
+
yarn test:watch
|
|
1288
|
+
|
|
1289
|
+
# Coverage (report HTML in coverage/lcov-report/index.html)
|
|
1290
|
+
yarn coverage
|
|
1291
|
+
|
|
1292
|
+
# Solo i test dei plugin
|
|
1293
|
+
yarn test src/__tests__/plugins
|
|
1294
|
+
|
|
1295
|
+
# Evita conflitti di config Jest (se servisse)
|
|
1296
|
+
yarn jest --ci --coverage --watchAll=false --config jest.config.js
|
|
1297
|
+
```
|
|
1298
|
+
|
|
1299
|
+
From repository root (senza cambiare directory):
|
|
1300
|
+
|
|
1301
|
+
```bash
|
|
1302
|
+
# Install deps
|
|
1303
|
+
yarn --cwd shogun-core install
|
|
1304
|
+
|
|
1305
|
+
# Tutti i test con coverage (CIβlike)
|
|
1306
|
+
yarn --cwd shogun-core test:ci
|
|
1307
|
+
|
|
1308
|
+
# Solo plugin tests
|
|
1309
|
+
yarn --cwd shogun-core test src/__tests__/plugins
|
|
1310
|
+
|
|
1311
|
+
# Coverage
|
|
1312
|
+
yarn --cwd shogun-core coverage
|
|
1313
|
+
|
|
1314
|
+
# Watch mode
|
|
1315
|
+
yarn --cwd shogun-core test:watch
|
|
1316
|
+
|
|
1317
|
+
# Se compaiono piΓΉ configurazioni Jest, specifica esplicitamente il config file
|
|
1318
|
+
yarn --cwd shogun-core jest --ci --coverage --watchAll=false --config jest.config.js
|
|
1319
|
+
```
|
|
1320
|
+
|
|
1321
|
+
CI & QA scripts:
|
|
1322
|
+
|
|
1323
|
+
```bash
|
|
1324
|
+
# Mutation testing (Stryker) β piΓΉ lento, richiede devDeps installate
|
|
1325
|
+
yarn --cwd shogun-core mutation
|
|
1326
|
+
|
|
1327
|
+
# SAST (Semgrep) β richiede semgrep installato (es. `pip install semgrep`)
|
|
1328
|
+
yarn --cwd shogun-core sast
|
|
1329
|
+
```
|
|
1330
|
+
|
|
1331
|
+
## Test Coverage
|
|
1332
|
+
|
|
1333
|
+
The test suite provides comprehensive coverage of:
|
|
1334
|
+
|
|
1335
|
+
- β
**Utility Functions** - 100% coverage
|
|
1336
|
+
- β
**Error Handling** - 100% coverage
|
|
1337
|
+
- β
**Event System** - 100% coverage
|
|
1338
|
+
- β
**Storage Operations** - 100% coverage
|
|
1339
|
+
- β
**Plugin System** - API validation
|
|
1340
|
+
- β
**Browser Compatibility** - Cross-browser support
|
|
1341
|
+
- β
**Configuration Validation** - Config handling
|
|
1342
|
+
|
|
1343
|
+
## Test Philosophy
|
|
1344
|
+
|
|
1345
|
+
These tests are designed to be **realistic and non-intrusive**:
|
|
1346
|
+
|
|
1347
|
+
- **No codebase modifications** - Tests work with existing code
|
|
1348
|
+
- **Comprehensive coverage** - All public APIs tested
|
|
1349
|
+
- **Error resilience** - Tests error handling and edge cases
|
|
1350
|
+
- **Browser compatibility** - Cross-browser support validation
|
|
1351
|
+
- **Performance aware** - Tests don't impact runtime performance
|
|
1352
|
+
|
|
1353
|
+
## Test Structure
|
|
1354
|
+
|
|
1355
|
+
```
|
|
1356
|
+
src/__tests__/
|
|
1357
|
+
βββ setup.ts # Global test setup
|
|
1358
|
+
βββ utils/
|
|
1359
|
+
β βββ validation.test.ts # Validation utility tests
|
|
1360
|
+
β βββ errorHandler.test.ts # Error handling tests
|
|
1361
|
+
β βββ eventEmitter.test.ts # Event system tests
|
|
1362
|
+
βββ storage/
|
|
1363
|
+
β βββ storage.test.ts # Storage operation tests
|
|
1364
|
+
βββ integration/
|
|
1365
|
+
β βββ shogunCore.test.ts # Core integration tests
|
|
1366
|
+
βββ browser/
|
|
1367
|
+
βββ compatibility.test.ts # Browser compatibility tests
|
|
1368
|
+
```
|
|
1369
|
+
|
|
1370
|
+
## Adding New Tests
|
|
1371
|
+
|
|
1372
|
+
When adding new tests:
|
|
1373
|
+
|
|
1374
|
+
1. Follow the existing test structure
|
|
1375
|
+
2. Use descriptive test names
|
|
1376
|
+
3. Test both success and failure cases
|
|
1377
|
+
4. Mock external dependencies appropriately
|
|
1378
|
+
5. Ensure tests are isolated and repeatable
|