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
@@ -1,490 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WebauthnPlugin = void 0;
4
- const base_1 = require("../base");
5
- const webauthn_1 = require("./webauthn");
6
- const webauthnSigner_1 = require("./webauthnSigner");
7
- const errorHandler_1 = require("../../utils/errorHandler");
8
- const seedPhrase_1 = require("../../utils/seedPhrase");
9
- const webauthn_2 = require("./webauthn");
10
- /**
11
- * Plugin per la gestione delle funzionalità WebAuthn in ShogunCore
12
- */
13
- class WebauthnPlugin extends base_1.BasePlugin {
14
- constructor() {
15
- super(...arguments);
16
- this.name = "webauthn";
17
- this.version = "1.0.0";
18
- this.description = "Provides WebAuthn authentication functionality for ShogunCore";
19
- this.webauthn = null;
20
- this.signer = null;
21
- }
22
- /**
23
- * @inheritdoc
24
- */
25
- initialize(core) {
26
- super.initialize(core);
27
- // Verifica se siamo in ambiente browser
28
- if (typeof window === "undefined") {
29
- console.warn("[webauthnPlugin] WebAuthn plugin disabled - not in browser environment");
30
- return;
31
- }
32
- // Verifica se WebAuthn è supportato
33
- if (!this.isSupported()) {
34
- console.warn("[webauthnPlugin] WebAuthn not supported in this environment");
35
- return;
36
- }
37
- // Inizializziamo il modulo WebAuthn
38
- this.webauthn = new webauthn_1.Webauthn(core.gun);
39
- this.signer = new webauthnSigner_1.WebAuthnSigner(this.webauthn);
40
- console.log("[webauthnPlugin] WebAuthn plugin initialized with signer support");
41
- }
42
- /**
43
- * @inheritdoc
44
- */
45
- destroy() {
46
- this.webauthn = null;
47
- this.signer = null;
48
- super.destroy();
49
- console.log("[webauthnPlugin] WebAuthn plugin destroyed");
50
- }
51
- /**
52
- * Assicura che il modulo Webauthn sia inizializzato
53
- * @private
54
- */
55
- assertWebauthn() {
56
- this.assertInitialized();
57
- if (!this.webauthn) {
58
- throw new Error("WebAuthn module not initialized");
59
- }
60
- return this.webauthn;
61
- }
62
- /**
63
- * Assicura che il signer sia inizializzato
64
- * @private
65
- */
66
- assertSigner() {
67
- this.assertInitialized();
68
- if (!this.signer) {
69
- throw new Error("WebAuthn signer not initialized");
70
- }
71
- return this.signer;
72
- }
73
- /**
74
- * Genera un pair SEA dalle credenziali WebAuthn
75
- * @private
76
- */
77
- async generatePairFromCredentials(credentials) {
78
- try {
79
- // Use the signer to create a derived key pair from the WebAuthn credentials
80
- const pair = await this.assertSigner().createDerivedKeyPair(credentials.credentialId, credentials.username);
81
- return pair;
82
- }
83
- catch (error) {
84
- console.error("Error generating pair from WebAuthn credentials:", error);
85
- return null;
86
- }
87
- }
88
- /**
89
- * @inheritdoc
90
- */
91
- isSupported() {
92
- // Verifica se siamo in ambiente browser
93
- if (typeof window === "undefined") {
94
- return false;
95
- }
96
- // Check if PublicKeyCredential is available
97
- if (typeof window.PublicKeyCredential === "undefined") {
98
- return false;
99
- }
100
- // In test environment, allow initialization if window.PublicKeyCredential is mocked
101
- if (process.env.NODE_ENV === "test") {
102
- return typeof window.PublicKeyCredential !== "undefined";
103
- }
104
- // Se il plugin non è stato inizializzato, verifica direttamente il supporto
105
- if (!this.webauthn) {
106
- return typeof window.PublicKeyCredential !== "undefined";
107
- }
108
- return this.webauthn.isSupported();
109
- }
110
- /**
111
- * @inheritdoc
112
- */
113
- async generateCredentials(username, existingCredential, isLogin = false) {
114
- return this.assertWebauthn().generateCredentials(username, existingCredential, isLogin);
115
- }
116
- /**
117
- * @inheritdoc
118
- */
119
- async createAccount(username, credentials, isNewDevice = false) {
120
- return this.assertWebauthn().createAccount(username, credentials, isNewDevice);
121
- }
122
- /**
123
- * @inheritdoc
124
- */
125
- async authenticateUser(username, salt, options) {
126
- return this.assertWebauthn().authenticateUser(username, salt, options);
127
- }
128
- /**
129
- * @inheritdoc
130
- */
131
- abortAuthentication() {
132
- this.assertWebauthn().abortAuthentication();
133
- }
134
- /**
135
- * @inheritdoc
136
- */
137
- async removeDevice(username, credentialId, credentials) {
138
- return this.assertWebauthn().removeDevice(username, credentialId, credentials);
139
- }
140
- /**
141
- * @inheritdoc
142
- */
143
- async createSigningCredential(username) {
144
- try {
145
- // Delegate to underlying WebAuthn module (tests mock these methods)
146
- const wa = this.assertWebauthn();
147
- if (typeof wa.createSigningCredential === "function") {
148
- return await wa.createSigningCredential(username);
149
- }
150
- // Fallback to signer implementation if available
151
- return await this.assertSigner().createSigningCredential(username);
152
- }
153
- catch (error) {
154
- console.error(`Error creating signing credential: ${error.message}`);
155
- throw error;
156
- }
157
- }
158
- /**
159
- * @inheritdoc
160
- */
161
- createAuthenticator(credentialId) {
162
- try {
163
- const wa = this.assertWebauthn();
164
- if (typeof wa.createAuthenticator === "function") {
165
- return wa.createAuthenticator(credentialId);
166
- }
167
- return this.assertSigner().createAuthenticator(credentialId);
168
- }
169
- catch (error) {
170
- console.error(`Error creating authenticator: ${error.message}`);
171
- throw error;
172
- }
173
- }
174
- /**
175
- * @inheritdoc
176
- */
177
- async createDerivedKeyPair(credentialId, username, extra) {
178
- try {
179
- const wa = this.assertWebauthn();
180
- if (typeof wa.createDerivedKeyPair === "function") {
181
- return await wa.createDerivedKeyPair(credentialId, username, extra);
182
- }
183
- return await this.assertSigner().createDerivedKeyPair(credentialId, username, extra);
184
- }
185
- catch (error) {
186
- console.error(`Error creating derived key pair: ${error.message}`);
187
- throw error;
188
- }
189
- }
190
- /**
191
- * @inheritdoc
192
- */
193
- async signWithDerivedKeys(data, credentialId, username, extra) {
194
- try {
195
- const wa = this.assertWebauthn();
196
- if (typeof wa.signWithDerivedKeys === "function") {
197
- return await wa.signWithDerivedKeys(data, credentialId, username, extra);
198
- }
199
- return await this.assertSigner().signWithDerivedKeys(data, credentialId, username, extra);
200
- }
201
- catch (error) {
202
- console.error(`Error signing with derived keys: ${error.message}`);
203
- throw error;
204
- }
205
- }
206
- /**
207
- * @inheritdoc
208
- */
209
- getSigningCredential(credentialId) {
210
- const wa = this.assertWebauthn();
211
- if (typeof wa.getSigningCredential === "function") {
212
- return wa.getSigningCredential(credentialId);
213
- }
214
- return this.assertSigner().getCredential(credentialId);
215
- }
216
- /**
217
- * @inheritdoc
218
- */
219
- listSigningCredentials() {
220
- const wa = this.assertWebauthn();
221
- if (typeof wa.listSigningCredentials === "function") {
222
- return wa.listSigningCredentials();
223
- }
224
- return this.assertSigner().listCredentials();
225
- }
226
- /**
227
- * @inheritdoc
228
- */
229
- removeSigningCredential(credentialId) {
230
- const wa = this.assertWebauthn();
231
- if (typeof wa.removeSigningCredential === "function") {
232
- return wa.removeSigningCredential(credentialId);
233
- }
234
- return this.assertSigner().removeCredential(credentialId);
235
- }
236
- // === CONSISTENCY METHODS ===
237
- /**
238
- * Creates a Gun user from WebAuthn signing credential
239
- * This ensures the SAME user is created as with normal approach
240
- */
241
- async createGunUserFromSigningCredential(credentialId, username) {
242
- try {
243
- const wa = this.assertWebauthn();
244
- if (typeof wa.createGunUserFromSigningCredential === "function") {
245
- return await wa.createGunUserFromSigningCredential(credentialId, username);
246
- }
247
- const core = this.assertInitialized();
248
- return await this.assertSigner().createGunUser(credentialId, username, core.gun);
249
- }
250
- catch (error) {
251
- console.error(`Error creating Gun user from signing credential: ${error.message}`);
252
- throw error;
253
- }
254
- }
255
- /**
256
- * Get the Gun user public key for a signing credential
257
- */
258
- getGunUserPubFromSigningCredential(credentialId) {
259
- const wa = this.assertWebauthn();
260
- if (typeof wa.getGunUserPubFromSigningCredential === "function") {
261
- return wa.getGunUserPubFromSigningCredential(credentialId);
262
- }
263
- return this.assertSigner().getGunUserPub(credentialId);
264
- }
265
- /**
266
- * Get the hashed credential ID (for consistency checking)
267
- */
268
- getHashedCredentialId(credentialId) {
269
- const wa = this.assertWebauthn();
270
- if (typeof wa.getHashedCredentialId === "function") {
271
- return wa.getHashedCredentialId(credentialId);
272
- }
273
- return this.assertSigner().getHashedCredentialId(credentialId);
274
- }
275
- /**
276
- * Verify consistency between oneshot and normal approaches
277
- * This ensures both approaches create the same Gun user
278
- */
279
- async verifyConsistency(credentialId, username, expectedUserPub) {
280
- try {
281
- const wa = this.assertWebauthn();
282
- if (typeof wa.verifyConsistency === "function") {
283
- return await wa.verifyConsistency(credentialId, username, expectedUserPub);
284
- }
285
- return await this.assertSigner().verifyConsistency(credentialId, username, expectedUserPub);
286
- }
287
- catch (error) {
288
- console.error(`Error verifying consistency: ${error.message}`);
289
- return { consistent: false };
290
- }
291
- }
292
- /**
293
- * Complete oneshot workflow that creates the SAME Gun user as normal approach
294
- * This is the recommended method for oneshot signing with full consistency
295
- */
296
- async setupConsistentOneshotSigning(username) {
297
- try {
298
- const wa = this.assertWebauthn();
299
- if (typeof wa.setupConsistentOneshotSigning === "function") {
300
- return await wa.setupConsistentOneshotSigning(username);
301
- }
302
- // Fallback to local flow when not available
303
- const credential = await this.createSigningCredential(username);
304
- const authenticator = this.createAuthenticator(credential.id);
305
- const gunUser = await this.createGunUserFromSigningCredential(credential.id, username);
306
- return {
307
- credential,
308
- authenticator,
309
- gunUser,
310
- pub: credential.pub,
311
- hashedCredentialId: credential.hashedCredentialId,
312
- };
313
- }
314
- catch (error) {
315
- console.error(`Error setting up consistent oneshot signing: ${error.message}`);
316
- throw error;
317
- }
318
- }
319
- /**
320
- * Login with WebAuthn
321
- * This is the recommended method for WebAuthn authentication
322
- * @param username - Username
323
- * @returns {Promise<AuthResult>} Authentication result
324
- * @description Authenticates user using WebAuthn credentials.
325
- * Requires browser support for WebAuthn and existing credentials.
326
- */
327
- async login(username) {
328
- try {
329
- const core = this.assertInitialized();
330
- if (!username) {
331
- throw new Error("Username required for WebAuthn login");
332
- }
333
- if (!this.isSupported()) {
334
- throw new Error("WebAuthn is not supported by this browser");
335
- }
336
- // Prefer the oneshot consistent signing flow (tests mock this)
337
- const { authenticator, pub } = (await this.setupConsistentOneshotSigning(username));
338
- // If core has an authenticate method (tests), use it
339
- if (core.authenticate) {
340
- return await core.authenticate(username, authenticator, pub);
341
- }
342
- // Fallback to credentials-based flow
343
- const credentials = await this.generateCredentials(username, null, true);
344
- if (!credentials?.success) {
345
- throw new Error(credentials?.error || "WebAuthn verification failed");
346
- }
347
- core.setAuthMethod("webauthn");
348
- return await core.login(username, "", credentials.key);
349
- }
350
- catch (error) {
351
- console.error(`Error during WebAuthn login: ${error}`);
352
- // Log but do not depend on handler return value
353
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.WEBAUTHN, "WEBAUTHN_LOGIN_ERROR", error.message || "Error during WebAuthn login", error);
354
- return {
355
- success: false,
356
- error: error.message || "Error during WebAuthn login",
357
- };
358
- }
359
- }
360
- /**
361
- * Register new user with WebAuthn
362
- * This is the recommended method for WebAuthn registration
363
- * @param username - Username
364
- * @param options - Optional signup options (seed phrase support)
365
- * @returns {Promise<SignUpResult>} Registration result with optional seed phrase
366
- * @description Creates a new user account using WebAuthn credentials.
367
- * Requires browser support for WebAuthn.
368
- * If generateSeedPhrase is true, returns a BIP39 mnemonic for multi-device support.
369
- */
370
- async signUp(username, options) {
371
- try {
372
- const core = this.assertInitialized();
373
- if (!username) {
374
- throw new Error("Username required for WebAuthn registration");
375
- }
376
- if (!this.isSupported()) {
377
- throw new Error("WebAuthn is not supported by this browser");
378
- }
379
- // Determine seed phrase to use
380
- let seedPhrase;
381
- const shouldGenerateSeed = options?.generateSeedPhrase !== false; // Default to true
382
- if (options?.seedPhrase) {
383
- // Use provided seed phrase
384
- if (!(0, seedPhrase_1.validateSeedPhrase)(options.seedPhrase)) {
385
- throw new Error("Invalid seed phrase provided");
386
- }
387
- seedPhrase = options.seedPhrase;
388
- }
389
- else if (shouldGenerateSeed) {
390
- // Generate new seed phrase for multi-device support
391
- seedPhrase = (0, seedPhrase_1.generateSeedPhrase)();
392
- console.log("[webauthnPlugin] Generated seed phrase for multi-device support");
393
- }
394
- // Derive Gun credentials from seed phrase if available
395
- let pair;
396
- if (seedPhrase) {
397
- // Use seed phrase derivation
398
- const { password } = (0, seedPhrase_1.deriveCredentialsFromMnemonic)(seedPhrase, username);
399
- const derivedKeys = await (0, webauthn_2.deriveWebauthnKeys)(username, seedPhrase, true);
400
- pair = {
401
- pub: derivedKeys.pub,
402
- priv: derivedKeys.priv,
403
- epub: derivedKeys.epub,
404
- epriv: derivedKeys.epriv,
405
- };
406
- }
407
- else {
408
- // Legacy WebAuthn credential-based flow (device-bound)
409
- const credentials = await this.generateCredentials(username, null, false);
410
- if (!credentials?.success) {
411
- throw new Error(credentials?.error || "Unable to generate WebAuthn credentials");
412
- }
413
- // Use the key directly from credentials instead of calling generatePairFromCredentials
414
- // since generateCredentials already returns the derived key pair
415
- if (!credentials.key) {
416
- throw new Error("Failed to generate SEA pair from WebAuthn credentials");
417
- }
418
- pair = credentials.key;
419
- }
420
- core.setAuthMethod("webauthn");
421
- // Register user with Gun (using email parameter slot for pair)
422
- const result = await core.signUp(username, undefined, pair);
423
- // Add seed phrase to result if generated
424
- if (seedPhrase && shouldGenerateSeed) {
425
- return {
426
- ...result,
427
- message: seedPhrase
428
- ? "🔑 IMPORTANT: Save your 12-word seed phrase to access your account from other devices!"
429
- : result.message,
430
- seedPhrase: seedPhrase,
431
- };
432
- }
433
- return result;
434
- }
435
- catch (error) {
436
- console.error(`Error during WebAuthn registration: ${error}`);
437
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.WEBAUTHN, "WEBAUTHN_SIGNUP_ERROR", error.message || "Error during WebAuthn registration", error);
438
- return {
439
- success: false,
440
- error: error.message || "Error during WebAuthn registration",
441
- };
442
- }
443
- }
444
- /**
445
- * Import existing account from seed phrase
446
- * Allows accessing the same account across multiple devices
447
- * @param username - Username
448
- * @param seedPhrase - 12-word BIP39 mnemonic seed phrase
449
- * @returns {Promise<SignUpResult>} Registration result
450
- */
451
- async importFromSeed(username, seedPhrase) {
452
- try {
453
- if (!username) {
454
- throw new Error("Username required");
455
- }
456
- // Normalize and validate seed phrase
457
- const normalizedSeed = (0, seedPhrase_1.normalizeSeedPhrase)(seedPhrase);
458
- if (!(0, seedPhrase_1.validateSeedPhrase)(normalizedSeed)) {
459
- throw new Error("Invalid seed phrase. Please check and try again.");
460
- }
461
- console.log("[webauthnPlugin] Importing account from seed phrase");
462
- // Use signUp with existing seed phrase
463
- return await this.signUp(username, {
464
- seedPhrase: normalizedSeed,
465
- generateSeedPhrase: false, // Don't generate new seed
466
- });
467
- }
468
- catch (error) {
469
- console.error(`Error importing from seed: ${error.message}`);
470
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.WEBAUTHN, "WEBAUTHN_IMPORT_ERROR", error.message || "Error importing from seed phrase", error);
471
- return {
472
- success: false,
473
- error: error.message || "Error importing from seed phrase",
474
- };
475
- }
476
- }
477
- /**
478
- * Get seed phrase for current user (if stored)
479
- * Note: Seed phrases are NOT stored by default for security
480
- * Users should save their seed phrase during registration
481
- * @param username - Username
482
- * @returns {Promise<string | null>} Seed phrase or null
483
- */
484
- async getSeedPhrase(username) {
485
- console.warn("[webauthnPlugin] Seed phrases are not stored for security reasons");
486
- console.warn("[webauthnPlugin] Users must save their seed phrase during registration");
487
- return null;
488
- }
489
- }
490
- exports.WebauthnPlugin = WebauthnPlugin;