shogun-core 5.2.0 → 5.2.2
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 +145 -1143
- package/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js +1220 -0
- package/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js +844 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js +2335 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js +999 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +1651 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js +825 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js +508 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js +747 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +1608 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js +2117 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js +86 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js.map +1 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js +539 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/shogun-core.js +160386 -0
- package/dist/browser/shogun-core.js.map +1 -0
- package/dist/config/simplified-config.js +236 -0
- package/dist/core.js +329 -0
- package/dist/crypto/asymmetric.js +99 -0
- package/dist/crypto/double-ratchet.js +370 -0
- package/dist/crypto/file-encryption.js +213 -0
- package/dist/crypto/hashing.js +87 -0
- package/dist/crypto/index.js +34 -0
- package/dist/crypto/mls-codec.js +202 -0
- package/dist/crypto/mls.js +550 -0
- package/dist/crypto/pgp.js +390 -0
- package/dist/crypto/random-generation.js +341 -0
- package/dist/crypto/sframe.js +350 -0
- package/dist/crypto/signal-protocol.js +376 -0
- package/dist/crypto/symmetric.js +91 -0
- package/dist/crypto/types.js +2 -0
- package/dist/crypto/utils.js +140 -0
- package/dist/examples/auth-test.js +253 -0
- package/dist/examples/crypto-identity-example.js +151 -0
- package/dist/examples/crypto-working-test.js +83 -0
- package/dist/examples/double-ratchet-test.js +155 -0
- package/dist/examples/mls-advanced-example.js +294 -0
- package/dist/examples/mls-sframe-test.js +304 -0
- package/dist/examples/pgp-example.js +200 -0
- package/dist/examples/quick-auth-test.js +61 -0
- package/dist/examples/random-generation-test.js +151 -0
- package/dist/examples/signal-protocol-test.js +38 -0
- package/dist/examples/simple-api-test.js +114 -0
- package/dist/examples/simple-crypto-identity-example.js +84 -0
- package/dist/examples/timeout-test.js +227 -0
- package/dist/examples/zkproof-credentials-example.js +212 -0
- package/dist/examples/zkproof-example.js +201 -0
- package/dist/gundb/api.js +435 -0
- package/dist/gundb/crypto.js +283 -0
- package/dist/gundb/db.js +1946 -0
- package/dist/gundb/derive.js +232 -0
- package/dist/gundb/errors.js +76 -0
- package/dist/gundb/index.js +22 -0
- package/dist/gundb/rxjs.js +447 -0
- package/dist/gundb/types.js +5 -0
- package/dist/index.js +58 -0
- package/dist/interfaces/common.js +2 -0
- package/dist/interfaces/events.js +40 -0
- package/dist/interfaces/plugin.js +2 -0
- package/dist/interfaces/shogun.js +37 -0
- package/dist/managers/AuthManager.js +226 -0
- package/dist/managers/CoreInitializer.js +228 -0
- package/dist/managers/CryptoIdentityManager.js +366 -0
- package/dist/managers/EventManager.js +70 -0
- package/dist/managers/PluginManager.js +299 -0
- package/dist/plugins/base.js +50 -0
- package/dist/plugins/index.js +32 -0
- package/dist/plugins/nostr/index.js +20 -0
- package/dist/plugins/nostr/nostrConnector.js +419 -0
- package/dist/plugins/nostr/nostrConnectorPlugin.js +453 -0
- package/dist/plugins/nostr/nostrSigner.js +319 -0
- package/dist/plugins/nostr/types.js +2 -0
- package/dist/plugins/smartwallet/index.js +18 -0
- package/dist/plugins/smartwallet/smartWalletPlugin.js +511 -0
- package/dist/plugins/smartwallet/types.js +2 -0
- package/dist/plugins/web3/index.js +20 -0
- package/dist/plugins/web3/types.js +2 -0
- package/dist/plugins/web3/web3Connector.js +533 -0
- package/dist/plugins/web3/web3ConnectorPlugin.js +455 -0
- package/dist/plugins/web3/web3Signer.js +314 -0
- package/dist/plugins/webauthn/index.js +19 -0
- package/dist/plugins/webauthn/types.js +14 -0
- package/dist/plugins/webauthn/webauthn.js +496 -0
- package/dist/plugins/webauthn/webauthnPlugin.js +489 -0
- package/dist/plugins/webauthn/webauthnSigner.js +310 -0
- package/dist/plugins/zkproof/index.js +53 -0
- package/dist/plugins/zkproof/types.js +2 -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/storage/storage.js +145 -0
- package/dist/types/config/simplified-config.d.ts +114 -0
- package/dist/types/core.d.ts +305 -0
- package/dist/types/crypto/asymmetric.d.ts +6 -0
- package/dist/types/crypto/double-ratchet.d.ts +22 -0
- package/dist/types/crypto/file-encryption.d.ts +19 -0
- package/dist/types/crypto/hashing.d.ts +9 -0
- package/dist/types/crypto/index.d.ts +13 -0
- package/dist/types/crypto/mls-codec.d.ts +39 -0
- package/dist/types/crypto/mls.d.ts +130 -0
- package/dist/types/crypto/pgp.d.ts +95 -0
- package/dist/types/crypto/random-generation.d.ts +35 -0
- package/dist/types/crypto/sframe.d.ts +102 -0
- package/dist/types/crypto/signal-protocol.d.ts +26 -0
- package/dist/types/crypto/symmetric.d.ts +9 -0
- package/dist/types/crypto/types.d.ts +144 -0
- package/dist/types/crypto/utils.d.ts +22 -0
- package/dist/types/examples/auth-test.d.ts +8 -0
- package/dist/types/examples/crypto-identity-example.d.ts +5 -0
- package/dist/types/examples/crypto-working-test.d.ts +1 -0
- package/dist/types/examples/double-ratchet-test.d.ts +1 -0
- package/dist/types/examples/mls-advanced-example.d.ts +53 -0
- package/dist/types/examples/mls-sframe-test.d.ts +1 -0
- package/dist/types/examples/pgp-example.d.ts +75 -0
- package/dist/types/examples/quick-auth-test.d.ts +8 -0
- package/dist/types/examples/random-generation-test.d.ts +1 -0
- package/dist/types/examples/signal-protocol-test.d.ts +1 -0
- package/dist/types/examples/simple-api-test.d.ts +10 -0
- package/dist/types/examples/simple-crypto-identity-example.d.ts +6 -0
- package/dist/types/examples/timeout-test.d.ts +8 -0
- 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 +185 -0
- package/dist/types/gundb/crypto.d.ts +95 -0
- package/dist/types/gundb/db.d.ts +397 -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/types.d.ts +255 -0
- package/dist/types/index.d.ts +16 -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 +208 -0
- package/dist/types/managers/AuthManager.d.ts +72 -0
- package/dist/types/managers/CoreInitializer.d.ts +40 -0
- package/dist/types/managers/CryptoIdentityManager.d.ts +102 -0
- package/dist/types/managers/EventManager.d.ts +49 -0
- package/dist/types/managers/PluginManager.d.ts +145 -0
- package/dist/types/plugins/base.d.ts +35 -0
- package/dist/types/plugins/index.d.ts +18 -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/smartwallet/index.d.ts +2 -0
- package/dist/types/plugins/smartwallet/smartWalletPlugin.d.ts +67 -0
- package/dist/types/plugins/smartwallet/types.d.ts +80 -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 +183 -0
- package/dist/types/plugins/webauthn/webauthn.d.ts +129 -0
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +179 -0
- package/dist/types/plugins/webauthn/webauthnSigner.d.ts +91 -0
- 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/storage/storage.d.ts +51 -0
- package/dist/types/utils/errorHandler.d.ts +119 -0
- package/dist/types/utils/eventEmitter.d.ts +39 -0
- package/dist/types/utils/seedPhrase.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +27 -0
- package/dist/utils/errorHandler.js +246 -0
- package/dist/utils/eventEmitter.js +79 -0
- package/dist/utils/seedPhrase.js +97 -0
- package/dist/utils/validation.js +81 -0
- package/package.json +10 -57
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthManager = void 0;
|
|
4
|
+
const errorHandler_1 = require("../utils/errorHandler");
|
|
5
|
+
/**
|
|
6
|
+
* Manages authentication operations for ShogunCore
|
|
7
|
+
*/
|
|
8
|
+
class AuthManager {
|
|
9
|
+
constructor(core) {
|
|
10
|
+
this.core = core;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Check if user is logged in
|
|
14
|
+
* @returns {boolean} True if user is logged in, false otherwise
|
|
15
|
+
* @description Verifies authentication status by checking GunInstance login state
|
|
16
|
+
* and presence of authentication credentials in storage
|
|
17
|
+
*/
|
|
18
|
+
isLoggedIn() {
|
|
19
|
+
return this.core.db.isLoggedIn();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Perform user logout
|
|
23
|
+
* @description Logs out the current user from GunInstance and emits logout event.
|
|
24
|
+
* If user is not authenticated, the logout operation is ignored.
|
|
25
|
+
*/
|
|
26
|
+
logout() {
|
|
27
|
+
try {
|
|
28
|
+
if (!this.isLoggedIn()) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
this.core.db.logout();
|
|
32
|
+
this.core.emit("auth:logout");
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.AUTHENTICATION, "LOGOUT_FAILED", error instanceof Error ? error.message : "Error during logout", error);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Authenticate user with username and password
|
|
40
|
+
* @param username - Username
|
|
41
|
+
* @param password - User password
|
|
42
|
+
* @returns {Promise<AuthResult>} Promise with authentication result
|
|
43
|
+
* @description Attempts to log in user with provided credentials.
|
|
44
|
+
* Emits login event on success.
|
|
45
|
+
*/
|
|
46
|
+
async login(username, password, pair) {
|
|
47
|
+
try {
|
|
48
|
+
if (!this.currentAuthMethod) {
|
|
49
|
+
this.currentAuthMethod = "password";
|
|
50
|
+
}
|
|
51
|
+
const result = await this.core.db.login(username, password, pair);
|
|
52
|
+
if (result.success) {
|
|
53
|
+
// Include SEA pair in the response
|
|
54
|
+
const seaPair = this.core.user?._?.sea;
|
|
55
|
+
if (seaPair) {
|
|
56
|
+
result.sea = seaPair;
|
|
57
|
+
}
|
|
58
|
+
this.core.emit("auth:login", {
|
|
59
|
+
userPub: result.userPub ?? "",
|
|
60
|
+
method: this.currentAuthMethod === "pair"
|
|
61
|
+
? "password"
|
|
62
|
+
: this.currentAuthMethod || "password",
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
result.error = result.error || "Wrong user or password";
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.AUTHENTICATION, "LOGIN_FAILED", error.message ?? "Unknown error during login", error);
|
|
72
|
+
return {
|
|
73
|
+
success: false,
|
|
74
|
+
error: error.message ?? "Unknown error during login",
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Login with GunDB pair directly
|
|
80
|
+
* @param pair - GunDB SEA pair for authentication
|
|
81
|
+
* @returns {Promise<AuthResult>} Promise with authentication result
|
|
82
|
+
* @description Authenticates user using a GunDB pair directly.
|
|
83
|
+
* Emits login event on success.
|
|
84
|
+
*/
|
|
85
|
+
async loginWithPair(username, pair) {
|
|
86
|
+
try {
|
|
87
|
+
if (!pair || !pair.pub || !pair.priv || !pair.epub || !pair.epriv) {
|
|
88
|
+
return {
|
|
89
|
+
success: false,
|
|
90
|
+
error: "Invalid pair structure - missing required keys",
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
// Use the new loginWithPair method from GunInstance
|
|
94
|
+
const result = await this.core.db.loginWithPair(username, pair);
|
|
95
|
+
if (result.success) {
|
|
96
|
+
// Include SEA pair in the response
|
|
97
|
+
const seaPair = this.core.user?._?.sea;
|
|
98
|
+
if (seaPair) {
|
|
99
|
+
result.sea = seaPair;
|
|
100
|
+
}
|
|
101
|
+
this.currentAuthMethod = "pair";
|
|
102
|
+
this.core.emit("auth:login", {
|
|
103
|
+
userPub: result.userPub ?? "",
|
|
104
|
+
method: "pair",
|
|
105
|
+
username,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
result.error =
|
|
110
|
+
result.error || "Authentication failed with provided pair";
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.AUTHENTICATION, "PAIR_LOGIN_FAILED", error.message ?? "Unknown error during pair login", error);
|
|
116
|
+
return {
|
|
117
|
+
success: false,
|
|
118
|
+
error: error.message ?? "Unknown error during pair login",
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Register a new user with provided credentials
|
|
124
|
+
* @param username - Username
|
|
125
|
+
* @param password - Password
|
|
126
|
+
* @param email - Email (optional)
|
|
127
|
+
* @param pair - Pair of keys
|
|
128
|
+
* @returns {Promise<SignUpResult>} Registration result
|
|
129
|
+
* @description Creates a new user account with the provided credentials.
|
|
130
|
+
* Validates password requirements and emits signup event on success.
|
|
131
|
+
*/
|
|
132
|
+
async signUp(username, password, pair) {
|
|
133
|
+
try {
|
|
134
|
+
if (!this.core.db) {
|
|
135
|
+
throw new Error("Database not initialized");
|
|
136
|
+
}
|
|
137
|
+
// For password-based signup, ensure password is provided
|
|
138
|
+
if (!pair && (!password || password.trim() === "")) {
|
|
139
|
+
throw new Error("Password is required for password-based signup");
|
|
140
|
+
}
|
|
141
|
+
const result = await this.core.db.signUp(username, password || "", pair);
|
|
142
|
+
if (result.success) {
|
|
143
|
+
// Update current authentication method
|
|
144
|
+
this.currentAuthMethod = pair ? "web3" : "password";
|
|
145
|
+
this.core.emit("auth:signup", {
|
|
146
|
+
userPub: result.userPub,
|
|
147
|
+
username,
|
|
148
|
+
method: this.currentAuthMethod,
|
|
149
|
+
});
|
|
150
|
+
this.core.emit("debug", {
|
|
151
|
+
action: "signup_success",
|
|
152
|
+
userPub: result.userPub,
|
|
153
|
+
method: this.currentAuthMethod,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
this.core.emit("debug", {
|
|
158
|
+
action: "signup_failed",
|
|
159
|
+
error: result.error,
|
|
160
|
+
username,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
if (typeof console !== "undefined" && console.error) {
|
|
167
|
+
console.error(`Error during registration for user ${username}:`, error);
|
|
168
|
+
}
|
|
169
|
+
this.core.emit("debug", {
|
|
170
|
+
action: "signup_error",
|
|
171
|
+
error: error instanceof Error ? error.message : String(error),
|
|
172
|
+
username,
|
|
173
|
+
});
|
|
174
|
+
return {
|
|
175
|
+
success: false,
|
|
176
|
+
error: `Registration failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Set the current authentication method
|
|
182
|
+
* This is used by plugins to indicate which authentication method was used
|
|
183
|
+
* @param method The authentication method used
|
|
184
|
+
*/
|
|
185
|
+
setAuthMethod(method) {
|
|
186
|
+
this.currentAuthMethod = method;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Get the current authentication method
|
|
190
|
+
* @returns The current authentication method or undefined if not set
|
|
191
|
+
*/
|
|
192
|
+
getAuthMethod() {
|
|
193
|
+
return this.currentAuthMethod;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Get an authentication method plugin by type
|
|
197
|
+
* @param type The type of authentication method
|
|
198
|
+
* @returns The authentication plugin or undefined if not available
|
|
199
|
+
* This is a more modern approach to accessing authentication methods
|
|
200
|
+
*/
|
|
201
|
+
getAuthenticationMethod(type) {
|
|
202
|
+
switch (type) {
|
|
203
|
+
case "webauthn":
|
|
204
|
+
return this.core.getPlugin("webauthn");
|
|
205
|
+
case "web3":
|
|
206
|
+
return this.core.getPlugin("web3");
|
|
207
|
+
case "nostr":
|
|
208
|
+
return this.core.getPlugin("nostr");
|
|
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
|
+
}
|
|
226
|
+
exports.AuthManager = AuthManager;
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CoreInitializer = void 0;
|
|
4
|
+
const storage_1 = require("../storage/storage");
|
|
5
|
+
const errorHandler_1 = require("../utils/errorHandler");
|
|
6
|
+
const webauthnPlugin_1 = require("../plugins/webauthn/webauthnPlugin");
|
|
7
|
+
const web3ConnectorPlugin_1 = require("../plugins/web3/web3ConnectorPlugin");
|
|
8
|
+
const nostrConnectorPlugin_1 = require("../plugins/nostr/nostrConnectorPlugin");
|
|
9
|
+
const zkProofPlugin_1 = require("../plugins/zkproof/zkProofPlugin");
|
|
10
|
+
const gundb_1 = require("../gundb");
|
|
11
|
+
/**
|
|
12
|
+
* Handles initialization of ShogunCore components
|
|
13
|
+
*/
|
|
14
|
+
class CoreInitializer {
|
|
15
|
+
constructor(core) {
|
|
16
|
+
this.core = core;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Initialize the Shogun SDK
|
|
20
|
+
* @param config - SDK Configuration object
|
|
21
|
+
* @description Creates a new instance of ShogunCore with the provided configuration.
|
|
22
|
+
* Initializes all required components including storage, event emitter, GunInstance connection,
|
|
23
|
+
* and plugin system.
|
|
24
|
+
*/
|
|
25
|
+
async initialize(config) {
|
|
26
|
+
// Polyfill console for environments where it might be missing
|
|
27
|
+
if (typeof console === "undefined") {
|
|
28
|
+
global.console = {
|
|
29
|
+
log: () => { },
|
|
30
|
+
warn: () => { },
|
|
31
|
+
error: () => { },
|
|
32
|
+
info: () => { },
|
|
33
|
+
debug: () => { },
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
// Initialize storage
|
|
37
|
+
this.core.storage = new storage_1.ShogunStorage(config.silent);
|
|
38
|
+
// Setup error handler
|
|
39
|
+
errorHandler_1.ErrorHandler.addListener((error) => {
|
|
40
|
+
this.core.emit("error", {
|
|
41
|
+
action: error.code,
|
|
42
|
+
message: error.message,
|
|
43
|
+
type: error.type,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
// Setup Gun instance
|
|
47
|
+
this.initializeGun(config);
|
|
48
|
+
// Setup Gun user
|
|
49
|
+
this.initializeGunUser();
|
|
50
|
+
// Setup Gun event forwarding
|
|
51
|
+
this.setupGunEventForwarding();
|
|
52
|
+
// Setup wallet derivation
|
|
53
|
+
this.setupWalletDerivation();
|
|
54
|
+
// Initialize RxJS
|
|
55
|
+
this.core.rx = new gundb_1.RxJS(this.core.gun);
|
|
56
|
+
// Register built-in plugins
|
|
57
|
+
this.registerBuiltinPlugins(config);
|
|
58
|
+
// Initialize async components
|
|
59
|
+
this.initializeDb();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Initialize Gun instance
|
|
63
|
+
*/
|
|
64
|
+
// Sì, è corretto.
|
|
65
|
+
initializeGun(config) {
|
|
66
|
+
try {
|
|
67
|
+
if (config.gunInstance) {
|
|
68
|
+
console.log("Using existing Gun instance:", config.gunInstance);
|
|
69
|
+
this.core._gun = config.gunInstance;
|
|
70
|
+
}
|
|
71
|
+
else if (config.gunOptions && config.gunInstance === undefined) {
|
|
72
|
+
console.log("Creating Gun instance with config:", config.gunOptions);
|
|
73
|
+
this.core._gun = (0, gundb_1.createGun)(config.gunOptions);
|
|
74
|
+
}
|
|
75
|
+
else if (config.gunInstance && config.gunOptions) {
|
|
76
|
+
throw new Error("Gun instance and gun options cannot be provided together");
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (typeof console !== "undefined" && console.error) {
|
|
82
|
+
console.error("Error creating Gun instance:", error);
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
this.core.db = new gundb_1.DataBase(this.core._gun, config.gunOptions?.scope || "", this.core);
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (typeof console !== "undefined" && console.error) {
|
|
92
|
+
console.error("Error initializing GunInstance:", error);
|
|
93
|
+
}
|
|
94
|
+
throw new Error(`Failed to initialize GunInstance: ${error}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Initialize Gun user
|
|
99
|
+
*/
|
|
100
|
+
initializeGunUser() {
|
|
101
|
+
try {
|
|
102
|
+
this.core._user = this.core.gun.user().recall({ sessionStorage: true });
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
if (typeof console !== "undefined" && console.error) {
|
|
106
|
+
console.error("Error initializing Gun user:", error);
|
|
107
|
+
}
|
|
108
|
+
throw new Error(`Failed to initialize Gun user: ${error}`);
|
|
109
|
+
}
|
|
110
|
+
this.core.gun.on("auth", (user) => {
|
|
111
|
+
this.core._user = this.core.gun.user().recall({ sessionStorage: true });
|
|
112
|
+
this.core.emit("auth:login", {
|
|
113
|
+
userPub: user.pub,
|
|
114
|
+
method: "password",
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Setup Gun event forwarding
|
|
120
|
+
*/
|
|
121
|
+
setupGunEventForwarding() {
|
|
122
|
+
const gunEvents = ["gun:put", "gun:get", "gun:set", "gun:remove"];
|
|
123
|
+
gunEvents.forEach((eventName) => {
|
|
124
|
+
this.core.db.on(eventName, (data) => {
|
|
125
|
+
this.core.emit(eventName, data);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
const peerEvents = [
|
|
129
|
+
"gun:peer:add",
|
|
130
|
+
"gun:peer:remove",
|
|
131
|
+
"gun:peer:connect",
|
|
132
|
+
"gun:peer:disconnect",
|
|
133
|
+
];
|
|
134
|
+
peerEvents.forEach((eventName) => {
|
|
135
|
+
this.core.db.on(eventName, (data) => {
|
|
136
|
+
this.core.emit(eventName, data);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Setup wallet derivation
|
|
142
|
+
*/
|
|
143
|
+
setupWalletDerivation() {
|
|
144
|
+
this.core.gun.on("auth", async (user) => {
|
|
145
|
+
if (!user.is)
|
|
146
|
+
return;
|
|
147
|
+
const priv = user._?.sea?.epriv;
|
|
148
|
+
const pub = user._?.sea?.epub;
|
|
149
|
+
this.core.wallets = await (0, gundb_1.derive)(priv, pub, {
|
|
150
|
+
includeSecp256k1Bitcoin: true,
|
|
151
|
+
includeSecp256k1Ethereum: true,
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Register built-in plugins based on configuration
|
|
157
|
+
*/
|
|
158
|
+
registerBuiltinPlugins(config) {
|
|
159
|
+
try {
|
|
160
|
+
// Register WebAuthn plugin if configuration is provided
|
|
161
|
+
if (config.webauthn) {
|
|
162
|
+
if (typeof console !== "undefined" && console.warn) {
|
|
163
|
+
console.warn("WebAuthn plugin will be registered with provided configuration");
|
|
164
|
+
}
|
|
165
|
+
const webauthnPlugin = new webauthnPlugin_1.WebauthnPlugin();
|
|
166
|
+
if (typeof webauthnPlugin.configure === "function") {
|
|
167
|
+
webauthnPlugin.configure(config.webauthn);
|
|
168
|
+
}
|
|
169
|
+
this.core.pluginManager.register(webauthnPlugin);
|
|
170
|
+
}
|
|
171
|
+
// Register Web3 plugin if configuration is provided
|
|
172
|
+
if (config.web3) {
|
|
173
|
+
if (typeof console !== "undefined" && console.warn) {
|
|
174
|
+
console.warn("Web3 plugin will be registered with provided configuration");
|
|
175
|
+
}
|
|
176
|
+
const web3Plugin = new web3ConnectorPlugin_1.Web3ConnectorPlugin();
|
|
177
|
+
if (typeof web3Plugin.configure === "function") {
|
|
178
|
+
web3Plugin.configure(config.web3);
|
|
179
|
+
}
|
|
180
|
+
this.core.pluginManager.register(web3Plugin);
|
|
181
|
+
}
|
|
182
|
+
// Register Nostr plugin if configuration is provided
|
|
183
|
+
if (config.nostr) {
|
|
184
|
+
if (typeof console !== "undefined" && console.warn) {
|
|
185
|
+
console.warn("Nostr plugin will be registered with provided configuration");
|
|
186
|
+
}
|
|
187
|
+
const nostrPlugin = new nostrConnectorPlugin_1.NostrConnectorPlugin();
|
|
188
|
+
if (typeof nostrPlugin.configure === "function") {
|
|
189
|
+
nostrPlugin.configure(config.nostr);
|
|
190
|
+
}
|
|
191
|
+
this.core.pluginManager.register(nostrPlugin);
|
|
192
|
+
}
|
|
193
|
+
// Register ZK-Proof plugin if configuration is provided
|
|
194
|
+
if (config.zkproof) {
|
|
195
|
+
if (typeof console !== "undefined" && console.warn) {
|
|
196
|
+
console.warn("ZK-Proof plugin will be registered with provided configuration");
|
|
197
|
+
}
|
|
198
|
+
const zkproofPlugin = new zkProofPlugin_1.ZkProofPlugin(config.zkproof);
|
|
199
|
+
this.core.pluginManager.register(zkproofPlugin);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
if (typeof console !== "undefined" && console.error) {
|
|
204
|
+
console.error("Error registering builtin plugins:", error);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Initialize async components
|
|
210
|
+
*/
|
|
211
|
+
initializeDb() {
|
|
212
|
+
try {
|
|
213
|
+
this.core.db.initialize();
|
|
214
|
+
this.core.emit("debug", {
|
|
215
|
+
action: "core_initialized",
|
|
216
|
+
timestamp: Date.now(),
|
|
217
|
+
});
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
if (typeof console !== "undefined" && console.error) {
|
|
222
|
+
console.error("Error during Shogun Core initialization:", error);
|
|
223
|
+
}
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
exports.CoreInitializer = CoreInitializer;
|