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,198 +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.ZkProofConnector = void 0;
7
- const identity_1 = require("@semaphore-protocol/identity");
8
- const group_1 = require("@semaphore-protocol/group");
9
- const proof_1 = require("@semaphore-protocol/proof");
10
- const ethers_1 = require("ethers");
11
- const derive_1 = __importDefault(require("../../gundb/derive"));
12
- const errorHandler_1 = require("../../utils/errorHandler");
13
- /**
14
- * Connector for ZK-Proof operations using Semaphore protocol
15
- */
16
- class ZkProofConnector {
17
- constructor() {
18
- this.identityCache = new Map();
19
- this.credentialCache = new Map();
20
- this.groups = new Map();
21
- }
22
- /**
23
- * Generate a new Semaphore identity
24
- */
25
- async generateIdentity(seed) {
26
- try {
27
- let identity;
28
- if (seed) {
29
- // Deterministic generation from seed
30
- identity = new identity_1.Identity(seed);
31
- }
32
- else {
33
- // Random generation
34
- identity = new identity_1.Identity();
35
- }
36
- const commitment = identity.commitment.toString();
37
- const trapdoor = identity.trapdoor.toString();
38
- const nullifier = identity.nullifier.toString();
39
- // Cache the identity
40
- this.identityCache.set(commitment, identity);
41
- return {
42
- commitment,
43
- trapdoor,
44
- nullifier,
45
- createdAt: Date.now(),
46
- };
47
- }
48
- catch (error) {
49
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.ENCRYPTION, "ZK_IDENTITY_GENERATION_FAILED", `Failed to generate ZK identity: ${error.message}`, error);
50
- throw error;
51
- }
52
- }
53
- /**
54
- * Restore identity from trapdoor
55
- */
56
- async restoreIdentity(trapdoor) {
57
- try {
58
- // Reconstruct identity from trapdoor
59
- const identity = new identity_1.Identity(trapdoor);
60
- const commitment = identity.commitment.toString();
61
- // Cache the identity
62
- this.identityCache.set(commitment, identity);
63
- return {
64
- commitment,
65
- trapdoor: identity.trapdoor.toString(),
66
- nullifier: identity.nullifier.toString(),
67
- createdAt: Date.now(),
68
- };
69
- }
70
- catch (error) {
71
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.ENCRYPTION, "ZK_IDENTITY_RESTORE_FAILED", `Failed to restore ZK identity: ${error.message}`, error);
72
- throw error;
73
- }
74
- }
75
- /**
76
- * Generate Gun credentials from ZK identity
77
- */
78
- async generateCredentials(identityData) {
79
- try {
80
- // Use commitment as username and trapdoor as password for Gun
81
- const username = `zk_${identityData.commitment.slice(0, 16)}`;
82
- // Derive password from trapdoor and nullifier
83
- const password = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(`${identityData.trapdoor}_${identityData.nullifier}`));
84
- // Derive Gun SEA pair
85
- const gunPair = await (0, derive_1.default)(password, username, {
86
- includeP256: true,
87
- });
88
- // Cache credential
89
- this.credentialCache.set(identityData.commitment, {
90
- commitment: identityData.commitment,
91
- gunPair,
92
- createdAt: identityData.createdAt,
93
- });
94
- return gunPair;
95
- }
96
- catch (error) {
97
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.ENCRYPTION, "ZK_CREDENTIAL_GENERATION_FAILED", `Failed to generate credentials from ZK identity: ${error.message}`, error);
98
- throw error;
99
- }
100
- }
101
- /**
102
- * Get or create a Semaphore group
103
- */
104
- getOrCreateGroup(groupId = "default") {
105
- if (!this.groups.has(groupId)) {
106
- // Convert string groupId to BigNumber using keccak256 hash
107
- const groupIdHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(groupId));
108
- const groupIdNumber = BigInt(groupIdHash);
109
- this.groups.set(groupId, new group_1.Group(groupIdNumber));
110
- }
111
- return this.groups.get(groupId);
112
- }
113
- /**
114
- * Add identity to a group
115
- */
116
- addToGroup(commitment, groupId = "default") {
117
- const group = this.getOrCreateGroup(groupId);
118
- group.addMember(BigInt(commitment));
119
- }
120
- /**
121
- * Generate a Semaphore proof
122
- */
123
- async generateProof(identityData, options = {}) {
124
- try {
125
- const groupId = options.groupId || "default";
126
- const messageString = options.message || "authenticate";
127
- const scopeString = options.scope || "shogun-auth";
128
- // Convert message and scope to BigNumber (Semaphore requires BigInt)
129
- const messageHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(messageString));
130
- const message = BigInt(messageHash);
131
- const scopeHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(scopeString));
132
- const scope = BigInt(scopeHash);
133
- // Get or reconstruct identity
134
- let identity = this.identityCache.get(identityData.commitment);
135
- if (!identity && identityData.trapdoor) {
136
- identity = new identity_1.Identity(identityData.trapdoor);
137
- this.identityCache.set(identityData.commitment, identity);
138
- }
139
- if (!identity) {
140
- throw new Error("Identity not found and cannot be reconstructed");
141
- }
142
- // Get group
143
- const group = this.getOrCreateGroup(groupId);
144
- // Add identity to group if not already added
145
- if (group.indexOf(identity.commitment) === -1) {
146
- group.addMember(identity.commitment);
147
- }
148
- // Generate proof
149
- const fullProof = await (0, proof_1.generateProof)(identity, group, message, scope);
150
- return {
151
- merkleTreeRoot: fullProof.merkleTreeRoot.toString(),
152
- nullifierHash: fullProof.nullifierHash.toString(),
153
- signal: fullProof.signal.toString(),
154
- externalNullifier: fullProof.externalNullifier.toString(),
155
- proof: fullProof.proof.map((p) => p.toString()),
156
- };
157
- }
158
- catch (error) {
159
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.ENCRYPTION, "ZK_PROOF_GENERATION_FAILED", `Failed to generate ZK proof: ${error.message}`, error);
160
- throw error;
161
- }
162
- }
163
- /**
164
- * Verify a Semaphore proof
165
- */
166
- async verifyProof(proof, treeDepth = 20) {
167
- try {
168
- const verified = await (0, proof_1.verifyProof)(proof, treeDepth);
169
- return {
170
- success: true,
171
- verified,
172
- };
173
- }
174
- catch (error) {
175
- errorHandler_1.ErrorHandler.handle(errorHandler_1.ErrorType.ENCRYPTION, "ZK_PROOF_VERIFICATION_FAILED", `Failed to verify ZK proof: ${error.message}`, error);
176
- return {
177
- success: false,
178
- verified: false,
179
- error: error.message,
180
- };
181
- }
182
- }
183
- /**
184
- * Get cached credential by commitment
185
- */
186
- getCredential(commitment) {
187
- return this.credentialCache.get(commitment);
188
- }
189
- /**
190
- * Clear all caches
191
- */
192
- cleanup() {
193
- this.identityCache.clear();
194
- this.credentialCache.clear();
195
- this.groups.clear();
196
- }
197
- }
198
- exports.ZkProofConnector = ZkProofConnector;
@@ -1,272 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ZkProofPlugin = void 0;
4
- const base_1 = require("../base");
5
- const zkProofConnector_1 = require("./zkProofConnector");
6
- const shogun_1 = require("../../interfaces/shogun");
7
- const errorHandler_1 = require("../../utils/errorHandler");
8
- /**
9
- * Plugin for Zero-Knowledge Proof authentication using Semaphore protocol
10
- *
11
- * Features:
12
- * - Anonymous authentication with ZK proofs
13
- * - Multi-device support via trapdoor backup
14
- * - Privacy-preserving identity management
15
- * - Compatible with Gun decentralized storage
16
- */
17
- class ZkProofPlugin extends base_1.BasePlugin {
18
- constructor(config = {}) {
19
- super();
20
- this.name = "zkproof";
21
- this.version = "1.0.0";
22
- this.description = "Zero-Knowledge Proof authentication using Semaphore protocol for ShogunCore";
23
- this._category = shogun_1.PluginCategory.Authentication;
24
- this.connector = null;
25
- this.config = {
26
- defaultGroupId: "shogun-users",
27
- deterministic: false,
28
- minEntropy: 128,
29
- ...config,
30
- };
31
- }
32
- /**
33
- * Initialize the plugin
34
- */
35
- initialize(core) {
36
- super.initialize(core);
37
- this.connector = new zkProofConnector_1.ZkProofConnector();
38
- }
39
- /**
40
- * Clean up resources
41
- */
42
- destroy() {
43
- if (this.connector) {
44
- this.connector.cleanup();
45
- }
46
- this.connector = null;
47
- super.destroy();
48
- }
49
- /**
50
- * Ensure connector is initialized
51
- */
52
- assertConnector() {
53
- this.assertInitialized();
54
- if (!this.connector) {
55
- throw new Error("ZK-Proof connector not initialized");
56
- }
57
- return this.connector;
58
- }
59
- /**
60
- * Generate a new ZK identity
61
- */
62
- async generateIdentity(seed) {
63
- try {
64
- return await this.assertConnector().generateIdentity(seed);
65
- }
66
- catch (error) {
67
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.ENCRYPTION, "ZK_IDENTITY_GENERATION_FAILED", `Failed to generate ZK identity: ${error.message}`);
68
- }
69
- }
70
- /**
71
- * Restore identity from trapdoor/seed phrase
72
- */
73
- async restoreIdentity(trapdoor) {
74
- try {
75
- if (!trapdoor || trapdoor.trim().length === 0) {
76
- throw new Error("Trapdoor is required");
77
- }
78
- return await this.assertConnector().restoreIdentity(trapdoor);
79
- }
80
- catch (error) {
81
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.ENCRYPTION, "ZK_IDENTITY_RESTORE_FAILED", `Failed to restore ZK identity: ${error.message}`);
82
- }
83
- }
84
- /**
85
- * Generate credentials for Gun authentication
86
- */
87
- async generateCredentials(identityData) {
88
- try {
89
- return await this.assertConnector().generateCredentials(identityData);
90
- }
91
- catch (error) {
92
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.ENCRYPTION, "ZK_CREDENTIAL_GENERATION_FAILED", `Failed to generate credentials: ${error.message}`);
93
- }
94
- }
95
- /**
96
- * Generate a zero-knowledge proof
97
- */
98
- async generateProof(identityData, options) {
99
- try {
100
- return await this.assertConnector().generateProof(identityData, options);
101
- }
102
- catch (error) {
103
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.ENCRYPTION, "ZK_PROOF_GENERATION_FAILED", `Failed to generate ZK proof: ${error.message}`);
104
- }
105
- }
106
- /**
107
- * Verify a zero-knowledge proof
108
- */
109
- async verifyProof(proof, treeDepth = 20) {
110
- try {
111
- return await this.assertConnector().verifyProof(proof, treeDepth);
112
- }
113
- catch (error) {
114
- return {
115
- success: false,
116
- verified: false,
117
- error: error.message,
118
- };
119
- }
120
- }
121
- /**
122
- * Add identity to a group
123
- */
124
- addToGroup(commitment, groupId) {
125
- const group = groupId || this.config.defaultGroupId || "default";
126
- this.assertConnector().addToGroup(commitment, group);
127
- }
128
- /**
129
- * Login with ZK proof
130
- * @param trapdoor - User's trapdoor/seed phrase
131
- * @returns Authentication result
132
- */
133
- async login(trapdoor) {
134
- try {
135
- const core = this.assertInitialized();
136
- const connector = this.assertConnector();
137
- if (!trapdoor || trapdoor.trim().length === 0) {
138
- throw (0, errorHandler_1.createError)(errorHandler_1.ErrorType.VALIDATION, "TRAPDOOR_REQUIRED", "Trapdoor is required for ZK-Proof login");
139
- }
140
- console.log("🔐 ZK-Proof login - restoring identity from trapdoor");
141
- // Restore identity from trapdoor
142
- const identityData = await connector.restoreIdentity(trapdoor);
143
- console.log(`🔐 ZK-Proof login - identity restored, commitment: ${identityData.commitment.slice(0, 16)}...`);
144
- // Generate credentials for Gun
145
- const gunPair = await connector.generateCredentials(identityData);
146
- console.log(`🔐 ZK-Proof login - Gun credentials generated, pub: ${gunPair.pub.slice(0, 16)}...`);
147
- // Authenticate with Gun using the derived pair via ShogunCore
148
- const username = `zk_${identityData.commitment.slice(0, 16)}`;
149
- try {
150
- // Try to authenticate with existing account using ShogunCore
151
- const loginResult = await core.loginWithPair(username, gunPair);
152
- if (!loginResult.success) {
153
- console.log("🔐 ZK-Proof login - existing account not found, this might be first login");
154
- }
155
- else {
156
- console.log("🔐 ZK-Proof login - Gun authentication successful");
157
- }
158
- }
159
- catch (authError) {
160
- console.log("🔐 ZK-Proof login - existing account not found, this might be first login");
161
- }
162
- // Set authentication method
163
- core.setAuthMethod("zkproof");
164
- // Add to default group
165
- this.addToGroup(identityData.commitment);
166
- const result = {
167
- success: true,
168
- userPub: gunPair.pub,
169
- username: username,
170
- authMethod: "zkproof",
171
- sea: {
172
- pub: gunPair.pub,
173
- priv: gunPair.priv,
174
- epub: gunPair.epub,
175
- epriv: gunPair.epriv,
176
- },
177
- };
178
- // Emit login event
179
- core.emit("auth:login", {
180
- userPub: gunPair.pub,
181
- username: username,
182
- method: "zkproof",
183
- });
184
- console.log("🔐 ZK-Proof login - complete");
185
- return result;
186
- }
187
- catch (error) {
188
- const errorType = error?.type || errorHandler_1.ErrorType.AUTHENTICATION;
189
- const errorCode = error?.code || "ZK_LOGIN_ERROR";
190
- const errorMessage = error?.message || "Unknown error during ZK-Proof login";
191
- errorHandler_1.ErrorHandler.handle(errorType, errorCode, errorMessage, error);
192
- return { success: false, error: errorMessage };
193
- }
194
- }
195
- /**
196
- * Sign up with new ZK identity
197
- * @param seed - Optional seed for deterministic generation
198
- * @returns Authentication result with trapdoor for backup
199
- */
200
- async signUp(seed) {
201
- try {
202
- const core = this.assertInitialized();
203
- const connector = this.assertConnector();
204
- console.log("🔐 ZK-Proof signup - generating new identity");
205
- // Generate new identity
206
- const identityData = await connector.generateIdentity(seed);
207
- console.log(`🔐 ZK-Proof signup - identity generated, commitment: ${identityData.commitment.slice(0, 16)}...`);
208
- // Generate credentials for Gun
209
- const gunPair = await connector.generateCredentials(identityData);
210
- console.log(`🔐 ZK-Proof signup - Gun credentials generated, pub: ${gunPair.pub.slice(0, 16)}...`);
211
- // Create Gun user account using ShogunCore methods
212
- const username = `zk_${identityData.commitment.slice(0, 16)}`;
213
- try {
214
- // Try to create user with signUp
215
- const signUpResult = await core.signUp(username, undefined, gunPair);
216
- if (!signUpResult.success) {
217
- // If user already exists, login with the pair
218
- const loginResult = await core.loginWithPair(username, gunPair);
219
- if (!loginResult.success) {
220
- throw new Error(loginResult.error || "Failed to authenticate");
221
- }
222
- }
223
- console.log("🔐 ZK-Proof signup - Gun user created/authenticated");
224
- }
225
- catch (createError) {
226
- throw createError;
227
- }
228
- // Set authentication method
229
- core.setAuthMethod("zkproof");
230
- // Add to default group
231
- this.addToGroup(identityData.commitment);
232
- const result = {
233
- success: true,
234
- userPub: gunPair.pub,
235
- username: username,
236
- authMethod: "zkproof",
237
- isNewUser: true,
238
- // CRITICAL: Include trapdoor for user backup (like seed phrase in WebAuthn)
239
- seedPhrase: identityData.trapdoor,
240
- sea: {
241
- pub: gunPair.pub,
242
- priv: gunPair.priv,
243
- epub: gunPair.epub,
244
- epriv: gunPair.epriv,
245
- },
246
- };
247
- // Emit signup event
248
- core.emit("auth:signup", {
249
- userPub: gunPair.pub,
250
- username: username,
251
- method: "zkproof",
252
- });
253
- console.log("🔐 ZK-Proof signup - complete");
254
- console.log(`⚠️ IMPORTANT: Save the trapdoor (seed phrase) for account recovery!`);
255
- return result;
256
- }
257
- catch (error) {
258
- const errorType = error?.type || errorHandler_1.ErrorType.AUTHENTICATION;
259
- const errorCode = error?.code || "ZK_SIGNUP_ERROR";
260
- const errorMessage = error?.message || "Unknown error during ZK-Proof signup";
261
- errorHandler_1.ErrorHandler.handle(errorType, errorCode, errorMessage, error);
262
- return { success: false, error: errorMessage };
263
- }
264
- }
265
- /**
266
- * Check if ZK-Proof is available
267
- */
268
- isAvailable() {
269
- return typeof window !== "undefined" || typeof global !== "undefined";
270
- }
271
- }
272
- exports.ZkProofPlugin = ZkProofPlugin;
@@ -1,145 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ShogunStorage = void 0;
4
- /**
5
- * Storage implementation based on StorageMock
6
- * Provides a unified storage interface that works in both browser and non-browser environments
7
- * In browser environments, data is persisted to localStorage as a backup
8
- */
9
- class ShogunStorage {
10
- /**
11
- * Initializes storage and loads any existing keypair from localStorage if available
12
- */
13
- constructor(silent = false) {
14
- this.store = new Map();
15
- this.silent = silent;
16
- this.isTestMode = process.env.NODE_ENV === "test";
17
- this.useLocalStorage = false;
18
- // In test mode, don't use localStorage to avoid test pollution
19
- if (this.isTestMode) {
20
- this.useLocalStorage = false;
21
- return;
22
- }
23
- if (typeof localStorage !== "undefined") {
24
- try {
25
- // Probe localStorage without polluting expectations in tests
26
- const testKey = "_shogun_test";
27
- localStorage.setItem(testKey, testKey);
28
- localStorage.removeItem(testKey);
29
- this.useLocalStorage = true;
30
- if (!this.silent) {
31
- console.log("ShogunStorage: localStorage enabled");
32
- }
33
- const storedPair = localStorage.getItem("shogun_keypair");
34
- if (storedPair) {
35
- this.store.set("keypair", JSON.parse(storedPair));
36
- }
37
- }
38
- catch (error) {
39
- this.useLocalStorage = false;
40
- // Silence logs in tests; tests expect no console.error during constructor
41
- if (!this.silent) {
42
- console.log("ShogunStorage: localStorage error:", error.message);
43
- }
44
- }
45
- }
46
- }
47
- /**
48
- * Gets the stored keypair asynchronously
49
- * @returns Promise resolving to the keypair or null if not found
50
- */
51
- async getPair() {
52
- return this.getPairSync();
53
- }
54
- /**
55
- * Gets the stored keypair synchronously
56
- * @returns The keypair or null if not found
57
- */
58
- getPairSync() {
59
- return this.store.get("keypair") || null;
60
- }
61
- /**
62
- * Stores a keypair both in memory and localStorage if available
63
- * @param pair - The keypair to store
64
- */
65
- async setPair(pair) {
66
- this.store.set("keypair", pair);
67
- // Also save to localStorage in browser environments
68
- if (this.useLocalStorage) {
69
- try {
70
- localStorage.setItem("shogun_keypair", JSON.stringify(pair));
71
- }
72
- catch (error) {
73
- if (!this.isTestMode) {
74
- console.error("Error saving data to localStorage:", error);
75
- }
76
- }
77
- }
78
- }
79
- /**
80
- * Clears all stored data from both memory and localStorage
81
- */
82
- clearAll() {
83
- this.store.clear();
84
- // Also clear localStorage in browser environments
85
- if (this.useLocalStorage) {
86
- try {
87
- localStorage.removeItem("shogun_keypair");
88
- }
89
- catch (error) {
90
- if (!this.isTestMode) {
91
- console.error("Error removing data from localStorage:", error);
92
- }
93
- }
94
- }
95
- }
96
- /**
97
- * Gets an item from storage
98
- * @param key - The key to retrieve
99
- * @returns The stored value, or null if not found
100
- */
101
- getItem(key) {
102
- const value = this.store.get(key);
103
- if (value === undefined) {
104
- return null;
105
- }
106
- return value;
107
- }
108
- /**
109
- * Stores an item in both memory and localStorage if available
110
- * @param key - The key to store under
111
- * @param value - The value to store (must be JSON stringifiable)
112
- */
113
- setItem(key, value) {
114
- // Store the raw value as-is to preserve formatting
115
- this.store.set(key, value);
116
- if (this.useLocalStorage) {
117
- try {
118
- localStorage.setItem(key, typeof value === "string" ? value : JSON.stringify(value));
119
- }
120
- catch (error) {
121
- if (!this.isTestMode) {
122
- console.error(`Error saving ${key} to localStorage:`, error);
123
- }
124
- }
125
- }
126
- }
127
- /**
128
- * Removes an item from both memory and localStorage if available
129
- * @param key - The key to remove
130
- */
131
- removeItem(key) {
132
- this.store.delete(key);
133
- if (this.useLocalStorage) {
134
- try {
135
- localStorage.removeItem(key);
136
- }
137
- catch (error) {
138
- if (!this.isTestMode) {
139
- console.error(`Error removing ${key} from localStorage:`, error);
140
- }
141
- }
142
- }
143
- }
144
- }
145
- exports.ShogunStorage = ShogunStorage;
@@ -1,8 +0,0 @@
1
- import { Gun, SEA } from "./min";
2
- import "gun/lib/then";
3
- import "gun/lib/radix";
4
- import "gun/lib/radisk";
5
- import "gun/lib/store";
6
- import "gun/lib/rindexed";
7
- import "gun/lib/webrtc";
8
- export { Gun, SEA };