shogun-core 1.5.19 → 1.5.20
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 +261 -52
- package/dist/browser/shogun-core.js +1 -1
- package/dist/browser/shogun-core.js.map +1 -1
- package/dist/gundb/gun-Instance.js +159 -7
- package/dist/types/gundb/gun-Instance.d.ts +38 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
# Shogun Core 📦
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/shogun-core)
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
|
-
[](https://www.typescriptlang.org/)
|
|
6
6
|
|
|
7
7
|
## Overview
|
|
8
8
|
|
|
9
9
|
Shogun Core is a comprehensive SDK for building decentralized applications (dApps) that simplifies authentication, wallet management, and decentralized data storage. It combines GunDB's peer-to-peer networking with modern authentication standards and blockchain integration to provide a secure, user-friendly foundation for Web3 applications.
|
|
10
10
|
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 🔐 **Multiple Authentication Methods**: Traditional username/password, WebAuthn (biometrics), Web3 (MetaMask), Nostr, and OAuth
|
|
14
|
+
- 🌐 **Decentralized Storage**: Built on GunDB for peer-to-peer data synchronization
|
|
15
|
+
- 🔌 **Plugin System**: Extensible architecture with built-in plugins for various authentication methods
|
|
16
|
+
- 📱 **Reactive Programming**: RxJS integration for real-time data streams
|
|
17
|
+
- 🛡️ **Security**: End-to-end encryption and secure key management
|
|
18
|
+
- 🎯 **TypeScript**: Full TypeScript support with comprehensive type definitions
|
|
19
|
+
|
|
11
20
|
## Installation
|
|
12
21
|
|
|
13
22
|
```bash
|
|
@@ -18,7 +27,7 @@ yarn add shogun-core
|
|
|
18
27
|
|
|
19
28
|
## Quick Start
|
|
20
29
|
|
|
21
|
-
|
|
30
|
+
### Basic Setup
|
|
22
31
|
|
|
23
32
|
```typescript
|
|
24
33
|
import { ShogunCore } from "shogun-core";
|
|
@@ -43,19 +52,20 @@ const shogun = new ShogunCore({
|
|
|
43
52
|
},
|
|
44
53
|
|
|
45
54
|
// Enable and configure Web3 (e.g., MetaMask) authentication
|
|
46
|
-
web3: {
|
|
47
|
-
enabled: true
|
|
55
|
+
web3: {
|
|
56
|
+
enabled: true,
|
|
48
57
|
},
|
|
49
58
|
|
|
50
59
|
// Enable and configure WebAuthn (biometrics, security keys)
|
|
51
60
|
webauthn: {
|
|
52
61
|
enabled: true,
|
|
53
62
|
rpName: "My Awesome App", // Name of your application
|
|
63
|
+
rpId: window.location.hostname, // Relying party ID
|
|
54
64
|
},
|
|
55
65
|
|
|
56
66
|
// Enable and configure Nostr
|
|
57
|
-
nostr: {
|
|
58
|
-
enabled: true
|
|
67
|
+
nostr: {
|
|
68
|
+
enabled: true,
|
|
59
69
|
},
|
|
60
70
|
|
|
61
71
|
// Enable and configure OAuth providers
|
|
@@ -73,18 +83,67 @@ const shogun = new ShogunCore({
|
|
|
73
83
|
},
|
|
74
84
|
});
|
|
75
85
|
|
|
76
|
-
//
|
|
77
|
-
|
|
86
|
+
// Initialize the SDK
|
|
87
|
+
await shogun.initialize();
|
|
78
88
|
|
|
79
89
|
console.log("Shogun Core initialized!");
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Authentication Examples
|
|
93
|
+
|
|
94
|
+
#### Traditional Login
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// Sign up a new user
|
|
98
|
+
const signUpResult = await shogun.signUp("username", "password");
|
|
99
|
+
if (signUpResult.success) {
|
|
100
|
+
console.log("User created:", signUpResult.username);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Login with username and password
|
|
104
|
+
const loginResult = await shogun.login("username", "password");
|
|
105
|
+
if (loginResult.success) {
|
|
106
|
+
console.log("Logged in as:", loginResult.username);
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Web3 Authentication (MetaMask)
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
// Get the Web3 plugin
|
|
114
|
+
const web3Plugin = shogun.getPlugin("web3");
|
|
115
|
+
if (web3Plugin) {
|
|
116
|
+
try {
|
|
117
|
+
const provider = await web3Plugin.getProvider();
|
|
118
|
+
const signer = provider.getSigner();
|
|
119
|
+
const address = await signer.getAddress();
|
|
120
|
+
await web3Plugin.login(address);
|
|
121
|
+
console.log("Logged in with address:", address);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
console.error("Web3 login failed:", error);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### WebAuthn Authentication
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
// Get the WebAuthn plugin
|
|
132
|
+
const webauthnPlugin = shogun.getPlugin("webauthn");
|
|
133
|
+
if (webauthnPlugin) {
|
|
134
|
+
try {
|
|
135
|
+
// Register a new credential
|
|
136
|
+
await webauthnPlugin.register("username");
|
|
80
137
|
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
138
|
+
// Authenticate with existing credential
|
|
139
|
+
const result = await webauthnPlugin.authenticate();
|
|
140
|
+
if (result.success) {
|
|
141
|
+
console.log("WebAuthn authentication successful");
|
|
142
|
+
}
|
|
143
|
+
} catch (error) {
|
|
144
|
+
console.error("WebAuthn authentication failed:", error);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
88
147
|
```
|
|
89
148
|
|
|
90
149
|
### Browser Usage (via CDN)
|
|
@@ -94,55 +153,205 @@ You can also use Shogun Core directly in the browser by including it from a CDN.
|
|
|
94
153
|
```html
|
|
95
154
|
<!DOCTYPE html>
|
|
96
155
|
<html>
|
|
97
|
-
<head>
|
|
156
|
+
<head>
|
|
98
157
|
<title>Shogun Core in Browser</title>
|
|
99
|
-
</head>
|
|
100
|
-
<body>
|
|
158
|
+
</head>
|
|
159
|
+
<body>
|
|
101
160
|
<h1>My dApp</h1>
|
|
102
161
|
<!-- Required dependencies for Shogun Core -->
|
|
103
162
|
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
|
|
104
163
|
<script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>
|
|
105
|
-
|
|
164
|
+
|
|
106
165
|
<!-- Shogun Core library -->
|
|
107
166
|
<script src="https://cdn.jsdelivr.net/npm/shogun-core/dist/browser/shogun-core.js"></script>
|
|
108
|
-
|
|
167
|
+
|
|
109
168
|
<script>
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
169
|
+
// The script exposes a global `initShogun` function
|
|
170
|
+
const shogun = initShogun({
|
|
171
|
+
peers: ["https://gun-manhattan.herokuapp.com/gun"],
|
|
172
|
+
scope: "my-browser-app",
|
|
173
|
+
web3: { enabled: true },
|
|
174
|
+
webauthn: {
|
|
175
|
+
enabled: true,
|
|
176
|
+
rpName: "My Browser dApp",
|
|
177
|
+
rpId: window.location.hostname,
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
console.log("Shogun Core initialized in browser!", shogun);
|
|
182
|
+
|
|
183
|
+
async function connectWallet() {
|
|
184
|
+
if (shogun.hasPlugin("web3")) {
|
|
185
|
+
const web3Plugin = shogun.getPlugin("web3");
|
|
186
|
+
try {
|
|
187
|
+
const provider = await web3Plugin.getProvider();
|
|
188
|
+
const signer = provider.getSigner();
|
|
189
|
+
const address = await signer.getAddress();
|
|
190
|
+
await web3Plugin.login(address);
|
|
191
|
+
console.log("Logged in with address:", address);
|
|
192
|
+
} catch (error) {
|
|
193
|
+
console.error("Web3 login failed:", error);
|
|
194
|
+
}
|
|
137
195
|
}
|
|
196
|
+
}
|
|
138
197
|
</script>
|
|
139
|
-
</body>
|
|
198
|
+
</body>
|
|
140
199
|
</html>
|
|
141
200
|
```
|
|
142
201
|
|
|
143
|
-
##
|
|
202
|
+
## API Reference
|
|
203
|
+
|
|
204
|
+
### Core Methods
|
|
205
|
+
|
|
206
|
+
#### Authentication
|
|
207
|
+
|
|
208
|
+
- `login(username: string, password: string): Promise<AuthResult>` - Authenticate with username/password
|
|
209
|
+
- `signUp(username: string, password: string, passwordConfirmation?: string): Promise<SignUpResult>` - Create new user account
|
|
210
|
+
- `logout(): void` - Logout current user
|
|
211
|
+
- `isLoggedIn(): boolean` - Check if user is authenticated
|
|
212
|
+
|
|
213
|
+
#### Plugin Management
|
|
214
|
+
|
|
215
|
+
- `getPlugin<T>(name: string): T | undefined` - Get plugin by name
|
|
216
|
+
- `hasPlugin(name: string): boolean` - Check if plugin exists
|
|
217
|
+
- `register(plugin: ShogunPlugin): void` - Register custom plugin
|
|
218
|
+
- `unregister(pluginName: string): void` - Remove plugin
|
|
219
|
+
|
|
220
|
+
#### Event Handling
|
|
221
|
+
|
|
222
|
+
- `on(eventName: string, listener: Function): this` - Subscribe to events
|
|
223
|
+
- `off(eventName: string, listener: Function): this` - Unsubscribe from events
|
|
224
|
+
- `emit(eventName: string, data?: any): boolean` - Emit custom events
|
|
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
|
+
```
|
|
255
|
+
|
|
256
|
+
### Configuration Options
|
|
144
257
|
|
|
145
|
-
|
|
258
|
+
```typescript
|
|
259
|
+
interface ShogunSDKConfig {
|
|
260
|
+
peers?: string[]; // GunDB peer URLs
|
|
261
|
+
scope?: string; // Application scope
|
|
262
|
+
authToken?: string; // GunDB super peer secret
|
|
263
|
+
appToken?: string; // Application token
|
|
264
|
+
|
|
265
|
+
// Plugin configurations
|
|
266
|
+
webauthn?: {
|
|
267
|
+
enabled?: boolean;
|
|
268
|
+
rpName?: string;
|
|
269
|
+
rpId?: string;
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
web3?: {
|
|
273
|
+
enabled?: boolean;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
nostr?: {
|
|
277
|
+
enabled?: boolean;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
oauth?: {
|
|
281
|
+
enabled?: boolean;
|
|
282
|
+
usePKCE?: boolean;
|
|
283
|
+
providers?: Record<string, any>;
|
|
284
|
+
};
|
|
146
285
|
|
|
286
|
+
// Timeouts
|
|
287
|
+
timeouts?: {
|
|
288
|
+
login?: number;
|
|
289
|
+
signup?: number;
|
|
290
|
+
operation?: number;
|
|
291
|
+
};
|
|
292
|
+
}
|
|
147
293
|
```
|
|
148
|
-
|
|
294
|
+
|
|
295
|
+
## Event System
|
|
296
|
+
|
|
297
|
+
Shogun Core provides a comprehensive event system for monitoring authentication and data changes:
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
// Listen for authentication events
|
|
301
|
+
shogun.on("auth:login", (data) => {
|
|
302
|
+
console.log("User logged in:", data.username);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
shogun.on("auth:logout", () => {
|
|
306
|
+
console.log("User logged out");
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
shogun.on("auth:signup", (data) => {
|
|
310
|
+
console.log("New user signed up:", data.username);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
// Listen for errors
|
|
314
|
+
shogun.on("error", (error) => {
|
|
315
|
+
console.error("Shogun error:", error.message);
|
|
316
|
+
});
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
## Error Handling
|
|
320
|
+
|
|
321
|
+
Shogun Core includes comprehensive error handling with typed errors:
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import { ShogunError, ErrorType } from "shogun-core";
|
|
325
|
+
|
|
326
|
+
try {
|
|
327
|
+
await shogun.login("username", "password");
|
|
328
|
+
} catch (error) {
|
|
329
|
+
if (error instanceof ShogunError) {
|
|
330
|
+
switch (error.type) {
|
|
331
|
+
case ErrorType.AUTHENTICATION_FAILED:
|
|
332
|
+
console.error("Invalid credentials");
|
|
333
|
+
break;
|
|
334
|
+
case ErrorType.NETWORK_ERROR:
|
|
335
|
+
console.error("Network connection failed");
|
|
336
|
+
break;
|
|
337
|
+
default:
|
|
338
|
+
console.error("Unknown error:", error.message);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
## Contributing
|
|
345
|
+
|
|
346
|
+
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
347
|
+
|
|
348
|
+
## License
|
|
349
|
+
|
|
350
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
351
|
+
|
|
352
|
+
## Support
|
|
353
|
+
|
|
354
|
+
- 📖 [Documentation](https://docs.shogun.dev)
|
|
355
|
+
- 💬 [Discord Community](https://discord.gg/shogun)
|
|
356
|
+
- 🐛 [Issue Tracker](https://github.com/scobru/shogun-core/issues)
|
|
357
|
+
- 📧 [Email Support](mailto:support@shogun.dev)
|