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.
Files changed (93) hide show
  1. package/dist/browser/shogun-core.js +91002 -0
  2. package/dist/browser/shogun-core.js.map +1 -0
  3. package/dist/config/simplified-config.js +230 -0
  4. package/dist/core.js +338 -0
  5. package/dist/gundb/crypto.js +268 -0
  6. package/dist/gundb/db.js +1829 -0
  7. package/dist/gundb/derive.js +229 -0
  8. package/dist/gundb/errors.js +66 -0
  9. package/dist/gundb/index.js +6 -0
  10. package/dist/gundb/rxjs.js +445 -0
  11. package/dist/gundb/simple-api.js +438 -0
  12. package/dist/gundb/types.js +4 -0
  13. package/dist/index.js +16 -0
  14. package/dist/interfaces/common.js +1 -0
  15. package/dist/interfaces/events.js +36 -0
  16. package/dist/interfaces/plugin.js +1 -0
  17. package/dist/interfaces/shogun.js +34 -0
  18. package/dist/managers/AuthManager.js +225 -0
  19. package/dist/managers/CoreInitializer.js +227 -0
  20. package/dist/managers/EventManager.js +67 -0
  21. package/dist/managers/PluginManager.js +296 -0
  22. package/dist/migration-test.js +91 -0
  23. package/dist/plugins/base.js +47 -0
  24. package/dist/plugins/index.js +15 -0
  25. package/dist/plugins/nostr/index.js +4 -0
  26. package/dist/plugins/nostr/nostrConnector.js +413 -0
  27. package/dist/plugins/nostr/nostrConnectorPlugin.js +446 -0
  28. package/dist/plugins/nostr/nostrSigner.js +313 -0
  29. package/dist/plugins/nostr/types.js +1 -0
  30. package/dist/plugins/oauth/index.js +3 -0
  31. package/dist/plugins/oauth/oauthConnector.js +753 -0
  32. package/dist/plugins/oauth/oauthPlugin.js +396 -0
  33. package/dist/plugins/oauth/types.js +1 -0
  34. package/dist/plugins/web3/index.js +4 -0
  35. package/dist/plugins/web3/types.js +1 -0
  36. package/dist/plugins/web3/web3Connector.js +528 -0
  37. package/dist/plugins/web3/web3ConnectorPlugin.js +448 -0
  38. package/dist/plugins/web3/web3Signer.js +308 -0
  39. package/dist/plugins/webauthn/index.js +3 -0
  40. package/dist/plugins/webauthn/types.js +11 -0
  41. package/dist/plugins/webauthn/webauthn.js +478 -0
  42. package/dist/plugins/webauthn/webauthnPlugin.js +398 -0
  43. package/dist/plugins/webauthn/webauthnSigner.js +304 -0
  44. package/dist/storage/storage.js +147 -0
  45. package/dist/types/config/simplified-config.d.ts +114 -0
  46. package/dist/types/core.d.ts +305 -0
  47. package/dist/types/gundb/crypto.d.ts +95 -0
  48. package/dist/types/gundb/db.d.ts +401 -0
  49. package/dist/types/gundb/derive.d.ts +21 -0
  50. package/dist/types/gundb/errors.d.ts +42 -0
  51. package/dist/types/gundb/index.d.ts +3 -0
  52. package/dist/types/gundb/rxjs.d.ts +110 -0
  53. package/dist/types/gundb/simple-api.d.ts +90 -0
  54. package/dist/types/gundb/types.d.ts +264 -0
  55. package/dist/types/index.d.ts +14 -0
  56. package/dist/types/interfaces/common.d.ts +85 -0
  57. package/dist/types/interfaces/events.d.ts +131 -0
  58. package/dist/types/interfaces/plugin.d.ts +162 -0
  59. package/dist/types/interfaces/shogun.d.ts +215 -0
  60. package/dist/types/managers/AuthManager.d.ts +72 -0
  61. package/dist/types/managers/CoreInitializer.d.ts +40 -0
  62. package/dist/types/managers/EventManager.d.ts +49 -0
  63. package/dist/types/managers/PluginManager.d.ts +145 -0
  64. package/dist/types/migration-test.d.ts +16 -0
  65. package/dist/types/plugins/base.d.ts +35 -0
  66. package/dist/types/plugins/index.d.ts +14 -0
  67. package/dist/types/plugins/nostr/index.d.ts +4 -0
  68. package/dist/types/plugins/nostr/nostrConnector.d.ts +119 -0
  69. package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +163 -0
  70. package/dist/types/plugins/nostr/nostrSigner.d.ts +105 -0
  71. package/dist/types/plugins/nostr/types.d.ts +122 -0
  72. package/dist/types/plugins/oauth/index.d.ts +3 -0
  73. package/dist/types/plugins/oauth/oauthConnector.d.ts +110 -0
  74. package/dist/types/plugins/oauth/oauthPlugin.d.ts +91 -0
  75. package/dist/types/plugins/oauth/types.d.ts +114 -0
  76. package/dist/types/plugins/web3/index.d.ts +4 -0
  77. package/dist/types/plugins/web3/types.d.ts +107 -0
  78. package/dist/types/plugins/web3/web3Connector.d.ts +129 -0
  79. package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +160 -0
  80. package/dist/types/plugins/web3/web3Signer.d.ts +114 -0
  81. package/dist/types/plugins/webauthn/index.d.ts +3 -0
  82. package/dist/types/plugins/webauthn/types.d.ts +162 -0
  83. package/dist/types/plugins/webauthn/webauthn.d.ts +129 -0
  84. package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +158 -0
  85. package/dist/types/plugins/webauthn/webauthnSigner.d.ts +91 -0
  86. package/dist/types/storage/storage.d.ts +50 -0
  87. package/dist/types/utils/errorHandler.d.ts +119 -0
  88. package/dist/types/utils/eventEmitter.d.ts +39 -0
  89. package/dist/types/utils/validation.d.ts +27 -0
  90. package/dist/utils/errorHandler.js +241 -0
  91. package/dist/utils/eventEmitter.js +76 -0
  92. package/dist/utils/validation.js +72 -0
  93. package/package.json +1 -1
@@ -0,0 +1,308 @@
1
+ import { Web3Connector } from "./web3Connector";
2
+ import { ethers } from "ethers";
3
+ import derive from "../../gundb/derive";
4
+ /**
5
+ * Web3 Signer - Provides oneshot signing functionality
6
+ * Similar to webauthn.js but for Web3/MetaMask
7
+ * CONSISTENT with normal Web3 approach
8
+ */
9
+ export class Web3Signer {
10
+ web3Connector;
11
+ credentials = new Map();
12
+ MESSAGE_TO_SIGN = "I Love Shogun!"; // Same as normal approach
13
+ constructor(web3Connector) {
14
+ this.web3Connector = web3Connector || new Web3Connector();
15
+ }
16
+ /**
17
+ * Creates a new Web3 signing credential
18
+ * CONSISTENT with normal Web3 approach
19
+ */
20
+ async createSigningCredential(address) {
21
+ try {
22
+ // Validate address
23
+ const validAddress = ethers.getAddress(address.toLowerCase());
24
+ // Request signature using the same approach as normal Web3
25
+ const signature = await this.requestSignature(validAddress);
26
+ // Generate credentials using the SAME logic as normal approach
27
+ const username = `${validAddress.toLowerCase()}`;
28
+ // FIX: Use only address for password generation to ensure consistency
29
+ // The signature changes each time, causing different passwords for same user
30
+ const password = ethers.keccak256(ethers.toUtf8Bytes(`${validAddress.toLowerCase()}:shogun-web3`));
31
+ const signingCredential = {
32
+ address: validAddress,
33
+ signature,
34
+ message: this.MESSAGE_TO_SIGN,
35
+ username,
36
+ password, // This ensures consistency with normal approach
37
+ };
38
+ // Store credential for later use
39
+ this.credentials.set(validAddress.toLowerCase(), signingCredential);
40
+ return signingCredential;
41
+ }
42
+ catch (error) {
43
+ console.error("Error creating Web3 signing credential:", error);
44
+ throw new Error(`Failed to create Web3 signing credential: ${error.message}`);
45
+ }
46
+ }
47
+ /**
48
+ * Request signature from MetaMask
49
+ * Uses the same approach as normal Web3Connector
50
+ */
51
+ async requestSignature(address) {
52
+ try {
53
+ const signer = await this.web3Connector.getSigner();
54
+ const signerAddress = await signer.getAddress();
55
+ if (signerAddress.toLowerCase() !== address.toLowerCase()) {
56
+ throw new Error(`Signer address (${signerAddress}) does not match expected address (${address})`);
57
+ }
58
+ const signature = await signer.signMessage(this.MESSAGE_TO_SIGN);
59
+ return signature;
60
+ }
61
+ catch (error) {
62
+ console.error("Failed to request signature:", error);
63
+ throw error;
64
+ }
65
+ }
66
+ /**
67
+ * Creates an authenticator function compatible with SEA.sign
68
+ * This is the key function that makes it work like webauthn.js but for Web3
69
+ */
70
+ createAuthenticator(address) {
71
+ const credential = this.credentials.get(address.toLowerCase());
72
+ if (!credential) {
73
+ throw new Error(`Credential for address ${address} not found`);
74
+ }
75
+ return async (data) => {
76
+ try {
77
+ // Verify the user by requesting a new signature for the data
78
+ const signer = await this.web3Connector.getSigner();
79
+ const signerAddress = await signer.getAddress();
80
+ if (signerAddress.toLowerCase() !== address.toLowerCase()) {
81
+ throw new Error("Address mismatch during authentication");
82
+ }
83
+ // Sign the data
84
+ const dataToSign = JSON.stringify(data);
85
+ const signature = await signer.signMessage(dataToSign);
86
+ return signature;
87
+ }
88
+ catch (error) {
89
+ console.error("Web3 authentication error:", error);
90
+ throw error;
91
+ }
92
+ };
93
+ }
94
+ /**
95
+ * Creates a derived key pair from Web3 credential
96
+ * CONSISTENT with normal approach: uses password as seed
97
+ */
98
+ async createDerivedKeyPair(address, extra) {
99
+ // Use the deterministic approach instead of stored credentials
100
+ return this.createDerivedKeyPairFromAddress(address, extra);
101
+ }
102
+ /**
103
+ * Authenticate with existing pair (for login)
104
+ * This generates the deterministic pair from address and authenticates with GunDB
105
+ * GunDB will recognize the user because the pair is deterministic
106
+ */
107
+ async authenticateWithExistingPair(address, gunInstance) {
108
+ try {
109
+ console.log(`🔧 Web3Signer - authenticating with deterministic pair for address:`, address);
110
+ // Generate the deterministic pair directly from address (no need for stored credentials)
111
+ const derivedPair = await this.createDerivedKeyPairFromAddress(address);
112
+ console.log(`🔧 Web3Signer - deterministic pair created, attempting auth with GunDB`);
113
+ return new Promise((resolve) => {
114
+ // Authenticate directly with GunDB using the deterministic pair
115
+ gunInstance.user().auth(derivedPair, (authAck) => {
116
+ if (authAck.err) {
117
+ console.log(`🔧 Web3Signer - auth failed:`, authAck.err);
118
+ resolve({ success: false, error: authAck.err });
119
+ }
120
+ else {
121
+ const userPub = authAck.pub;
122
+ console.log(`🔧 Web3Signer - auth successful, userPub:`, userPub ? userPub.slice(0, 8) + "..." : "null");
123
+ resolve({ success: true, userPub });
124
+ }
125
+ });
126
+ });
127
+ }
128
+ catch (error) {
129
+ console.error("Error authenticating with deterministic pair:", error);
130
+ return { success: false, error: error.message };
131
+ }
132
+ }
133
+ /**
134
+ * Creates a derived key pair directly from address (deterministic)
135
+ * This ensures the same pair is generated every time for the same address
136
+ */
137
+ async createDerivedKeyPairFromAddress(address, extra) {
138
+ try {
139
+ // Generate deterministic password from address (same as createSigningCredential)
140
+ const validAddress = ethers.getAddress(address.toLowerCase());
141
+ const password = ethers.keccak256(ethers.toUtf8Bytes(`${validAddress.toLowerCase()}:shogun-web3`));
142
+ console.log(`🔧 Web3Signer - generating deterministic pair for address:`, validAddress);
143
+ // Use the same derive function as normal approach
144
+ const derivedKeys = await derive(password, // Deterministic password from address
145
+ extra, { includeP256: true });
146
+ return {
147
+ pub: derivedKeys.pub,
148
+ priv: derivedKeys.priv,
149
+ epub: derivedKeys.epub,
150
+ epriv: derivedKeys.epriv,
151
+ };
152
+ }
153
+ catch (error) {
154
+ console.error("Error creating derived key pair from address:", error);
155
+ throw error;
156
+ }
157
+ }
158
+ /**
159
+ * Creates a Gun user from Web3 credential
160
+ * This ensures the SAME user is created as with normal approach
161
+ * FIX: Use derived pair instead of username/password for GunDB auth
162
+ */
163
+ async createGunUser(address, gunInstance) {
164
+ try {
165
+ console.log(`🔧 Web3Signer - creating Gun user with deterministic pair for address:`, address);
166
+ // Generate the deterministic pair directly from address
167
+ const derivedPair = await this.createDerivedKeyPairFromAddress(address);
168
+ return new Promise((resolve) => {
169
+ // Use the derived pair directly for GunDB auth
170
+ gunInstance.user().create(derivedPair, (ack) => {
171
+ if (ack.err) {
172
+ console.log(`🔧 Web3Signer - user creation failed, trying auth:`, ack.err);
173
+ // Try to login if user already exists
174
+ gunInstance.user().auth(derivedPair, (authAck) => {
175
+ if (authAck.err) {
176
+ console.log(`🔧 Web3Signer - auth also failed:`, authAck.err);
177
+ resolve({ success: false, error: authAck.err });
178
+ }
179
+ else {
180
+ const userPub = authAck.pub;
181
+ console.log(`🔧 Web3Signer - auth successful, userPub:`, userPub ? userPub.slice(0, 8) + "..." : "null");
182
+ resolve({ success: true, userPub });
183
+ }
184
+ });
185
+ }
186
+ else {
187
+ console.log(`🔧 Web3Signer - user created successfully, now logging in`);
188
+ // User created, now login
189
+ gunInstance.user().auth(derivedPair, (authAck) => {
190
+ if (authAck.err) {
191
+ console.log(`🔧 Web3Signer - login after creation failed:`, authAck.err);
192
+ resolve({ success: false, error: authAck.err });
193
+ }
194
+ else {
195
+ const userPub = authAck.pub;
196
+ console.log(`🔧 Web3Signer - login successful, userPub:`, userPub ? userPub.slice(0, 8) + "..." : "null");
197
+ resolve({ success: true, userPub });
198
+ }
199
+ });
200
+ }
201
+ });
202
+ });
203
+ }
204
+ catch (error) {
205
+ console.error("Error creating Gun user:", error);
206
+ return { success: false, error: error.message };
207
+ }
208
+ }
209
+ /**
210
+ * Signs data using Web3 + derived keys
211
+ * This provides a hybrid approach: Web3 for user verification + derived keys for actual signing
212
+ * CONSISTENT with normal approach
213
+ */
214
+ async signWithDerivedKeys(data, address, extra) {
215
+ try {
216
+ // First, verify user with Web3
217
+ const authenticator = this.createAuthenticator(address);
218
+ await authenticator(data); // This verifies the user
219
+ // Then use derived keys for actual signing (CONSISTENT approach)
220
+ const keyPair = await this.createDerivedKeyPair(address, extra);
221
+ // Create signature using the same approach as SEA
222
+ const message = JSON.stringify(data);
223
+ const messageHash = ethers.keccak256(ethers.toUtf8Bytes(message));
224
+ // Use ethers for signing (compatible with SEA)
225
+ const wallet = new ethers.Wallet(keyPair.priv);
226
+ const signature = await wallet.signMessage(message);
227
+ // Format like SEA signature
228
+ const seaSignature = {
229
+ m: message,
230
+ s: signature,
231
+ };
232
+ return "SEA" + JSON.stringify(seaSignature);
233
+ }
234
+ catch (error) {
235
+ console.error("Error signing with derived keys:", error);
236
+ throw error;
237
+ }
238
+ }
239
+ /**
240
+ * Get the Gun user public key for a credential
241
+ * This allows checking if the same user would be created
242
+ */
243
+ async getGunUserPub(address) {
244
+ try {
245
+ // Generate the deterministic pair and return the public key
246
+ const derivedPair = await this.createDerivedKeyPairFromAddress(address);
247
+ return derivedPair.pub;
248
+ }
249
+ catch (error) {
250
+ console.error("Error getting Gun user pub:", error);
251
+ return undefined;
252
+ }
253
+ }
254
+ /**
255
+ * Get the password (for consistency checking)
256
+ */
257
+ getPassword(address) {
258
+ try {
259
+ // Generate deterministic password from address (same as createSigningCredential)
260
+ const validAddress = ethers.getAddress(address.toLowerCase());
261
+ const password = ethers.keccak256(ethers.toUtf8Bytes(`${validAddress.toLowerCase()}:shogun-web3`));
262
+ return password;
263
+ }
264
+ catch (error) {
265
+ console.error("Error getting password:", error);
266
+ return undefined;
267
+ }
268
+ }
269
+ /**
270
+ * Check if this credential would create the same Gun user as normal approach
271
+ */
272
+ async verifyConsistency(address, expectedUserPub) {
273
+ try {
274
+ // Generate the deterministic pair
275
+ const derivedKeys = await this.createDerivedKeyPairFromAddress(address);
276
+ return {
277
+ consistent: expectedUserPub
278
+ ? derivedKeys.pub === expectedUserPub
279
+ : true,
280
+ actualUserPub: derivedKeys.pub,
281
+ expectedUserPub,
282
+ };
283
+ }
284
+ catch (error) {
285
+ console.error("Error verifying consistency:", error);
286
+ return { consistent: false };
287
+ }
288
+ }
289
+ /**
290
+ * Get credential by address
291
+ */
292
+ getCredential(address) {
293
+ return this.credentials.get(address.toLowerCase());
294
+ }
295
+ /**
296
+ * List all stored credentials
297
+ */
298
+ listCredentials() {
299
+ return Array.from(this.credentials.values());
300
+ }
301
+ /**
302
+ * Remove a credential
303
+ */
304
+ removeCredential(address) {
305
+ return this.credentials.delete(address.toLowerCase());
306
+ }
307
+ }
308
+ export default Web3Signer;
@@ -0,0 +1,3 @@
1
+ export * from "./webauthnPlugin";
2
+ export * from "./types";
3
+ export * from "./webauthn";
@@ -0,0 +1,11 @@
1
+ /**
2
+ * WebAuthn event types
3
+ */
4
+ export var WebAuthnEventType;
5
+ (function (WebAuthnEventType) {
6
+ WebAuthnEventType["DEVICE_REGISTERED"] = "deviceRegistered";
7
+ WebAuthnEventType["DEVICE_REMOVED"] = "deviceRemoved";
8
+ WebAuthnEventType["AUTHENTICATION_SUCCESS"] = "authenticationSuccess";
9
+ WebAuthnEventType["AUTHENTICATION_FAILED"] = "authenticationFailed";
10
+ WebAuthnEventType["ERROR"] = "error";
11
+ })(WebAuthnEventType || (WebAuthnEventType = {}));