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,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,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 = {}));
|