shogun-core 6.2.3 → 6.3.0

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 (233) hide show
  1. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js +93 -341
  2. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -1
  3. package/dist/browser/shogun-core.js +138850 -146638
  4. package/dist/browser/shogun-core.js.map +1 -1
  5. package/dist/{core.js → src/core.js} +167 -107
  6. package/dist/src/crypto/asymmetric.js +168 -0
  7. package/dist/src/crypto/double-ratchet.js +908 -0
  8. package/dist/src/crypto/file-encryption.js +352 -0
  9. package/dist/src/crypto/hashing.js +160 -0
  10. package/dist/src/crypto/index.js +18 -0
  11. package/dist/{crypto → src/crypto}/mls-codec.js +24 -34
  12. package/dist/src/crypto/mls.js +734 -0
  13. package/dist/src/crypto/pgp.js +619 -0
  14. package/dist/{crypto → src/crypto}/random-generation.js +125 -103
  15. package/dist/src/crypto/sframe.js +466 -0
  16. package/dist/src/crypto/signal-protocol.js +943 -0
  17. package/dist/src/crypto/symmetric.js +165 -0
  18. package/dist/src/crypto/utils.js +220 -0
  19. package/dist/src/examples/auth-test.js +535 -0
  20. package/dist/src/examples/crypto-identity-example.js +294 -0
  21. package/dist/src/examples/crypto-working-test.js +149 -0
  22. package/dist/src/examples/double-ratchet-test.js +240 -0
  23. package/dist/src/examples/mls-3-member-test.js +183 -0
  24. package/dist/src/examples/mls-multi-member.js +439 -0
  25. package/dist/src/examples/mls-sframe-test.js +491 -0
  26. package/dist/src/examples/mls-simple-test.js +122 -0
  27. package/dist/src/examples/pgp-example.js +354 -0
  28. package/dist/src/examples/random-generation-test.js +191 -0
  29. package/dist/src/examples/shogun-core-example.js +204 -0
  30. package/dist/src/examples/signal-protocol-test.js +82 -0
  31. package/dist/src/examples/zkproof-credentials-example.js +357 -0
  32. package/dist/src/examples/zkproof-example.js +357 -0
  33. package/dist/src/gundb/crypto.js +420 -0
  34. package/dist/src/gundb/db.js +728 -0
  35. package/dist/src/gundb/derive.js +327 -0
  36. package/dist/src/gundb/errors.js +115 -0
  37. package/dist/src/gundb/gun-es.js +8 -0
  38. package/dist/src/gundb/index.js +5 -0
  39. package/dist/{gundb → src/gundb}/rxjs.js +147 -111
  40. package/dist/{gundb → src/gundb}/types.js +1 -2
  41. package/dist/src/index.js +19 -0
  42. package/dist/src/interfaces/events.js +57 -0
  43. package/dist/{interfaces → src/interfaces}/shogun.js +4 -7
  44. package/dist/src/managers/AuthManager.js +301 -0
  45. package/dist/src/managers/CoreInitializer.js +304 -0
  46. package/dist/src/managers/CryptoIdentityManager.js +230 -0
  47. package/dist/{managers → src/managers}/EventManager.js +19 -21
  48. package/dist/{managers → src/managers}/PluginManager.js +123 -89
  49. package/dist/src/plugins/base.js +90 -0
  50. package/dist/src/plugins/index.js +17 -0
  51. package/dist/src/plugins/nostr/index.js +4 -0
  52. package/dist/src/plugins/nostr/nostrConnector.js +539 -0
  53. package/dist/src/plugins/nostr/nostrConnectorPlugin.js +663 -0
  54. package/dist/src/plugins/nostr/nostrSigner.js +414 -0
  55. package/dist/src/plugins/smartwallet/index.js +2 -0
  56. package/dist/src/plugins/smartwallet/smartWalletPlugin.js +824 -0
  57. package/dist/src/plugins/web3/index.js +4 -0
  58. package/dist/src/plugins/web3/types.js +1 -0
  59. package/dist/src/plugins/web3/web3Connector.js +738 -0
  60. package/dist/src/plugins/web3/web3ConnectorPlugin.js +639 -0
  61. package/dist/src/plugins/web3/web3Signer.js +432 -0
  62. package/dist/src/plugins/webauthn/index.js +3 -0
  63. package/dist/{plugins → src/plugins}/webauthn/types.js +2 -5
  64. package/dist/src/plugins/webauthn/webauthn.js +647 -0
  65. package/dist/src/plugins/webauthn/webauthnPlugin.js +689 -0
  66. package/dist/src/plugins/webauthn/webauthnSigner.js +419 -0
  67. package/dist/{plugins → src/plugins}/zkproof/index.js +3 -10
  68. package/dist/src/plugins/zkproof/types.js +1 -0
  69. package/dist/src/plugins/zkproof/zkCredentials.js +287 -0
  70. package/dist/src/plugins/zkproof/zkProofConnector.js +267 -0
  71. package/dist/src/plugins/zkproof/zkProofPlugin.js +405 -0
  72. package/dist/src/storage/storage.js +189 -0
  73. package/dist/src/utils/errorHandler.js +339 -0
  74. package/dist/{utils → src/utils}/eventEmitter.js +26 -26
  75. package/dist/{utils → src/utils}/seedPhrase.js +23 -32
  76. package/dist/{utils → src/utils}/validation.js +14 -21
  77. package/dist/tsconfig.tsbuildinfo +1 -0
  78. package/dist/types/{crypto → src/crypto}/double-ratchet.d.ts +1 -1
  79. package/dist/types/{crypto → src/crypto}/signal-protocol.d.ts +25 -0
  80. package/dist/types/{crypto → src/crypto}/types.d.ts +3 -1
  81. package/dist/types/src/examples/crypto-working-test.d.ts +1 -0
  82. package/dist/types/src/examples/double-ratchet-test.d.ts +1 -0
  83. package/dist/types/src/examples/mls-sframe-test.d.ts +1 -0
  84. package/dist/types/src/examples/random-generation-test.d.ts +1 -0
  85. package/dist/types/src/examples/signal-protocol-test.d.ts +1 -0
  86. package/dist/types/{gundb → src/gundb}/db.d.ts +14 -1
  87. package/dist/types/src/gundb/gun-es.d.ts +8 -0
  88. package/dist/types/src/gundb/min.d.ts +3 -0
  89. package/dist/types/{index.d.ts → src/index.d.ts} +1 -0
  90. package/package.json +14 -11
  91. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +0 -1651
  92. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +0 -1
  93. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +0 -1608
  94. package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +0 -1
  95. package/dist/crypto/asymmetric.js +0 -99
  96. package/dist/crypto/double-ratchet.js +0 -370
  97. package/dist/crypto/file-encryption.js +0 -213
  98. package/dist/crypto/hashing.js +0 -87
  99. package/dist/crypto/index.js +0 -34
  100. package/dist/crypto/mls.js +0 -569
  101. package/dist/crypto/pgp.js +0 -390
  102. package/dist/crypto/sframe.js +0 -352
  103. package/dist/crypto/signal-protocol.js +0 -456
  104. package/dist/crypto/symmetric.js +0 -91
  105. package/dist/crypto/types.js +0 -2
  106. package/dist/crypto/utils.js +0 -140
  107. package/dist/examples/auth-test.js +0 -453
  108. package/dist/examples/crypto-identity-example.js +0 -196
  109. package/dist/examples/crypto-working-test.js +0 -83
  110. package/dist/examples/double-ratchet-test.js +0 -155
  111. package/dist/examples/mls-3-member-test.js +0 -97
  112. package/dist/examples/mls-multi-member.js +0 -153
  113. package/dist/examples/mls-sframe-test.js +0 -307
  114. package/dist/examples/mls-simple-test.js +0 -58
  115. package/dist/examples/pgp-example.js +0 -200
  116. package/dist/examples/random-generation-test.js +0 -151
  117. package/dist/examples/shogun-core-example.js +0 -150
  118. package/dist/examples/signal-protocol-test.js +0 -38
  119. package/dist/examples/zkproof-credentials-example.js +0 -217
  120. package/dist/examples/zkproof-example.js +0 -242
  121. package/dist/gundb/crypto.js +0 -306
  122. package/dist/gundb/db.js +0 -485
  123. package/dist/gundb/derive.js +0 -232
  124. package/dist/gundb/errors.js +0 -76
  125. package/dist/gundb/gun-es.js +0 -12
  126. package/dist/gundb/index.js +0 -21
  127. package/dist/gundb/min.js +0 -10
  128. package/dist/index.esm.js +0 -22
  129. package/dist/index.js +0 -47
  130. package/dist/interfaces/common.js +0 -2
  131. package/dist/interfaces/events.js +0 -40
  132. package/dist/interfaces/plugin.js +0 -2
  133. package/dist/managers/AuthManager.js +0 -226
  134. package/dist/managers/CoreInitializer.js +0 -250
  135. package/dist/managers/CryptoIdentityManager.js +0 -138
  136. package/dist/plugins/base.js +0 -50
  137. package/dist/plugins/index.js +0 -32
  138. package/dist/plugins/nostr/index.js +0 -20
  139. package/dist/plugins/nostr/nostrConnector.js +0 -419
  140. package/dist/plugins/nostr/nostrConnectorPlugin.js +0 -453
  141. package/dist/plugins/nostr/nostrSigner.js +0 -319
  142. package/dist/plugins/nostr/types.js +0 -2
  143. package/dist/plugins/smartwallet/index.js +0 -18
  144. package/dist/plugins/smartwallet/smartWalletPlugin.js +0 -511
  145. package/dist/plugins/smartwallet/types.js +0 -2
  146. package/dist/plugins/web3/index.js +0 -20
  147. package/dist/plugins/web3/types.js +0 -2
  148. package/dist/plugins/web3/web3Connector.js +0 -533
  149. package/dist/plugins/web3/web3ConnectorPlugin.js +0 -455
  150. package/dist/plugins/web3/web3Signer.js +0 -314
  151. package/dist/plugins/webauthn/index.js +0 -19
  152. package/dist/plugins/webauthn/webauthn.js +0 -496
  153. package/dist/plugins/webauthn/webauthnPlugin.js +0 -490
  154. package/dist/plugins/webauthn/webauthnSigner.js +0 -310
  155. package/dist/plugins/zkproof/types.js +0 -2
  156. package/dist/plugins/zkproof/zkCredentials.js +0 -216
  157. package/dist/plugins/zkproof/zkProofConnector.js +0 -198
  158. package/dist/plugins/zkproof/zkProofPlugin.js +0 -272
  159. package/dist/storage/storage.js +0 -145
  160. package/dist/types/gundb/gun-es.d.ts +0 -8
  161. package/dist/utils/errorHandler.js +0 -246
  162. /package/dist/{types/examples/crypto-working-test.d.ts → src/crypto/types.js} +0 -0
  163. /package/dist/{types/gundb/min.d.ts → src/gundb/min.js} +0 -0
  164. /package/dist/{types/examples/double-ratchet-test.d.ts → src/interfaces/common.js} +0 -0
  165. /package/dist/{types/examples/mls-sframe-test.d.ts → src/interfaces/plugin.js} +0 -0
  166. /package/dist/{types/examples/random-generation-test.d.ts → src/plugins/nostr/types.js} +0 -0
  167. /package/dist/{types/examples/signal-protocol-test.d.ts → src/plugins/smartwallet/types.js} +0 -0
  168. /package/dist/types/{core.d.ts → src/core.d.ts} +0 -0
  169. /package/dist/types/{crypto → src/crypto}/asymmetric.d.ts +0 -0
  170. /package/dist/types/{crypto → src/crypto}/file-encryption.d.ts +0 -0
  171. /package/dist/types/{crypto → src/crypto}/hashing.d.ts +0 -0
  172. /package/dist/types/{crypto → src/crypto}/index.d.ts +0 -0
  173. /package/dist/types/{crypto → src/crypto}/mls-codec.d.ts +0 -0
  174. /package/dist/types/{crypto → src/crypto}/mls.d.ts +0 -0
  175. /package/dist/types/{crypto → src/crypto}/pgp.d.ts +0 -0
  176. /package/dist/types/{crypto → src/crypto}/random-generation.d.ts +0 -0
  177. /package/dist/types/{crypto → src/crypto}/sframe.d.ts +0 -0
  178. /package/dist/types/{crypto → src/crypto}/symmetric.d.ts +0 -0
  179. /package/dist/types/{crypto → src/crypto}/utils.d.ts +0 -0
  180. /package/dist/types/{examples → src/examples}/auth-test.d.ts +0 -0
  181. /package/dist/types/{examples → src/examples}/crypto-identity-example.d.ts +0 -0
  182. /package/dist/types/{examples → src/examples}/mls-3-member-test.d.ts +0 -0
  183. /package/dist/types/{examples → src/examples}/mls-multi-member.d.ts +0 -0
  184. /package/dist/types/{examples → src/examples}/mls-simple-test.d.ts +0 -0
  185. /package/dist/types/{examples → src/examples}/pgp-example.d.ts +0 -0
  186. /package/dist/types/{examples → src/examples}/shogun-core-example.d.ts +0 -0
  187. /package/dist/types/{examples → src/examples}/zkproof-credentials-example.d.ts +0 -0
  188. /package/dist/types/{examples → src/examples}/zkproof-example.d.ts +0 -0
  189. /package/dist/types/{gundb → src/gundb}/crypto.d.ts +0 -0
  190. /package/dist/types/{gundb → src/gundb}/derive.d.ts +0 -0
  191. /package/dist/types/{gundb → src/gundb}/errors.d.ts +0 -0
  192. /package/dist/types/{gundb → src/gundb}/index.d.ts +0 -0
  193. /package/dist/types/{gundb → src/gundb}/rxjs.d.ts +0 -0
  194. /package/dist/types/{gundb → src/gundb}/types.d.ts +0 -0
  195. /package/dist/types/{interfaces → src/interfaces}/common.d.ts +0 -0
  196. /package/dist/types/{interfaces → src/interfaces}/events.d.ts +0 -0
  197. /package/dist/types/{interfaces → src/interfaces}/plugin.d.ts +0 -0
  198. /package/dist/types/{interfaces → src/interfaces}/shogun.d.ts +0 -0
  199. /package/dist/types/{managers → src/managers}/AuthManager.d.ts +0 -0
  200. /package/dist/types/{managers → src/managers}/CoreInitializer.d.ts +0 -0
  201. /package/dist/types/{managers → src/managers}/CryptoIdentityManager.d.ts +0 -0
  202. /package/dist/types/{managers → src/managers}/EventManager.d.ts +0 -0
  203. /package/dist/types/{managers → src/managers}/PluginManager.d.ts +0 -0
  204. /package/dist/types/{plugins → src/plugins}/base.d.ts +0 -0
  205. /package/dist/types/{plugins → src/plugins}/index.d.ts +0 -0
  206. /package/dist/types/{plugins → src/plugins}/nostr/index.d.ts +0 -0
  207. /package/dist/types/{plugins → src/plugins}/nostr/nostrConnector.d.ts +0 -0
  208. /package/dist/types/{plugins → src/plugins}/nostr/nostrConnectorPlugin.d.ts +0 -0
  209. /package/dist/types/{plugins → src/plugins}/nostr/nostrSigner.d.ts +0 -0
  210. /package/dist/types/{plugins → src/plugins}/nostr/types.d.ts +0 -0
  211. /package/dist/types/{plugins → src/plugins}/smartwallet/index.d.ts +0 -0
  212. /package/dist/types/{plugins → src/plugins}/smartwallet/smartWalletPlugin.d.ts +0 -0
  213. /package/dist/types/{plugins → src/plugins}/smartwallet/types.d.ts +0 -0
  214. /package/dist/types/{plugins → src/plugins}/web3/index.d.ts +0 -0
  215. /package/dist/types/{plugins → src/plugins}/web3/types.d.ts +0 -0
  216. /package/dist/types/{plugins → src/plugins}/web3/web3Connector.d.ts +0 -0
  217. /package/dist/types/{plugins → src/plugins}/web3/web3ConnectorPlugin.d.ts +0 -0
  218. /package/dist/types/{plugins → src/plugins}/web3/web3Signer.d.ts +0 -0
  219. /package/dist/types/{plugins → src/plugins}/webauthn/index.d.ts +0 -0
  220. /package/dist/types/{plugins → src/plugins}/webauthn/types.d.ts +0 -0
  221. /package/dist/types/{plugins → src/plugins}/webauthn/webauthn.d.ts +0 -0
  222. /package/dist/types/{plugins → src/plugins}/webauthn/webauthnPlugin.d.ts +0 -0
  223. /package/dist/types/{plugins → src/plugins}/webauthn/webauthnSigner.d.ts +0 -0
  224. /package/dist/types/{plugins → src/plugins}/zkproof/index.d.ts +0 -0
  225. /package/dist/types/{plugins → src/plugins}/zkproof/types.d.ts +0 -0
  226. /package/dist/types/{plugins → src/plugins}/zkproof/zkCredentials.d.ts +0 -0
  227. /package/dist/types/{plugins → src/plugins}/zkproof/zkProofConnector.d.ts +0 -0
  228. /package/dist/types/{plugins → src/plugins}/zkproof/zkProofPlugin.d.ts +0 -0
  229. /package/dist/types/{storage → src/storage}/storage.d.ts +0 -0
  230. /package/dist/types/{utils → src/utils}/errorHandler.d.ts +0 -0
  231. /package/dist/types/{utils → src/utils}/eventEmitter.d.ts +0 -0
  232. /package/dist/types/{utils → src/utils}/seedPhrase.d.ts +0 -0
  233. /package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
@@ -0,0 +1,728 @@
1
+ /**
2
+ * GunDB - Simplified database wrapper for Gun.js
3
+ * Provides only essential signup and login functionality
4
+ * Based on Gun.js User Authentication: https://deepwiki.com/amark/gun/6.1-user-authentication
5
+ */
6
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
7
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
8
+ return new (P || (P = Promise))(function (resolve, reject) {
9
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
10
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
11
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
12
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
13
+ });
14
+ };
15
+ var __generator = (this && this.__generator) || function (thisArg, body) {
16
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
17
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
18
+ function verb(n) { return function (v) { return step([n, v]); }; }
19
+ function step(op) {
20
+ if (f) throw new TypeError("Generator is already executing.");
21
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
22
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
23
+ if (y = 0, t) op = [op[0] & 2, t.value];
24
+ switch (op[0]) {
25
+ case 0: case 1: t = op; break;
26
+ case 4: _.label++; return { value: op[1], done: false };
27
+ case 5: _.label++; y = op[1]; op = [0]; continue;
28
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
29
+ default:
30
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
31
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
32
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
33
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
34
+ if (t[2]) _.ops.pop();
35
+ _.trys.pop(); continue;
36
+ }
37
+ op = body.call(thisArg, _);
38
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
39
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
40
+ }
41
+ };
42
+ import { RxJS } from "./rxjs.js";
43
+ import { EventEmitter } from "../utils/eventEmitter.js";
44
+ import * as GunErrors from "./errors.js";
45
+ import * as crypto from "./crypto.js";
46
+ /**
47
+ * Configuration constants
48
+ */
49
+ var CONFIG = {
50
+ PASSWORD: {
51
+ MIN_LENGTH: 8,
52
+ },
53
+ TIMEOUT: 11000, // 30 seconds
54
+ };
55
+ var DataBase = /** @class */ (function () {
56
+ function DataBase(gun, appScope, core, sea) {
57
+ if (appScope === void 0) { appScope = "shogun"; }
58
+ var _a;
59
+ this.user = null;
60
+ this.onAuthCallbacks = [];
61
+ this._isDestroyed = false;
62
+ console.log("[DB] Initializing DataBase");
63
+ // Initialize event emitter
64
+ this.eventEmitter = new EventEmitter();
65
+ // Validate Gun instance
66
+ if (!gun) {
67
+ throw new Error("Gun instance is required but was not provided");
68
+ }
69
+ if (typeof gun.user !== "function") {
70
+ throw new Error("Gun instance is invalid: gun.user is not a function");
71
+ }
72
+ this.gun = gun;
73
+ console.log("[DB] Gun instance validated");
74
+ // Recall user session if available
75
+ this.user = this.gun.user().recall({ sessionStorage: true });
76
+ console.log("[DB] User recall completed");
77
+ this.subscribeToAuthEvents();
78
+ console.log("[DB] Auth events subscribed");
79
+ this.crypto = crypto;
80
+ // Get SEA from gun instance or global
81
+ this.sea = sea || null;
82
+ if (!this.sea) {
83
+ if (this.gun.SEA) {
84
+ this.sea = this.gun.SEA;
85
+ }
86
+ else if ((_a = globalThis.Gun) === null || _a === void 0 ? void 0 : _a.SEA) {
87
+ this.sea = globalThis.Gun.SEA;
88
+ }
89
+ else if (globalThis.SEA) {
90
+ this.sea = globalThis.SEA;
91
+ }
92
+ }
93
+ this._rxjs = new RxJS(this.gun);
94
+ this.node = this.gun.get(appScope);
95
+ console.log("[DB] DataBase initialization completed");
96
+ }
97
+ /**
98
+ * Initialize with app scope
99
+ */
100
+ DataBase.prototype.initialize = function (appScope) {
101
+ if (appScope === void 0) { appScope = "shogun"; }
102
+ console.log("[DB] Initializing with appScope: ".concat(appScope));
103
+ this.node = this.gun.get(appScope);
104
+ console.log("[DB] App scope node initialized");
105
+ };
106
+ /**
107
+ * Subscribe to Gun auth events
108
+ */
109
+ DataBase.prototype.subscribeToAuthEvents = function () {
110
+ var _this = this;
111
+ this.gun.on("auth", function (ack) {
112
+ var _a;
113
+ if (ack.err) {
114
+ console.error("[DB] Auth event error:", ack.err);
115
+ }
116
+ else {
117
+ _this.notifyAuthListeners(((_a = ack.sea) === null || _a === void 0 ? void 0 : _a.pub) || "");
118
+ }
119
+ });
120
+ };
121
+ /**
122
+ * Notify all auth callbacks
123
+ */
124
+ DataBase.prototype.notifyAuthListeners = function (pub) {
125
+ var user = this.gun.user();
126
+ this.onAuthCallbacks.forEach(function (cb) { return cb(user); });
127
+ };
128
+ /**
129
+ * Register authentication callback
130
+ */
131
+ DataBase.prototype.onAuth = function (callback) {
132
+ var _this = this;
133
+ this.onAuthCallbacks.push(callback);
134
+ var user = this.gun.user();
135
+ if (user && user.is)
136
+ callback(user);
137
+ return function () {
138
+ var i = _this.onAuthCallbacks.indexOf(callback);
139
+ if (i !== -1)
140
+ _this.onAuthCallbacks.splice(i, 1);
141
+ };
142
+ };
143
+ /**
144
+ * Check if user is logged in
145
+ */
146
+ DataBase.prototype.isLoggedIn = function () {
147
+ try {
148
+ var user = this.gun.user();
149
+ return !!(user && user.is && user.is.pub);
150
+ }
151
+ catch (error) {
152
+ return false;
153
+ }
154
+ };
155
+ /**
156
+ * Restore session from storage
157
+ */
158
+ DataBase.prototype.restoreSession = function () {
159
+ try {
160
+ if (typeof sessionStorage === "undefined") {
161
+ return { success: false, error: "sessionStorage not available" };
162
+ }
163
+ var sessionData = sessionStorage.getItem("gunSessionData");
164
+ if (!sessionData) {
165
+ return { success: false, error: "No saved session" };
166
+ }
167
+ var session = JSON.parse(sessionData);
168
+ if (!session.userPub) {
169
+ return { success: false, error: "Invalid session data" };
170
+ }
171
+ // Check if session is expired
172
+ if (session.expiresAt && Date.now() > session.expiresAt) {
173
+ sessionStorage.removeItem("gunSessionData");
174
+ return { success: false, error: "Session expired" };
175
+ }
176
+ // Verify session restoration
177
+ var user = this.gun.user();
178
+ if (user.is && user.is.pub === session.userPub) {
179
+ this.user = user;
180
+ return { success: true, userPub: session.userPub };
181
+ }
182
+ return { success: false, error: "Session verification failed" };
183
+ }
184
+ catch (error) {
185
+ return { success: false, error: String(error) };
186
+ }
187
+ };
188
+ /**
189
+ * Logout user
190
+ */
191
+ DataBase.prototype.logout = function () {
192
+ try {
193
+ var currentUser = this.gun.user();
194
+ if (currentUser && currentUser.is) {
195
+ currentUser.leave();
196
+ }
197
+ this.user = null;
198
+ if (typeof sessionStorage !== "undefined") {
199
+ sessionStorage.removeItem("gunSessionData");
200
+ }
201
+ }
202
+ catch (error) {
203
+ console.error("[DB] Error during logout:", error);
204
+ }
205
+ };
206
+ /**
207
+ * Validate password strength
208
+ */
209
+ DataBase.prototype.validatePasswordStrength = function (password) {
210
+ if (password.length < CONFIG.PASSWORD.MIN_LENGTH) {
211
+ return {
212
+ valid: false,
213
+ error: "Password must be at least ".concat(CONFIG.PASSWORD.MIN_LENGTH, " characters long"),
214
+ };
215
+ }
216
+ return { valid: true };
217
+ };
218
+ /**
219
+ * Validate signup credentials
220
+ */
221
+ DataBase.prototype.validateSignupCredentials = function (username, password, pair) {
222
+ if (!username || username.length < 1) {
223
+ return {
224
+ valid: false,
225
+ error: "Username must be more than 0 characters long",
226
+ };
227
+ }
228
+ if (!/^[a-zA-Z0-9._-]+$/.test(username)) {
229
+ return {
230
+ valid: false,
231
+ error: "Username can only contain letters, numbers, dots, underscores, and hyphens",
232
+ };
233
+ }
234
+ if (pair) {
235
+ if (!pair.pub || !pair.priv || !pair.epub || !pair.epriv) {
236
+ return { valid: false, error: "Invalid pair provided" };
237
+ }
238
+ return { valid: true };
239
+ }
240
+ return this.validatePasswordStrength(password);
241
+ };
242
+ /**
243
+ * Reset authentication state
244
+ */
245
+ DataBase.prototype.resetAuthState = function () {
246
+ try {
247
+ var user = this.gun.user();
248
+ if (user && user._) {
249
+ var cat = user._;
250
+ // Reset Gun's internal auth state
251
+ cat.ing = false;
252
+ cat.auth = null;
253
+ cat.act = null;
254
+ // Clear any pending auth operations
255
+ if (cat.auth) {
256
+ cat.auth = null;
257
+ }
258
+ }
259
+ try {
260
+ user.leave();
261
+ }
262
+ catch (leaveError) {
263
+ // Ignore leave errors
264
+ }
265
+ this.user = null;
266
+ }
267
+ catch (e) {
268
+ // Ignore
269
+ }
270
+ };
271
+ /**
272
+ * Build login result
273
+ */
274
+ DataBase.prototype.buildLoginResult = function (username, userPub) {
275
+ var _a, _b;
276
+ var seaPair = (_b = (_a = this.gun.user()) === null || _a === void 0 ? void 0 : _a._) === null || _b === void 0 ? void 0 : _b.sea;
277
+ return {
278
+ success: true,
279
+ userPub: userPub,
280
+ username: username,
281
+ sea: seaPair
282
+ ? {
283
+ pub: seaPair.pub,
284
+ priv: seaPair.priv,
285
+ epub: seaPair.epub,
286
+ epriv: seaPair.epriv,
287
+ }
288
+ : undefined,
289
+ };
290
+ };
291
+ /**
292
+ * Save credentials to session storage
293
+ */
294
+ DataBase.prototype.saveCredentials = function (userInfo) {
295
+ try {
296
+ if (typeof sessionStorage !== "undefined") {
297
+ var sessionInfo = {
298
+ username: userInfo.alias,
299
+ pair: userInfo.pair,
300
+ userPub: userInfo.userPub,
301
+ timestamp: Date.now(),
302
+ expiresAt: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 days
303
+ };
304
+ sessionStorage.setItem("gunSessionData", JSON.stringify(sessionInfo));
305
+ }
306
+ }
307
+ catch (error) {
308
+ console.error("[DB] Error saving credentials:", error);
309
+ }
310
+ };
311
+ /**
312
+ * Sign up a new user
313
+ * Based on Gun.js user().create() - https://deepwiki.com/amark/gun/6.1-user-authentication
314
+ */
315
+ DataBase.prototype.signUp = function (username, password, pair) {
316
+ return __awaiter(this, void 0, void 0, function () {
317
+ var validation, normalizedUsername, user, loginResult, e_1, result;
318
+ var _this = this;
319
+ return __generator(this, function (_a) {
320
+ switch (_a.label) {
321
+ case 0:
322
+ validation = this.validateSignupCredentials(username, password, pair);
323
+ if (!validation.valid) {
324
+ return [2 /*return*/, { success: false, error: validation.error }];
325
+ }
326
+ console.log("[DB] Signup validation:", validation);
327
+ this.resetAuthState();
328
+ normalizedUsername = username.trim().toLowerCase();
329
+ user = this.gun.user();
330
+ if (!pair) return [3 /*break*/, 4];
331
+ _a.label = 1;
332
+ case 1:
333
+ _a.trys.push([1, 3, , 4]);
334
+ return [4 /*yield*/, new Promise(function (resolve) {
335
+ var callbackInvoked = false;
336
+ var timeoutId = setTimeout(function () {
337
+ if (!callbackInvoked) {
338
+ callbackInvoked = true;
339
+ resolve({ success: false, error: "Pair auth timeout" });
340
+ }
341
+ }, CONFIG.TIMEOUT);
342
+ user.auth(pair, function (ack) {
343
+ var _a, _b, _c;
344
+ if (callbackInvoked) {
345
+ return;
346
+ }
347
+ callbackInvoked = true;
348
+ clearTimeout(timeoutId);
349
+ if (ack.err) {
350
+ // Could not login with pair, try to create user with username/password
351
+ resolve({ success: false, error: ack.err });
352
+ return;
353
+ }
354
+ var userPub = (_a = user === null || user === void 0 ? void 0 : user.is) === null || _a === void 0 ? void 0 : _a.pub;
355
+ if (!userPub) {
356
+ _this.resetAuthState();
357
+ resolve({ success: false, error: "No userPub available" });
358
+ return;
359
+ }
360
+ _this.user = user;
361
+ var alias = (_b = user === null || user === void 0 ? void 0 : user.is) === null || _b === void 0 ? void 0 : _b.alias;
362
+ var userPair = (_c = user === null || user === void 0 ? void 0 : user._) === null || _c === void 0 ? void 0 : _c.sea;
363
+ _this.saveCredentials({
364
+ alias: alias || normalizedUsername,
365
+ pair: pair !== null && pair !== void 0 ? pair : userPair,
366
+ userPub: userPub,
367
+ });
368
+ resolve(_this.buildLoginResult(alias || normalizedUsername, userPub));
369
+ });
370
+ })];
371
+ case 2:
372
+ loginResult = _a.sent();
373
+ // If we got a successful result, return it
374
+ if (loginResult && loginResult.success) {
375
+ return [2 /*return*/, loginResult];
376
+ }
377
+ return [3 /*break*/, 4];
378
+ case 3:
379
+ e_1 = _a.sent();
380
+ return [3 /*break*/, 4];
381
+ case 4:
382
+ console.log("[DB] Falling back to classic username/password account creation");
383
+ return [4 /*yield*/, new Promise(function (resolve) {
384
+ var callbackInvoked = false;
385
+ var timeoutId = setTimeout(function () {
386
+ if (!callbackInvoked) {
387
+ callbackInvoked = true;
388
+ console.log("[DB] Signup timeout");
389
+ _this.resetAuthState();
390
+ resolve({ success: false, error: "Signup timeout" });
391
+ }
392
+ }, CONFIG.TIMEOUT);
393
+ user.create(normalizedUsername, password, function (createAck) {
394
+ if (callbackInvoked) {
395
+ return;
396
+ }
397
+ console.log("[DB] Signup callback received:", JSON.stringify(createAck));
398
+ // Check for error: ack.err or ack.ok !== 0 means error
399
+ if (createAck.err ||
400
+ (createAck.ok !== undefined && createAck.ok !== 0)) {
401
+ callbackInvoked = true;
402
+ clearTimeout(timeoutId);
403
+ _this.resetAuthState();
404
+ resolve({ success: false, error: createAck.err || "Signup failed" });
405
+ return;
406
+ }
407
+ // After create, we need to authenticate to get the user fully logged in
408
+ // Use ack.pub if available for the userPub
409
+ var userPub = createAck.pub;
410
+ if (!userPub) {
411
+ callbackInvoked = true;
412
+ clearTimeout(timeoutId);
413
+ _this.resetAuthState();
414
+ resolve({
415
+ success: false,
416
+ error: "No userPub available from signup",
417
+ });
418
+ return;
419
+ }
420
+ // Now authenticate with the username/password to complete the login
421
+ user.auth(normalizedUsername, password, function (authAck) {
422
+ var _a, _b, _c, _d;
423
+ if (callbackInvoked) {
424
+ return;
425
+ }
426
+ callbackInvoked = true;
427
+ clearTimeout(timeoutId);
428
+ if (authAck.err) {
429
+ _this.resetAuthState();
430
+ resolve({
431
+ success: false,
432
+ error: authAck.err || "Authentication after signup failed",
433
+ });
434
+ return;
435
+ }
436
+ // Verify user is authenticated
437
+ var authenticatedUserPub = (_a = user === null || user === void 0 ? void 0 : user.is) === null || _a === void 0 ? void 0 : _a.pub;
438
+ if (!authenticatedUserPub) {
439
+ _this.resetAuthState();
440
+ resolve({
441
+ success: false,
442
+ error: "User not authenticated after signup",
443
+ });
444
+ return;
445
+ }
446
+ _this.user = user;
447
+ var alias = (_b = user === null || user === void 0 ? void 0 : user.is) === null || _b === void 0 ? void 0 : _b.alias;
448
+ var userPair = (_c = user === null || user === void 0 ? void 0 : user._) === null || _c === void 0 ? void 0 : _c.sea;
449
+ try {
450
+ _this.saveCredentials({
451
+ alias: alias || normalizedUsername,
452
+ pair: pair !== null && pair !== void 0 ? pair : userPair,
453
+ userPub: authenticatedUserPub,
454
+ });
455
+ }
456
+ catch (saveError) {
457
+ // Ignore save errors
458
+ }
459
+ var sea = (_d = user === null || user === void 0 ? void 0 : user._) === null || _d === void 0 ? void 0 : _d.sea;
460
+ resolve({
461
+ success: true,
462
+ userPub: authenticatedUserPub,
463
+ username: normalizedUsername,
464
+ isNewUser: true,
465
+ sea: sea
466
+ ? {
467
+ pub: sea.pub,
468
+ priv: sea.priv,
469
+ epub: sea.epub,
470
+ epriv: sea.epriv,
471
+ }
472
+ : undefined,
473
+ });
474
+ });
475
+ });
476
+ })];
477
+ case 5:
478
+ result = _a.sent();
479
+ return [2 /*return*/, result];
480
+ }
481
+ });
482
+ });
483
+ };
484
+ /**
485
+ * Login with username and password
486
+ * Based on Gun.js user().auth() - https://deepwiki.com/amark/gun/6.1-user-authentication
487
+ */
488
+ DataBase.prototype.login = function (username, password, pair) {
489
+ return __awaiter(this, void 0, void 0, function () {
490
+ var normalizedUsername, user;
491
+ var _this = this;
492
+ return __generator(this, function (_a) {
493
+ this.resetAuthState();
494
+ normalizedUsername = username.trim().toLowerCase();
495
+ user = this.gun.user();
496
+ console.log("[DB] Login with username:", normalizedUsername);
497
+ return [2 /*return*/, new Promise(function (resolve) {
498
+ var timeoutId = setTimeout(function () {
499
+ _this.resetAuthState();
500
+ resolve({ success: false, error: "Login timeout" });
501
+ }, CONFIG.TIMEOUT);
502
+ if (pair) {
503
+ user.auth(pair, function (ack) {
504
+ var _a, _b, _c;
505
+ clearTimeout(timeoutId);
506
+ if (ack.err) {
507
+ _this.resetAuthState();
508
+ resolve({ success: false, error: ack.err });
509
+ return;
510
+ }
511
+ var userPub = (_a = user === null || user === void 0 ? void 0 : user.is) === null || _a === void 0 ? void 0 : _a.pub;
512
+ if (!userPub) {
513
+ _this.resetAuthState();
514
+ resolve({ success: false, error: "No userPub available" });
515
+ return;
516
+ }
517
+ _this.user = user;
518
+ var alias = (_b = user === null || user === void 0 ? void 0 : user.is) === null || _b === void 0 ? void 0 : _b.alias;
519
+ var userPair = (_c = user === null || user === void 0 ? void 0 : user._) === null || _c === void 0 ? void 0 : _c.sea;
520
+ try {
521
+ _this.saveCredentials({
522
+ alias: alias || normalizedUsername,
523
+ pair: pair !== null && pair !== void 0 ? pair : userPair,
524
+ userPub: userPub,
525
+ });
526
+ }
527
+ catch (saveError) {
528
+ // Ignore save errors
529
+ }
530
+ resolve(_this.buildLoginResult(alias || normalizedUsername, userPub));
531
+ });
532
+ }
533
+ else {
534
+ user.auth(normalizedUsername, password, function (ack) {
535
+ var _a, _b, _c;
536
+ clearTimeout(timeoutId);
537
+ if (ack.err) {
538
+ _this.resetAuthState();
539
+ resolve({ success: false, error: ack.err });
540
+ return;
541
+ }
542
+ var userPub = (_a = user === null || user === void 0 ? void 0 : user.is) === null || _a === void 0 ? void 0 : _a.pub;
543
+ if (!userPub) {
544
+ _this.resetAuthState();
545
+ resolve({ success: false, error: "No userPub available" });
546
+ return;
547
+ }
548
+ _this.user = user;
549
+ var alias = (_b = user === null || user === void 0 ? void 0 : user.is) === null || _b === void 0 ? void 0 : _b.alias;
550
+ var userPair = (_c = user === null || user === void 0 ? void 0 : user._) === null || _c === void 0 ? void 0 : _c.sea;
551
+ try {
552
+ _this.saveCredentials({
553
+ alias: alias || normalizedUsername,
554
+ pair: pair !== null && pair !== void 0 ? pair : userPair,
555
+ userPub: userPub,
556
+ });
557
+ }
558
+ catch (saveError) {
559
+ // Ignore save errors
560
+ }
561
+ resolve(_this.buildLoginResult(alias || normalizedUsername, userPub));
562
+ });
563
+ }
564
+ })];
565
+ });
566
+ });
567
+ };
568
+ /**
569
+ * Get current user
570
+ */
571
+ DataBase.prototype.getCurrentUser = function () {
572
+ try {
573
+ var user = this.gun.user();
574
+ if (user && user.is && user.is.pub) {
575
+ return {
576
+ pub: user.is.pub,
577
+ user: user,
578
+ };
579
+ }
580
+ return null;
581
+ }
582
+ catch (error) {
583
+ return null;
584
+ }
585
+ };
586
+ /**
587
+ * Get current user's public key
588
+ * @returns {string | null} User's public key or null if not logged in
589
+ */
590
+ DataBase.prototype.getUserPub = function () {
591
+ var _a;
592
+ try {
593
+ var user = this.gun.user();
594
+ return ((_a = user === null || user === void 0 ? void 0 : user.is) === null || _a === void 0 ? void 0 : _a.pub) || null;
595
+ }
596
+ catch (error) {
597
+ return null;
598
+ }
599
+ };
600
+ /**
601
+ * Login with SEA pair directly
602
+ * @param username - Username for identification
603
+ * @param pair - GunDB SEA pair for authentication
604
+ * @returns {Promise<AuthResult>} Promise with authentication result
605
+ * @description Authenticates user using a GunDB pair directly without password
606
+ */
607
+ DataBase.prototype.loginWithPair = function (username, pair) {
608
+ return __awaiter(this, void 0, void 0, function () {
609
+ var normalizedUsername, user;
610
+ var _this = this;
611
+ return __generator(this, function (_a) {
612
+ // Validate pair structure
613
+ if (!pair || !pair.pub || !pair.priv || !pair.epub || !pair.epriv) {
614
+ return [2 /*return*/, {
615
+ success: false,
616
+ error: "Invalid pair structure - missing required keys",
617
+ }];
618
+ }
619
+ this.resetAuthState();
620
+ normalizedUsername = username.trim().toLowerCase();
621
+ user = this.gun.user();
622
+ console.log("[DB] Login with pair for username:", normalizedUsername);
623
+ return [2 /*return*/, new Promise(function (resolve) {
624
+ var timeoutId = setTimeout(function () {
625
+ _this.resetAuthState();
626
+ resolve({ success: false, error: "Login with pair timeout" });
627
+ }, CONFIG.TIMEOUT);
628
+ user.auth(pair, function (ack) {
629
+ var _a, _b, _c;
630
+ clearTimeout(timeoutId);
631
+ if (ack.err) {
632
+ _this.resetAuthState();
633
+ resolve({ success: false, error: ack.err });
634
+ return;
635
+ }
636
+ var userPub = (_a = user === null || user === void 0 ? void 0 : user.is) === null || _a === void 0 ? void 0 : _a.pub;
637
+ if (!userPub) {
638
+ _this.resetAuthState();
639
+ resolve({ success: false, error: "No userPub available" });
640
+ return;
641
+ }
642
+ _this.user = user;
643
+ var alias = (_b = user === null || user === void 0 ? void 0 : user.is) === null || _b === void 0 ? void 0 : _b.alias;
644
+ var userPair = (_c = user === null || user === void 0 ? void 0 : user._) === null || _c === void 0 ? void 0 : _c.sea;
645
+ try {
646
+ _this.saveCredentials({
647
+ alias: alias || normalizedUsername,
648
+ pair: pair !== null && pair !== void 0 ? pair : userPair,
649
+ userPub: userPub,
650
+ });
651
+ }
652
+ catch (saveError) {
653
+ // Ignore save errors
654
+ }
655
+ resolve(_this.buildLoginResult(alias || normalizedUsername, userPub));
656
+ });
657
+ })];
658
+ });
659
+ });
660
+ };
661
+ /**
662
+ * Login with SEA pair
663
+ */
664
+ // Legacy method - kept for backward compatibility
665
+ DataBase.prototype.loginWithPairLegacy = function (username, pair) {
666
+ return __awaiter(this, void 0, void 0, function () {
667
+ return __generator(this, function (_a) {
668
+ return [2 /*return*/, this.login(username, "", pair)];
669
+ });
670
+ });
671
+ };
672
+ /**
673
+ * Get RxJS module
674
+ */
675
+ DataBase.prototype.rx = function () {
676
+ return this._rxjs;
677
+ };
678
+ /**
679
+ * Destroy database instance
680
+ */
681
+ DataBase.prototype.destroy = function () {
682
+ if (this._isDestroyed)
683
+ return;
684
+ console.log("[DB] Destroying DataBase instance...");
685
+ this._isDestroyed = true;
686
+ this.onAuthCallbacks.length = 0;
687
+ // Clear event listeners
688
+ this.eventEmitter.removeAllListeners();
689
+ if (this.user) {
690
+ try {
691
+ this.user.leave();
692
+ }
693
+ catch (error) {
694
+ // Ignore
695
+ }
696
+ this.user = null;
697
+ }
698
+ this._rxjs = undefined;
699
+ console.log("[DB] DataBase instance destroyed");
700
+ };
701
+ /**
702
+ * Aggressive auth cleanup (kept for compatibility with tests)
703
+ */
704
+ DataBase.prototype.aggressiveAuthCleanup = function () {
705
+ console.log("🧹 Performing aggressive auth cleanup...");
706
+ this.resetAuthState();
707
+ this.logout();
708
+ console.log("✓ Aggressive auth cleanup completed");
709
+ };
710
+ /**
711
+ * Event emitter methods for CoreInitializer compatibility
712
+ */
713
+ DataBase.prototype.on = function (event, listener) {
714
+ this.eventEmitter.on(event, listener);
715
+ };
716
+ DataBase.prototype.off = function (event, listener) {
717
+ this.eventEmitter.off(event, listener);
718
+ };
719
+ DataBase.prototype.once = function (event, listener) {
720
+ this.eventEmitter.once(event, listener);
721
+ };
722
+ DataBase.prototype.emit = function (event, data) {
723
+ return this.eventEmitter.emit(event, data);
724
+ };
725
+ return DataBase;
726
+ }());
727
+ export { DataBase, RxJS, crypto, GunErrors };
728
+ export { default as derive } from "./derive.js";