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,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ErrorHandler = exports.ErrorType = void 0;
|
|
4
|
+
exports.createError = createError;
|
|
5
|
+
/**
|
|
6
|
+
* Types of errors that can occur in the application
|
|
7
|
+
*/
|
|
8
|
+
var ErrorType;
|
|
9
|
+
(function (ErrorType) {
|
|
10
|
+
ErrorType["AUTHENTICATION"] = "AuthenticationError";
|
|
11
|
+
ErrorType["AUTHORIZATION"] = "AuthorizationError";
|
|
12
|
+
ErrorType["VALIDATION"] = "ValidationError";
|
|
13
|
+
ErrorType["NETWORK"] = "NetworkError";
|
|
14
|
+
ErrorType["DATABASE"] = "DatabaseError";
|
|
15
|
+
ErrorType["WALLET"] = "WalletError";
|
|
16
|
+
ErrorType["STORAGE"] = "StorageError";
|
|
17
|
+
ErrorType["ENCRYPTION"] = "EncryptionError";
|
|
18
|
+
ErrorType["SIGNATURE"] = "SignatureError";
|
|
19
|
+
ErrorType["ENVIRONMENT"] = "EnvironmentError";
|
|
20
|
+
ErrorType["SECURITY"] = "SecurityError";
|
|
21
|
+
ErrorType["GUN"] = "GunError";
|
|
22
|
+
ErrorType["STEALTH"] = "StealthError";
|
|
23
|
+
ErrorType["WEBAUTHN"] = "WebAuthnError";
|
|
24
|
+
ErrorType["PLUGIN"] = "PluginError";
|
|
25
|
+
ErrorType["UNKNOWN"] = "UnknownError";
|
|
26
|
+
ErrorType["CONNECTOR"] = "ConnectorError";
|
|
27
|
+
ErrorType["GENERAL"] = "GeneralError";
|
|
28
|
+
ErrorType["CONTRACT"] = "ContractError";
|
|
29
|
+
ErrorType["BIP32"] = "BIP32Error";
|
|
30
|
+
ErrorType["ETHEREUM"] = "EthereumError";
|
|
31
|
+
ErrorType["BITCOIN"] = "BitcoinError";
|
|
32
|
+
})(ErrorType || (exports.ErrorType = ErrorType = {}));
|
|
33
|
+
/**
|
|
34
|
+
* Wrapper to standardize errors
|
|
35
|
+
* @param type - Error type
|
|
36
|
+
* @param code - Error code
|
|
37
|
+
* @param message - Error message
|
|
38
|
+
* @param originalError - Original error
|
|
39
|
+
* @returns A structured error object
|
|
40
|
+
*/
|
|
41
|
+
function createError(type, code, message, originalError) {
|
|
42
|
+
return {
|
|
43
|
+
type,
|
|
44
|
+
code,
|
|
45
|
+
message,
|
|
46
|
+
originalError,
|
|
47
|
+
timestamp: Date.now(),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Centralized error handler
|
|
52
|
+
*/
|
|
53
|
+
class ErrorHandler {
|
|
54
|
+
/**
|
|
55
|
+
* Set an external logging service for production error monitoring
|
|
56
|
+
* @param logger - External logger function to send errors to a monitoring service
|
|
57
|
+
*/
|
|
58
|
+
static setExternalLogger(logger) {
|
|
59
|
+
this.externalLogger = logger;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Handles an error by logging it and notifying listeners
|
|
63
|
+
* @param error - The error to handle
|
|
64
|
+
*/
|
|
65
|
+
static handleError(error) {
|
|
66
|
+
// Log essential errors only
|
|
67
|
+
if (error.type === ErrorType.AUTHENTICATION ||
|
|
68
|
+
error.type === ErrorType.AUTHORIZATION ||
|
|
69
|
+
error.type === ErrorType.SECURITY) {
|
|
70
|
+
// Ensure console.error is available and safe to use
|
|
71
|
+
if (typeof console !== "undefined" && console.error) {
|
|
72
|
+
console.error(`[${error.type}] ${error.code}: ${error.message}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// Store the error in memory
|
|
76
|
+
this.errors.push(error);
|
|
77
|
+
// Keep only the last maxErrors
|
|
78
|
+
if (this.errors.length > this.maxErrors) {
|
|
79
|
+
this.errors = this.errors.slice(-this.maxErrors);
|
|
80
|
+
}
|
|
81
|
+
// Send to external logger if set (for production monitoring)
|
|
82
|
+
if (this.externalLogger) {
|
|
83
|
+
try {
|
|
84
|
+
this.externalLogger(error);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
// Fallback logging for external logger errors
|
|
88
|
+
console.error("Failed to send error to external logger:", e);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Notify all listeners
|
|
92
|
+
this.listeners.forEach((listener) => {
|
|
93
|
+
try {
|
|
94
|
+
listener(error);
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
// Silent error to prevent infinite loops
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Handles a raw error by converting it to ShogunError
|
|
103
|
+
* @param type - Error type
|
|
104
|
+
* @param code - Error code
|
|
105
|
+
* @param message - Error message
|
|
106
|
+
* @param originalError - Original error
|
|
107
|
+
* @param logLevel - Log level for the error
|
|
108
|
+
*/
|
|
109
|
+
static handle(type, code, message, originalError, logLevel = "error") {
|
|
110
|
+
// Create a formatted error message (tests expect the plain message)
|
|
111
|
+
const finalMessage = message;
|
|
112
|
+
// Log the error
|
|
113
|
+
switch (logLevel) {
|
|
114
|
+
case "debug":
|
|
115
|
+
console.log(`[${type}] ${code}: ${finalMessage}`);
|
|
116
|
+
break;
|
|
117
|
+
case "warn":
|
|
118
|
+
console.log(`[${type}] ${code}: ${finalMessage}`);
|
|
119
|
+
break;
|
|
120
|
+
case "info":
|
|
121
|
+
console.log(`[${type}] ${code}: ${finalMessage}`);
|
|
122
|
+
break;
|
|
123
|
+
case "error":
|
|
124
|
+
default:
|
|
125
|
+
console.log(`[${type}] ${code}: ${finalMessage}`);
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
const error = createError(type, code, finalMessage, originalError);
|
|
129
|
+
this.handleError(error);
|
|
130
|
+
return error;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Handles errors and throws them as standardized ShogunError objects
|
|
134
|
+
* @param type - Error type
|
|
135
|
+
* @param code - Error code
|
|
136
|
+
* @param message - Error message
|
|
137
|
+
* @param originalError - Original error
|
|
138
|
+
* @throws ShogunError
|
|
139
|
+
*/
|
|
140
|
+
static handleAndThrow(type, code, message, originalError) {
|
|
141
|
+
const error = this.handle(type, code, message, originalError);
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Retrieves the last N errors
|
|
146
|
+
* @param count - Number of errors to retrieve
|
|
147
|
+
* @returns List of most recent errors
|
|
148
|
+
*/
|
|
149
|
+
static getRecentErrors(count = 10) {
|
|
150
|
+
return this.errors.slice(-Math.min(count, this.errors.length));
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Adds a listener for errors
|
|
154
|
+
* @param listener - Function that will be called when an error occurs
|
|
155
|
+
*/
|
|
156
|
+
static addListener(listener) {
|
|
157
|
+
this.listeners.push(listener);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Removes an error listener
|
|
161
|
+
* @param listener - Function to remove
|
|
162
|
+
*/
|
|
163
|
+
static removeListener(listener) {
|
|
164
|
+
const index = this.listeners.indexOf(listener);
|
|
165
|
+
if (index !== -1) {
|
|
166
|
+
this.listeners.splice(index, 1);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Notifies all listeners of an error
|
|
171
|
+
* @param error - Error to notify
|
|
172
|
+
*/
|
|
173
|
+
static notifyListeners(error) {
|
|
174
|
+
for (const listener of this.listeners) {
|
|
175
|
+
try {
|
|
176
|
+
listener(error);
|
|
177
|
+
}
|
|
178
|
+
catch (listenerError) {
|
|
179
|
+
console.error(`Error in error listener: ${listenerError}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Helper function to format error messages from native errors
|
|
185
|
+
* @param error - Error to format
|
|
186
|
+
* @returns Formatted error message
|
|
187
|
+
*/
|
|
188
|
+
static formatError(error) {
|
|
189
|
+
if (!error) {
|
|
190
|
+
return "Unknown error";
|
|
191
|
+
}
|
|
192
|
+
if (error instanceof Error) {
|
|
193
|
+
return `${error.name}: ${error.message}`;
|
|
194
|
+
}
|
|
195
|
+
if (typeof error === "string") {
|
|
196
|
+
return error;
|
|
197
|
+
}
|
|
198
|
+
if (typeof error === "object") {
|
|
199
|
+
try {
|
|
200
|
+
return JSON.stringify(error);
|
|
201
|
+
}
|
|
202
|
+
catch (e) {
|
|
203
|
+
return `Object: ${Object.prototype.toString.call(error)}`;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return String(error);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Error handling with retry logic
|
|
210
|
+
*/
|
|
211
|
+
static async withRetry(fn, errorType, errorCode, maxRetries = 3, retryDelay = 1000) {
|
|
212
|
+
let lastError;
|
|
213
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
214
|
+
try {
|
|
215
|
+
return await fn();
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
lastError = error;
|
|
219
|
+
const delay = retryDelay * attempt;
|
|
220
|
+
if (attempt < maxRetries) {
|
|
221
|
+
console.log(`Retrying operation after ${delay}ms (attempt ${attempt}/${maxRetries})`);
|
|
222
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// If we got here, all retries failed.
|
|
227
|
+
// Log the failure and rethrow the last error message for test expectations compatibility.
|
|
228
|
+
this.handle(errorType, errorCode, `Operation failed after ${maxRetries} attempts`, lastError);
|
|
229
|
+
// Prefer the original error message if available
|
|
230
|
+
if (lastError instanceof Error) {
|
|
231
|
+
throw new Error(lastError.message);
|
|
232
|
+
}
|
|
233
|
+
throw new Error(this.formatError(lastError));
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Clear all stored errors
|
|
237
|
+
*/
|
|
238
|
+
static clearErrors() {
|
|
239
|
+
this.errors = [];
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
exports.ErrorHandler = ErrorHandler;
|
|
243
|
+
ErrorHandler.errors = [];
|
|
244
|
+
ErrorHandler.maxErrors = 100;
|
|
245
|
+
ErrorHandler.listeners = [];
|
|
246
|
+
ErrorHandler.externalLogger = null;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventEmitter = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Simple event emitter implementation with generic event types
|
|
6
|
+
*/
|
|
7
|
+
class EventEmitter {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.events = new Map();
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Registers an event listener
|
|
13
|
+
*/
|
|
14
|
+
on(event, listener) {
|
|
15
|
+
if (!this.events.has(event)) {
|
|
16
|
+
this.events.set(event, []);
|
|
17
|
+
}
|
|
18
|
+
this.events.get(event)?.push(listener);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Emits an event with arguments
|
|
22
|
+
*/
|
|
23
|
+
emit(event, data) {
|
|
24
|
+
if (!this.events.has(event))
|
|
25
|
+
return false;
|
|
26
|
+
const listeners = this.events.get(event) || [];
|
|
27
|
+
listeners.forEach((listener) => {
|
|
28
|
+
try {
|
|
29
|
+
listener(data);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
// Ensure error is properly formatted for console.error and pass Error instance
|
|
33
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
34
|
+
console.error(`Error in event listener for ${String(event)}:`, err);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Removes an event listener
|
|
41
|
+
*/
|
|
42
|
+
off(event, listener) {
|
|
43
|
+
if (!this.events.has(event))
|
|
44
|
+
return;
|
|
45
|
+
const listeners = this.events.get(event) || [];
|
|
46
|
+
const index = listeners.indexOf(listener);
|
|
47
|
+
if (index !== -1) {
|
|
48
|
+
listeners.splice(index, 1);
|
|
49
|
+
if (listeners.length === 0) {
|
|
50
|
+
this.events.delete(event);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this.events.set(event, listeners);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Registers a one-time event listener
|
|
59
|
+
*/
|
|
60
|
+
once(event, listener) {
|
|
61
|
+
const onceWrapper = (data) => {
|
|
62
|
+
listener(data);
|
|
63
|
+
this.off(event, onceWrapper);
|
|
64
|
+
};
|
|
65
|
+
this.on(event, onceWrapper);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Removes all listeners for an event or all events
|
|
69
|
+
*/
|
|
70
|
+
removeAllListeners(event) {
|
|
71
|
+
if (event) {
|
|
72
|
+
this.events.delete(event);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
this.events.clear();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.EventEmitter = EventEmitter;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Seed Phrase Utilities for Multi-Device Authentication
|
|
4
|
+
* Provides BIP39-compatible seed phrase generation and validation
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.generateSeedPhrase = generateSeedPhrase;
|
|
8
|
+
exports.validateSeedPhrase = validateSeedPhrase;
|
|
9
|
+
exports.mnemonicToSeed = mnemonicToSeed;
|
|
10
|
+
exports.seedToPassword = seedToPassword;
|
|
11
|
+
exports.deriveCredentialsFromMnemonic = deriveCredentialsFromMnemonic;
|
|
12
|
+
exports.formatSeedPhrase = formatSeedPhrase;
|
|
13
|
+
exports.normalizeSeedPhrase = normalizeSeedPhrase;
|
|
14
|
+
const bip39_1 = require("@scure/bip39");
|
|
15
|
+
const english_1 = require("@scure/bip39/wordlists/english");
|
|
16
|
+
const sha256_1 = require("@noble/hashes/sha256");
|
|
17
|
+
const utils_1 = require("@noble/hashes/utils");
|
|
18
|
+
/**
|
|
19
|
+
* Generate a new 12-word BIP39 mnemonic seed phrase
|
|
20
|
+
* @returns {string} 12-word mnemonic seed phrase
|
|
21
|
+
*/
|
|
22
|
+
function generateSeedPhrase() {
|
|
23
|
+
return (0, bip39_1.generateMnemonic)(english_1.wordlist, 128); // 128 bits = 12 words
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Validate a BIP39 mnemonic seed phrase
|
|
27
|
+
* @param {string} mnemonic - The seed phrase to validate
|
|
28
|
+
* @returns {boolean} True if valid, false otherwise
|
|
29
|
+
*/
|
|
30
|
+
function validateSeedPhrase(mnemonic) {
|
|
31
|
+
try {
|
|
32
|
+
return (0, bip39_1.validateMnemonic)(mnemonic, english_1.wordlist);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Derive a deterministic seed from mnemonic and username
|
|
40
|
+
* @param {string} mnemonic - The BIP39 mnemonic seed phrase
|
|
41
|
+
* @param {string} username - Username to include in derivation
|
|
42
|
+
* @returns {Uint8Array} 64-byte seed for key derivation
|
|
43
|
+
*/
|
|
44
|
+
function mnemonicToSeed(mnemonic, username) {
|
|
45
|
+
if (!validateSeedPhrase(mnemonic)) {
|
|
46
|
+
throw new Error("Invalid mnemonic seed phrase");
|
|
47
|
+
}
|
|
48
|
+
// Use username as additional entropy in the passphrase
|
|
49
|
+
// This ensures different users with same seed phrase get different keys
|
|
50
|
+
const passphrase = `shogun-${username}`;
|
|
51
|
+
return (0, bip39_1.mnemonicToSeedSync)(mnemonic, passphrase);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Convert seed to deterministic password for GunDB
|
|
55
|
+
* @param {Uint8Array} seed - The seed from mnemonic
|
|
56
|
+
* @returns {string} Hex-encoded password
|
|
57
|
+
*/
|
|
58
|
+
function seedToPassword(seed) {
|
|
59
|
+
// Hash the seed to create a deterministic password
|
|
60
|
+
const hash = (0, sha256_1.sha256)(seed);
|
|
61
|
+
return (0, utils_1.bytesToHex)(hash);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Derive GunDB credentials from mnemonic
|
|
65
|
+
* @param {string} mnemonic - The BIP39 mnemonic
|
|
66
|
+
* @param {string} username - Username for derivation
|
|
67
|
+
* @returns {{password: string; seed: Uint8Array}} Credentials for GunDB
|
|
68
|
+
*/
|
|
69
|
+
function deriveCredentialsFromMnemonic(mnemonic, username) {
|
|
70
|
+
const seed = mnemonicToSeed(mnemonic, username);
|
|
71
|
+
const password = seedToPassword(seed);
|
|
72
|
+
return {
|
|
73
|
+
password,
|
|
74
|
+
seed,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Format seed phrase for display (with word numbers)
|
|
79
|
+
* @param {string} mnemonic - The seed phrase
|
|
80
|
+
* @returns {string} Formatted seed phrase with numbers
|
|
81
|
+
*/
|
|
82
|
+
function formatSeedPhrase(mnemonic) {
|
|
83
|
+
const words = mnemonic.split(" ");
|
|
84
|
+
return words.map((word, index) => `${index + 1}. ${word}`).join("\n");
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Normalize and clean user input for seed phrase
|
|
88
|
+
* @param {string} input - User-provided seed phrase
|
|
89
|
+
* @returns {string} Normalized seed phrase
|
|
90
|
+
*/
|
|
91
|
+
function normalizeSeedPhrase(input) {
|
|
92
|
+
return input
|
|
93
|
+
.toLowerCase()
|
|
94
|
+
.trim()
|
|
95
|
+
.replace(/\s+/g, " ") // Replace multiple spaces with single space
|
|
96
|
+
.replace(/[^\w\s]/g, ""); // Remove special characters
|
|
97
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Utility di validazione e generazione credenziali per ShogunCore
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.validateUsername = validateUsername;
|
|
5
|
+
exports.validateEmail = validateEmail;
|
|
6
|
+
exports.validateProvider = validateProvider;
|
|
7
|
+
exports.generateUsernameFromIdentity = generateUsernameFromIdentity;
|
|
8
|
+
exports.generateDeterministicPassword = generateDeterministicPassword;
|
|
9
|
+
// --- VALIDAZIONE ---
|
|
10
|
+
/**
|
|
11
|
+
* Valida uno username secondo le regole comuni
|
|
12
|
+
*/
|
|
13
|
+
function validateUsername(username) {
|
|
14
|
+
if (!username || typeof username !== "string")
|
|
15
|
+
return false;
|
|
16
|
+
if (username.length < 3 || username.length > 64)
|
|
17
|
+
return false;
|
|
18
|
+
if (!/^[a-zA-Z0-9._-]+$/.test(username))
|
|
19
|
+
return false;
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Valida una email
|
|
24
|
+
*/
|
|
25
|
+
function validateEmail(email) {
|
|
26
|
+
if (!email || typeof email !== "string")
|
|
27
|
+
return false;
|
|
28
|
+
// Regex semplice per email
|
|
29
|
+
return /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Valida un provider OAuth supportato
|
|
33
|
+
* @deprecated OAuth has been removed from Shogun Core
|
|
34
|
+
*/
|
|
35
|
+
function validateProvider(provider) {
|
|
36
|
+
console.warn("[validation] OAuth has been removed from Shogun Core");
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
// --- GENERAZIONE USERNAME ---
|
|
40
|
+
/**
|
|
41
|
+
* Genera uno username uniforme a partire da provider e userInfo
|
|
42
|
+
* Esempio: google_utente, github_12345, nostr_pubkey, web3_0xabc...
|
|
43
|
+
*/
|
|
44
|
+
function generateUsernameFromIdentity(provider, userInfo) {
|
|
45
|
+
if (provider === "web3" && userInfo.id) {
|
|
46
|
+
return `web3_${userInfo.id.toLowerCase()}`;
|
|
47
|
+
}
|
|
48
|
+
if (provider === "nostr" && userInfo.id) {
|
|
49
|
+
return `nostr_${userInfo.id}`;
|
|
50
|
+
}
|
|
51
|
+
if (provider === "webauthn" && userInfo.id) {
|
|
52
|
+
return `webauthn_${userInfo.id}`;
|
|
53
|
+
}
|
|
54
|
+
if (userInfo.email && validateEmail(userInfo.email)) {
|
|
55
|
+
return `${provider}_${userInfo.email.split("@")[0]}`;
|
|
56
|
+
}
|
|
57
|
+
if (userInfo.name) {
|
|
58
|
+
return `${provider}_${userInfo.name.replace(/\s+/g, "_")}`;
|
|
59
|
+
}
|
|
60
|
+
if (userInfo.id) {
|
|
61
|
+
return `${provider}_${userInfo.id}`;
|
|
62
|
+
}
|
|
63
|
+
return `${provider}_user`;
|
|
64
|
+
}
|
|
65
|
+
// --- GENERAZIONE PASSWORD DETERMINISTICA ---
|
|
66
|
+
const ethers_1 = require("ethers");
|
|
67
|
+
/**
|
|
68
|
+
* Genera una password deterministica sicura a partire da un salt
|
|
69
|
+
* Usare per OAuth, Web3, Nostr, ecc.
|
|
70
|
+
*/
|
|
71
|
+
function generateDeterministicPassword(salt) {
|
|
72
|
+
try {
|
|
73
|
+
// Restituisce una stringa hex di 32 caratteri
|
|
74
|
+
return ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(salt)).slice(2, 34);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
// Fallback in case ethers is not available
|
|
78
|
+
console.warn("ethers not available, using fallback password generation");
|
|
79
|
+
return Buffer.from(salt).toString("hex").slice(0, 32);
|
|
80
|
+
}
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shogun-core",
|
|
3
|
-
"version": "5.2.
|
|
3
|
+
"version": "5.2.2",
|
|
4
4
|
"description": "SHOGUN CORE - Core library for Shogun Ecosystem",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -47,6 +47,8 @@
|
|
|
47
47
|
"test:ci": "jest --ci --coverage --watchAll=false",
|
|
48
48
|
"test:integration": "jest --config src/__tests__/integration/jest.integration.config.js",
|
|
49
49
|
"test:integration:watch": "jest --config src/__tests__/integration/jest.integration.config.js --watch",
|
|
50
|
+
"crypto-identities:example": "yarn tsx src/examples/crypto-identity-example.ts",
|
|
51
|
+
"crypto-identities:simple": "yarn tsx src/examples/simple-crypto-identity-example.ts",
|
|
50
52
|
"coverage": "jest --coverage"
|
|
51
53
|
},
|
|
52
54
|
"keywords": [
|
|
@@ -59,8 +61,13 @@
|
|
|
59
61
|
"author": "Scobru",
|
|
60
62
|
"license": "MIT",
|
|
61
63
|
"dependencies": {
|
|
64
|
+
"@hpke/chacha20poly1305": "^1.7.1",
|
|
65
|
+
"@hpke/hybridkem-x-wing": "^0.6.1",
|
|
66
|
+
"@hpke/ml-kem": "^0.2.1",
|
|
67
|
+
"@noble/ciphers": "2.0.1",
|
|
62
68
|
"@noble/curves": "^1.9.1",
|
|
63
69
|
"@noble/hashes": "^1.5.0",
|
|
70
|
+
"@noble/post-quantum": "^0.5.2",
|
|
64
71
|
"@scure/bip39": "^1.5.0",
|
|
65
72
|
"@semaphore-protocol/group": "^3.15.0",
|
|
66
73
|
"@semaphore-protocol/identity": "^3.15.0",
|
|
@@ -72,8 +79,10 @@
|
|
|
72
79
|
"ethers": "^6.13.5",
|
|
73
80
|
"gun": "git+https://github.com/amark/gun.git",
|
|
74
81
|
"nostr-tools": "^2.15.0",
|
|
82
|
+
"openpgp": "^6.2.2",
|
|
75
83
|
"react": "^19.2.0",
|
|
76
84
|
"rxjs": "^7.8.2",
|
|
85
|
+
"ts-mls": "^1.3.2",
|
|
77
86
|
"uuid": "^11.1.0",
|
|
78
87
|
"vm-browserify": "^1.1.2"
|
|
79
88
|
},
|
|
@@ -106,61 +115,5 @@
|
|
|
106
115
|
},
|
|
107
116
|
"engines": {
|
|
108
117
|
"node": ">=18"
|
|
109
|
-
},
|
|
110
|
-
"jest": {
|
|
111
|
-
"preset": "ts-jest",
|
|
112
|
-
"testEnvironment": "node",
|
|
113
|
-
"roots": [
|
|
114
|
-
"<rootDir>/src"
|
|
115
|
-
],
|
|
116
|
-
"testMatch": [
|
|
117
|
-
"**/__tests__/**/*.ts",
|
|
118
|
-
"**/?(*.)+(spec|test).ts"
|
|
119
|
-
],
|
|
120
|
-
"testPathIgnorePatterns": [
|
|
121
|
-
".*complete-integration\\.test\\.ts$",
|
|
122
|
-
".*user_manager\\.integration\\.test\\.ts$",
|
|
123
|
-
".*saveUser\\.integration\\.test\\.ts$",
|
|
124
|
-
".*oauthPlugin\\.integration\\.test\\.ts$",
|
|
125
|
-
".*gun-Instance\\.integration\\.test\\.ts$",
|
|
126
|
-
".*robust-integration\\.test\\.ts$",
|
|
127
|
-
".*final-integration\\.test\\.ts$",
|
|
128
|
-
".*quick-integration\\.test\\.ts$"
|
|
129
|
-
],
|
|
130
|
-
"transform": {
|
|
131
|
-
"^.+\\.ts$": "ts-jest"
|
|
132
|
-
},
|
|
133
|
-
"transformIgnorePatterns": [
|
|
134
|
-
"node_modules/(?!(@zk-kit|@semaphore-protocol|ffjavascript)/)"
|
|
135
|
-
],
|
|
136
|
-
"collectCoverageFrom": [
|
|
137
|
-
"src/**/*.ts",
|
|
138
|
-
"!src/**/*.d.ts",
|
|
139
|
-
"!src/index.ts"
|
|
140
|
-
],
|
|
141
|
-
"coverageDirectory": "coverage",
|
|
142
|
-
"coverageReporters": [
|
|
143
|
-
"text",
|
|
144
|
-
"lcov",
|
|
145
|
-
"html"
|
|
146
|
-
],
|
|
147
|
-
"setupFilesAfterEnv": [
|
|
148
|
-
"<rootDir>/src/__tests__/setup.ts"
|
|
149
|
-
],
|
|
150
|
-
"moduleNameMapper": {
|
|
151
|
-
"^@/(.*)$": "<rootDir>/src/$1"
|
|
152
|
-
},
|
|
153
|
-
"testTimeout": 30000,
|
|
154
|
-
"forceExit": true,
|
|
155
|
-
"detectOpenHandles": true,
|
|
156
|
-
"maxWorkers": 1,
|
|
157
|
-
"coverageThreshold": {
|
|
158
|
-
"global": {
|
|
159
|
-
"branches": 10,
|
|
160
|
-
"functions": 15,
|
|
161
|
-
"lines": 20,
|
|
162
|
-
"statements": 20
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
118
|
}
|
|
166
119
|
}
|