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,647 @@
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = function (d, b) {
3
+ extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
+ return extendStatics(d, b);
7
+ };
8
+ return function (d, b) {
9
+ if (typeof b !== "function" && b !== null)
10
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
+ extendStatics(d, b);
12
+ function __() { this.constructor = d; }
13
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
+ };
15
+ })();
16
+ var __assign = (this && this.__assign) || function () {
17
+ __assign = Object.assign || function(t) {
18
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
19
+ s = arguments[i];
20
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
21
+ t[p] = s[p];
22
+ }
23
+ return t;
24
+ };
25
+ return __assign.apply(this, arguments);
26
+ };
27
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
28
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
29
+ return new (P || (P = Promise))(function (resolve, reject) {
30
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
31
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
32
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
33
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
34
+ });
35
+ };
36
+ var __generator = (this && this.__generator) || function (thisArg, body) {
37
+ 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);
38
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
39
+ function verb(n) { return function (v) { return step([n, v]); }; }
40
+ function step(op) {
41
+ if (f) throw new TypeError("Generator is already executing.");
42
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
43
+ 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;
44
+ if (y = 0, t) op = [op[0] & 2, t.value];
45
+ switch (op[0]) {
46
+ case 0: case 1: t = op; break;
47
+ case 4: _.label++; return { value: op[1], done: false };
48
+ case 5: _.label++; y = op[1]; op = [0]; continue;
49
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
50
+ default:
51
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
52
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
53
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
54
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
55
+ if (t[2]) _.ops.pop();
56
+ _.trys.pop(); continue;
57
+ }
58
+ op = body.call(thisArg, _);
59
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
60
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
61
+ }
62
+ };
63
+ /**
64
+ * Constants for WebAuthn configuration
65
+ */
66
+ var MIN_USERNAME_LENGTH = 3;
67
+ var MAX_USERNAME_LENGTH = 64;
68
+ import { ethers } from "ethers";
69
+ import { ErrorHandler, ErrorType } from "../../utils/errorHandler.js";
70
+ import { EventEmitter } from "../../utils/eventEmitter.js";
71
+ import { WebAuthnEventType, } from "./types.js";
72
+ import derive from "../../gundb/derive.js";
73
+ import { deriveCredentialsFromMnemonic } from "../../utils/seedPhrase.js";
74
+ /**
75
+ * Constants for WebAuthn configuration
76
+ */
77
+ var DEFAULT_CONFIG = {
78
+ rpName: "Shogun Wallet",
79
+ timeout: 60000,
80
+ userVerification: "preferred",
81
+ attestation: "none",
82
+ authenticatorAttachment: "platform",
83
+ requireResidentKey: false,
84
+ };
85
+ /**
86
+ * Main WebAuthn class for authentication management
87
+ */
88
+ var Webauthn = /** @class */ (function (_super) {
89
+ __extends(Webauthn, _super);
90
+ /**
91
+ * Creates a new WebAuthn instance
92
+ */
93
+ function Webauthn(gunInstance, config) {
94
+ var _a;
95
+ var _this = _super.call(this) || this;
96
+ _this.abortController = null;
97
+ _this.gunInstance = gunInstance;
98
+ _this.credential = null;
99
+ // Merge default config with provided config
100
+ _this.config = __assign(__assign(__assign({}, DEFAULT_CONFIG), config), { rpId: (_a = config === null || config === void 0 ? void 0 : config.rpId) !== null && _a !== void 0 ? _a : (typeof window !== "undefined" &&
101
+ window.location &&
102
+ window.location.hostname
103
+ ? window.location.hostname.split(":")[0]
104
+ : "localhost") });
105
+ return _this;
106
+ }
107
+ /**
108
+ * Validates a username
109
+ */
110
+ Webauthn.prototype.validateUsername = function (username) {
111
+ if (!username || typeof username !== "string") {
112
+ throw new Error("Username must be a non-empty string");
113
+ }
114
+ if (username.length < MIN_USERNAME_LENGTH ||
115
+ username.length > MAX_USERNAME_LENGTH) {
116
+ throw new Error("Username must be between ".concat(MIN_USERNAME_LENGTH, " and ").concat(MAX_USERNAME_LENGTH, " characters"));
117
+ }
118
+ if (!/^[a-zA-Z0-9_-]+$/.test(username)) {
119
+ throw new Error("Username can only contain letters, numbers, underscores and hyphens");
120
+ }
121
+ };
122
+ /**
123
+ * Creates a new WebAuthn account with retry logic
124
+ */
125
+ Webauthn.prototype.createAccount = function (username_1, credentials_1) {
126
+ return __awaiter(this, arguments, void 0, function (username, credentials, isNewDevice) {
127
+ var maxRetries, lastError, _loop_1, this_1, attempt, state_1, error_1;
128
+ var _a;
129
+ if (isNewDevice === void 0) { isNewDevice = false; }
130
+ return __generator(this, function (_b) {
131
+ switch (_b.label) {
132
+ case 0:
133
+ _b.trys.push([0, 5, , 6]);
134
+ this.validateUsername(username);
135
+ maxRetries = 3;
136
+ lastError = null;
137
+ _loop_1 = function (attempt) {
138
+ var result, error_2;
139
+ return __generator(this, function (_c) {
140
+ switch (_c.label) {
141
+ case 0:
142
+ _c.trys.push([0, 2, , 5]);
143
+ return [4 /*yield*/, this_1.generateCredentials(username, credentials, isNewDevice)];
144
+ case 1:
145
+ result = _c.sent();
146
+ if (result.success) {
147
+ this_1.emit(WebAuthnEventType.DEVICE_REGISTERED, {
148
+ type: WebAuthnEventType.DEVICE_REGISTERED,
149
+ data: { username: username },
150
+ timestamp: Date.now(),
151
+ });
152
+ return [2 /*return*/, { value: result }];
153
+ }
154
+ lastError = new Error((_a = result.error) !== null && _a !== void 0 ? _a : "Unknown error");
155
+ return [3 /*break*/, 5];
156
+ case 2:
157
+ error_2 = _c.sent();
158
+ lastError = error_2;
159
+ if (!(attempt < maxRetries)) return [3 /*break*/, 4];
160
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000 * attempt); })];
161
+ case 3:
162
+ _c.sent();
163
+ return [2 /*return*/, "continue"];
164
+ case 4: return [3 /*break*/, 5];
165
+ case 5: return [2 /*return*/];
166
+ }
167
+ });
168
+ };
169
+ this_1 = this;
170
+ attempt = 1;
171
+ _b.label = 1;
172
+ case 1:
173
+ if (!(attempt <= maxRetries)) return [3 /*break*/, 4];
174
+ return [5 /*yield**/, _loop_1(attempt)];
175
+ case 2:
176
+ state_1 = _b.sent();
177
+ if (typeof state_1 === "object")
178
+ return [2 /*return*/, state_1.value];
179
+ _b.label = 3;
180
+ case 3:
181
+ attempt++;
182
+ return [3 /*break*/, 1];
183
+ case 4: throw lastError || new Error("Failed to create account after retries");
184
+ case 5:
185
+ error_1 = _b.sent();
186
+ this.emit(WebAuthnEventType.ERROR, {
187
+ type: WebAuthnEventType.ERROR,
188
+ data: { error: error_1.message },
189
+ timestamp: Date.now(),
190
+ });
191
+ throw error_1;
192
+ case 6: return [2 /*return*/];
193
+ }
194
+ });
195
+ });
196
+ };
197
+ /**
198
+ * Authenticates a user with timeout and abort handling
199
+ */
200
+ Webauthn.prototype.authenticateUser = function (username_1, salt_1) {
201
+ return __awaiter(this, arguments, void 0, function (username, salt, options) {
202
+ var error, timeout, timeoutId, challenge, assertionOptions, assertion, password, deviceInfo, result, error_3, errorMessage;
203
+ var _this = this;
204
+ if (options === void 0) { options = {}; }
205
+ return __generator(this, function (_a) {
206
+ switch (_a.label) {
207
+ case 0:
208
+ _a.trys.push([0, 5, , 6]);
209
+ this.validateUsername(username);
210
+ if (!salt) {
211
+ error = new Error("No WebAuthn credentials found for this username");
212
+ ErrorHandler.handle(ErrorType.WEBAUTHN, "NO_CREDENTIALS", error.message, error);
213
+ return [2 /*return*/, { success: false, error: error.message }];
214
+ }
215
+ // Cancel any existing authentication attempt
216
+ this.abortAuthentication();
217
+ // Create new abort controller
218
+ this.abortController = new AbortController();
219
+ timeout = options.timeout || this.config.timeout;
220
+ timeoutId = setTimeout(function () { var _a; return (_a = _this.abortController) === null || _a === void 0 ? void 0 : _a.abort(); }, timeout);
221
+ _a.label = 1;
222
+ case 1:
223
+ _a.trys.push([1, , 3, 4]);
224
+ challenge = this.generateChallenge(username);
225
+ assertionOptions = {
226
+ challenge: challenge,
227
+ allowCredentials: [],
228
+ timeout: timeout,
229
+ userVerification: options.userVerification || this.config.userVerification,
230
+ rpId: this.config.rpId,
231
+ };
232
+ return [4 /*yield*/, navigator.credentials.get({
233
+ publicKey: assertionOptions,
234
+ signal: this.abortController.signal,
235
+ })];
236
+ case 2:
237
+ assertion = (_a.sent());
238
+ if (!assertion) {
239
+ throw new Error("WebAuthn verification failed");
240
+ }
241
+ password = this.generateCredentialsFromSalt(username, salt).password;
242
+ deviceInfo = this.getDeviceInfo(assertion.id);
243
+ result = {
244
+ success: true,
245
+ username: username,
246
+ password: password,
247
+ credentialId: this.bufferToBase64(assertion.rawId),
248
+ deviceInfo: deviceInfo,
249
+ };
250
+ this.emit(WebAuthnEventType.AUTHENTICATION_SUCCESS, {
251
+ type: WebAuthnEventType.AUTHENTICATION_SUCCESS,
252
+ data: { username: username, deviceInfo: deviceInfo },
253
+ timestamp: Date.now(),
254
+ });
255
+ return [2 /*return*/, result];
256
+ case 3:
257
+ clearTimeout(timeoutId);
258
+ this.abortController = null;
259
+ return [7 /*endfinally*/];
260
+ case 4: return [3 /*break*/, 6];
261
+ case 5:
262
+ error_3 = _a.sent();
263
+ errorMessage = error_3 instanceof Error ? error_3.message : "Unknown WebAuthn error";
264
+ this.emit(WebAuthnEventType.AUTHENTICATION_FAILED, {
265
+ type: WebAuthnEventType.AUTHENTICATION_FAILED,
266
+ data: { username: username, error: errorMessage },
267
+ timestamp: Date.now(),
268
+ });
269
+ ErrorHandler.handle(ErrorType.WEBAUTHN, "AUTH_ERROR", errorMessage, error_3);
270
+ return [2 /*return*/, { success: false, error: errorMessage }];
271
+ case 6: return [2 /*return*/];
272
+ }
273
+ });
274
+ });
275
+ };
276
+ /**
277
+ * Aborts current authentication attempt
278
+ */
279
+ Webauthn.prototype.abortAuthentication = function () {
280
+ if (this.abortController) {
281
+ this.abortController.abort();
282
+ this.abortController = null;
283
+ }
284
+ };
285
+ /**
286
+ * Gets device information
287
+ */
288
+ Webauthn.prototype.getDeviceInfo = function (credentialId) {
289
+ var platformInfo = this.getPlatformInfo();
290
+ return {
291
+ deviceId: credentialId,
292
+ timestamp: Date.now(),
293
+ name: platformInfo.name,
294
+ platform: platformInfo.platform,
295
+ lastUsed: Date.now(),
296
+ };
297
+ };
298
+ /**
299
+ * Gets platform information
300
+ */
301
+ Webauthn.prototype.getPlatformInfo = function () {
302
+ if (typeof navigator === "undefined") {
303
+ return { name: "unknown", platform: "unknown" };
304
+ }
305
+ var platform = navigator.platform;
306
+ var userAgent = navigator.userAgent;
307
+ if (/iPhone|iPad|iPod/.test(platform)) {
308
+ return { name: "iOS Device", platform: platform };
309
+ }
310
+ if (/Android/.test(userAgent)) {
311
+ return { name: "Android Device", platform: platform };
312
+ }
313
+ if (/Win/.test(platform)) {
314
+ return { name: "Windows Device", platform: platform };
315
+ }
316
+ if (/Mac/.test(platform)) {
317
+ return { name: "Mac Device", platform: platform };
318
+ }
319
+ if (/Linux/.test(platform)) {
320
+ return { name: "Linux Device", platform: platform };
321
+ }
322
+ return { name: "Unknown Device", platform: platform };
323
+ };
324
+ /**
325
+ * Generates a challenge for WebAuthn operations
326
+ */
327
+ Webauthn.prototype.generateChallenge = function (username) {
328
+ var timestamp = Date.now().toString();
329
+ var randomBytes = this.getRandomBytes(32);
330
+ var challengeData = "".concat(username, "-").concat(timestamp, "-").concat(this.uint8ArrayToHex(randomBytes));
331
+ return new TextEncoder().encode(challengeData);
332
+ };
333
+ /**
334
+ * Gets cryptographically secure random bytes
335
+ */
336
+ Webauthn.prototype.getRandomBytes = function (length) {
337
+ if (typeof window !== "undefined" && window.crypto) {
338
+ return window.crypto.getRandomValues(new Uint8Array(length));
339
+ }
340
+ throw new Error("No cryptographic implementation available");
341
+ };
342
+ /**
343
+ * Converts Uint8Array to hexadecimal string
344
+ */
345
+ Webauthn.prototype.uint8ArrayToHex = function (arr) {
346
+ return Array.from(arr)
347
+ .map(function (b) { return b.toString(16).padStart(2, "0"); })
348
+ .join("");
349
+ };
350
+ /**
351
+ * Converts ArrayBuffer to URL-safe base64 string
352
+ */
353
+ Webauthn.prototype.bufferToBase64 = function (buffer) {
354
+ var bytes = new Uint8Array(buffer);
355
+ var binary = bytes.reduce(function (str, byte) { return str + String.fromCharCode(byte); }, "");
356
+ return btoa(binary)
357
+ .replace(/\+/g, "-")
358
+ .replace(/\//g, "_")
359
+ .replace(/=/g, "");
360
+ };
361
+ /**
362
+ * Generates credentials from username and salt
363
+ */
364
+ Webauthn.prototype.generateCredentialsFromSalt = function (username, salt) {
365
+ var data = ethers.toUtf8Bytes(username + salt);
366
+ return {
367
+ password: ethers.sha256(data),
368
+ };
369
+ };
370
+ /**
371
+ * Checks if WebAuthn is supported
372
+ */
373
+ Webauthn.prototype.isSupported = function () {
374
+ return (typeof window !== "undefined" && window.PublicKeyCredential !== undefined);
375
+ };
376
+ /**
377
+ * Creates a WebAuthn credential for registration
378
+ */
379
+ Webauthn.prototype.createCredential = function (username) {
380
+ return __awaiter(this, void 0, void 0, function () {
381
+ var challenge, userId, publicKeyCredentialCreationOptions, credential, webAuthnCredential, credentialData, error_4, errorMessage;
382
+ return __generator(this, function (_a) {
383
+ switch (_a.label) {
384
+ case 0:
385
+ _a.trys.push([0, 2, , 3]);
386
+ challenge = crypto.getRandomValues(new Uint8Array(32));
387
+ userId = new TextEncoder().encode(username);
388
+ publicKeyCredentialCreationOptions = {
389
+ challenge: challenge,
390
+ rp: __assign({ name: "Shogun Wallet" }, (this.config.rpId !== "localhost" && { id: this.config.rpId })),
391
+ user: {
392
+ id: userId,
393
+ name: username,
394
+ displayName: username,
395
+ },
396
+ pubKeyCredParams: [{ type: "public-key", alg: -7 }],
397
+ timeout: this.config.timeout,
398
+ attestation: this.config.attestation,
399
+ authenticatorSelection: {
400
+ authenticatorAttachment: this.config.authenticatorAttachment,
401
+ userVerification: this.config.userVerification,
402
+ requireResidentKey: this.config.requireResidentKey,
403
+ },
404
+ };
405
+ return [4 /*yield*/, navigator.credentials.create({
406
+ publicKey: publicKeyCredentialCreationOptions,
407
+ })];
408
+ case 1:
409
+ credential = _a.sent();
410
+ if (!credential) {
411
+ throw new Error("Credential creation failed");
412
+ }
413
+ webAuthnCredential = credential;
414
+ credentialData = {
415
+ id: webAuthnCredential.id,
416
+ rawId: webAuthnCredential.rawId,
417
+ type: webAuthnCredential.type,
418
+ response: {
419
+ clientDataJSON: webAuthnCredential.response.clientDataJSON,
420
+ },
421
+ getClientExtensionResults: webAuthnCredential.getClientExtensionResults,
422
+ };
423
+ // Add additional response properties if available
424
+ if ("attestationObject" in webAuthnCredential.response) {
425
+ credentialData.response.attestationObject = webAuthnCredential.response.attestationObject;
426
+ }
427
+ this.credential = credentialData;
428
+ return [2 /*return*/, credentialData];
429
+ case 2:
430
+ error_4 = _a.sent();
431
+ console.error("Detailed error in credential creation:", error_4);
432
+ errorMessage = error_4 instanceof Error ? error_4.message : "Unknown error";
433
+ throw new Error("Error creating credentials: ".concat(errorMessage));
434
+ case 3: return [2 /*return*/];
435
+ }
436
+ });
437
+ });
438
+ };
439
+ /**
440
+ * Generates WebAuthn credentials (uniforme con altri plugin)
441
+ */
442
+ Webauthn.prototype.generateCredentials = function (username_1, existingCredential_1) {
443
+ return __awaiter(this, arguments, void 0, function (username, existingCredential, isLogin) {
444
+ var verificationResult, key, credential, credentialId, publicKey, key, error_5, errorMessage;
445
+ var _a;
446
+ if (isLogin === void 0) { isLogin = false; }
447
+ return __generator(this, function (_b) {
448
+ switch (_b.label) {
449
+ case 0:
450
+ _b.trys.push([0, 7, , 8]);
451
+ if (!isLogin) return [3 /*break*/, 3];
452
+ return [4 /*yield*/, this.verifyCredential(username)];
453
+ case 1:
454
+ verificationResult = _b.sent();
455
+ if (!verificationResult.success || !verificationResult.credentialId) {
456
+ return [2 /*return*/, {
457
+ success: false,
458
+ username: username,
459
+ key: undefined,
460
+ credentialId: "",
461
+ error: verificationResult.error,
462
+ publicKey: null,
463
+ }];
464
+ }
465
+ return [4 /*yield*/, deriveWebauthnKeys(username, verificationResult.credentialId)];
466
+ case 2:
467
+ key = _b.sent();
468
+ return [2 /*return*/, {
469
+ success: true,
470
+ username: username,
471
+ key: key,
472
+ credentialId: verificationResult.credentialId,
473
+ publicKey: null,
474
+ }];
475
+ case 3: return [4 /*yield*/, this.createCredential(username)];
476
+ case 4:
477
+ credential = _b.sent();
478
+ credentialId = credential.id;
479
+ publicKey = null;
480
+ if ((_a = credential === null || credential === void 0 ? void 0 : credential.response) === null || _a === void 0 ? void 0 : _a.getPublicKey) {
481
+ publicKey = credential.response.getPublicKey();
482
+ }
483
+ return [4 /*yield*/, deriveWebauthnKeys(username, credentialId)];
484
+ case 5:
485
+ key = _b.sent();
486
+ return [2 /*return*/, {
487
+ success: true,
488
+ username: username,
489
+ key: key,
490
+ credentialId: credentialId,
491
+ publicKey: publicKey,
492
+ }];
493
+ case 6: return [3 /*break*/, 8];
494
+ case 7:
495
+ error_5 = _b.sent();
496
+ console.error("Error in generateCredentials:", error_5);
497
+ errorMessage = error_5 instanceof Error
498
+ ? error_5.message
499
+ : "Unknown error during WebAuthn operation";
500
+ return [2 /*return*/, {
501
+ success: false,
502
+ username: username,
503
+ key: undefined,
504
+ credentialId: "",
505
+ error: errorMessage,
506
+ publicKey: null,
507
+ }];
508
+ case 8: return [2 /*return*/];
509
+ }
510
+ });
511
+ });
512
+ };
513
+ /**
514
+ * Verifies a credential
515
+ */
516
+ Webauthn.prototype.verifyCredential = function (username) {
517
+ return __awaiter(this, void 0, void 0, function () {
518
+ var challenge, options, assertion, error_6, errorMessage;
519
+ var _a;
520
+ return __generator(this, function (_b) {
521
+ switch (_b.label) {
522
+ case 0:
523
+ _b.trys.push([0, 2, , 3]);
524
+ challenge = crypto.getRandomValues(new Uint8Array(32));
525
+ options = __assign({ challenge: challenge, timeout: this.config.timeout, userVerification: this.config.userVerification }, (this.config.rpId !== "localhost" && { rpId: this.config.rpId }));
526
+ if ((_a = this.credential) === null || _a === void 0 ? void 0 : _a.rawId) {
527
+ options.allowCredentials = [
528
+ {
529
+ id: this.credential.rawId,
530
+ type: "public-key",
531
+ },
532
+ ];
533
+ }
534
+ return [4 /*yield*/, navigator.credentials.get({
535
+ publicKey: options,
536
+ })];
537
+ case 1:
538
+ assertion = _b.sent();
539
+ if (!assertion) {
540
+ return [2 /*return*/, {
541
+ success: false,
542
+ error: "Credential verification failed",
543
+ }];
544
+ }
545
+ return [2 /*return*/, {
546
+ success: true,
547
+ credentialId: assertion.id,
548
+ username: username,
549
+ }];
550
+ case 2:
551
+ error_6 = _b.sent();
552
+ console.error("Error verifying credentials:", error_6);
553
+ errorMessage = error_6 instanceof Error
554
+ ? error_6.message
555
+ : "Unknown error verifying credentials";
556
+ return [2 /*return*/, {
557
+ success: false,
558
+ error: errorMessage,
559
+ }];
560
+ case 3: return [2 /*return*/];
561
+ }
562
+ });
563
+ });
564
+ };
565
+ /**
566
+ * Removes device credentials
567
+ */
568
+ Webauthn.prototype.removeDevice = function (username, credentialId, credentials) {
569
+ return __awaiter(this, void 0, void 0, function () {
570
+ var updatedCreds;
571
+ return __generator(this, function (_a) {
572
+ if (!credentials ||
573
+ !credentials.credentials ||
574
+ !credentials.credentials[credentialId]) {
575
+ return [2 /*return*/, { success: false }];
576
+ }
577
+ updatedCreds = __assign({}, credentials);
578
+ // Make sure credentials exists before modifying it
579
+ if (updatedCreds.credentials) {
580
+ delete updatedCreds.credentials[credentialId];
581
+ }
582
+ return [2 /*return*/, {
583
+ success: true,
584
+ updatedCredentials: updatedCreds,
585
+ }];
586
+ });
587
+ });
588
+ };
589
+ /**
590
+ * Signs data with the credential
591
+ */
592
+ Webauthn.prototype.sign = function (data) {
593
+ return __awaiter(this, void 0, void 0, function () {
594
+ var signature;
595
+ return __generator(this, function (_a) {
596
+ switch (_a.label) {
597
+ case 0: return [4 /*yield*/, navigator.credentials.get({
598
+ publicKey: {
599
+ challenge: new Uint8Array(16),
600
+ rpId: this.config.rpId,
601
+ },
602
+ })];
603
+ case 1:
604
+ signature = _a.sent();
605
+ return [2 /*return*/, signature];
606
+ }
607
+ });
608
+ });
609
+ };
610
+ return Webauthn;
611
+ }(EventEmitter));
612
+ export { Webauthn };
613
+ // Add to global scope if available
614
+ if (typeof window !== "undefined") {
615
+ window.Webauthn = Webauthn;
616
+ }
617
+ else if (typeof global !== "undefined") {
618
+ global.Webauthn = Webauthn;
619
+ }
620
+ // Funzione helper per derivare chiavi WebAuthn
621
+ // Supporta sia credentialId (legacy) che seed phrase (nuovo, multi-device)
622
+ export function deriveWebauthnKeys(username_1, credentialIdOrSeedPhrase_1) {
623
+ return __awaiter(this, arguments, void 0, function (username, credentialIdOrSeedPhrase, useSeedPhrase) {
624
+ var _a, password, seed, hashedCredentialId, salt;
625
+ if (useSeedPhrase === void 0) { useSeedPhrase = false; }
626
+ return __generator(this, function (_b) {
627
+ switch (_b.label) {
628
+ case 0:
629
+ if (!useSeedPhrase) return [3 /*break*/, 2];
630
+ _a = deriveCredentialsFromMnemonic(credentialIdOrSeedPhrase, username), password = _a.password, seed = _a.seed;
631
+ return [4 /*yield*/, derive(password, username, {
632
+ includeP256: true,
633
+ })];
634
+ case 1:
635
+ // Use the seed phrase-derived password for Gun key derivation
636
+ return [2 /*return*/, _b.sent()];
637
+ case 2:
638
+ hashedCredentialId = ethers.keccak256(ethers.toUtf8Bytes(credentialIdOrSeedPhrase));
639
+ salt = "".concat(username, "_").concat(credentialIdOrSeedPhrase);
640
+ return [4 /*yield*/, derive(hashedCredentialId, salt, {
641
+ includeP256: true,
642
+ })];
643
+ case 3: return [2 /*return*/, _b.sent()];
644
+ }
645
+ });
646
+ });
647
+ }