shogun-core 1.6.11 → 1.6.12
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 +279 -71
- package/dist/browser/shogun-core.js +103228 -2
- package/dist/browser/shogun-core.js.map +1 -1
- package/dist/gundb/gun-Instance.js +113 -70
- package/dist/index.js +41 -49
- package/dist/plugins/base.js +2 -25
- package/dist/plugins/nostr/nostrConnector.js +5 -3
- package/dist/plugins/nostr/nostrConnectorPlugin.js +2 -31
- package/dist/plugins/nostr/nostrSigner.js +0 -4
- package/dist/plugins/oauth/oauthConnector.js +11 -6
- package/dist/plugins/oauth/oauthPlugin.js +6 -22
- package/dist/plugins/web3/web3Connector.js +0 -21
- package/dist/plugins/web3/web3ConnectorPlugin.js +4 -21
- package/dist/plugins/web3/web3Signer.js +0 -5
- package/dist/plugins/webauthn/webauthn.js +0 -2
- package/dist/plugins/webauthn/webauthnPlugin.js +0 -13
- package/dist/plugins/webauthn/webauthnSigner.js +0 -3
- package/dist/types/index.d.ts +7 -6
- package/dist/types/plugins/base.d.ts +1 -3
- package/dist/types/plugins/oauth/oauthConnector.d.ts +2 -2
- package/dist/types/plugins/oauth/oauthPlugin.d.ts +1 -1
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +1 -3
- package/dist/types/types/events.d.ts +25 -9
- package/dist/types/types/plugin.d.ts +2 -2
- package/dist/types/types/shogun.d.ts +13 -2
- package/dist/types/utils/errorHandler.d.ts +6 -0
- package/dist/utils/errorHandler.js +17 -0
- package/package.json +1 -1
- package/dist/browser/shogun-core.js.LICENSE.txt +0 -22
package/README.md
CHANGED
|
@@ -16,6 +16,8 @@ Shogun Core is a comprehensive SDK for building decentralized applications (dApp
|
|
|
16
16
|
- 📱 **Reactive Programming**: RxJS integration for real-time data streams
|
|
17
17
|
- 🛡️ **Security**: End-to-end encryption and secure key management
|
|
18
18
|
- 🎯 **TypeScript**: Full TypeScript support with comprehensive type definitions
|
|
19
|
+
- 📡 **Event System**: Typed event system for monitoring authentication and data changes
|
|
20
|
+
- 🔑 **Cryptographic Wallets**: Automatic derivation of Bitcoin and Ethereum wallets from user keys
|
|
19
21
|
|
|
20
22
|
## Installation
|
|
21
23
|
|
|
@@ -45,12 +47,6 @@ const shogun = new ShogunCore({
|
|
|
45
47
|
scope: "my-awesome-app",
|
|
46
48
|
authToken: "YOUR_GUN_SUPER_PEER_SECRET", // Optional, for private peers
|
|
47
49
|
|
|
48
|
-
// Enable and configure logging
|
|
49
|
-
logging: {
|
|
50
|
-
enabled: true,
|
|
51
|
-
level: "info", // "none", "error", "warn", "info", "debug", "verbose"
|
|
52
|
-
},
|
|
53
|
-
|
|
54
50
|
// Enable and configure Web3 (e.g., MetaMask) authentication
|
|
55
51
|
web3: {
|
|
56
52
|
enabled: true,
|
|
@@ -89,9 +85,57 @@ await shogun.initialize();
|
|
|
89
85
|
console.log("Shogun Core initialized!");
|
|
90
86
|
```
|
|
91
87
|
|
|
92
|
-
|
|
88
|
+
## Plugin Authentication APIs
|
|
93
89
|
|
|
94
|
-
|
|
90
|
+
Shogun Core provides a unified plugin system for different authentication methods. Each plugin implements standardized `login()` and `signUp()` methods that return consistent `AuthResult` objects.
|
|
91
|
+
|
|
92
|
+
### Core Types
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
// Authentication result interface
|
|
96
|
+
interface AuthResult {
|
|
97
|
+
success: boolean;
|
|
98
|
+
error?: string;
|
|
99
|
+
userPub?: string; // User's public key
|
|
100
|
+
username?: string; // Username or identifier
|
|
101
|
+
sessionToken?: string; // Session token if applicable
|
|
102
|
+
authMethod?: AuthMethod; // Authentication method used
|
|
103
|
+
sea?: { // GunDB SEA pair for session persistence
|
|
104
|
+
pub: string;
|
|
105
|
+
priv: string;
|
|
106
|
+
epub: string;
|
|
107
|
+
epriv: string;
|
|
108
|
+
};
|
|
109
|
+
// OAuth-specific properties
|
|
110
|
+
redirectUrl?: string; // OAuth redirect URL
|
|
111
|
+
pendingAuth?: boolean; // Indicates pending OAuth flow
|
|
112
|
+
message?: string; // Status message
|
|
113
|
+
provider?: string; // OAuth provider name
|
|
114
|
+
isNewUser?: boolean; // True if this was a registration
|
|
115
|
+
user?: { // OAuth user data
|
|
116
|
+
userPub?: string;
|
|
117
|
+
username?: string;
|
|
118
|
+
email?: string;
|
|
119
|
+
name?: string;
|
|
120
|
+
picture?: string;
|
|
121
|
+
oauth?: {
|
|
122
|
+
provider: string;
|
|
123
|
+
id: string;
|
|
124
|
+
email?: string;
|
|
125
|
+
name?: string;
|
|
126
|
+
picture?: string;
|
|
127
|
+
lastLogin: number;
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Supported authentication methods
|
|
133
|
+
type AuthMethod = "password" | "webauthn" | "web3" | "nostr" | "oauth" | "bitcoin" | "pair";
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### 1. Traditional Authentication
|
|
137
|
+
|
|
138
|
+
Direct username/password authentication using ShogunCore methods:
|
|
95
139
|
|
|
96
140
|
```typescript
|
|
97
141
|
// Sign up a new user
|
|
@@ -107,43 +151,187 @@ if (loginResult.success) {
|
|
|
107
151
|
}
|
|
108
152
|
```
|
|
109
153
|
|
|
110
|
-
|
|
154
|
+
### 2. Web3 Plugin API
|
|
155
|
+
|
|
156
|
+
Ethereum wallet authentication via MetaMask or other Web3 providers:
|
|
111
157
|
|
|
112
158
|
```typescript
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (web3Plugin) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
159
|
+
const web3Plugin = shogun.getPlugin<Web3ConnectorPlugin>("web3");
|
|
160
|
+
|
|
161
|
+
if (web3Plugin && web3Plugin.isAvailable()) {
|
|
162
|
+
// Connect to MetaMask
|
|
163
|
+
const connectionResult = await web3Plugin.connectMetaMask();
|
|
164
|
+
|
|
165
|
+
if (connectionResult.success) {
|
|
166
|
+
const address = connectionResult.address!;
|
|
167
|
+
|
|
168
|
+
// Login with Web3 wallet
|
|
169
|
+
const loginResult = await web3Plugin.login(address);
|
|
170
|
+
if (loginResult.success) {
|
|
171
|
+
console.log("Web3 login successful");
|
|
172
|
+
console.log("User public key:", loginResult.userPub);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Register new user with Web3 wallet
|
|
176
|
+
const signUpResult = await web3Plugin.signUp(address);
|
|
177
|
+
if (signUpResult.success) {
|
|
178
|
+
console.log("Web3 registration successful");
|
|
179
|
+
}
|
|
124
180
|
}
|
|
125
181
|
}
|
|
182
|
+
|
|
183
|
+
// Plugin Interface
|
|
184
|
+
interface Web3ConnectorPluginInterface {
|
|
185
|
+
// Authentication methods
|
|
186
|
+
login(address: string): Promise<AuthResult>;
|
|
187
|
+
signUp(address: string): Promise<AuthResult>;
|
|
188
|
+
|
|
189
|
+
// Connection methods
|
|
190
|
+
isAvailable(): boolean;
|
|
191
|
+
connectMetaMask(): Promise<ConnectionResult>;
|
|
192
|
+
getProvider(): Promise<ethers.JsonRpcProvider | ethers.BrowserProvider>;
|
|
193
|
+
getSigner(): Promise<ethers.Signer>;
|
|
194
|
+
|
|
195
|
+
// Credential management
|
|
196
|
+
generateCredentials(address: string): Promise<ISEAPair>;
|
|
197
|
+
generatePassword(signature: string): Promise<string>;
|
|
198
|
+
verifySignature(message: string, signature: string): Promise<string>;
|
|
199
|
+
}
|
|
126
200
|
```
|
|
127
201
|
|
|
128
|
-
|
|
202
|
+
### 3. WebAuthn Plugin API
|
|
203
|
+
|
|
204
|
+
Biometric and hardware key authentication:
|
|
129
205
|
|
|
130
206
|
```typescript
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (webauthnPlugin) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
207
|
+
const webauthnPlugin = shogun.getPlugin<WebauthnPlugin>("webauthn");
|
|
208
|
+
|
|
209
|
+
if (webauthnPlugin && webauthnPlugin.isSupported()) {
|
|
210
|
+
// Register new user with WebAuthn
|
|
211
|
+
const signUpResult = await webauthnPlugin.signUp("username");
|
|
212
|
+
if (signUpResult.success) {
|
|
213
|
+
console.log("WebAuthn registration successful");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Authenticate existing user
|
|
217
|
+
const loginResult = await webauthnPlugin.login("username");
|
|
218
|
+
if (loginResult.success) {
|
|
219
|
+
console.log("WebAuthn authentication successful");
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Plugin Interface
|
|
224
|
+
interface WebauthnPluginInterface {
|
|
225
|
+
// Authentication methods
|
|
226
|
+
login(username: string): Promise<AuthResult>;
|
|
227
|
+
signUp(username: string): Promise<AuthResult>;
|
|
228
|
+
|
|
229
|
+
// Capability checks
|
|
230
|
+
isSupported(): boolean;
|
|
231
|
+
|
|
232
|
+
// WebAuthn-specific methods
|
|
233
|
+
register(username: string, displayName?: string): Promise<WebAuthnCredential>;
|
|
234
|
+
authenticate(username?: string): Promise<WebAuthnCredential>;
|
|
235
|
+
generateCredentials(username: string, pair?: ISEAPair | null, login?: boolean): Promise<WebAuthnUniformCredentials>;
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### 4. Nostr Plugin API
|
|
240
|
+
|
|
241
|
+
Bitcoin wallet and Nostr protocol authentication:
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
const nostrPlugin = shogun.getPlugin<NostrConnectorPlugin>("nostr");
|
|
245
|
+
|
|
246
|
+
if (nostrPlugin && nostrPlugin.isAvailable()) {
|
|
247
|
+
// Connect to Nostr wallet (Bitcoin extension)
|
|
248
|
+
const connectionResult = await nostrPlugin.connectNostrWallet();
|
|
249
|
+
|
|
250
|
+
if (connectionResult.success) {
|
|
251
|
+
const address = connectionResult.address!;
|
|
252
|
+
|
|
253
|
+
// Login with Nostr/Bitcoin wallet
|
|
254
|
+
const loginResult = await nostrPlugin.login(address);
|
|
255
|
+
if (loginResult.success) {
|
|
256
|
+
console.log("Nostr login successful");
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Register with Nostr/Bitcoin wallet
|
|
260
|
+
const signUpResult = await nostrPlugin.signUp(address);
|
|
261
|
+
if (signUpResult.success) {
|
|
262
|
+
console.log("Nostr registration successful");
|
|
142
263
|
}
|
|
143
|
-
} catch (error) {
|
|
144
|
-
console.error("WebAuthn authentication failed:", error);
|
|
145
264
|
}
|
|
146
265
|
}
|
|
266
|
+
|
|
267
|
+
// Plugin Interface
|
|
268
|
+
interface NostrConnectorPluginInterface {
|
|
269
|
+
// Authentication methods
|
|
270
|
+
login(address: string): Promise<AuthResult>;
|
|
271
|
+
signUp(address: string): Promise<AuthResult>;
|
|
272
|
+
|
|
273
|
+
// Connection methods
|
|
274
|
+
isAvailable(): boolean;
|
|
275
|
+
connectBitcoinWallet(type?: "alby" | "nostr" | "manual"): Promise<ConnectionResult>;
|
|
276
|
+
connectNostrWallet(): Promise<ConnectionResult>;
|
|
277
|
+
|
|
278
|
+
// Credential and signature management
|
|
279
|
+
generateCredentials(address: string, signature: string, message: string): Promise<NostrConnectorCredentials>;
|
|
280
|
+
verifySignature(message: string, signature: string, address: string): Promise<boolean>;
|
|
281
|
+
generatePassword(signature: string): Promise<string>;
|
|
282
|
+
}
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### 5. OAuth Plugin API
|
|
286
|
+
|
|
287
|
+
Social login with external providers (Google, GitHub, etc.):
|
|
288
|
+
|
|
289
|
+
```typescript
|
|
290
|
+
const oauthPlugin = shogun.getPlugin<OAuthPlugin>("oauth");
|
|
291
|
+
|
|
292
|
+
if (oauthPlugin && oauthPlugin.isSupported()) {
|
|
293
|
+
// Get available providers
|
|
294
|
+
const providers = oauthPlugin.getAvailableProviders(); // ["google", "github", ...]
|
|
295
|
+
|
|
296
|
+
// Initiate login with OAuth (returns redirect URL)
|
|
297
|
+
const loginResult = await oauthPlugin.login("google");
|
|
298
|
+
if (loginResult.success && loginResult.redirectUrl) {
|
|
299
|
+
// Redirect user to OAuth provider
|
|
300
|
+
window.location.href = loginResult.redirectUrl;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Handle OAuth callback (after redirect back from provider)
|
|
304
|
+
const callbackResult = await oauthPlugin.handleOAuthCallback(
|
|
305
|
+
"google",
|
|
306
|
+
authCode, // From URL params
|
|
307
|
+
state // From URL params
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
if (callbackResult.success) {
|
|
311
|
+
console.log("OAuth authentication successful");
|
|
312
|
+
if (callbackResult.user) {
|
|
313
|
+
console.log("User email:", callbackResult.user.email);
|
|
314
|
+
console.log("User name:", callbackResult.user.name);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Plugin Interface
|
|
320
|
+
interface OAuthPluginInterface {
|
|
321
|
+
// Authentication methods
|
|
322
|
+
login(provider: OAuthProvider): Promise<AuthResult>;
|
|
323
|
+
signUp(provider: OAuthProvider): Promise<AuthResult>;
|
|
324
|
+
|
|
325
|
+
// OAuth flow management
|
|
326
|
+
isSupported(): boolean;
|
|
327
|
+
getAvailableProviders(): OAuthProvider[];
|
|
328
|
+
initiateOAuth(provider: OAuthProvider): Promise<OAuthConnectionResult>;
|
|
329
|
+
completeOAuth(provider: OAuthProvider, authCode: string, state?: string): Promise<OAuthConnectionResult>;
|
|
330
|
+
handleOAuthCallback(provider: OAuthProvider, authCode: string, state: string): Promise<AuthResult>;
|
|
331
|
+
|
|
332
|
+
// Credential management
|
|
333
|
+
generateCredentials(userInfo: OAuthUserInfo, provider: OAuthProvider): Promise<OAuthCredentials>;
|
|
334
|
+
}
|
|
147
335
|
```
|
|
148
336
|
|
|
149
337
|
### Browser Usage (via CDN)
|
|
@@ -219,39 +407,9 @@ You can also use Shogun Core directly in the browser by including it from a CDN.
|
|
|
219
407
|
|
|
220
408
|
#### Event Handling
|
|
221
409
|
|
|
222
|
-
- `on(eventName:
|
|
223
|
-
- `off(eventName:
|
|
224
|
-
- `emit(eventName:
|
|
225
|
-
|
|
226
|
-
### Built-in Plugins
|
|
227
|
-
|
|
228
|
-
#### Web3 Plugin
|
|
229
|
-
|
|
230
|
-
```typescript
|
|
231
|
-
const web3Plugin = shogun.getPlugin("web3");
|
|
232
|
-
// Methods: getProvider(), login(address), logout(), isConnected()
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
#### WebAuthn Plugin
|
|
236
|
-
|
|
237
|
-
```typescript
|
|
238
|
-
const webauthnPlugin = shogun.getPlugin("webauthn");
|
|
239
|
-
// Methods: register(username), authenticate(), isSupported()
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
#### Nostr Plugin
|
|
243
|
-
|
|
244
|
-
```typescript
|
|
245
|
-
const nostrPlugin = shogun.getPlugin("nostr");
|
|
246
|
-
// Methods: connect(), login(), logout()
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
#### OAuth Plugin
|
|
250
|
-
|
|
251
|
-
```typescript
|
|
252
|
-
const oauthPlugin = shogun.getPlugin("oauth");
|
|
253
|
-
// Methods: login(provider), handleCallback(code), logout()
|
|
254
|
-
```
|
|
410
|
+
- `on<K extends keyof ShogunEventMap>(eventName: K, listener: Function): this` - Subscribe to typed events
|
|
411
|
+
- `off<K extends keyof ShogunEventMap>(eventName: K, listener: Function): this` - Unsubscribe from events
|
|
412
|
+
- `emit<K extends keyof ShogunEventMap>(eventName: K, data?: ShogunEventMap[K]): boolean` - Emit custom events
|
|
255
413
|
|
|
256
414
|
### Configuration Options
|
|
257
415
|
|
|
@@ -294,12 +452,36 @@ interface ShogunSDKConfig {
|
|
|
294
452
|
|
|
295
453
|
## Event System
|
|
296
454
|
|
|
297
|
-
Shogun Core provides a comprehensive event system for monitoring authentication and data changes:
|
|
455
|
+
Shogun Core provides a comprehensive typed event system for monitoring authentication and data changes:
|
|
298
456
|
|
|
299
457
|
```typescript
|
|
300
|
-
//
|
|
458
|
+
// Available events with their data types
|
|
459
|
+
interface ShogunEventMap {
|
|
460
|
+
"auth:login": AuthEventData; // User logged in
|
|
461
|
+
"auth:logout": void; // User logged out
|
|
462
|
+
"auth:signup": AuthEventData; // New user registered
|
|
463
|
+
"wallet:created": WalletEventData; // Wallet derived from user keys
|
|
464
|
+
"gun:put": GunDataEventData; // Data written to GunDB
|
|
465
|
+
"gun:get": GunDataEventData; // Data read from GunDB
|
|
466
|
+
"gun:set": GunDataEventData; // Data updated in GunDB
|
|
467
|
+
"gun:remove": GunDataEventData; // Data removed from GunDB
|
|
468
|
+
"gun:peer:add": GunPeerEventData; // Peer added
|
|
469
|
+
"gun:peer:remove": GunPeerEventData; // Peer removed
|
|
470
|
+
"gun:peer:connect": GunPeerEventData; // Peer connected
|
|
471
|
+
"gun:peer:disconnect": GunPeerEventData; // Peer disconnected
|
|
472
|
+
"plugin:registered": { name: string; version?: string; category?: string }; // Plugin registered
|
|
473
|
+
"plugin:unregistered": { name: string }; // Plugin unregistered
|
|
474
|
+
"debug": { action: string; [key: string]: any }; // Debug information
|
|
475
|
+
"error": ErrorEventData; // Error occurred
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// Listen for authentication events with full type safety
|
|
301
479
|
shogun.on("auth:login", (data) => {
|
|
302
480
|
console.log("User logged in:", data.username);
|
|
481
|
+
console.log("Authentication method:", data.method);
|
|
482
|
+
if (data.provider) {
|
|
483
|
+
console.log("OAuth provider:", data.provider);
|
|
484
|
+
}
|
|
303
485
|
});
|
|
304
486
|
|
|
305
487
|
shogun.on("auth:logout", () => {
|
|
@@ -310,12 +492,38 @@ shogun.on("auth:signup", (data) => {
|
|
|
310
492
|
console.log("New user signed up:", data.username);
|
|
311
493
|
});
|
|
312
494
|
|
|
495
|
+
// Listen for wallet creation (Bitcoin and Ethereum wallets derived from user keys)
|
|
496
|
+
shogun.on("wallet:created", (data) => {
|
|
497
|
+
console.log("Wallet created:", data.address);
|
|
498
|
+
});
|
|
499
|
+
|
|
313
500
|
// Listen for errors
|
|
314
501
|
shogun.on("error", (error) => {
|
|
315
502
|
console.error("Shogun error:", error.message);
|
|
316
503
|
});
|
|
317
504
|
```
|
|
318
505
|
|
|
506
|
+
## Cryptographic Wallets
|
|
507
|
+
|
|
508
|
+
Shogun Core automatically derives Bitcoin and Ethereum wallets from user authentication keys:
|
|
509
|
+
|
|
510
|
+
```typescript
|
|
511
|
+
// After successful authentication, wallets are available
|
|
512
|
+
if (shogun.wallets) {
|
|
513
|
+
console.log("Bitcoin wallet:", {
|
|
514
|
+
address: shogun.wallets.secp256k1Bitcoin.address,
|
|
515
|
+
publicKey: shogun.wallets.secp256k1Bitcoin.publicKey,
|
|
516
|
+
// privateKey is available but should be handled securely
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
console.log("Ethereum wallet:", {
|
|
520
|
+
address: shogun.wallets.secp256k1Ethereum.address,
|
|
521
|
+
publicKey: shogun.wallets.secp256k1Ethereum.publicKey,
|
|
522
|
+
// privateKey is available but should be handled securely
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
```
|
|
526
|
+
|
|
319
527
|
## Error Handling
|
|
320
528
|
|
|
321
529
|
Shogun Core includes comprehensive error handling with typed errors:
|
|
@@ -328,10 +536,10 @@ try {
|
|
|
328
536
|
} catch (error) {
|
|
329
537
|
if (error instanceof ShogunError) {
|
|
330
538
|
switch (error.type) {
|
|
331
|
-
case ErrorType.
|
|
539
|
+
case ErrorType.AUTHENTICATION:
|
|
332
540
|
console.error("Invalid credentials");
|
|
333
541
|
break;
|
|
334
|
-
case ErrorType.
|
|
542
|
+
case ErrorType.NETWORK:
|
|
335
543
|
console.error("Network connection failed");
|
|
336
544
|
break;
|
|
337
545
|
default:
|