shogun-core 6.2.4 → 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 +138979 -146789
  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 -392
  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
@@ -1,496 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Webauthn = void 0;
7
- exports.deriveWebauthnKeys = deriveWebauthnKeys;
8
- /**
9
- * Constants for WebAuthn configuration
10
- */
11
- const MIN_USERNAME_LENGTH = 3;
12
- const MAX_USERNAME_LENGTH = 64;
13
- const ethers_1 = require("ethers");
14
- const errorHandler_1 = require("../../utils/errorHandler");
15
- const eventEmitter_1 = require("../../utils/eventEmitter");
16
- const types_1 = require("./types");
17
- const derive_1 = __importDefault(require("../../gundb/derive"));
18
- const seedPhrase_1 = require("../../utils/seedPhrase");
19
- /**
20
- * Constants for WebAuthn configuration
21
- */
22
- const DEFAULT_CONFIG = {
23
- rpName: "Shogun Wallet",
24
- timeout: 60000,
25
- userVerification: "preferred",
26
- attestation: "none",
27
- authenticatorAttachment: "platform",
28
- requireResidentKey: false,
29
- };
30
- /**
31
- * Main WebAuthn class for authentication management
32
- */
33
- class Webauthn extends eventEmitter_1.EventEmitter {
34
- /**
35
- * Creates a new WebAuthn instance
36
- */
37
- constructor(gunInstance, config) {
38
- super();
39
- this.abortController = null;
40
- this.gunInstance = gunInstance;
41
- this.credential = null;
42
- // Merge default config with provided config
43
- this.config = {
44
- ...DEFAULT_CONFIG,
45
- ...config,
46
- rpId: config?.rpId ??
47
- (typeof window !== "undefined" &&
48
- window.location &&
49
- window.location.hostname
50
- ? window.location.hostname.split(":")[0]
51
- : "localhost"),
52
- };
53
- }
54
- /**
55
- * Validates a username
56
- */
57
- validateUsername(username) {
58
- if (!username || typeof username !== "string") {
59
- throw new Error("Username must be a non-empty string");
60
- }
61
- if (username.length < MIN_USERNAME_LENGTH ||
62
- username.length > MAX_USERNAME_LENGTH) {
63
- throw new Error(`Username must be between ${MIN_USERNAME_LENGTH} and ${MAX_USERNAME_LENGTH} characters`);
64
- }
65
- if (!/^[a-zA-Z0-9_-]+$/.test(username)) {
66
- throw new Error("Username can only contain letters, numbers, underscores and hyphens");
67
- }
68
- }
69
- /**
70
- * Creates a new WebAuthn account with retry logic
71
- */
72
- async createAccount(username, credentials, isNewDevice = false) {
73
- try {
74
- this.validateUsername(username);
75
- const maxRetries = 3;
76
- let lastError = null;
77
- for (let attempt = 1; attempt <= maxRetries; attempt++) {
78
- try {
79
- const result = await this.generateCredentials(username, credentials, isNewDevice);
80
- if (result.success) {
81
- this.emit(types_1.WebAuthnEventType.DEVICE_REGISTERED, {
82
- type: types_1.WebAuthnEventType.DEVICE_REGISTERED,
83
- data: { username },
84
- timestamp: Date.now(),
85
- });
86
- return result;
87
- }
88
- lastError = new Error(result.error ?? "Unknown error");
89
- }
90
- catch (error) {
91
- lastError = error;
92
- if (attempt < maxRetries) {
93
- await new Promise((resolve) => setTimeout(resolve, 1000 * attempt));
94
- continue;
95
- }
96
- }
97
- }
98
- throw lastError || new Error("Failed to create account after retries");
99
- }
100
- catch (error) {
101
- this.emit(types_1.WebAuthnEventType.ERROR, {
102
- type: types_1.WebAuthnEventType.ERROR,
103
- data: { error: error.message },
104
- timestamp: Date.now(),
105
- });
106
- throw error;
107
- }
108
- }
109
- /**
110
- * Authenticates a user with timeout and abort handling
111
- */
112
- async authenticateUser(username, salt, options = {}) {
113
- try {
114
- this.validateUsername(username);
115
- if (!salt) {
116
- const error = new Error("No WebAuthn credentials found for this username");
117
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.WEBAUTHN, "NO_CREDENTIALS", error.message, error);
118
- return { success: false, error: error.message };
119
- }
120
- // Cancel any existing authentication attempt
121
- this.abortAuthentication();
122
- // Create new abort controller
123
- this.abortController = new AbortController();
124
- const timeout = options.timeout || this.config.timeout;
125
- const timeoutId = setTimeout(() => this.abortController?.abort(), timeout);
126
- try {
127
- const challenge = this.generateChallenge(username);
128
- const assertionOptions = {
129
- challenge,
130
- allowCredentials: [],
131
- timeout,
132
- userVerification: options.userVerification || this.config.userVerification,
133
- rpId: this.config.rpId,
134
- };
135
- const assertion = (await navigator.credentials.get({
136
- publicKey: assertionOptions,
137
- signal: this.abortController.signal,
138
- }));
139
- if (!assertion) {
140
- throw new Error("WebAuthn verification failed");
141
- }
142
- const { password } = this.generateCredentialsFromSalt(username, salt);
143
- const deviceInfo = this.getDeviceInfo(assertion.id);
144
- const result = {
145
- success: true,
146
- username,
147
- password,
148
- credentialId: this.bufferToBase64(assertion.rawId),
149
- deviceInfo,
150
- };
151
- this.emit(types_1.WebAuthnEventType.AUTHENTICATION_SUCCESS, {
152
- type: types_1.WebAuthnEventType.AUTHENTICATION_SUCCESS,
153
- data: { username, deviceInfo },
154
- timestamp: Date.now(),
155
- });
156
- return result;
157
- }
158
- finally {
159
- clearTimeout(timeoutId);
160
- this.abortController = null;
161
- }
162
- }
163
- catch (error) {
164
- const errorMessage = error instanceof Error ? error.message : "Unknown WebAuthn error";
165
- this.emit(types_1.WebAuthnEventType.AUTHENTICATION_FAILED, {
166
- type: types_1.WebAuthnEventType.AUTHENTICATION_FAILED,
167
- data: { username, error: errorMessage },
168
- timestamp: Date.now(),
169
- });
170
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.WEBAUTHN, "AUTH_ERROR", errorMessage, error);
171
- return { success: false, error: errorMessage };
172
- }
173
- }
174
- /**
175
- * Aborts current authentication attempt
176
- */
177
- abortAuthentication() {
178
- if (this.abortController) {
179
- this.abortController.abort();
180
- this.abortController = null;
181
- }
182
- }
183
- /**
184
- * Gets device information
185
- */
186
- getDeviceInfo(credentialId) {
187
- const platformInfo = this.getPlatformInfo();
188
- return {
189
- deviceId: credentialId,
190
- timestamp: Date.now(),
191
- name: platformInfo.name,
192
- platform: platformInfo.platform,
193
- lastUsed: Date.now(),
194
- };
195
- }
196
- /**
197
- * Gets platform information
198
- */
199
- getPlatformInfo() {
200
- if (typeof navigator === "undefined") {
201
- return { name: "unknown", platform: "unknown" };
202
- }
203
- const platform = navigator.platform;
204
- const userAgent = navigator.userAgent;
205
- if (/iPhone|iPad|iPod/.test(platform)) {
206
- return { name: "iOS Device", platform };
207
- }
208
- if (/Android/.test(userAgent)) {
209
- return { name: "Android Device", platform };
210
- }
211
- if (/Win/.test(platform)) {
212
- return { name: "Windows Device", platform };
213
- }
214
- if (/Mac/.test(platform)) {
215
- return { name: "Mac Device", platform };
216
- }
217
- if (/Linux/.test(platform)) {
218
- return { name: "Linux Device", platform };
219
- }
220
- return { name: "Unknown Device", platform };
221
- }
222
- /**
223
- * Generates a challenge for WebAuthn operations
224
- */
225
- generateChallenge(username) {
226
- const timestamp = Date.now().toString();
227
- const randomBytes = this.getRandomBytes(32);
228
- const challengeData = `${username}-${timestamp}-${this.uint8ArrayToHex(randomBytes)}`;
229
- return new TextEncoder().encode(challengeData);
230
- }
231
- /**
232
- * Gets cryptographically secure random bytes
233
- */
234
- getRandomBytes(length) {
235
- if (typeof window !== "undefined" && window.crypto) {
236
- return window.crypto.getRandomValues(new Uint8Array(length));
237
- }
238
- throw new Error("No cryptographic implementation available");
239
- }
240
- /**
241
- * Converts Uint8Array to hexadecimal string
242
- */
243
- uint8ArrayToHex(arr) {
244
- return Array.from(arr)
245
- .map((b) => b.toString(16).padStart(2, "0"))
246
- .join("");
247
- }
248
- /**
249
- * Converts ArrayBuffer to URL-safe base64 string
250
- */
251
- bufferToBase64(buffer) {
252
- const bytes = new Uint8Array(buffer);
253
- const binary = bytes.reduce((str, byte) => str + String.fromCharCode(byte), "");
254
- return btoa(binary)
255
- .replace(/\+/g, "-")
256
- .replace(/\//g, "_")
257
- .replace(/=/g, "");
258
- }
259
- /**
260
- * Generates credentials from username and salt
261
- */
262
- generateCredentialsFromSalt(username, salt) {
263
- const data = ethers_1.ethers.toUtf8Bytes(username + salt);
264
- return {
265
- password: ethers_1.ethers.sha256(data),
266
- };
267
- }
268
- /**
269
- * Checks if WebAuthn is supported
270
- */
271
- isSupported() {
272
- return (typeof window !== "undefined" && window.PublicKeyCredential !== undefined);
273
- }
274
- /**
275
- * Creates a WebAuthn credential for registration
276
- */
277
- async createCredential(username) {
278
- try {
279
- const challenge = crypto.getRandomValues(new Uint8Array(32));
280
- const userId = new TextEncoder().encode(username);
281
- const publicKeyCredentialCreationOptions = {
282
- challenge,
283
- rp: {
284
- name: "Shogun Wallet",
285
- ...(this.config.rpId !== "localhost" && { id: this.config.rpId }),
286
- },
287
- user: {
288
- id: userId,
289
- name: username,
290
- displayName: username,
291
- },
292
- pubKeyCredParams: [{ type: "public-key", alg: -7 }],
293
- timeout: this.config.timeout,
294
- attestation: this.config.attestation,
295
- authenticatorSelection: {
296
- authenticatorAttachment: this.config.authenticatorAttachment,
297
- userVerification: this.config.userVerification,
298
- requireResidentKey: this.config.requireResidentKey,
299
- },
300
- };
301
- const credential = await navigator.credentials.create({
302
- publicKey: publicKeyCredentialCreationOptions,
303
- });
304
- if (!credential) {
305
- throw new Error("Credential creation failed");
306
- }
307
- const webAuthnCredential = credential;
308
- // Convert to WebAuthnCredentialData
309
- const credentialData = {
310
- id: webAuthnCredential.id,
311
- rawId: webAuthnCredential.rawId,
312
- type: webAuthnCredential.type,
313
- response: {
314
- clientDataJSON: webAuthnCredential.response.clientDataJSON,
315
- },
316
- getClientExtensionResults: webAuthnCredential.getClientExtensionResults,
317
- };
318
- // Add additional response properties if available
319
- if ("attestationObject" in webAuthnCredential.response) {
320
- credentialData.response.attestationObject = webAuthnCredential.response.attestationObject;
321
- }
322
- this.credential = credentialData;
323
- return credentialData;
324
- }
325
- catch (error) {
326
- console.error("Detailed error in credential creation:", error);
327
- const errorMessage = error instanceof Error ? error.message : "Unknown error";
328
- throw new Error(`Error creating credentials: ${errorMessage}`);
329
- }
330
- }
331
- /**
332
- * Generates WebAuthn credentials (uniforme con altri plugin)
333
- */
334
- async generateCredentials(username, existingCredential, isLogin = false) {
335
- try {
336
- if (isLogin) {
337
- const verificationResult = await this.verifyCredential(username);
338
- if (!verificationResult.success || !verificationResult.credentialId) {
339
- return {
340
- success: false,
341
- username,
342
- key: undefined,
343
- credentialId: "",
344
- error: verificationResult.error,
345
- publicKey: null,
346
- };
347
- }
348
- // Deriva la chiave GunDB
349
- const key = await deriveWebauthnKeys(username, verificationResult.credentialId);
350
- return {
351
- success: true,
352
- username,
353
- key,
354
- credentialId: verificationResult.credentialId,
355
- publicKey: null,
356
- };
357
- }
358
- else {
359
- const credential = await this.createCredential(username);
360
- const credentialId = credential.id;
361
- let publicKey = null;
362
- if (credential?.response?.getPublicKey) {
363
- publicKey = credential.response.getPublicKey();
364
- }
365
- // Deriva la chiave GunDB
366
- const key = await deriveWebauthnKeys(username, credentialId);
367
- return {
368
- success: true,
369
- username,
370
- key,
371
- credentialId,
372
- publicKey,
373
- };
374
- }
375
- }
376
- catch (error) {
377
- console.error("Error in generateCredentials:", error);
378
- const errorMessage = error instanceof Error
379
- ? error.message
380
- : "Unknown error during WebAuthn operation";
381
- return {
382
- success: false,
383
- username,
384
- key: undefined,
385
- credentialId: "",
386
- error: errorMessage,
387
- publicKey: null,
388
- };
389
- }
390
- }
391
- /**
392
- * Verifies a credential
393
- */
394
- async verifyCredential(username) {
395
- try {
396
- const challenge = crypto.getRandomValues(new Uint8Array(32));
397
- const options = {
398
- challenge,
399
- timeout: this.config.timeout,
400
- userVerification: this.config.userVerification,
401
- ...(this.config.rpId !== "localhost" && { rpId: this.config.rpId }),
402
- };
403
- if (this.credential?.rawId) {
404
- options.allowCredentials = [
405
- {
406
- id: this.credential.rawId,
407
- type: "public-key",
408
- },
409
- ];
410
- }
411
- const assertion = await navigator.credentials.get({
412
- publicKey: options,
413
- });
414
- if (!assertion) {
415
- return {
416
- success: false,
417
- error: "Credential verification failed",
418
- };
419
- }
420
- return {
421
- success: true,
422
- credentialId: assertion.id,
423
- username,
424
- };
425
- }
426
- catch (error) {
427
- console.error("Error verifying credentials:", error);
428
- const errorMessage = error instanceof Error
429
- ? error.message
430
- : "Unknown error verifying credentials";
431
- return {
432
- success: false,
433
- error: errorMessage,
434
- };
435
- }
436
- }
437
- /**
438
- * Removes device credentials
439
- */
440
- async removeDevice(username, credentialId, credentials) {
441
- if (!credentials ||
442
- !credentials.credentials ||
443
- !credentials.credentials[credentialId]) {
444
- return { success: false };
445
- }
446
- const updatedCreds = { ...credentials };
447
- // Make sure credentials exists before modifying it
448
- if (updatedCreds.credentials) {
449
- delete updatedCreds.credentials[credentialId];
450
- }
451
- return {
452
- success: true,
453
- updatedCredentials: updatedCreds,
454
- };
455
- }
456
- /**
457
- * Signs data with the credential
458
- */
459
- async sign(data) {
460
- const signature = await navigator.credentials.get({
461
- publicKey: {
462
- challenge: new Uint8Array(16),
463
- rpId: this.config.rpId,
464
- },
465
- });
466
- return signature;
467
- }
468
- }
469
- exports.Webauthn = Webauthn;
470
- // Add to global scope if available
471
- if (typeof window !== "undefined") {
472
- window.Webauthn = Webauthn;
473
- }
474
- else if (typeof global !== "undefined") {
475
- global.Webauthn = Webauthn;
476
- }
477
- // Funzione helper per derivare chiavi WebAuthn
478
- // Supporta sia credentialId (legacy) che seed phrase (nuovo, multi-device)
479
- async function deriveWebauthnKeys(username, credentialIdOrSeedPhrase, useSeedPhrase = false) {
480
- if (useSeedPhrase) {
481
- // New method: derive from BIP39 seed phrase for multi-device support
482
- const { password, seed } = (0, seedPhrase_1.deriveCredentialsFromMnemonic)(credentialIdOrSeedPhrase, username);
483
- // Use the seed phrase-derived password for Gun key derivation
484
- return await (0, derive_1.default)(password, username, {
485
- includeP256: true,
486
- });
487
- }
488
- else {
489
- // Legacy method: derive from credentialId (device-bound)
490
- const hashedCredentialId = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(credentialIdOrSeedPhrase));
491
- const salt = `${username}_${credentialIdOrSeedPhrase}`;
492
- return await (0, derive_1.default)(hashedCredentialId, salt, {
493
- includeP256: true,
494
- });
495
- }
496
- }