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,301 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ 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);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ 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;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { ErrorHandler, ErrorType } from "../utils/errorHandler.js";
38
+ /**
39
+ * Manages authentication operations for ShogunCore
40
+ */
41
+ var AuthManager = /** @class */ (function () {
42
+ function AuthManager(core) {
43
+ this.core = core;
44
+ }
45
+ /**
46
+ * Check if user is logged in
47
+ * @returns {boolean} True if user is logged in, false otherwise
48
+ * @description Verifies authentication status by checking GunInstance login state
49
+ * and presence of authentication credentials in storage
50
+ */
51
+ AuthManager.prototype.isLoggedIn = function () {
52
+ return this.core.db.isLoggedIn();
53
+ };
54
+ /**
55
+ * Perform user logout
56
+ * @description Logs out the current user from GunInstance and emits logout event.
57
+ * If user is not authenticated, the logout operation is ignored.
58
+ */
59
+ AuthManager.prototype.logout = function () {
60
+ try {
61
+ if (!this.isLoggedIn()) {
62
+ return;
63
+ }
64
+ this.core.db.logout();
65
+ this.core.emit("auth:logout");
66
+ }
67
+ catch (error) {
68
+ ErrorHandler.handle(ErrorType.AUTHENTICATION, "LOGOUT_FAILED", error instanceof Error ? error.message : "Error during logout", error);
69
+ }
70
+ };
71
+ /**
72
+ * Authenticate user with username and password
73
+ * @param username - Username
74
+ * @param password - User password
75
+ * @returns {Promise<AuthResult>} Promise with authentication result
76
+ * @description Attempts to log in user with provided credentials.
77
+ * Emits login event on success.
78
+ */
79
+ AuthManager.prototype.login = function (username, password, pair) {
80
+ return __awaiter(this, void 0, void 0, function () {
81
+ var result, seaPair, error_1;
82
+ var _a, _b, _c, _d, _e;
83
+ return __generator(this, function (_f) {
84
+ switch (_f.label) {
85
+ case 0:
86
+ _f.trys.push([0, 2, , 3]);
87
+ if (!this.currentAuthMethod) {
88
+ this.currentAuthMethod = "password";
89
+ }
90
+ return [4 /*yield*/, this.core.db.login(username, password, pair)];
91
+ case 1:
92
+ result = _f.sent();
93
+ if (result.success) {
94
+ seaPair = (_b = (_a = this.core.user) === null || _a === void 0 ? void 0 : _a._) === null || _b === void 0 ? void 0 : _b.sea;
95
+ if (seaPair) {
96
+ result.sea = seaPair;
97
+ }
98
+ this.core.emit("auth:login", {
99
+ userPub: (_c = result.userPub) !== null && _c !== void 0 ? _c : "",
100
+ method: this.currentAuthMethod === "pair"
101
+ ? "password"
102
+ : this.currentAuthMethod || "password",
103
+ });
104
+ }
105
+ else {
106
+ result.error = result.error || "Wrong user or password";
107
+ }
108
+ return [2 /*return*/, result];
109
+ case 2:
110
+ error_1 = _f.sent();
111
+ ErrorHandler.handle(ErrorType.AUTHENTICATION, "LOGIN_FAILED", (_d = error_1.message) !== null && _d !== void 0 ? _d : "Unknown error during login", error_1);
112
+ return [2 /*return*/, {
113
+ success: false,
114
+ error: (_e = error_1.message) !== null && _e !== void 0 ? _e : "Unknown error during login",
115
+ }];
116
+ case 3: return [2 /*return*/];
117
+ }
118
+ });
119
+ });
120
+ };
121
+ /**
122
+ * Login with GunDB pair directly
123
+ * @param pair - GunDB SEA pair for authentication
124
+ * @returns {Promise<AuthResult>} Promise with authentication result
125
+ * @description Authenticates user using a GunDB pair directly.
126
+ * Emits login event on success.
127
+ */
128
+ AuthManager.prototype.loginWithPair = function (username, pair) {
129
+ return __awaiter(this, void 0, void 0, function () {
130
+ var result, seaPair, error_2;
131
+ var _a, _b, _c, _d, _e;
132
+ return __generator(this, function (_f) {
133
+ switch (_f.label) {
134
+ case 0:
135
+ _f.trys.push([0, 2, , 3]);
136
+ if (!pair || !pair.pub || !pair.priv || !pair.epub || !pair.epriv) {
137
+ return [2 /*return*/, {
138
+ success: false,
139
+ error: "Invalid pair structure - missing required keys",
140
+ }];
141
+ }
142
+ return [4 /*yield*/, this.core.db.loginWithPair(username, pair)];
143
+ case 1:
144
+ result = _f.sent();
145
+ if (result.success) {
146
+ seaPair = (_b = (_a = this.core.user) === null || _a === void 0 ? void 0 : _a._) === null || _b === void 0 ? void 0 : _b.sea;
147
+ if (seaPair) {
148
+ result.sea = seaPair;
149
+ }
150
+ this.currentAuthMethod = "pair";
151
+ this.core.emit("auth:login", {
152
+ userPub: (_c = result.userPub) !== null && _c !== void 0 ? _c : "",
153
+ method: "pair",
154
+ username: username,
155
+ });
156
+ }
157
+ else {
158
+ result.error =
159
+ result.error || "Authentication failed with provided pair";
160
+ }
161
+ return [2 /*return*/, result];
162
+ case 2:
163
+ error_2 = _f.sent();
164
+ ErrorHandler.handle(ErrorType.AUTHENTICATION, "PAIR_LOGIN_FAILED", (_d = error_2.message) !== null && _d !== void 0 ? _d : "Unknown error during pair login", error_2);
165
+ return [2 /*return*/, {
166
+ success: false,
167
+ error: (_e = error_2.message) !== null && _e !== void 0 ? _e : "Unknown error during pair login",
168
+ }];
169
+ case 3: return [2 /*return*/];
170
+ }
171
+ });
172
+ });
173
+ };
174
+ /**
175
+ * Register a new user with provided credentials
176
+ * @param username - Username
177
+ * @param password - Password
178
+ * @param email - Email (optional)
179
+ * @param pair - Pair of keys
180
+ * @returns {Promise<SignUpResult>} Registration result
181
+ * @description Creates a new user account with the provided credentials.
182
+ * Validates password requirements and emits signup event on success.
183
+ */
184
+ AuthManager.prototype.signUp = function (username, password, pair) {
185
+ return __awaiter(this, void 0, void 0, function () {
186
+ var result, error_3;
187
+ return __generator(this, function (_a) {
188
+ switch (_a.label) {
189
+ case 0:
190
+ _a.trys.push([0, 2, , 3]);
191
+ if (!this.core.db) {
192
+ throw new Error("Database not initialized");
193
+ }
194
+ // For password-based signup, ensure password is provided
195
+ if (!pair && (!password || password.trim() === "")) {
196
+ throw new Error("Password is required for password-based signup");
197
+ }
198
+ return [4 /*yield*/, this.core.db.signUp(username, password || "", pair)];
199
+ case 1:
200
+ result = _a.sent();
201
+ if (result.success) {
202
+ // Update current authentication method
203
+ this.currentAuthMethod = pair ? "web3" : "password";
204
+ this.core.emit("auth:signup", {
205
+ userPub: result.userPub,
206
+ username: username,
207
+ method: this.currentAuthMethod,
208
+ });
209
+ this.core.emit("debug", {
210
+ action: "signup_success",
211
+ userPub: result.userPub,
212
+ method: this.currentAuthMethod,
213
+ });
214
+ }
215
+ else {
216
+ this.core.emit("debug", {
217
+ action: "signup_failed",
218
+ error: result.error,
219
+ username: username,
220
+ });
221
+ }
222
+ return [2 /*return*/, result];
223
+ case 2:
224
+ error_3 = _a.sent();
225
+ if (typeof console !== "undefined" && console.error) {
226
+ console.error("Error during registration for user ".concat(username, ":"), error_3);
227
+ }
228
+ this.core.emit("debug", {
229
+ action: "signup_error",
230
+ error: error_3 instanceof Error ? error_3.message : String(error_3),
231
+ username: username,
232
+ });
233
+ return [2 /*return*/, {
234
+ success: false,
235
+ error: "Registration failed: ".concat(error_3 instanceof Error ? error_3.message : String(error_3)),
236
+ }];
237
+ case 3: return [2 /*return*/];
238
+ }
239
+ });
240
+ });
241
+ };
242
+ /**
243
+ * Set the current authentication method
244
+ * This is used by plugins to indicate which authentication method was used
245
+ * @param method The authentication method used
246
+ */
247
+ AuthManager.prototype.setAuthMethod = function (method) {
248
+ this.currentAuthMethod = method;
249
+ };
250
+ /**
251
+ * Get the current authentication method
252
+ * @returns The current authentication method or undefined if not set
253
+ */
254
+ AuthManager.prototype.getAuthMethod = function () {
255
+ return this.currentAuthMethod;
256
+ };
257
+ /**
258
+ * Get an authentication method plugin by type
259
+ * @param type The type of authentication method
260
+ * @returns The authentication plugin or undefined if not available
261
+ * This is a more modern approach to accessing authentication methods
262
+ */
263
+ AuthManager.prototype.getAuthenticationMethod = function (type) {
264
+ var _this = this;
265
+ switch (type) {
266
+ case "webauthn":
267
+ return this.core.getPlugin("webauthn");
268
+ case "web3":
269
+ return this.core.getPlugin("web3");
270
+ case "nostr":
271
+ return this.core.getPlugin("nostr");
272
+ case "password":
273
+ default:
274
+ return {
275
+ login: function (username, password) { return __awaiter(_this, void 0, void 0, function () {
276
+ return __generator(this, function (_a) {
277
+ switch (_a.label) {
278
+ case 0: return [4 /*yield*/, this.login(username, password)];
279
+ case 1: return [2 /*return*/, _a.sent()];
280
+ }
281
+ });
282
+ }); },
283
+ signUp: function (username, password, confirm) { return __awaiter(_this, void 0, void 0, function () {
284
+ return __generator(this, function (_a) {
285
+ switch (_a.label) {
286
+ case 0:
287
+ // For password-based signup, validate password confirmation
288
+ if (confirm && password !== confirm) {
289
+ throw new Error("Password and confirm password do not match");
290
+ }
291
+ return [4 /*yield*/, this.signUp(username, password)];
292
+ case 1: return [2 /*return*/, _a.sent()];
293
+ }
294
+ });
295
+ }); },
296
+ };
297
+ }
298
+ };
299
+ return AuthManager;
300
+ }());
301
+ export { AuthManager };
@@ -0,0 +1,304 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ 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);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ 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;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { ShogunStorage } from "../storage/storage.js";
38
+ import { ErrorHandler } from "../utils/errorHandler.js";
39
+ import { WebauthnPlugin } from "../plugins/webauthn/webauthnPlugin.js";
40
+ import { Web3ConnectorPlugin } from "../plugins/web3/web3ConnectorPlugin.js";
41
+ import { NostrConnectorPlugin } from "../plugins/nostr/nostrConnectorPlugin.js";
42
+ import { ZkProofPlugin } from "../plugins/zkproof/zkProofPlugin.js";
43
+ import { DataBase, RxJS, derive } from "../gundb/db.js";
44
+ /**
45
+ * Handles initialization of ShogunCore components
46
+ */
47
+ var CoreInitializer = /** @class */ (function () {
48
+ function CoreInitializer(core) {
49
+ this.core = core;
50
+ }
51
+ /**
52
+ * Initialize the Shogun SDK
53
+ * @param config - SDK Configuration object
54
+ * @description Creates a new instance of ShogunCore with the provided configuration.
55
+ * Initializes all required components including storage, event emitter, GunInstance connection,
56
+ * and plugin system.
57
+ */
58
+ CoreInitializer.prototype.initialize = function (config) {
59
+ return __awaiter(this, void 0, void 0, function () {
60
+ var _this = this;
61
+ return __generator(this, function (_a) {
62
+ // Polyfill console for environments where it might be missing
63
+ if (typeof console === "undefined") {
64
+ global.console = {
65
+ log: function () { },
66
+ warn: function () { },
67
+ error: function () { },
68
+ info: function () { },
69
+ debug: function () { },
70
+ };
71
+ }
72
+ // Initialize storage
73
+ this.core.storage = new ShogunStorage(config.silent);
74
+ // Setup error handler
75
+ ErrorHandler.addListener(function (error) {
76
+ _this.core.emit("error", {
77
+ action: error.code,
78
+ message: error.message,
79
+ type: error.type,
80
+ });
81
+ });
82
+ // Setup Gun instance
83
+ this.initializeGun(config);
84
+ // Setup Gun user
85
+ this.initializeGunUser();
86
+ // Setup Gun event forwarding
87
+ this.setupGunEventForwarding();
88
+ // Setup wallet derivation
89
+ this.setupWalletDerivation();
90
+ // Initialize RxJS
91
+ this.core.rx = new RxJS(this.core.gun);
92
+ // Register built-in plugins
93
+ this.registerBuiltinPlugins(config);
94
+ // Initialize async components
95
+ this.initializeDb();
96
+ return [2 /*return*/];
97
+ });
98
+ });
99
+ };
100
+ /**
101
+ * Initialize Gun instance
102
+ */
103
+ CoreInitializer.prototype.initializeGun = function (config) {
104
+ try {
105
+ if (!config.gunInstance) {
106
+ throw new Error("Gun instance is required but was not provided");
107
+ }
108
+ // Validate Gun instance
109
+ if (typeof config.gunInstance !== "object") {
110
+ throw new Error("Gun instance must be an object, received: ".concat(typeof config.gunInstance));
111
+ }
112
+ if (typeof config.gunInstance.user !== "function") {
113
+ throw new Error("Gun instance is invalid: gun.user is not a function. Received gun.user type: ".concat(typeof config.gunInstance.user));
114
+ }
115
+ if (typeof config.gunInstance.get !== "function") {
116
+ throw new Error("Gun instance is invalid: gun.get is not a function. Received gun.get type: ".concat(typeof config.gunInstance.get));
117
+ }
118
+ if (typeof config.gunInstance.on !== "function") {
119
+ throw new Error("Gun instance is invalid: gun.on is not a function. Received gun.on type: ".concat(typeof config.gunInstance.on));
120
+ }
121
+ console.log("Using provided Gun instance:", config.gunInstance);
122
+ this.core._gun = config.gunInstance;
123
+ }
124
+ catch (error) {
125
+ if (typeof console !== "undefined" && console.error) {
126
+ console.error("Error validating Gun instance:", error);
127
+ }
128
+ throw new Error("Failed to validate Gun instance: ".concat(error));
129
+ }
130
+ try {
131
+ // Get SEA from Gun instance or global
132
+ var sea = this.core._gun.SEA || null;
133
+ if (!sea) {
134
+ // Try to find SEA in various global locations
135
+ if (typeof window !== "undefined" &&
136
+ window.Gun &&
137
+ window.Gun.SEA) {
138
+ sea = window.Gun.SEA;
139
+ }
140
+ else if (globalThis.Gun && globalThis.Gun.SEA) {
141
+ sea = globalThis.Gun.SEA;
142
+ }
143
+ else if (global.Gun && global.Gun.SEA) {
144
+ sea = global.Gun.SEA;
145
+ }
146
+ }
147
+ this.core.db = new DataBase(this.core._gun, "shogun", // Default app scope
148
+ this.core, sea);
149
+ return true;
150
+ }
151
+ catch (error) {
152
+ if (typeof console !== "undefined" && console.error) {
153
+ console.error("Error initializing DataBase:", error);
154
+ }
155
+ throw new Error("Failed to initialize DataBase: ".concat(error));
156
+ }
157
+ };
158
+ /**
159
+ * Initialize Gun user
160
+ */
161
+ CoreInitializer.prototype.initializeGunUser = function () {
162
+ var _this = this;
163
+ try {
164
+ this.core._user = this.core.gun.user().recall({ sessionStorage: true });
165
+ }
166
+ catch (error) {
167
+ if (typeof console !== "undefined" && console.error) {
168
+ console.error("Error initializing Gun user:", error);
169
+ }
170
+ throw new Error("Failed to initialize Gun user: ".concat(error));
171
+ }
172
+ this.core.gun.on("auth", function (user) {
173
+ _this.core._user = _this.core.gun.user().recall({ sessionStorage: true });
174
+ _this.core.emit("auth:login", {
175
+ userPub: user.pub,
176
+ method: "password",
177
+ });
178
+ });
179
+ };
180
+ /**
181
+ * Setup Gun event forwarding
182
+ */
183
+ CoreInitializer.prototype.setupGunEventForwarding = function () {
184
+ var _this = this;
185
+ var gunEvents = ["gun:put", "gun:get", "gun:set", "gun:remove"];
186
+ gunEvents.forEach(function (eventName) {
187
+ _this.core.db.on(eventName, function (data) {
188
+ _this.core.emit(eventName, data);
189
+ });
190
+ });
191
+ var peerEvents = [
192
+ "gun:peer:add",
193
+ "gun:peer:remove",
194
+ "gun:peer:connect",
195
+ "gun:peer:disconnect",
196
+ ];
197
+ peerEvents.forEach(function (eventName) {
198
+ _this.core.db.on(eventName, function (data) {
199
+ _this.core.emit(eventName, data);
200
+ });
201
+ });
202
+ };
203
+ /**
204
+ * Setup wallet derivation
205
+ */
206
+ CoreInitializer.prototype.setupWalletDerivation = function () {
207
+ var _this = this;
208
+ this.core.gun.on("auth", function (user) { return __awaiter(_this, void 0, void 0, function () {
209
+ var priv, pub, _a;
210
+ var _b, _c, _d, _e;
211
+ return __generator(this, function (_f) {
212
+ switch (_f.label) {
213
+ case 0:
214
+ if (!user.is)
215
+ return [2 /*return*/];
216
+ priv = (_c = (_b = user._) === null || _b === void 0 ? void 0 : _b.sea) === null || _c === void 0 ? void 0 : _c.epriv;
217
+ pub = (_e = (_d = user._) === null || _d === void 0 ? void 0 : _d.sea) === null || _e === void 0 ? void 0 : _e.epub;
218
+ _a = this.core;
219
+ return [4 /*yield*/, derive(priv, pub, {
220
+ includeSecp256k1Bitcoin: true,
221
+ includeSecp256k1Ethereum: true,
222
+ })];
223
+ case 1:
224
+ _a.wallets = _f.sent();
225
+ return [2 /*return*/];
226
+ }
227
+ });
228
+ }); });
229
+ };
230
+ /**
231
+ * Register built-in plugins based on configuration
232
+ */
233
+ CoreInitializer.prototype.registerBuiltinPlugins = function (config) {
234
+ try {
235
+ // Register WebAuthn plugin if configuration is provided
236
+ if (config.webauthn) {
237
+ if (typeof console !== "undefined" && console.warn) {
238
+ console.warn("WebAuthn plugin will be registered with provided configuration");
239
+ }
240
+ var webauthnPlugin = new WebauthnPlugin();
241
+ if (typeof webauthnPlugin.configure === "function") {
242
+ webauthnPlugin.configure(config.webauthn);
243
+ }
244
+ this.core.pluginManager.register(webauthnPlugin);
245
+ }
246
+ // Register Web3 plugin if configuration is provided
247
+ if (config.web3) {
248
+ if (typeof console !== "undefined" && console.warn) {
249
+ console.warn("Web3 plugin will be registered with provided configuration");
250
+ }
251
+ var web3Plugin = new Web3ConnectorPlugin();
252
+ if (typeof web3Plugin.configure === "function") {
253
+ web3Plugin.configure(config.web3);
254
+ }
255
+ this.core.pluginManager.register(web3Plugin);
256
+ }
257
+ // Register Nostr plugin if configuration is provided
258
+ if (config.nostr) {
259
+ if (typeof console !== "undefined" && console.warn) {
260
+ console.warn("Nostr plugin will be registered with provided configuration");
261
+ }
262
+ var nostrPlugin = new NostrConnectorPlugin();
263
+ if (typeof nostrPlugin.configure === "function") {
264
+ nostrPlugin.configure(config.nostr);
265
+ }
266
+ this.core.pluginManager.register(nostrPlugin);
267
+ }
268
+ // Register ZK-Proof plugin if configuration is provided
269
+ if (config.zkproof) {
270
+ if (typeof console !== "undefined" && console.warn) {
271
+ console.warn("ZK-Proof plugin will be registered with provided configuration");
272
+ }
273
+ var zkproofPlugin = new ZkProofPlugin(config.zkproof);
274
+ this.core.pluginManager.register(zkproofPlugin);
275
+ }
276
+ }
277
+ catch (error) {
278
+ if (typeof console !== "undefined" && console.error) {
279
+ console.error("Error registering builtin plugins:", error);
280
+ }
281
+ }
282
+ };
283
+ /**
284
+ * Initialize async components
285
+ */
286
+ CoreInitializer.prototype.initializeDb = function () {
287
+ try {
288
+ this.core.db.initialize();
289
+ this.core.emit("debug", {
290
+ action: "core_initialized",
291
+ timestamp: Date.now(),
292
+ });
293
+ return true;
294
+ }
295
+ catch (error) {
296
+ if (typeof console !== "undefined" && console.error) {
297
+ console.error("Error during Shogun Core initialization:", error);
298
+ }
299
+ return false;
300
+ }
301
+ };
302
+ return CoreInitializer;
303
+ }());
304
+ export { CoreInitializer };