shogun-core 3.0.4 → 3.0.6
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/dist/browser/shogun-core.js +91002 -0
- package/dist/browser/shogun-core.js.map +1 -0
- package/dist/config/simplified-config.js +230 -0
- package/dist/core.js +338 -0
- package/dist/gundb/crypto.js +268 -0
- package/dist/gundb/db.js +1829 -0
- package/dist/gundb/derive.js +229 -0
- package/dist/gundb/errors.js +66 -0
- package/dist/gundb/index.js +6 -0
- package/dist/gundb/rxjs.js +445 -0
- package/dist/gundb/simple-api.js +438 -0
- package/dist/gundb/types.js +4 -0
- package/dist/index.js +16 -0
- package/dist/interfaces/common.js +1 -0
- package/dist/interfaces/events.js +36 -0
- package/dist/interfaces/plugin.js +1 -0
- package/dist/interfaces/shogun.js +34 -0
- package/dist/managers/AuthManager.js +225 -0
- package/dist/managers/CoreInitializer.js +227 -0
- package/dist/managers/EventManager.js +67 -0
- package/dist/managers/PluginManager.js +296 -0
- package/dist/migration-test.js +91 -0
- package/dist/plugins/base.js +47 -0
- package/dist/plugins/index.js +15 -0
- package/dist/plugins/nostr/index.js +4 -0
- package/dist/plugins/nostr/nostrConnector.js +413 -0
- package/dist/plugins/nostr/nostrConnectorPlugin.js +446 -0
- package/dist/plugins/nostr/nostrSigner.js +313 -0
- package/dist/plugins/nostr/types.js +1 -0
- package/dist/plugins/oauth/index.js +3 -0
- package/dist/plugins/oauth/oauthConnector.js +753 -0
- package/dist/plugins/oauth/oauthPlugin.js +396 -0
- package/dist/plugins/oauth/types.js +1 -0
- package/dist/plugins/web3/index.js +4 -0
- package/dist/plugins/web3/types.js +1 -0
- package/dist/plugins/web3/web3Connector.js +528 -0
- package/dist/plugins/web3/web3ConnectorPlugin.js +448 -0
- package/dist/plugins/web3/web3Signer.js +308 -0
- package/dist/plugins/webauthn/index.js +3 -0
- package/dist/plugins/webauthn/types.js +11 -0
- package/dist/plugins/webauthn/webauthn.js +478 -0
- package/dist/plugins/webauthn/webauthnPlugin.js +398 -0
- package/dist/plugins/webauthn/webauthnSigner.js +304 -0
- package/dist/storage/storage.js +147 -0
- package/dist/types/config/simplified-config.d.ts +114 -0
- package/dist/types/core.d.ts +305 -0
- package/dist/types/gundb/crypto.d.ts +95 -0
- package/dist/types/gundb/db.d.ts +401 -0
- package/dist/types/gundb/derive.d.ts +21 -0
- package/dist/types/gundb/errors.d.ts +42 -0
- package/dist/types/gundb/index.d.ts +3 -0
- package/dist/types/gundb/rxjs.d.ts +110 -0
- package/dist/types/gundb/simple-api.d.ts +90 -0
- package/dist/types/gundb/types.d.ts +264 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/interfaces/common.d.ts +85 -0
- package/dist/types/interfaces/events.d.ts +131 -0
- package/dist/types/interfaces/plugin.d.ts +162 -0
- package/dist/types/interfaces/shogun.d.ts +215 -0
- package/dist/types/managers/AuthManager.d.ts +72 -0
- package/dist/types/managers/CoreInitializer.d.ts +40 -0
- package/dist/types/managers/EventManager.d.ts +49 -0
- package/dist/types/managers/PluginManager.d.ts +145 -0
- package/dist/types/migration-test.d.ts +16 -0
- package/dist/types/plugins/base.d.ts +35 -0
- package/dist/types/plugins/index.d.ts +14 -0
- package/dist/types/plugins/nostr/index.d.ts +4 -0
- package/dist/types/plugins/nostr/nostrConnector.d.ts +119 -0
- package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +163 -0
- package/dist/types/plugins/nostr/nostrSigner.d.ts +105 -0
- package/dist/types/plugins/nostr/types.d.ts +122 -0
- package/dist/types/plugins/oauth/index.d.ts +3 -0
- package/dist/types/plugins/oauth/oauthConnector.d.ts +110 -0
- package/dist/types/plugins/oauth/oauthPlugin.d.ts +91 -0
- package/dist/types/plugins/oauth/types.d.ts +114 -0
- package/dist/types/plugins/web3/index.d.ts +4 -0
- package/dist/types/plugins/web3/types.d.ts +107 -0
- package/dist/types/plugins/web3/web3Connector.d.ts +129 -0
- package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +160 -0
- package/dist/types/plugins/web3/web3Signer.d.ts +114 -0
- package/dist/types/plugins/webauthn/index.d.ts +3 -0
- package/dist/types/plugins/webauthn/types.d.ts +162 -0
- package/dist/types/plugins/webauthn/webauthn.d.ts +129 -0
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +158 -0
- package/dist/types/plugins/webauthn/webauthnSigner.d.ts +91 -0
- package/dist/types/storage/storage.d.ts +50 -0
- package/dist/types/utils/errorHandler.d.ts +119 -0
- package/dist/types/utils/eventEmitter.d.ts +39 -0
- package/dist/types/utils/validation.d.ts +27 -0
- package/dist/utils/errorHandler.js +241 -0
- package/dist/utils/eventEmitter.js +76 -0
- package/dist/utils/validation.js +72 -0
- package/package.json +1 -1
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { ErrorHandler, ErrorType } from "../utils/errorHandler";
|
|
2
|
+
/**
|
|
3
|
+
* Manages authentication operations for ShogunCore
|
|
4
|
+
*/
|
|
5
|
+
export class AuthManager {
|
|
6
|
+
core;
|
|
7
|
+
currentAuthMethod;
|
|
8
|
+
constructor(core) {
|
|
9
|
+
this.core = core;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Check if user is logged in
|
|
13
|
+
* @returns {boolean} True if user is logged in, false otherwise
|
|
14
|
+
* @description Verifies authentication status by checking GunInstance login state
|
|
15
|
+
* and presence of authentication credentials in storage
|
|
16
|
+
*/
|
|
17
|
+
isLoggedIn() {
|
|
18
|
+
return this.core.db.isLoggedIn();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Perform user logout
|
|
22
|
+
* @description Logs out the current user from GunInstance and emits logout event.
|
|
23
|
+
* If user is not authenticated, the logout operation is ignored.
|
|
24
|
+
*/
|
|
25
|
+
logout() {
|
|
26
|
+
try {
|
|
27
|
+
if (!this.isLoggedIn()) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.core.db.logout();
|
|
31
|
+
this.core.emit("auth:logout");
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
ErrorHandler.handle(ErrorType.AUTHENTICATION, "LOGOUT_FAILED", error instanceof Error ? error.message : "Error during logout", error);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Authenticate user with username and password
|
|
39
|
+
* @param username - Username
|
|
40
|
+
* @param password - User password
|
|
41
|
+
* @returns {Promise<AuthResult>} Promise with authentication result
|
|
42
|
+
* @description Attempts to log in user with provided credentials.
|
|
43
|
+
* Emits login event on success.
|
|
44
|
+
*/
|
|
45
|
+
async login(username, password, pair) {
|
|
46
|
+
try {
|
|
47
|
+
if (!this.currentAuthMethod) {
|
|
48
|
+
this.currentAuthMethod = "password";
|
|
49
|
+
}
|
|
50
|
+
const result = await this.core.db.login(username, password, pair);
|
|
51
|
+
if (result.success) {
|
|
52
|
+
// Include SEA pair in the response
|
|
53
|
+
const seaPair = this.core.user?._?.sea;
|
|
54
|
+
if (seaPair) {
|
|
55
|
+
result.sea = seaPair;
|
|
56
|
+
}
|
|
57
|
+
this.core.emit("auth:login", {
|
|
58
|
+
userPub: result.userPub ?? "",
|
|
59
|
+
method: this.currentAuthMethod === "pair"
|
|
60
|
+
? "password"
|
|
61
|
+
: this.currentAuthMethod || "password",
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
result.error = result.error || "Wrong user or password";
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
ErrorHandler.handle(ErrorType.AUTHENTICATION, "LOGIN_FAILED", error.message ?? "Unknown error during login", error);
|
|
71
|
+
return {
|
|
72
|
+
success: false,
|
|
73
|
+
error: error.message ?? "Unknown error during login",
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Login with GunDB pair directly
|
|
79
|
+
* @param pair - GunDB SEA pair for authentication
|
|
80
|
+
* @returns {Promise<AuthResult>} Promise with authentication result
|
|
81
|
+
* @description Authenticates user using a GunDB pair directly.
|
|
82
|
+
* Emits login event on success.
|
|
83
|
+
*/
|
|
84
|
+
async loginWithPair(pair) {
|
|
85
|
+
try {
|
|
86
|
+
if (!pair || !pair.pub || !pair.priv || !pair.epub || !pair.epriv) {
|
|
87
|
+
return {
|
|
88
|
+
success: false,
|
|
89
|
+
error: "Invalid pair structure - missing required keys",
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
// Use the new loginWithPair method from GunInstance
|
|
93
|
+
const result = await this.core.db.login("", "", pair);
|
|
94
|
+
if (result.success) {
|
|
95
|
+
// Include SEA pair in the response
|
|
96
|
+
const seaPair = this.core.user?._?.sea;
|
|
97
|
+
if (seaPair) {
|
|
98
|
+
result.sea = seaPair;
|
|
99
|
+
}
|
|
100
|
+
this.currentAuthMethod = "pair";
|
|
101
|
+
this.core.emit("auth:login", {
|
|
102
|
+
userPub: result.userPub ?? "",
|
|
103
|
+
method: "password",
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
result.error =
|
|
108
|
+
result.error || "Authentication failed with provided pair";
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
ErrorHandler.handle(ErrorType.AUTHENTICATION, "PAIR_LOGIN_FAILED", error.message ?? "Unknown error during pair login", error);
|
|
114
|
+
return {
|
|
115
|
+
success: false,
|
|
116
|
+
error: error.message ?? "Unknown error during pair login",
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Register a new user with provided credentials
|
|
122
|
+
* @param username - Username
|
|
123
|
+
* @param password - Password
|
|
124
|
+
* @param email - Email (optional)
|
|
125
|
+
* @param pair - Pair of keys
|
|
126
|
+
* @returns {Promise<SignUpResult>} Registration result
|
|
127
|
+
* @description Creates a new user account with the provided credentials.
|
|
128
|
+
* Validates password requirements and emits signup event on success.
|
|
129
|
+
*/
|
|
130
|
+
async signUp(username, password, pair) {
|
|
131
|
+
try {
|
|
132
|
+
if (!this.core.db) {
|
|
133
|
+
throw new Error("Database not initialized");
|
|
134
|
+
}
|
|
135
|
+
// For password-based signup, ensure password is provided
|
|
136
|
+
if (!pair && (!password || password.trim() === "")) {
|
|
137
|
+
throw new Error("Password is required for password-based signup");
|
|
138
|
+
}
|
|
139
|
+
const result = await this.core.db.signUp(username, password || "", pair);
|
|
140
|
+
if (result.success) {
|
|
141
|
+
// Update current authentication method
|
|
142
|
+
this.currentAuthMethod = pair ? "web3" : "password";
|
|
143
|
+
this.core.emit("auth:signup", {
|
|
144
|
+
userPub: result.userPub,
|
|
145
|
+
username,
|
|
146
|
+
method: this.currentAuthMethod,
|
|
147
|
+
});
|
|
148
|
+
this.core.emit("debug", {
|
|
149
|
+
action: "signup_success",
|
|
150
|
+
userPub: result.userPub,
|
|
151
|
+
method: this.currentAuthMethod,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
this.core.emit("debug", {
|
|
156
|
+
action: "signup_failed",
|
|
157
|
+
error: result.error,
|
|
158
|
+
username,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
if (typeof console !== "undefined" && console.error) {
|
|
165
|
+
console.error(`Error during registration for user ${username}:`, error);
|
|
166
|
+
}
|
|
167
|
+
this.core.emit("debug", {
|
|
168
|
+
action: "signup_error",
|
|
169
|
+
error: error instanceof Error ? error.message : String(error),
|
|
170
|
+
username,
|
|
171
|
+
});
|
|
172
|
+
return {
|
|
173
|
+
success: false,
|
|
174
|
+
error: `Registration failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Set the current authentication method
|
|
180
|
+
* This is used by plugins to indicate which authentication method was used
|
|
181
|
+
* @param method The authentication method used
|
|
182
|
+
*/
|
|
183
|
+
setAuthMethod(method) {
|
|
184
|
+
this.currentAuthMethod = method;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get the current authentication method
|
|
188
|
+
* @returns The current authentication method or undefined if not set
|
|
189
|
+
*/
|
|
190
|
+
getAuthMethod() {
|
|
191
|
+
return this.currentAuthMethod;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Get an authentication method plugin by type
|
|
195
|
+
* @param type The type of authentication method
|
|
196
|
+
* @returns The authentication plugin or undefined if not available
|
|
197
|
+
* This is a more modern approach to accessing authentication methods
|
|
198
|
+
*/
|
|
199
|
+
getAuthenticationMethod(type) {
|
|
200
|
+
switch (type) {
|
|
201
|
+
case "webauthn":
|
|
202
|
+
return this.core.getPlugin("WebAuthn");
|
|
203
|
+
case "web3":
|
|
204
|
+
return this.core.getPlugin("Web3");
|
|
205
|
+
case "nostr":
|
|
206
|
+
return this.core.getPlugin("Nostr");
|
|
207
|
+
case "oauth":
|
|
208
|
+
return this.core.getPlugin("OAuth");
|
|
209
|
+
case "password":
|
|
210
|
+
default:
|
|
211
|
+
return {
|
|
212
|
+
login: async (username, password) => {
|
|
213
|
+
return await this.login(username, password);
|
|
214
|
+
},
|
|
215
|
+
signUp: async (username, password, confirm) => {
|
|
216
|
+
// For password-based signup, validate password confirmation
|
|
217
|
+
if (confirm && password !== confirm) {
|
|
218
|
+
throw new Error("Password and confirm password do not match");
|
|
219
|
+
}
|
|
220
|
+
return await this.signUp(username, password);
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { ShogunStorage } from "../storage/storage";
|
|
2
|
+
import { ErrorHandler } from "../utils/errorHandler";
|
|
3
|
+
import { WebauthnPlugin } from "../plugins/webauthn/webauthnPlugin";
|
|
4
|
+
import { Web3ConnectorPlugin } from "../plugins/web3/web3ConnectorPlugin";
|
|
5
|
+
import { NostrConnectorPlugin } from "../plugins/nostr/nostrConnectorPlugin";
|
|
6
|
+
import { OAuthPlugin } from "../plugins/oauth/oauthPlugin";
|
|
7
|
+
import { DataBase, RxJS, createGun, derive } from "../gundb";
|
|
8
|
+
/**
|
|
9
|
+
* Handles initialization of ShogunCore components
|
|
10
|
+
*/
|
|
11
|
+
export class CoreInitializer {
|
|
12
|
+
core;
|
|
13
|
+
constructor(core) {
|
|
14
|
+
this.core = core;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Initialize the Shogun SDK
|
|
18
|
+
* @param config - SDK Configuration object
|
|
19
|
+
* @description Creates a new instance of ShogunCore with the provided configuration.
|
|
20
|
+
* Initializes all required components including storage, event emitter, GunInstance connection,
|
|
21
|
+
* and plugin system.
|
|
22
|
+
*/
|
|
23
|
+
async initialize(config) {
|
|
24
|
+
// Polyfill console for environments where it might be missing
|
|
25
|
+
if (typeof console === "undefined") {
|
|
26
|
+
global.console = {
|
|
27
|
+
log: () => { },
|
|
28
|
+
warn: () => { },
|
|
29
|
+
error: () => { },
|
|
30
|
+
info: () => { },
|
|
31
|
+
debug: () => { },
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
// Initialize storage
|
|
35
|
+
this.core.storage = new ShogunStorage();
|
|
36
|
+
// Setup error handler
|
|
37
|
+
ErrorHandler.addListener((error) => {
|
|
38
|
+
this.core.emit("error", {
|
|
39
|
+
action: error.code,
|
|
40
|
+
message: error.message,
|
|
41
|
+
type: error.type,
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
// Setup Gun instance
|
|
45
|
+
await this.initializeGun(config);
|
|
46
|
+
// Setup Gun user
|
|
47
|
+
await this.initializeGunUser();
|
|
48
|
+
// Setup Gun event forwarding
|
|
49
|
+
this.setupGunEventForwarding();
|
|
50
|
+
// Setup wallet derivation
|
|
51
|
+
this.setupWalletDerivation();
|
|
52
|
+
// Initialize RxJS
|
|
53
|
+
this.core.rx = new RxJS(this.core.gun);
|
|
54
|
+
// Register built-in plugins
|
|
55
|
+
this.registerBuiltinPlugins(config);
|
|
56
|
+
// Initialize async components
|
|
57
|
+
await this.initializeAsync();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Initialize Gun instance
|
|
61
|
+
*/
|
|
62
|
+
async initializeGun(config) {
|
|
63
|
+
console.log("Initialize Gun instance", config);
|
|
64
|
+
try {
|
|
65
|
+
if (config.gunInstance && config.gunOptions === undefined) {
|
|
66
|
+
console.log("Using provided Gun instance");
|
|
67
|
+
this.core._gun = config.gunInstance;
|
|
68
|
+
}
|
|
69
|
+
else if (config.gunOptions && config.gunInstance === undefined) {
|
|
70
|
+
console.log("Creating new Gun instance");
|
|
71
|
+
this.core._gun = createGun(config.gunOptions);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
throw new Error("!!! NO GUN INSTANCE OR GUN OPTIONS PROVIDED !!!");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (typeof console !== "undefined" && console.error) {
|
|
79
|
+
console.error("Error creating Gun instance:", error);
|
|
80
|
+
}
|
|
81
|
+
throw new Error(`Failed to create Gun instance: ${error}`);
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
console.log("Initialize Gun instance", this.core.gun);
|
|
85
|
+
this.core.db = new DataBase(this.core._gun, config.gunOptions?.scope || "");
|
|
86
|
+
// Note: user is a getter that returns _user, so we don't need to assign it
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (typeof console !== "undefined" && console.error) {
|
|
90
|
+
console.error("Error initializing GunInstance:", error);
|
|
91
|
+
}
|
|
92
|
+
throw new Error(`Failed to initialize GunInstance: ${error}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Initialize Gun user
|
|
97
|
+
*/
|
|
98
|
+
async initializeGunUser() {
|
|
99
|
+
try {
|
|
100
|
+
this.core._user = this.core.gun.user().recall({ sessionStorage: true });
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
if (typeof console !== "undefined" && console.error) {
|
|
104
|
+
console.error("Error initializing Gun user:", error);
|
|
105
|
+
}
|
|
106
|
+
throw new Error(`Failed to initialize Gun user: ${error}`);
|
|
107
|
+
}
|
|
108
|
+
this.core.gun.on("auth", (user) => {
|
|
109
|
+
this.core._user = this.core.gun.user().recall({ sessionStorage: true });
|
|
110
|
+
this.core.emit("auth:login", {
|
|
111
|
+
userPub: user.pub,
|
|
112
|
+
method: "password",
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Setup Gun event forwarding
|
|
118
|
+
*/
|
|
119
|
+
setupGunEventForwarding() {
|
|
120
|
+
const gunEvents = ["gun:put", "gun:get", "gun:set", "gun:remove"];
|
|
121
|
+
gunEvents.forEach((eventName) => {
|
|
122
|
+
this.core.db.on(eventName, (data) => {
|
|
123
|
+
this.core.emit(eventName, data);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
const peerEvents = [
|
|
127
|
+
"gun:peer:add",
|
|
128
|
+
"gun:peer:remove",
|
|
129
|
+
"gun:peer:connect",
|
|
130
|
+
"gun:peer:disconnect",
|
|
131
|
+
];
|
|
132
|
+
peerEvents.forEach((eventName) => {
|
|
133
|
+
this.core.db.on(eventName, (data) => {
|
|
134
|
+
this.core.emit(eventName, data);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Setup wallet derivation
|
|
140
|
+
*/
|
|
141
|
+
setupWalletDerivation() {
|
|
142
|
+
this.core.gun.on("auth", async (user) => {
|
|
143
|
+
if (!user)
|
|
144
|
+
return;
|
|
145
|
+
const priv = user._?.sea?.epriv;
|
|
146
|
+
const pub = user._?.sea?.epub;
|
|
147
|
+
this.core.wallets = await derive(priv, pub, {
|
|
148
|
+
includeSecp256k1Bitcoin: true,
|
|
149
|
+
includeSecp256k1Ethereum: true,
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Register built-in plugins based on configuration
|
|
155
|
+
*/
|
|
156
|
+
registerBuiltinPlugins(config) {
|
|
157
|
+
try {
|
|
158
|
+
// Register OAuth plugin if configuration is provided
|
|
159
|
+
if (config.oauth) {
|
|
160
|
+
if (typeof console !== "undefined" && console.warn) {
|
|
161
|
+
console.warn("OAuth plugin will be registered with provided configuration");
|
|
162
|
+
}
|
|
163
|
+
const oauthPlugin = new OAuthPlugin();
|
|
164
|
+
if (typeof oauthPlugin.configure === "function") {
|
|
165
|
+
oauthPlugin.configure(config.oauth);
|
|
166
|
+
}
|
|
167
|
+
this.core.pluginManager.register(oauthPlugin);
|
|
168
|
+
}
|
|
169
|
+
// Register WebAuthn plugin if configuration is provided
|
|
170
|
+
if (config.webauthn) {
|
|
171
|
+
if (typeof console !== "undefined" && console.warn) {
|
|
172
|
+
console.warn("WebAuthn plugin will be registered with provided configuration");
|
|
173
|
+
}
|
|
174
|
+
const webauthnPlugin = new WebauthnPlugin();
|
|
175
|
+
if (typeof webauthnPlugin.configure === "function") {
|
|
176
|
+
webauthnPlugin.configure(config.webauthn);
|
|
177
|
+
}
|
|
178
|
+
this.core.pluginManager.register(webauthnPlugin);
|
|
179
|
+
}
|
|
180
|
+
// Register Web3 plugin if configuration is provided
|
|
181
|
+
if (config.web3) {
|
|
182
|
+
if (typeof console !== "undefined" && console.warn) {
|
|
183
|
+
console.warn("Web3 plugin will be registered with provided configuration");
|
|
184
|
+
}
|
|
185
|
+
const web3Plugin = new Web3ConnectorPlugin();
|
|
186
|
+
if (typeof web3Plugin.configure === "function") {
|
|
187
|
+
web3Plugin.configure(config.web3);
|
|
188
|
+
}
|
|
189
|
+
this.core.pluginManager.register(web3Plugin);
|
|
190
|
+
}
|
|
191
|
+
// Register Nostr plugin if configuration is provided
|
|
192
|
+
if (config.nostr) {
|
|
193
|
+
if (typeof console !== "undefined" && console.warn) {
|
|
194
|
+
console.warn("Nostr plugin will be registered with provided configuration");
|
|
195
|
+
}
|
|
196
|
+
const nostrPlugin = new NostrConnectorPlugin();
|
|
197
|
+
if (typeof nostrPlugin.configure === "function") {
|
|
198
|
+
nostrPlugin.configure(config.nostr);
|
|
199
|
+
}
|
|
200
|
+
this.core.pluginManager.register(nostrPlugin);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
if (typeof console !== "undefined" && console.error) {
|
|
205
|
+
console.error("Error registering builtin plugins:", error);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Initialize async components
|
|
211
|
+
*/
|
|
212
|
+
async initializeAsync() {
|
|
213
|
+
try {
|
|
214
|
+
await this.core.db.initialize();
|
|
215
|
+
this.core.emit("debug", {
|
|
216
|
+
action: "core_initialized",
|
|
217
|
+
timestamp: Date.now(),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
if (typeof console !== "undefined" && console.error) {
|
|
222
|
+
console.error("Error during Shogun Core initialization:", error);
|
|
223
|
+
}
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ShogunEventEmitter } from "../interfaces/events";
|
|
2
|
+
/**
|
|
3
|
+
* Manages event operations for ShogunCore
|
|
4
|
+
*/
|
|
5
|
+
export class EventManager {
|
|
6
|
+
eventEmitter;
|
|
7
|
+
constructor() {
|
|
8
|
+
this.eventEmitter = new ShogunEventEmitter();
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Emits an event through the core's event emitter.
|
|
12
|
+
* Plugins should use this method to emit events instead of accessing the private eventEmitter directly.
|
|
13
|
+
* @param eventName The name of the event to emit.
|
|
14
|
+
* @param data The data to pass with the event.
|
|
15
|
+
* @returns {boolean} Indicates if the event had listeners.
|
|
16
|
+
*/
|
|
17
|
+
emit(eventName, data) {
|
|
18
|
+
return this.eventEmitter.emit(eventName, data);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Add an event listener
|
|
22
|
+
* @param eventName The name of the event to listen for
|
|
23
|
+
* @param listener The callback function to execute when the event is emitted
|
|
24
|
+
* @returns {this} Returns this instance for method chaining
|
|
25
|
+
*/
|
|
26
|
+
on(eventName, listener) {
|
|
27
|
+
this.eventEmitter.on(eventName, listener);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Add a one-time event listener
|
|
32
|
+
* @param eventName The name of the event to listen for
|
|
33
|
+
* @param listener The callback function to execute when the event is emitted
|
|
34
|
+
* @returns {this} Returns this instance for method chaining
|
|
35
|
+
*/
|
|
36
|
+
once(eventName, listener) {
|
|
37
|
+
this.eventEmitter.once(eventName, listener);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Remove an event listener
|
|
42
|
+
* @param eventName The name of the event to stop listening for
|
|
43
|
+
* @param listener The callback function to remove
|
|
44
|
+
* @returns {this} Returns this instance for method chaining
|
|
45
|
+
*/
|
|
46
|
+
off(eventName, listener) {
|
|
47
|
+
this.eventEmitter.off(eventName, listener);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Remove all listeners for a specific event or all events
|
|
52
|
+
* @param eventName Optional. The name of the event to remove listeners for.
|
|
53
|
+
* If not provided, all listeners for all events are removed.
|
|
54
|
+
* @returns {this} Returns this instance for method chaining
|
|
55
|
+
*/
|
|
56
|
+
removeAllListeners(eventName) {
|
|
57
|
+
this.eventEmitter.removeAllListeners(eventName);
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get the underlying event emitter instance
|
|
62
|
+
* @returns The ShogunEventEmitter instance
|
|
63
|
+
*/
|
|
64
|
+
getEventEmitter() {
|
|
65
|
+
return this.eventEmitter;
|
|
66
|
+
}
|
|
67
|
+
}
|