shogun-core 4.2.4 → 4.2.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 +29 -500
- package/dist/browser/shogun-core.js.map +1 -1
- package/dist/core.js +11 -0
- package/dist/gundb/db.js +18 -500
- package/dist/types/core.d.ts +5 -0
- package/dist/types/gundb/db.d.ts +0 -77
- package/dist/types/interfaces/shogun.d.ts +1 -0
- package/package.json +1 -1
package/dist/core.js
CHANGED
|
@@ -41,6 +41,17 @@ class ShogunCore {
|
|
|
41
41
|
}
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Manually initialize the Shogun SDK (for testing or delayed initialization)
|
|
46
|
+
* @returns Promise that resolves when initialization is complete
|
|
47
|
+
*/
|
|
48
|
+
async initialize() {
|
|
49
|
+
// If already initialized, just wait a bit for any pending initialization
|
|
50
|
+
if (this._gun) {
|
|
51
|
+
return Promise.resolve();
|
|
52
|
+
}
|
|
53
|
+
return this.coreInitializer.initialize(this.config);
|
|
54
|
+
}
|
|
44
55
|
/**
|
|
45
56
|
* Access to the Gun instance
|
|
46
57
|
* @returns The Gun instance
|
package/dist/gundb/db.js
CHANGED
|
@@ -854,30 +854,22 @@ class DataBase {
|
|
|
854
854
|
}
|
|
855
855
|
// Set the user instance
|
|
856
856
|
this.user = this.gun.user();
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
pub: this.gun.user()._?.sea.pub,
|
|
874
|
-
priv: this.gun.user()._?.sea.priv,
|
|
875
|
-
epub: this.gun.user()._?.sea.epub,
|
|
876
|
-
epriv: this.gun.user()._?.sea.epriv,
|
|
877
|
-
}
|
|
878
|
-
: undefined,
|
|
879
|
-
};
|
|
880
|
-
}
|
|
857
|
+
console.log(`[SIGNUP] Signup completed successfully for user: ${username}`);
|
|
858
|
+
// Return the signup result
|
|
859
|
+
return {
|
|
860
|
+
success: true,
|
|
861
|
+
userPub: authResult.userPub,
|
|
862
|
+
username: username,
|
|
863
|
+
isNewUser: true,
|
|
864
|
+
sea: this.gun.user()?._?.sea
|
|
865
|
+
? {
|
|
866
|
+
pub: this.gun.user()._?.sea.pub,
|
|
867
|
+
priv: this.gun.user()._?.sea.priv,
|
|
868
|
+
epub: this.gun.user()._?.sea.epub,
|
|
869
|
+
epriv: this.gun.user()._?.sea.epriv,
|
|
870
|
+
}
|
|
871
|
+
: undefined,
|
|
872
|
+
};
|
|
881
873
|
}
|
|
882
874
|
catch (error) {
|
|
883
875
|
console.error(`Exception during signup for ${username}: ${error}`);
|
|
@@ -915,467 +907,6 @@ class DataBase {
|
|
|
915
907
|
resolve({ success: true, userPub: pair.pub });
|
|
916
908
|
});
|
|
917
909
|
}
|
|
918
|
-
async runPostAuthOnAuthResult(username, userPub, authResult) {
|
|
919
|
-
console.log(`[POSTAUTH] Starting post-auth setup for user: ${username}, userPub: ${userPub}`);
|
|
920
|
-
try {
|
|
921
|
-
console.log(`[POSTAUTH] Validating parameters for user: ${username}`);
|
|
922
|
-
// Validate required parameters
|
|
923
|
-
if (!username ||
|
|
924
|
-
typeof username !== "string" ||
|
|
925
|
-
username.trim().length === 0) {
|
|
926
|
-
console.error(`[POSTAUTH] Invalid username provided: ${username}`);
|
|
927
|
-
throw new Error("Invalid username provided");
|
|
928
|
-
}
|
|
929
|
-
if (!userPub ||
|
|
930
|
-
typeof userPub !== "string" ||
|
|
931
|
-
userPub.trim().length === 0) {
|
|
932
|
-
console.error("[POSTAUTH] Invalid userPub provided:", {
|
|
933
|
-
userPub,
|
|
934
|
-
type: typeof userPub,
|
|
935
|
-
authResult,
|
|
936
|
-
});
|
|
937
|
-
throw new Error("Invalid userPub provided");
|
|
938
|
-
}
|
|
939
|
-
// Additional validation for userPub format
|
|
940
|
-
if (!userPub.includes(".") || userPub.length < 10) {
|
|
941
|
-
console.error(`[POSTAUTH] Invalid userPub format: ${userPub}`);
|
|
942
|
-
throw new Error("Invalid userPub format");
|
|
943
|
-
}
|
|
944
|
-
console.log(`[POSTAUTH] Parameters validated for user: ${username}`);
|
|
945
|
-
// Normalize username to prevent path issues
|
|
946
|
-
const normalizedUsername = username.trim().toLowerCase();
|
|
947
|
-
if (normalizedUsername.length === 0) {
|
|
948
|
-
console.error(`[POSTAUTH] Normalized username is empty for user: ${username}`);
|
|
949
|
-
throw new Error("Username cannot be empty");
|
|
950
|
-
}
|
|
951
|
-
console.log(`[POSTAUTH] Normalized username: ${normalizedUsername}`);
|
|
952
|
-
console.log(`[POSTAUTH] Checking if user exists: ${userPub}`);
|
|
953
|
-
const existingUser = await this.gun.get(userPub).then();
|
|
954
|
-
const isNewUser = !existingUser || !existingUser.alias;
|
|
955
|
-
console.log(`[POSTAUTH] User is ${isNewUser ? "NEW" : "EXISTING"}: ${userPub}`);
|
|
956
|
-
// Get user's encryption public key (epub) for comprehensive tracking
|
|
957
|
-
const userInstance = this.gun.user();
|
|
958
|
-
const userSea = userInstance?._?.sea;
|
|
959
|
-
const epub = userSea?.epub;
|
|
960
|
-
console.log(`[POSTAUTH] User epub retrieved: ${epub ? "yes" : "no"}`);
|
|
961
|
-
// Enhanced user tracking system
|
|
962
|
-
console.log(`[POSTAUTH] Setting up comprehensive user tracking for: ${normalizedUsername}`);
|
|
963
|
-
const trackingResult = await this.setupComprehensiveUserTracking(normalizedUsername, userPub, epub);
|
|
964
|
-
if (!trackingResult) {
|
|
965
|
-
console.error(`[POSTAUTH] Comprehensive user tracking setup failed for: ${normalizedUsername}`);
|
|
966
|
-
return {
|
|
967
|
-
success: false,
|
|
968
|
-
error: "Comprehensive user tracking setup failed",
|
|
969
|
-
};
|
|
970
|
-
}
|
|
971
|
-
console.log(`[POSTAUTH] User tracking setup completed successfully for: ${normalizedUsername}`);
|
|
972
|
-
const result = {
|
|
973
|
-
success: true,
|
|
974
|
-
userPub: userPub,
|
|
975
|
-
username: normalizedUsername,
|
|
976
|
-
isNewUser: isNewUser,
|
|
977
|
-
// Get the SEA pair from the user object
|
|
978
|
-
sea: userSea
|
|
979
|
-
? {
|
|
980
|
-
pub: userSea.pub,
|
|
981
|
-
priv: userSea.priv,
|
|
982
|
-
epub: userSea.epub,
|
|
983
|
-
epriv: userSea.epriv,
|
|
984
|
-
}
|
|
985
|
-
: undefined,
|
|
986
|
-
};
|
|
987
|
-
console.log(`[POSTAUTH] Post-auth setup completed successfully for user: ${username}`);
|
|
988
|
-
return result;
|
|
989
|
-
}
|
|
990
|
-
catch (error) {
|
|
991
|
-
console.error(`[POSTAUTH] Error in post-authentication setup for ${username}:`, error);
|
|
992
|
-
return {
|
|
993
|
-
success: false,
|
|
994
|
-
error: `Post-authentication setup failed: ${error}`,
|
|
995
|
-
};
|
|
996
|
-
}
|
|
997
|
-
}
|
|
998
|
-
/**
|
|
999
|
-
* Sets up comprehensive user tracking system for agile user lookup
|
|
1000
|
-
* Creates multiple indexes for efficient user discovery
|
|
1001
|
-
*/
|
|
1002
|
-
async setupComprehensiveUserTracking(username, userPub, epub) {
|
|
1003
|
-
console.log(`[TRACKING] Starting comprehensive user tracking setup for: ${username}, userPub: ${userPub}`);
|
|
1004
|
-
try {
|
|
1005
|
-
// 1. Create alias index: ~@alias -> userPub (for GunDB compatibility)
|
|
1006
|
-
console.log(`[TRACKING] Step 1: Creating alias index for ${username}`);
|
|
1007
|
-
const aliasIndexResult = await this.createAliasIndex(username, userPub);
|
|
1008
|
-
if (!aliasIndexResult) {
|
|
1009
|
-
console.error(`[TRACKING] Failed to create alias index for ${username}`);
|
|
1010
|
-
return false;
|
|
1011
|
-
}
|
|
1012
|
-
console.log(`[TRACKING] Step 1 completed: Alias index created for ${username}`);
|
|
1013
|
-
// 2. Create username mapping: usernames/alias -> userPub
|
|
1014
|
-
console.log(`[TRACKING] Step 2: Creating username mapping for ${username}`);
|
|
1015
|
-
const usernameMappingResult = await this.createUsernameMapping(username, userPub);
|
|
1016
|
-
if (!usernameMappingResult) {
|
|
1017
|
-
console.error(`[TRACKING] Failed to create username mapping for ${username}`);
|
|
1018
|
-
return false;
|
|
1019
|
-
}
|
|
1020
|
-
console.log(`[TRACKING] Step 2 completed: Username mapping created for ${username}`);
|
|
1021
|
-
// 3. Create user registry: users/userPub -> user data
|
|
1022
|
-
console.log(`[TRACKING] Step 3: Creating user registry for ${username}`);
|
|
1023
|
-
const userRegistryResult = await this.createUserRegistry(username, userPub, epub);
|
|
1024
|
-
if (!userRegistryResult) {
|
|
1025
|
-
console.error(`[TRACKING] Failed to create user registry for ${username}`);
|
|
1026
|
-
return false;
|
|
1027
|
-
}
|
|
1028
|
-
console.log(`[TRACKING] Step 3 completed: User registry created for ${username}`);
|
|
1029
|
-
// 4. Create reverse lookup: userPub -> alias
|
|
1030
|
-
console.log(`[TRACKING] Step 4: Creating reverse lookup for ${username}`);
|
|
1031
|
-
const reverseLookupResult = await this.createReverseLookup(username, userPub);
|
|
1032
|
-
if (!reverseLookupResult) {
|
|
1033
|
-
console.error(`[TRACKING] Failed to create reverse lookup for ${username}`);
|
|
1034
|
-
return false;
|
|
1035
|
-
}
|
|
1036
|
-
console.log(`[TRACKING] Step 4 completed: Reverse lookup created for ${username}`);
|
|
1037
|
-
// 5. Create epub index: epubKeys/epub -> userPub (for encryption lookups)
|
|
1038
|
-
if (epub) {
|
|
1039
|
-
console.log(`[TRACKING] Step 5: Creating epub index for ${username}`);
|
|
1040
|
-
const epubIndexResult = await this.createEpubIndex(epub, userPub);
|
|
1041
|
-
if (!epubIndexResult) {
|
|
1042
|
-
console.error(`[TRACKING] Failed to create epub index for ${username}`);
|
|
1043
|
-
return false;
|
|
1044
|
-
}
|
|
1045
|
-
console.log(`[TRACKING] Step 5 completed: Epub index created for ${username}`);
|
|
1046
|
-
}
|
|
1047
|
-
else {
|
|
1048
|
-
console.log(`[TRACKING] Step 5 skipped: No epub available for ${username}`);
|
|
1049
|
-
}
|
|
1050
|
-
// 6. Create user metadata in user's own node
|
|
1051
|
-
console.log(`[TRACKING] Step 6: Creating user metadata for ${username}`);
|
|
1052
|
-
const userMetadataResult = await this.createUserMetadata(username, userPub, epub);
|
|
1053
|
-
if (!userMetadataResult) {
|
|
1054
|
-
console.error(`[TRACKING] Failed to create user metadata for ${username}`);
|
|
1055
|
-
return false;
|
|
1056
|
-
}
|
|
1057
|
-
console.log(`[TRACKING] Step 6 completed: User metadata created for ${username}`);
|
|
1058
|
-
console.log(`[TRACKING] Comprehensive user tracking setup completed successfully for: ${username}`);
|
|
1059
|
-
return true;
|
|
1060
|
-
}
|
|
1061
|
-
catch (error) {
|
|
1062
|
-
console.error(`[TRACKING] Error in comprehensive user tracking setup for ${username}:`, error);
|
|
1063
|
-
// Don't throw - continue with other operations
|
|
1064
|
-
return false;
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
/**
|
|
1068
|
-
* Creates alias index following GunDB pattern: ~@alias -> userPub
|
|
1069
|
-
*/
|
|
1070
|
-
async createAliasIndex(username, userPub) {
|
|
1071
|
-
try {
|
|
1072
|
-
const aliasNode = this.gun.get(`~@${username}`);
|
|
1073
|
-
// For Gun.js alias validation to pass, the data must be exactly equal to the key
|
|
1074
|
-
// The key is `~@${username}`, so we store that as the data
|
|
1075
|
-
return new Promise((resolve) => {
|
|
1076
|
-
aliasNode.put(`~@${username}`, (ack) => {
|
|
1077
|
-
if (ack && ack.err) {
|
|
1078
|
-
console.error(`Error creating alias index: ${ack.err}`);
|
|
1079
|
-
resolve(false);
|
|
1080
|
-
}
|
|
1081
|
-
else {
|
|
1082
|
-
resolve(true);
|
|
1083
|
-
}
|
|
1084
|
-
});
|
|
1085
|
-
});
|
|
1086
|
-
}
|
|
1087
|
-
catch (error) {
|
|
1088
|
-
console.error(`Error creating alias index: ${error}`);
|
|
1089
|
-
return false;
|
|
1090
|
-
}
|
|
1091
|
-
}
|
|
1092
|
-
/**
|
|
1093
|
-
* Creates username mapping: usernames/alias -> userPub
|
|
1094
|
-
*/
|
|
1095
|
-
async createUsernameMapping(username, userPub) {
|
|
1096
|
-
try {
|
|
1097
|
-
return new Promise((resolve) => {
|
|
1098
|
-
this.node
|
|
1099
|
-
.get("usernames")
|
|
1100
|
-
.get(username)
|
|
1101
|
-
.put(userPub, (ack) => {
|
|
1102
|
-
if (ack && ack.err) {
|
|
1103
|
-
console.error(`Error creating username mapping: ${ack.err}`);
|
|
1104
|
-
resolve(false);
|
|
1105
|
-
}
|
|
1106
|
-
else {
|
|
1107
|
-
resolve(true);
|
|
1108
|
-
}
|
|
1109
|
-
});
|
|
1110
|
-
});
|
|
1111
|
-
}
|
|
1112
|
-
catch (error) {
|
|
1113
|
-
console.error(`Error creating username mapping: ${error}`);
|
|
1114
|
-
return false;
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
/**
|
|
1118
|
-
* Creates user registry: users/userPub -> user data
|
|
1119
|
-
*/
|
|
1120
|
-
async createUserRegistry(username, userPub, epub) {
|
|
1121
|
-
try {
|
|
1122
|
-
const userData = {
|
|
1123
|
-
username: username,
|
|
1124
|
-
userPub: userPub,
|
|
1125
|
-
epub: epub,
|
|
1126
|
-
registeredAt: Date.now().toString(),
|
|
1127
|
-
lastSeen: Date.now().toString(),
|
|
1128
|
-
};
|
|
1129
|
-
return new Promise((resolve) => {
|
|
1130
|
-
this.node
|
|
1131
|
-
.get("users")
|
|
1132
|
-
.get(userPub)
|
|
1133
|
-
.put(userData, (ack) => {
|
|
1134
|
-
if (ack && ack.err) {
|
|
1135
|
-
console.error(`Error creating user registry: ${ack.err}`);
|
|
1136
|
-
resolve(false);
|
|
1137
|
-
}
|
|
1138
|
-
else {
|
|
1139
|
-
resolve(true);
|
|
1140
|
-
}
|
|
1141
|
-
});
|
|
1142
|
-
});
|
|
1143
|
-
}
|
|
1144
|
-
catch (error) {
|
|
1145
|
-
console.error(`Error creating user registry: ${error}`);
|
|
1146
|
-
return false;
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
/**
|
|
1150
|
-
* Creates reverse lookup: userPub -> alias
|
|
1151
|
-
*/
|
|
1152
|
-
async createReverseLookup(username, userPub) {
|
|
1153
|
-
try {
|
|
1154
|
-
const ack = await this.node
|
|
1155
|
-
.get("userAliases")
|
|
1156
|
-
.get(userPub)
|
|
1157
|
-
.put(username)
|
|
1158
|
-
.then();
|
|
1159
|
-
if (ack.err) {
|
|
1160
|
-
console.error(`Error creating reverse lookup: ${ack.err}`);
|
|
1161
|
-
return false;
|
|
1162
|
-
}
|
|
1163
|
-
else {
|
|
1164
|
-
return true;
|
|
1165
|
-
}
|
|
1166
|
-
}
|
|
1167
|
-
catch (error) {
|
|
1168
|
-
console.error(`Error creating reverse lookup: ${error}`);
|
|
1169
|
-
return false;
|
|
1170
|
-
}
|
|
1171
|
-
}
|
|
1172
|
-
/**
|
|
1173
|
-
* Creates epub index: epubKeys/epub -> userPub
|
|
1174
|
-
*/
|
|
1175
|
-
async createEpubIndex(epub, userPub) {
|
|
1176
|
-
try {
|
|
1177
|
-
return new Promise((resolve) => {
|
|
1178
|
-
this.node
|
|
1179
|
-
.get("epubKeys")
|
|
1180
|
-
.get(epub)
|
|
1181
|
-
.put(userPub, (ack) => {
|
|
1182
|
-
if (ack && ack.err) {
|
|
1183
|
-
console.error(`Error creating epub index: ${ack.err}`);
|
|
1184
|
-
resolve(false);
|
|
1185
|
-
}
|
|
1186
|
-
else {
|
|
1187
|
-
resolve(true);
|
|
1188
|
-
}
|
|
1189
|
-
});
|
|
1190
|
-
});
|
|
1191
|
-
}
|
|
1192
|
-
catch (error) {
|
|
1193
|
-
console.error(`Error creating epub index: ${error}`);
|
|
1194
|
-
return false;
|
|
1195
|
-
}
|
|
1196
|
-
}
|
|
1197
|
-
/**
|
|
1198
|
-
* Creates user metadata in user's own node
|
|
1199
|
-
*/
|
|
1200
|
-
async createUserMetadata(username, userPub, epub) {
|
|
1201
|
-
try {
|
|
1202
|
-
const userMetadata = {
|
|
1203
|
-
username: username,
|
|
1204
|
-
epub: epub,
|
|
1205
|
-
registeredAt: Date.now(),
|
|
1206
|
-
lastSeen: Date.now(),
|
|
1207
|
-
};
|
|
1208
|
-
return new Promise((resolve) => {
|
|
1209
|
-
this.gun.get(userPub).put(userMetadata, (ack) => {
|
|
1210
|
-
if (ack && ack.err) {
|
|
1211
|
-
console.error(`Error creating user metadata: ${ack.err}`);
|
|
1212
|
-
resolve(false);
|
|
1213
|
-
}
|
|
1214
|
-
else {
|
|
1215
|
-
resolve(true);
|
|
1216
|
-
}
|
|
1217
|
-
});
|
|
1218
|
-
});
|
|
1219
|
-
}
|
|
1220
|
-
catch (error) {
|
|
1221
|
-
console.error(`Error creating user metadata: ${error}`);
|
|
1222
|
-
return false;
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
/**
|
|
1226
|
-
* Gets user information by alias using the comprehensive tracking system
|
|
1227
|
-
* @param alias Username/alias to lookup
|
|
1228
|
-
* @returns Promise resolving to user information or null if not found
|
|
1229
|
-
*/
|
|
1230
|
-
async getUserByAlias(alias) {
|
|
1231
|
-
try {
|
|
1232
|
-
const normalizedAlias = alias.trim().toLowerCase();
|
|
1233
|
-
if (!normalizedAlias) {
|
|
1234
|
-
return null;
|
|
1235
|
-
}
|
|
1236
|
-
// Method 1: Try GunDB standard alias lookup (~@alias)
|
|
1237
|
-
try {
|
|
1238
|
-
const aliasData = await this.gun.get(`~@${normalizedAlias}`).then();
|
|
1239
|
-
if (aliasData && aliasData["~pubKeyOfUser"]) {
|
|
1240
|
-
const userPub = aliasData["~pubKeyOfUser"]["#"] || aliasData["~pubKeyOfUser"];
|
|
1241
|
-
if (userPub) {
|
|
1242
|
-
const userData = await this.getUserDataByPub(userPub);
|
|
1243
|
-
if (userData) {
|
|
1244
|
-
return userData;
|
|
1245
|
-
}
|
|
1246
|
-
}
|
|
1247
|
-
}
|
|
1248
|
-
}
|
|
1249
|
-
catch (error) {
|
|
1250
|
-
console.error(`GunDB alias lookup failed for ${normalizedAlias}:`, error);
|
|
1251
|
-
}
|
|
1252
|
-
// Method 2: Try username mapping (usernames/alias -> userPub)
|
|
1253
|
-
try {
|
|
1254
|
-
const userPub = await this.node
|
|
1255
|
-
.get("usernames")
|
|
1256
|
-
.get(normalizedAlias)
|
|
1257
|
-
.then();
|
|
1258
|
-
if (userPub) {
|
|
1259
|
-
const userData = await this.getUserDataByPub(userPub);
|
|
1260
|
-
if (userData) {
|
|
1261
|
-
return userData;
|
|
1262
|
-
}
|
|
1263
|
-
}
|
|
1264
|
-
}
|
|
1265
|
-
catch (error) {
|
|
1266
|
-
console.error(`Username mapping lookup failed for ${normalizedAlias}:`, error);
|
|
1267
|
-
}
|
|
1268
|
-
return null;
|
|
1269
|
-
}
|
|
1270
|
-
catch (error) {
|
|
1271
|
-
console.error(`Error looking up user by alias ${alias}:`, error);
|
|
1272
|
-
return null;
|
|
1273
|
-
}
|
|
1274
|
-
}
|
|
1275
|
-
/**
|
|
1276
|
-
* Gets user information by public key
|
|
1277
|
-
* @param userPub User's public key
|
|
1278
|
-
* @returns Promise resolving to user information or null if not found
|
|
1279
|
-
*/
|
|
1280
|
-
async getUserDataByPub(userPub) {
|
|
1281
|
-
try {
|
|
1282
|
-
if (!userPub || typeof userPub !== "string") {
|
|
1283
|
-
return null;
|
|
1284
|
-
}
|
|
1285
|
-
// Method 1: Try user registry (users/userPub -> user data)
|
|
1286
|
-
try {
|
|
1287
|
-
const userData = await this.node.get("users").get(userPub).then();
|
|
1288
|
-
if (userData && userData.username) {
|
|
1289
|
-
return {
|
|
1290
|
-
userPub: userData.userPub || userPub,
|
|
1291
|
-
epub: userData.epub || null,
|
|
1292
|
-
username: userData.username,
|
|
1293
|
-
registeredAt: userData.registeredAt || 0,
|
|
1294
|
-
lastSeen: userData.lastSeen || 0,
|
|
1295
|
-
};
|
|
1296
|
-
}
|
|
1297
|
-
}
|
|
1298
|
-
catch (error) {
|
|
1299
|
-
console.error(`User registry lookup failed for ${userPub}:`, error);
|
|
1300
|
-
}
|
|
1301
|
-
// Method 2: Try user's own node
|
|
1302
|
-
try {
|
|
1303
|
-
const userNodeData = await this.gun.get(userPub).then();
|
|
1304
|
-
if (userNodeData && userNodeData.username) {
|
|
1305
|
-
return {
|
|
1306
|
-
userPub: userPub,
|
|
1307
|
-
epub: userNodeData.epub || null,
|
|
1308
|
-
username: userNodeData.username,
|
|
1309
|
-
registeredAt: userNodeData.registeredAt || 0,
|
|
1310
|
-
lastSeen: userNodeData.lastSeen || 0,
|
|
1311
|
-
};
|
|
1312
|
-
}
|
|
1313
|
-
}
|
|
1314
|
-
catch (error) {
|
|
1315
|
-
console.error(`User node lookup failed for ${userPub}:`, error);
|
|
1316
|
-
}
|
|
1317
|
-
return null;
|
|
1318
|
-
}
|
|
1319
|
-
catch (error) {
|
|
1320
|
-
console.error(`Error looking up user data by pub ${userPub}:`, error);
|
|
1321
|
-
return null;
|
|
1322
|
-
}
|
|
1323
|
-
}
|
|
1324
|
-
/**
|
|
1325
|
-
* Gets user public key by encryption public key (epub)
|
|
1326
|
-
* @param epub User's encryption public key
|
|
1327
|
-
* @returns Promise resolving to user public key or null if not found
|
|
1328
|
-
*/
|
|
1329
|
-
async getUserPubByEpub(epub) {
|
|
1330
|
-
try {
|
|
1331
|
-
if (!epub || typeof epub !== "string") {
|
|
1332
|
-
return null;
|
|
1333
|
-
}
|
|
1334
|
-
const userPub = await this.node.get("epubKeys").get(epub).then();
|
|
1335
|
-
return userPub || null;
|
|
1336
|
-
}
|
|
1337
|
-
catch (error) {
|
|
1338
|
-
console.error(`Error looking up user pub by epub ${epub}:`, error);
|
|
1339
|
-
return null;
|
|
1340
|
-
}
|
|
1341
|
-
}
|
|
1342
|
-
/**
|
|
1343
|
-
* Gets user alias by public key
|
|
1344
|
-
* @param userPub User's public key
|
|
1345
|
-
* @returns Promise resolving to user alias or null if not found
|
|
1346
|
-
*/
|
|
1347
|
-
async getUserAliasByPub(userPub) {
|
|
1348
|
-
try {
|
|
1349
|
-
if (!userPub || typeof userPub !== "string") {
|
|
1350
|
-
return null;
|
|
1351
|
-
}
|
|
1352
|
-
const alias = await this.node.get("userAliases").get(userPub).then();
|
|
1353
|
-
return alias || null;
|
|
1354
|
-
}
|
|
1355
|
-
catch (error) {
|
|
1356
|
-
console.error(`Error looking up user alias by pub ${userPub}:`, error);
|
|
1357
|
-
return null;
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
/**
|
|
1361
|
-
* Gets all registered users (for admin purposes)
|
|
1362
|
-
* @returns Promise resolving to array of user information
|
|
1363
|
-
*/
|
|
1364
|
-
async getAllRegisteredUsers() {
|
|
1365
|
-
try {
|
|
1366
|
-
const users = [];
|
|
1367
|
-
// Get all users from the users registry
|
|
1368
|
-
const usersNode = this.node.get("users");
|
|
1369
|
-
// Note: This is a simplified approach. In a real implementation,
|
|
1370
|
-
// you might want to use Gun's map functionality or iterate through
|
|
1371
|
-
// known user public keys
|
|
1372
|
-
return users;
|
|
1373
|
-
}
|
|
1374
|
-
catch (error) {
|
|
1375
|
-
console.error(`Error getting all registered users:`, error);
|
|
1376
|
-
return [];
|
|
1377
|
-
}
|
|
1378
|
-
}
|
|
1379
910
|
/**
|
|
1380
911
|
* Updates user's last seen timestamp
|
|
1381
912
|
* @param userPub User's public key
|
|
@@ -1490,17 +1021,7 @@ class DataBase {
|
|
|
1490
1021
|
error: "Authentication failed: No user pub returned.",
|
|
1491
1022
|
};
|
|
1492
1023
|
}
|
|
1493
|
-
|
|
1494
|
-
try {
|
|
1495
|
-
await this.runPostAuthOnAuthResult(alias, userPub, {
|
|
1496
|
-
success: true,
|
|
1497
|
-
userPub: userPub,
|
|
1498
|
-
});
|
|
1499
|
-
}
|
|
1500
|
-
catch (postAuthError) {
|
|
1501
|
-
console.error(`Post-auth error during login: ${postAuthError}`);
|
|
1502
|
-
// Continue with login even if post-auth fails
|
|
1503
|
-
}
|
|
1024
|
+
console.log(`[LOGIN] Login completed successfully for user: ${username}`);
|
|
1504
1025
|
// Update user's last seen timestamp
|
|
1505
1026
|
try {
|
|
1506
1027
|
await this.updateUserLastSeen(userPub);
|
|
@@ -1543,10 +1064,7 @@ class DataBase {
|
|
|
1543
1064
|
error: `User '${username}' not found. Please check your username or register first.`,
|
|
1544
1065
|
};
|
|
1545
1066
|
}
|
|
1546
|
-
|
|
1547
|
-
success: true,
|
|
1548
|
-
userPub: pair.pub,
|
|
1549
|
-
});
|
|
1067
|
+
console.log(`[LOGIN] Login with pair completed for user: ${username}`);
|
|
1550
1068
|
try {
|
|
1551
1069
|
await this.updateUserLastSeen(pair.pub);
|
|
1552
1070
|
}
|
package/dist/types/core.d.ts
CHANGED
|
@@ -40,6 +40,11 @@ export declare class ShogunCore implements IShogunCore {
|
|
|
40
40
|
* and plugin system.
|
|
41
41
|
*/
|
|
42
42
|
constructor(config: ShogunCoreConfig);
|
|
43
|
+
/**
|
|
44
|
+
* Manually initialize the Shogun SDK (for testing or delayed initialization)
|
|
45
|
+
* @returns Promise that resolves when initialization is complete
|
|
46
|
+
*/
|
|
47
|
+
initialize(): Promise<void>;
|
|
43
48
|
/**
|
|
44
49
|
* Access to the Gun instance
|
|
45
50
|
* @returns The Gun instance
|
package/dist/types/gundb/db.d.ts
CHANGED
|
@@ -185,83 +185,6 @@ declare class DataBase {
|
|
|
185
185
|
* Creates a new user in Gun with pair-based authentication (for Web3/plugins)
|
|
186
186
|
*/
|
|
187
187
|
private createNewUserWithPair;
|
|
188
|
-
private runPostAuthOnAuthResult;
|
|
189
|
-
/**
|
|
190
|
-
* Sets up comprehensive user tracking system for agile user lookup
|
|
191
|
-
* Creates multiple indexes for efficient user discovery
|
|
192
|
-
*/
|
|
193
|
-
private setupComprehensiveUserTracking;
|
|
194
|
-
/**
|
|
195
|
-
* Creates alias index following GunDB pattern: ~@alias -> userPub
|
|
196
|
-
*/
|
|
197
|
-
private createAliasIndex;
|
|
198
|
-
/**
|
|
199
|
-
* Creates username mapping: usernames/alias -> userPub
|
|
200
|
-
*/
|
|
201
|
-
private createUsernameMapping;
|
|
202
|
-
/**
|
|
203
|
-
* Creates user registry: users/userPub -> user data
|
|
204
|
-
*/
|
|
205
|
-
private createUserRegistry;
|
|
206
|
-
/**
|
|
207
|
-
* Creates reverse lookup: userPub -> alias
|
|
208
|
-
*/
|
|
209
|
-
private createReverseLookup;
|
|
210
|
-
/**
|
|
211
|
-
* Creates epub index: epubKeys/epub -> userPub
|
|
212
|
-
*/
|
|
213
|
-
private createEpubIndex;
|
|
214
|
-
/**
|
|
215
|
-
* Creates user metadata in user's own node
|
|
216
|
-
*/
|
|
217
|
-
private createUserMetadata;
|
|
218
|
-
/**
|
|
219
|
-
* Gets user information by alias using the comprehensive tracking system
|
|
220
|
-
* @param alias Username/alias to lookup
|
|
221
|
-
* @returns Promise resolving to user information or null if not found
|
|
222
|
-
*/
|
|
223
|
-
getUserByAlias(alias: string): Promise<{
|
|
224
|
-
userPub: string;
|
|
225
|
-
epub: string | null;
|
|
226
|
-
username: string;
|
|
227
|
-
registeredAt: number;
|
|
228
|
-
lastSeen: number;
|
|
229
|
-
} | null>;
|
|
230
|
-
/**
|
|
231
|
-
* Gets user information by public key
|
|
232
|
-
* @param userPub User's public key
|
|
233
|
-
* @returns Promise resolving to user information or null if not found
|
|
234
|
-
*/
|
|
235
|
-
getUserDataByPub(userPub: string): Promise<{
|
|
236
|
-
userPub: string;
|
|
237
|
-
epub: string | null;
|
|
238
|
-
username: string;
|
|
239
|
-
registeredAt: number;
|
|
240
|
-
lastSeen: number;
|
|
241
|
-
} | null>;
|
|
242
|
-
/**
|
|
243
|
-
* Gets user public key by encryption public key (epub)
|
|
244
|
-
* @param epub User's encryption public key
|
|
245
|
-
* @returns Promise resolving to user public key or null if not found
|
|
246
|
-
*/
|
|
247
|
-
getUserPubByEpub(epub: string): Promise<string | null>;
|
|
248
|
-
/**
|
|
249
|
-
* Gets user alias by public key
|
|
250
|
-
* @param userPub User's public key
|
|
251
|
-
* @returns Promise resolving to user alias or null if not found
|
|
252
|
-
*/
|
|
253
|
-
getUserAliasByPub(userPub: string): Promise<string | null>;
|
|
254
|
-
/**
|
|
255
|
-
* Gets all registered users (for admin purposes)
|
|
256
|
-
* @returns Promise resolving to array of user information
|
|
257
|
-
*/
|
|
258
|
-
getAllRegisteredUsers(): Promise<Array<{
|
|
259
|
-
userPub: string;
|
|
260
|
-
epub: string | null;
|
|
261
|
-
username: string;
|
|
262
|
-
registeredAt: number;
|
|
263
|
-
lastSeen: number;
|
|
264
|
-
}>>;
|
|
265
188
|
/**
|
|
266
189
|
* Updates user's last seen timestamp
|
|
267
190
|
* @param userPub User's public key
|
|
@@ -123,6 +123,7 @@ export interface IShogunCore extends PluginManager {
|
|
|
123
123
|
removeAllListeners(eventName?: string | symbol): this;
|
|
124
124
|
emit<K extends keyof ShogunEventMap>(eventName: K, data?: ShogunEventMap[K] extends void ? never : ShogunEventMap[K]): boolean;
|
|
125
125
|
getRecentErrors(count?: number): ShogunError[];
|
|
126
|
+
initialize(): Promise<void>;
|
|
126
127
|
login(username: string, password: string, pair?: ISEAPair | null): Promise<AuthResult>;
|
|
127
128
|
loginWithPair(username: string, pair: ISEAPair): Promise<AuthResult>;
|
|
128
129
|
signUp(username: string, password?: string, pair?: ISEAPair | null): Promise<SignUpResult>;
|