shogun-core 3.3.7 → 4.0.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.
- package/README.md +1378 -1221
- package/dist/browser/shogun-core.js +78074 -45286
- package/dist/browser/shogun-core.js.map +1 -1
- package/dist/core.js +2 -3
- package/dist/examples/simple-api-test.js +90 -65
- package/dist/examples/zkproof-credentials-example.js +218 -0
- package/dist/examples/zkproof-example.js +206 -0
- package/dist/gundb/api.js +111 -467
- package/dist/index.js +10 -1
- package/dist/interfaces/shogun.js +2 -2
- package/dist/managers/AuthManager.js +0 -2
- package/dist/managers/CoreInitializer.js +9 -12
- package/dist/plugins/index.js +9 -21
- package/dist/plugins/nostr/nostrConnectorPlugin.js +2 -2
- package/dist/plugins/webauthn/webauthn.js +20 -7
- package/dist/plugins/webauthn/webauthnPlugin.js +101 -17
- package/dist/plugins/zkproof/index.js +53 -0
- package/dist/plugins/zkproof/zkCredentials.js +213 -0
- package/dist/plugins/zkproof/zkProofConnector.js +198 -0
- package/dist/plugins/zkproof/zkProofPlugin.js +272 -0
- package/dist/types/core.d.ts +1 -1
- package/dist/types/examples/simple-api-test.d.ts +6 -1
- package/dist/types/examples/zkproof-credentials-example.d.ts +12 -0
- package/dist/types/examples/zkproof-example.d.ts +11 -0
- package/dist/types/gundb/api.d.ts +77 -165
- package/dist/types/gundb/types.d.ts +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/interfaces/events.d.ts +3 -3
- package/dist/types/interfaces/shogun.d.ts +9 -24
- package/dist/types/plugins/index.d.ts +5 -3
- package/dist/types/plugins/webauthn/types.d.ts +22 -1
- package/dist/types/plugins/webauthn/webauthn.d.ts +1 -1
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +23 -2
- package/dist/types/plugins/zkproof/index.d.ts +48 -0
- package/dist/types/plugins/zkproof/types.d.ts +123 -0
- package/dist/types/plugins/zkproof/zkCredentials.d.ts +112 -0
- package/dist/types/plugins/zkproof/zkProofConnector.d.ts +46 -0
- package/dist/types/plugins/zkproof/zkProofPlugin.d.ts +76 -0
- package/dist/types/utils/seedPhrase.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +2 -2
- package/dist/utils/seedPhrase.js +97 -0
- package/dist/utils/validation.js +3 -1
- package/package.json +14 -8
- package/dist/examples/api-test.js +0 -273
- package/dist/migration-test.js +0 -96
- package/dist/plugins/oauth/index.js +0 -8
- package/dist/plugins/oauth/oauthConnector.js +0 -759
- package/dist/plugins/oauth/oauthPlugin.js +0 -400
- package/dist/types/examples/api-test.d.ts +0 -12
- package/dist/types/migration-test.d.ts +0 -16
- package/dist/types/plugins/oauth/index.d.ts +0 -3
- package/dist/types/plugins/oauth/oauthConnector.d.ts +0 -110
- package/dist/types/plugins/oauth/oauthPlugin.d.ts +0 -91
- package/dist/types/plugins/oauth/types.d.ts +0 -114
- /package/dist/plugins/{oauth → zkproof}/types.js +0 -0
|
@@ -0,0 +1,198 @@
|
|
|
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.members.includes(identity.commitment)) {
|
|
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;
|
|
@@ -0,0 +1,272 @@
|
|
|
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;
|
package/dist/types/core.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ import { PluginManager } from "./managers/PluginManager";
|
|
|
19
19
|
* @since 2.0.0
|
|
20
20
|
*/
|
|
21
21
|
export declare class ShogunCore implements IShogunCore {
|
|
22
|
-
static readonly API_VERSION = "^3.
|
|
22
|
+
static readonly API_VERSION = "^3.3.8";
|
|
23
23
|
db: DataBase;
|
|
24
24
|
storage: ShogunStorage;
|
|
25
25
|
provider?: ethers.Provider;
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Example showing how to use the simplified ShogunCore API
|
|
3
|
+
*
|
|
4
|
+
* The API has been streamlined:
|
|
5
|
+
* - AutoQuickStart: Quick initialization helper
|
|
6
|
+
* - api.database: Direct access to DataBase for basic operations (get, put, set, remove, auth)
|
|
7
|
+
* - api helper methods: High-level helpers for profile, settings, collections, and array utilities
|
|
3
8
|
*/
|
|
4
9
|
declare function simpleAPITest(): Promise<void>;
|
|
5
10
|
export { simpleAPITest };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ZK-Proof Verifiable Credentials Example
|
|
3
|
+
*
|
|
4
|
+
* This demonstrates how to use ZK-Proof for proving attributes
|
|
5
|
+
* about documents and identity without revealing sensitive data
|
|
6
|
+
*/
|
|
7
|
+
declare function ageVerificationExample(): Promise<void>;
|
|
8
|
+
declare function citizenshipExample(): Promise<void>;
|
|
9
|
+
declare function educationExample(): Promise<void>;
|
|
10
|
+
declare function incomeExample(): Promise<void>;
|
|
11
|
+
declare function customCredentialExample(): Promise<void>;
|
|
12
|
+
export { ageVerificationExample, citizenshipExample, educationExample, incomeExample, customCredentialExample, };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-Knowledge Proof Authentication Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to use the ZK-Proof plugin with Shogun Core
|
|
5
|
+
* for privacy-preserving authentication using Semaphore protocol.
|
|
6
|
+
*/
|
|
7
|
+
declare function basicExample(): Promise<void>;
|
|
8
|
+
declare function deterministicExample(): Promise<void>;
|
|
9
|
+
declare function proofExample(): Promise<void>;
|
|
10
|
+
declare function multiDeviceExample(): Promise<void>;
|
|
11
|
+
export { basicExample, deterministicExample, proofExample, multiDeviceExample };
|