shogun-core
Version:
SHOGUN CORE - Core library for Shogun Ecosystem
358 lines (357 loc) âĸ 18.4 kB
JavaScript
/**
* ZK-Proof Verifiable Credentials Example
*
* This demonstrates how to use ZK-Proof for proving attributes
* about documents and identity without revealing sensitive data
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import { Gun } from '../index.js';
import { ShogunCore } from '../core.js';
import { ZkCredentials, CredentialType, } from '../plugins/zkproof/zkCredentials.js';
import { Identity } from '@semaphore-protocol/identity';
// Example 1: Age Verification
function ageVerificationExample() {
return __awaiter(this, void 0, void 0, function () {
var peers, shogun, zkPlugin, identity, semaphoreIdentity, zkCreds, birthDate, actualAge, ageProof, verification, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
console.log('=== Age Verification Example ===\n');
peers = [
'https://g3ru5bwxmezpuu3ktnoclbpiw4.srv.us/gun',
'https://5eh4twk2f62autunsje4panime.srv.us/gun',
];
shogun = new ShogunCore({
gunInstance: Gun({ peers: peers }),
zkproof: { enabled: true },
});
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })];
case 1:
_a.sent();
zkPlugin = shogun.getPlugin('zkproof');
if (!zkPlugin)
return [2 /*return*/];
return [4 /*yield*/, zkPlugin.generateIdentity()];
case 2:
identity = _a.sent();
semaphoreIdentity = new Identity(identity.trapdoor);
zkCreds = new ZkCredentials();
console.log("Scenario: User wants to prove they're 18+ without revealing birthdate\n");
birthDate = new Date('1990-05-15');
actualAge = Math.floor((Date.now() - birthDate.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
console.log("Private data (NOT revealed):");
console.log(" Birth date: ".concat(birthDate.toDateString()));
console.log(" Actual age: ".concat(actualAge, "\n"));
_a.label = 3;
case 3:
_a.trys.push([3, 6, , 7]);
return [4 /*yield*/, zkCreds.proveAge(semaphoreIdentity, birthDate, 18)];
case 4:
ageProof = _a.sent();
console.log("Public proof generated:");
console.log(" Claim: \"".concat(ageProof.claim, "\""));
console.log(" Type: ".concat(ageProof.type));
console.log(" \u2705 Birth date NOT revealed!");
console.log(" \u2705 Exact age NOT revealed!");
return [4 /*yield*/, zkCreds.verifyCredential(ageProof)];
case 5:
verification = _a.sent();
console.log("\nVerification result: ".concat(verification.verified ? 'â
VALID' : 'â INVALID'));
return [3 /*break*/, 7];
case 6:
error_1 = _a.sent();
console.log("Note: Full proof generation requires circuit files");
console.log("Run: yarn setup:zkproof");
return [3 /*break*/, 7];
case 7: return [2 /*return*/];
}
});
});
}
// Example 2: Citizenship Verification
function citizenshipExample() {
return __awaiter(this, void 0, void 0, function () {
var shogun, zkPlugin, identity, semaphoreIdentity, zkCreds, citizenshipProof, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
console.log('\n=== Citizenship Verification Example ===\n');
shogun = new ShogunCore({
gunInstance: Gun({ peers: ['https://peer.wallie.io/gun'] }),
zkproof: { enabled: true },
});
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })];
case 1:
_a.sent();
zkPlugin = shogun.getPlugin('zkproof');
if (!zkPlugin)
return [2 /*return*/];
return [4 /*yield*/, zkPlugin.generateIdentity()];
case 2:
identity = _a.sent();
semaphoreIdentity = new Identity(identity.trapdoor);
zkCreds = new ZkCredentials();
console.log('Scenario: Prove EU citizenship without revealing country\n');
console.log("Private data (NOT revealed):");
console.log(" Country: Italy");
console.log(" Passport: IT123456789\n");
_a.label = 3;
case 3:
_a.trys.push([3, 5, , 6]);
return [4 /*yield*/, zkCreds.proveCitizenship(semaphoreIdentity, 'Italy', 'EU')];
case 4:
citizenshipProof = _a.sent();
console.log("Public proof:");
console.log(" Claim: \"".concat(citizenshipProof.claim, "\""));
console.log(" \u2705 Specific country NOT revealed!");
console.log(" \u2705 Passport number NOT revealed!");
return [3 /*break*/, 6];
case 5:
error_2 = _a.sent();
console.log("Note: Full proof requires circuit files");
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
});
}
// Example 3: Education Credentials
function educationExample() {
return __awaiter(this, void 0, void 0, function () {
var shogun, zkPlugin, identity, semaphoreIdentity, zkCreds, eduProof, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
console.log('\n=== Education Credential Example ===\n');
shogun = new ShogunCore({
gunInstance: Gun({ peers: ['https://peer.wallie.io/gun'] }),
zkproof: { enabled: true },
});
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })];
case 1:
_a.sent();
zkPlugin = shogun.getPlugin('zkproof');
if (!zkPlugin)
return [2 /*return*/];
return [4 /*yield*/, zkPlugin.generateIdentity()];
case 2:
identity = _a.sent();
semaphoreIdentity = new Identity(identity.trapdoor);
zkCreds = new ZkCredentials();
console.log('Scenario: Prove you have a degree without revealing details\n');
console.log("Private data (NOT revealed):");
console.log(" University: MIT");
console.log(" Degree: Computer Science");
console.log(" Year: 2020");
console.log(" Grade: 110/110\n");
_a.label = 3;
case 3:
_a.trys.push([3, 5, , 6]);
return [4 /*yield*/, zkCreds.proveEducation(semaphoreIdentity, 'Bachelor of Science', 'MIT', 2020)];
case 4:
eduProof = _a.sent();
console.log("Public proof:");
console.log(" Claim: \"".concat(eduProof.claim, "\""));
console.log(" \u2705 University name NOT revealed!");
console.log(" \u2705 Grades NOT revealed!");
console.log(" \u2705 Only proves you HAVE the degree");
return [3 /*break*/, 6];
case 5:
error_3 = _a.sent();
console.log("Note: Full proof requires circuit files");
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
});
}
// Example 4: Income Verification
function incomeExample() {
return __awaiter(this, void 0, void 0, function () {
var shogun, zkPlugin, identity, semaphoreIdentity, zkCreds, incomeProof, error_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
console.log('\n=== Income Verification Example ===\n');
shogun = new ShogunCore({
gunInstance: Gun({ peers: ['https://peer.wallie.io/gun'] }),
zkproof: { enabled: true },
});
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })];
case 1:
_a.sent();
zkPlugin = shogun.getPlugin('zkproof');
if (!zkPlugin)
return [2 /*return*/];
return [4 /*yield*/, zkPlugin.generateIdentity()];
case 2:
identity = _a.sent();
semaphoreIdentity = new Identity(identity.trapdoor);
zkCreds = new ZkCredentials();
console.log('Scenario: Apply for loan proving income > 50k without revealing exact salary\n');
console.log("Private data (NOT revealed):");
console.log(" Actual salary: 75,000 EUR");
console.log(" Employer: Tech Company XYZ\n");
_a.label = 3;
case 3:
_a.trys.push([3, 5, , 6]);
return [4 /*yield*/, zkCreds.proveIncome(semaphoreIdentity, 75000, 50000, 'EUR')];
case 4:
incomeProof = _a.sent();
console.log("Public proof sent to bank:");
console.log(" Claim: \"".concat(incomeProof.claim, "\""));
console.log(" \u2705 Exact salary NOT revealed!");
console.log(" \u2705 Only proves income >= 50,000 EUR");
console.log("\n Bank sees: \"This person earns at least 50k\"");
console.log(" Bank does NOT see: Actual amount or employer");
return [3 /*break*/, 6];
case 5:
error_4 = _a.sent();
console.log("Note: Full proof requires circuit files");
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
});
}
// Example 5: Custom Credential
function customCredentialExample() {
return __awaiter(this, void 0, void 0, function () {
var shogun, zkPlugin, identity, semaphoreIdentity, zkCreds, devProof, error_5;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
console.log('\n=== Custom Credential Example ===\n');
shogun = new ShogunCore({
gunInstance: Gun({ peers: ['https://peer.wallie.io/gun'] }),
zkproof: { enabled: true },
});
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })];
case 1:
_a.sent();
zkPlugin = shogun.getPlugin('zkproof');
if (!zkPlugin)
return [2 /*return*/];
return [4 /*yield*/, zkPlugin.generateIdentity()];
case 2:
identity = _a.sent();
semaphoreIdentity = new Identity(identity.trapdoor);
zkCreds = new ZkCredentials();
console.log("Scenario: Prove you're a verified developer without revealing GitHub profile\n");
console.log("Private data (NOT revealed):");
console.log(" GitHub: @johndoe");
console.log(" Repositories: 150");
console.log(" Stars: 5,234");
console.log(" Years active: 8\n");
_a.label = 3;
case 3:
_a.trys.push([3, 5, , 6]);
return [4 /*yield*/, zkCreds.proveAttribute(semaphoreIdentity, {
type: CredentialType.CUSTOM,
claim: 'Verified GitHub developer with 5+ years experience',
privateData: {
githubUsername: 'johndoe',
repositories: 150,
stars: 5234,
yearsActive: 8,
verified: true,
},
})];
case 4:
devProof = _a.sent();
console.log("Public proof:");
console.log(" Claim: \"".concat(devProof.claim, "\""));
console.log(" \u2705 GitHub username NOT revealed!");
console.log(" \u2705 Exact stats NOT revealed!");
console.log(" \u2705 Privacy-preserving reputation proof");
return [3 /*break*/, 6];
case 5:
error_5 = _a.sent();
console.log("Note: Full proof requires circuit files");
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
});
}
// Run all examples
function main() {
return __awaiter(this, void 0, void 0, function () {
var error_6;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
console.log('đ ZK-Proof Verifiable Credentials Examples');
console.log('==========================================\n');
_a.label = 1;
case 1:
_a.trys.push([1, 7, , 8]);
return [4 /*yield*/, ageVerificationExample()];
case 2:
_a.sent();
return [4 /*yield*/, citizenshipExample()];
case 3:
_a.sent();
return [4 /*yield*/, educationExample()];
case 4:
_a.sent();
return [4 /*yield*/, incomeExample()];
case 5:
_a.sent();
return [4 /*yield*/, customCredentialExample()];
case 6:
_a.sent();
console.log('\n⨠All credential examples completed!');
console.log('\nâšī¸ Note: These examples show the credential structure.');
console.log(' For full ZK proof generation, run: yarn setup:zkproof');
return [3 /*break*/, 8];
case 7:
error_6 = _a.sent();
console.error('\nâ Error:', error_6);
return [3 /*break*/, 8];
case 8:
process.exit(0);
return [2 /*return*/];
}
});
});
}
if (require.main === module) {
main();
}
export { ageVerificationExample, citizenshipExample, educationExample, incomeExample, customCredentialExample, };