shogun-core 6.1.1 → 6.2.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 CHANGED
@@ -18,7 +18,6 @@ Shogun Core is a comprehensive SDK for building decentralized applications (dApp
18
18
  - 📱 **Reactive Programming**: RxJS integration for real-time data streams
19
19
  - 🛡️ **Security**: End-to-end encryption and secure key management
20
20
  - 🎯 **TypeScript**: Full TypeScript support with comprehensive type definitions
21
- - ⭐ **Simple API**: Easy-to-use wrapper for common operations
22
21
 
23
22
  ## Installation
24
23
 
@@ -30,52 +29,18 @@ yarn add shogun-core
30
29
 
31
30
  ## Quick Start
32
31
 
33
- ### Simple API Setup (Recommended)
34
-
35
32
  ```typescript
36
- import { quickStart, Gun } from "shogun-core";
33
+ import { ShogunCore } from "shogun-core";
34
+ import Gun from "gun";
37
35
 
38
- // Create Gun instance
36
+ // Create Gun instance first
39
37
  const gun = Gun({
40
38
  peers: ['https://gun-manhattan.herokuapp.com/gun']
41
39
  });
42
40
 
43
- // Quick start with simple API
44
- const shogun = quickStart(gun, 'my-app');
45
- await shogun.init();
46
-
47
- // Use simplified API
48
- const user = await shogun.api.signup('alice', 'password123');
49
- if (user) {
50
- console.log('User created:', user.username);
51
-
52
- // User space operations
53
- await shogun.api.updateProfile({
54
- name: 'Alice',
55
- email: 'alice@example.com'
56
- });
57
-
58
- await shogun.api.saveSettings({
59
- theme: 'dark',
60
- language: 'en'
61
- });
62
-
63
- // Create collections
64
- await shogun.api.createCollection('todos', {
65
- '1': { text: 'Learn Shogun Core', done: false },
66
- '2': { text: 'Build dApp', done: false }
67
- });
68
- }
69
- ```
70
-
71
- ### Advanced Setup (Full Features)
72
-
73
- ```typescript
74
- import { ShogunCore } from "shogun-core";
75
-
41
+ // Initialize Shogun Core with the Gun instance
76
42
  const shogun = new ShogunCore({
77
- peers: ["https://gun-manhattan.herokuapp.com/gun"],
78
- scope: "my-awesome-app",
43
+ gunInstance: gun,
79
44
 
80
45
  // Enable authentication plugins
81
46
  web3: { enabled: true },
@@ -89,58 +54,47 @@ const shogun = new ShogunCore({
89
54
  enabled: true,
90
55
  defaultGroupId: "my-app-users",
91
56
  },
92
- smartwallet: {
93
- enabled: true,
94
- factoryAddress: "0x...",
95
- defaultRequiredSignatures: 1,
96
- defaultRequiredGuardians: 2,
57
+
58
+ // Optional: Configure crypto identity auto-generation
59
+ crypto: {
60
+ autoGenerateOnAuth: true, // Generate crypto identities automatically after auth
97
61
  },
98
62
  });
63
+
64
+ // Register Smart Wallet plugin separately if needed
65
+ import { SmartWalletPlugin } from "shogun-core";
66
+
67
+ const smartWalletPlugin = new SmartWalletPlugin({
68
+ enabled: true,
69
+ factoryAddress: "0x...",
70
+ defaultRequiredSignatures: 1,
71
+ defaultRequiredGuardians: 2,
72
+ });
73
+
74
+ shogun.register(smartWalletPlugin);
99
75
  ```
100
76
 
101
- ## Simple API Methods
77
+ ## Basic Database Operations
102
78
 
103
79
  ```typescript
104
- const api = shogun.api;
105
- const db = api.database; // Access database directly
106
-
107
- // Authentication
108
- const user = await db.signUp('username', 'password');
109
- const loginResult = await db.login('username', 'password');
110
- db.logout();
111
- const isLoggedIn = db.isLoggedIn();
112
-
113
- // Basic data operations
114
- await db.put('path/to/data', { value: 'hello' });
115
- const data = await db.getData('path/to/data');
116
- await db.set('path/to/data', { value: 'updated' });
117
- await db.remove('path/to/data');
118
-
119
- // Profile management
120
- await api.updateProfile({
121
- name: 'John Doe',
122
- email: 'john@example.com',
123
- bio: 'Developer'
80
+ const db = shogun.db;
81
+
82
+ // Store data using Gun chaining
83
+ await db.get('users').get('alice').get('profile').put({
84
+ name: 'Alice Smith',
85
+ email: 'alice@example.com'
124
86
  });
125
- const profile = await api.getProfile();
126
87
 
127
- // Settings and preferences
128
- await api.saveSettings({ language: 'en', notifications: true });
129
- const settings = await api.getSettings();
88
+ // Read data
89
+ const profile = await db.get('users').get('alice').get('profile').once().then();
130
90
 
131
- // Collections
132
- await api.createCollection('todos', {
133
- '1': { text: 'Learn Shogun Core', done: false },
134
- '2': { text: 'Build dApp', done: false }
135
- });
91
+ // Update specific fields
92
+ await db.get('users').get('alice').get('profile').get('name').put('Alice Johnson');
136
93
 
137
- await api.addToCollection('todos', '3', {
138
- text: 'Deploy to production',
139
- done: false
94
+ // Iterate over collections with .map()
95
+ db.get('users').map((user, userId) => {
96
+ console.log(`User ${userId}:`, user);
140
97
  });
141
-
142
- const todos = await api.getCollection('todos');
143
- await api.removeFromCollection('todos', '2');
144
98
  ```
145
99
 
146
100
  ## Authentication Methods
@@ -264,13 +218,26 @@ if (zkPlugin && zkPlugin.isAvailable()) {
264
218
  ### 6. Smart Wallet Plugin API
265
219
 
266
220
  ```typescript
267
- const smartWalletPlugin = shogun.getPlugin("smartwallet");
221
+ import { SmartWalletPlugin } from "shogun-core";
222
+
223
+ // Register Smart Wallet plugin
224
+ const smartWalletPlugin = new SmartWalletPlugin({
225
+ enabled: true,
226
+ factoryAddress: "0x...", // Smart Wallet Factory contract address
227
+ defaultRequiredSignatures: 1,
228
+ defaultRequiredGuardians: 2,
229
+ });
230
+
231
+ shogun.register(smartWalletPlugin);
268
232
 
269
233
  // Configure signer (derive EOA from WebAuthn seed phrase)
234
+ const webauthnPlugin = shogun.getPlugin("webauthn");
270
235
  const signUpResult = await webauthnPlugin.signUp("alice", {
271
236
  generateSeedPhrase: true
272
237
  });
273
238
 
239
+ import { derive } from "shogun-core";
240
+
274
241
  const wallet = await derive(signUpResult.seedPhrase!, "alice", {
275
242
  includeSecp256k1Ethereum: true
276
243
  });
@@ -305,54 +272,58 @@ When a user signs up, Shogun Core automatically generates comprehensive crypto i
305
272
  - **MLS Groups**: For group messaging and collaboration
306
273
  - **SFrame Keys**: For media encryption and streaming
307
274
 
308
- ### Accessing Crypto Identities
275
+ ### Generating Crypto Identities
309
276
 
310
277
  ```typescript
311
278
  import { CryptoIdentityManager } from "shogun-core";
279
+ import type { ISEAPair } from "gun/types";
312
280
 
313
281
  // Create CryptoIdentityManager instance
314
- const cryptoManager = new CryptoIdentityManager(shogun);
315
-
316
- // Get current user's crypto identities
317
- const identities = await cryptoManager.getCurrentUserIdentities();
318
-
319
- if (identities.success && identities.identities) {
320
- console.log("RSA Key Pair:", !!identities.identities.rsa);
321
- console.log("AES Key:", !!identities.identities.aes);
322
- console.log("Signal Identity:", !!identities.identities.signal);
323
- console.log("PGP Keys:", !!identities.identities.pgp);
324
- console.log("MLS Group:", !!identities.identities.mls);
325
- console.log("SFrame Key:", !!identities.identities.sframe);
282
+ const cryptoManager = new CryptoIdentityManager();
283
+
284
+ // Generate crypto identities for a user
285
+ const seaPair: ISEAPair = /* your SEA pair */;
286
+ const result = await cryptoManager.generateAllIdentities("username", seaPair);
287
+
288
+ if (result.success && result.identities) {
289
+ console.log("RSA Key Pair:", !!result.identities.rsa);
290
+ console.log("AES Key:", !!result.identities.aes);
291
+ console.log("Signal Identity:", !!result.identities.signal);
292
+ console.log("PGP Keys:", !!result.identities.pgp);
293
+ console.log("MLS Group:", !!result.identities.mls);
294
+ console.log("SFrame Key:", !!result.identities.sframe);
326
295
 
327
296
  // Access specific identity data
328
- if (identities.identities.rsa) {
329
- console.log("RSA Public Key:", identities.identities.rsa.publicKey);
330
- console.log("RSA Private Key:", identities.identities.rsa.privateKey);
297
+ if (result.identities.rsa) {
298
+ console.log("RSA Public Key:", result.identities.rsa.publicKey);
299
+ console.log("RSA Private Key:", result.identities.rsa.privateKey);
331
300
  }
332
301
 
333
- if (identities.identities.aes) {
334
- console.log("AES Key:", identities.identities.aes);
302
+ if (result.identities.aes) {
303
+ console.log("AES Key:", result.identities.aes);
335
304
  }
336
305
 
337
- if (identities.identities.signal) {
338
- console.log("Signal Identity:", identities.identities.signal);
306
+ if (result.identities.signal) {
307
+ console.log("Signal Identity:", result.identities.signal);
339
308
  }
340
309
 
341
- if (identities.identities.pgp) {
342
- console.log("PGP Public Key:", identities.identities.pgp.publicKey);
343
- console.log("PGP Private Key:", identities.identities.pgp.privateKey);
310
+ if (result.identities.pgp) {
311
+ console.log("PGP Public Key:", result.identities.pgp.publicKey);
312
+ console.log("PGP Private Key:", result.identities.pgp.privateKey);
344
313
  }
345
314
 
346
- if (identities.identities.mls) {
347
- console.log("MLS Group ID:", identities.identities.mls.groupId);
348
- console.log("MLS Member ID:", identities.identities.mls.memberId);
315
+ if (result.identities.mls) {
316
+ console.log("MLS Group ID:", result.identities.mls.groupId);
317
+ console.log("MLS Member ID:", result.identities.mls.memberId);
349
318
  }
350
319
 
351
- if (identities.identities.sframe) {
352
- console.log("SFrame Key ID:", identities.identities.sframe.keyId);
320
+ if (result.identities.sframe) {
321
+ console.log("SFrame Key ID:", result.identities.sframe.keyId);
353
322
  }
323
+
324
+ // Note: Save identities to GunDB from the frontend if needed
354
325
  } else {
355
- console.error("Failed to retrieve identities:", identities.error);
326
+ console.error("Failed to generate identities:", result.error);
356
327
  }
357
328
  ```
358
329
 
@@ -368,23 +339,28 @@ const gun = Gun({
368
339
  });
369
340
 
370
341
  const shogun = new ShogunCore({
371
- gunInstance: gun,
372
- scope: "my-app"
342
+ gunInstance: gun
373
343
  });
374
344
 
375
- // Register user (crypto identities generated automatically)
345
+ // Register user
376
346
  const signupResult = await shogun.signUp("alice", "password123");
377
347
 
378
- if (signupResult.success) {
348
+ if (signupResult.success && signupResult.sea) {
379
349
  console.log("User registered:", signupResult.username);
380
350
 
381
- // Access crypto identities
382
- const cryptoManager = new CryptoIdentityManager(shogun);
383
- const identities = await cryptoManager.getCurrentUserIdentities();
351
+ // Generate crypto identities
352
+ const cryptoManager = new CryptoIdentityManager();
353
+ const result = await cryptoManager.generateAllIdentities(
354
+ signupResult.username,
355
+ signupResult.sea
356
+ );
384
357
 
385
- if (identities.success) {
358
+ if (result.success && result.identities) {
386
359
  console.log("✅ All crypto identities generated successfully");
387
- console.log("Identities available:", Object.keys(identities.identities || {}));
360
+ console.log("Identities available:", Object.keys(result.identities));
361
+
362
+ // Save identities to GunDB from the frontend if needed
363
+ // The manager only generates identities, saving is handled by the frontend
388
364
  }
389
365
  }
390
366
  ```
@@ -414,51 +390,19 @@ interface CryptoIdentities {
414
390
  ### Error Handling
415
391
 
416
392
  ```typescript
417
- const identities = await cryptoManager.getCurrentUserIdentities();
393
+ const result = await cryptoManager.generateAllIdentities("username", seaPair);
418
394
 
419
- if (!identities.success) {
420
- switch (identities.error) {
421
- case "No authenticated user found":
422
- console.log("Please log in first");
423
- break;
424
- case "Database not available":
425
- console.log("Database connection issue");
426
- break;
427
- case "No SEA pair found for current user":
428
- console.log("User authentication issue");
395
+ if (!result.success) {
396
+ switch (result.error) {
397
+ case "Failed to generate crypto identities":
398
+ console.log("Identity generation failed");
429
399
  break;
430
400
  default:
431
- console.error("Unknown error:", identities.error);
401
+ console.error("Unknown error:", result.error);
432
402
  }
433
403
  }
434
404
  ```
435
405
 
436
- ## Advanced Gun Operations
437
-
438
- ```typescript
439
- const db = api.database;
440
-
441
- // Store nested data with Gun chaining
442
- await db.get('users').get('alice').get('profile').put({
443
- name: 'Alice Smith',
444
- email: 'alice@example.com',
445
- preferences: {
446
- theme: 'dark',
447
- language: 'en'
448
- }
449
- });
450
-
451
- // Read nested data
452
- const profile = await db.get('users').get('alice').get('profile').once().then();
453
-
454
- // Update specific fields
455
- await db.get('users').get('alice').get('profile').get('preferences').get('theme').put('light');
456
-
457
- // Iterate over collections with .map()
458
- db.get('users').map((user, userId) => {
459
- console.log(`User ${userId}:`, user);
460
- });
461
- ```
462
406
 
463
407
  ## Browser Usage (CDN)
464
408
 
@@ -475,9 +419,14 @@ db.get('users').map((user, userId) => {
475
419
  <script src="https://cdn.jsdelivr.net/npm/shogun-core/dist/browser/shogun-core.js"></script>
476
420
 
477
421
  <script>
478
- const shogunCore = window.SHOGUN_CORE({
479
- peers: ["https://gun-manhattan.herokuapp.com/gun"],
480
- scope: "my-browser-app",
422
+ // Create Gun instance first
423
+ const gun = Gun({
424
+ peers: ["https://gun-manhattan.herokuapp.com/gun"]
425
+ });
426
+
427
+ // Initialize Shogun Core
428
+ const shogunCore = new window.SHOGUN_CORE({
429
+ gunInstance: gun,
481
430
  web3: { enabled: true },
482
431
  webauthn: {
483
432
  enabled: true,
@@ -519,11 +468,8 @@ shogun.on("error", (error) => {
519
468
 
520
469
  ```typescript
521
470
  interface ShogunCoreConfig {
522
- peers?: string[]; // GunDB peer URLs
523
- scope?: string; // Application scope
524
- authToken?: string; // GunDB super peer secret
525
- appToken?: string; // Application token
526
-
471
+ gunInstance: IGunInstance<any>; // Required: existing Gun instance
472
+
527
473
  // Plugin configurations
528
474
  webauthn?: {
529
475
  enabled?: boolean;
@@ -543,13 +489,20 @@ interface ShogunCoreConfig {
543
489
  enabled?: boolean;
544
490
  defaultGroupId?: string;
545
491
  deterministic?: boolean;
492
+ minEntropy?: number;
493
+ };
494
+
495
+ // Crypto identity configuration
496
+ crypto?: {
497
+ /**
498
+ * When true (default), generate and save crypto identities automatically after auth.
499
+ * Set to false to skip automatic crypto identity generation.
500
+ */
501
+ autoGenerateOnAuth?: boolean;
546
502
  };
547
503
 
548
- smartwallet?: {
504
+ postAuth?: {
549
505
  enabled?: boolean;
550
- factoryAddress?: string;
551
- defaultRequiredSignatures?: number;
552
- defaultRequiredGuardians?: number;
553
506
  };
554
507
 
555
508
  // Timeouts
@@ -558,9 +511,18 @@ interface ShogunCoreConfig {
558
511
  signup?: number;
559
512
  operation?: number;
560
513
  };
514
+
515
+ plugins?: {
516
+ autoRegister?: ShogunPlugin[];
517
+ };
518
+
519
+ disableAutoRecall?: boolean; // Disable automatic session recall on init
520
+ silent?: boolean; // Disable console logs
561
521
  }
562
522
  ```
563
523
 
524
+ **Note**: `SmartWalletPlugin` must be registered separately using `shogun.register()` as it's not included in the main configuration.
525
+
564
526
  ## Testing
565
527
 
566
528
  ```bash