@kynesyslabs/demosdk 2.4.26 → 2.5.1
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/build/encryption/zK/identity/CommitmentService.d.ts +49 -0
- package/build/encryption/zK/identity/CommitmentService.js +121 -0
- package/build/encryption/zK/identity/CommitmentService.js.map +1 -0
- package/build/encryption/zK/identity/ProofGenerator.d.ts +60 -0
- package/build/encryption/zK/identity/ProofGenerator.js +167 -0
- package/build/encryption/zK/identity/ProofGenerator.js.map +1 -0
- package/build/encryption/zK/identity/ZKIdentity.d.ts +141 -0
- package/build/encryption/zK/identity/ZKIdentity.js +231 -0
- package/build/encryption/zK/identity/ZKIdentity.js.map +1 -0
- package/build/encryption/zK/identity/index.d.ts +12 -0
- package/build/encryption/zK/identity/index.js +48 -0
- package/build/encryption/zK/identity/index.js.map +1 -0
- package/build/encryption/zK/index.d.ts +1 -0
- package/build/encryption/zK/index.js +3 -1
- package/build/encryption/zK/index.js.map +1 -1
- package/package.json +3 -1
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CommitmentService - Generate identity commitments and nullifiers
|
|
3
|
+
*
|
|
4
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
5
|
+
* REVIEW: Phase 10.1 - Production Implementation (Real Poseidon hash)
|
|
6
|
+
*
|
|
7
|
+
* Provides methods for generating cryptographic commitments and nullifiers
|
|
8
|
+
* for the ZK identity system using Poseidon hash.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Generate a Poseidon hash commitment from provider ID and secret
|
|
12
|
+
*
|
|
13
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
14
|
+
* @param secret - User's secret value (generated client-side)
|
|
15
|
+
* @returns Commitment hash as string
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const commitment = CommitmentService.generateCommitment("github:12345", "secret123")
|
|
20
|
+
* // commitment: "1234567890..."
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function generateCommitment(providerId: string, secret: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* Generate a nullifier from provider ID and context
|
|
26
|
+
*
|
|
27
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
28
|
+
* @param context - Context string (e.g., "dao_vote_123")
|
|
29
|
+
* @returns Nullifier hash as string
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const nullifier = CommitmentService.generateNullifier("github:12345", "dao_vote_123")
|
|
34
|
+
* // nullifier: "9876543210..."
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function generateNullifier(providerId: string, context: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* Generate a cryptographically secure random secret
|
|
40
|
+
*
|
|
41
|
+
* @returns Random secret as hex string
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const secret = CommitmentService.generateSecret()
|
|
46
|
+
* // secret: "a1b2c3d4e5f6..."
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function generateSecret(): string;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* CommitmentService - Generate identity commitments and nullifiers
|
|
4
|
+
*
|
|
5
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
6
|
+
* REVIEW: Phase 10.1 - Production Implementation (Real Poseidon hash)
|
|
7
|
+
*
|
|
8
|
+
* Provides methods for generating cryptographic commitments and nullifiers
|
|
9
|
+
* for the ZK identity system using Poseidon hash.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.generateCommitment = generateCommitment;
|
|
13
|
+
exports.generateNullifier = generateNullifier;
|
|
14
|
+
exports.generateSecret = generateSecret;
|
|
15
|
+
// REVIEW: Phase 10.1 - Production cryptographic implementation
|
|
16
|
+
const poseidon_lite_1 = require("poseidon-lite");
|
|
17
|
+
/**
|
|
18
|
+
* Generate a Poseidon hash commitment from provider ID and secret
|
|
19
|
+
*
|
|
20
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
21
|
+
* @param secret - User's secret value (generated client-side)
|
|
22
|
+
* @returns Commitment hash as string
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const commitment = CommitmentService.generateCommitment("github:12345", "secret123")
|
|
27
|
+
* // commitment: "1234567890..."
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
function generateCommitment(providerId, secret) {
|
|
31
|
+
// Convert strings to BigInt for Poseidon hashing
|
|
32
|
+
const providerHash = stringToBigInt(providerId);
|
|
33
|
+
const secretHash = stringToBigInt(secret);
|
|
34
|
+
// Use Poseidon hash (ZK-friendly)
|
|
35
|
+
// NOTE: This will be implemented using poseidon-lite or browser-compatible alternative
|
|
36
|
+
// For now, using a placeholder that will be replaced with actual Poseidon implementation
|
|
37
|
+
const commitment = poseidonHash([providerHash, secretHash]);
|
|
38
|
+
return commitment.toString();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Generate a nullifier from provider ID and context
|
|
42
|
+
*
|
|
43
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
44
|
+
* @param context - Context string (e.g., "dao_vote_123")
|
|
45
|
+
* @returns Nullifier hash as string
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const nullifier = CommitmentService.generateNullifier("github:12345", "dao_vote_123")
|
|
50
|
+
* // nullifier: "9876543210..."
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
function generateNullifier(providerId, context) {
|
|
54
|
+
const providerHash = stringToBigInt(providerId);
|
|
55
|
+
const contextHash = stringToBigInt(context);
|
|
56
|
+
const nullifier = poseidonHash([providerHash, contextHash]);
|
|
57
|
+
return nullifier.toString();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Generate a cryptographically secure random secret
|
|
61
|
+
*
|
|
62
|
+
* @returns Random secret as hex string
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const secret = CommitmentService.generateSecret()
|
|
67
|
+
* // secret: "a1b2c3d4e5f6..."
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
function generateSecret() {
|
|
71
|
+
// Use Web Crypto API for secure random generation
|
|
72
|
+
if (typeof window !== 'undefined' && window.crypto) {
|
|
73
|
+
const array = new Uint8Array(32);
|
|
74
|
+
window.crypto.getRandomValues(array);
|
|
75
|
+
return uint8ArrayToHex(array);
|
|
76
|
+
}
|
|
77
|
+
// Node.js environment
|
|
78
|
+
if (typeof require !== 'undefined') {
|
|
79
|
+
const crypto = require('crypto');
|
|
80
|
+
return crypto.randomBytes(32).toString('hex');
|
|
81
|
+
}
|
|
82
|
+
throw new Error('No secure random number generator available');
|
|
83
|
+
}
|
|
84
|
+
// ============================================================================
|
|
85
|
+
// Helper Functions
|
|
86
|
+
// ============================================================================
|
|
87
|
+
/**
|
|
88
|
+
* Convert string to BigInt using simple hashing
|
|
89
|
+
* NOTE: For production, should use more robust hashing
|
|
90
|
+
*/
|
|
91
|
+
function stringToBigInt(str) {
|
|
92
|
+
// Simple conversion: encode to UTF-8 bytes then to hex
|
|
93
|
+
const encoder = new TextEncoder();
|
|
94
|
+
const bytes = encoder.encode(str);
|
|
95
|
+
const hex = uint8ArrayToHex(bytes);
|
|
96
|
+
return BigInt('0x' + hex);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Convert Uint8Array to hex string
|
|
100
|
+
*/
|
|
101
|
+
function uint8ArrayToHex(array) {
|
|
102
|
+
return Array.from(array)
|
|
103
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
104
|
+
.join('');
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Poseidon hash implementation using poseidon-lite
|
|
108
|
+
*
|
|
109
|
+
* Poseidon is a ZK-friendly hash function optimized for use in
|
|
110
|
+
* zero-knowledge proof circuits. We use poseidon2 for hashing 2 inputs.
|
|
111
|
+
*
|
|
112
|
+
* @param inputs - Array of exactly 2 BigInt values to hash
|
|
113
|
+
* @returns Poseidon hash output as BigInt
|
|
114
|
+
*/
|
|
115
|
+
function poseidonHash(inputs) {
|
|
116
|
+
if (inputs.length !== 2) {
|
|
117
|
+
throw new Error('poseidonHash expects exactly 2 inputs');
|
|
118
|
+
}
|
|
119
|
+
return (0, poseidon_lite_1.poseidon2)(inputs);
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=CommitmentService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CommitmentService.js","sourceRoot":"","sources":["../../../../../src/encryption/zK/identity/CommitmentService.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;AAkBH,gDAWC;AAeD,8CAOC;AAaD,wCAeC;AA7ED,+DAA+D;AAC/D,iDAAyC;AAEzC;;;;;;;;;;;;GAYG;AACH,SAAgB,kBAAkB,CAAC,UAAkB,EAAE,MAAc;IACjE,iDAAiD;IACjD,MAAM,YAAY,GAAG,cAAc,CAAC,UAAU,CAAC,CAAA;IAC/C,MAAM,UAAU,GAAG,cAAc,CAAC,MAAM,CAAC,CAAA;IAEzC,kCAAkC;IAClC,uFAAuF;IACvF,yFAAyF;IACzF,MAAM,UAAU,GAAG,YAAY,CAAC,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC,CAAA;IAE3D,OAAO,UAAU,CAAC,QAAQ,EAAE,CAAA;AAChC,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,iBAAiB,CAAC,UAAkB,EAAE,OAAe;IACjE,MAAM,YAAY,GAAG,cAAc,CAAC,UAAU,CAAC,CAAA;IAC/C,MAAM,WAAW,GAAG,cAAc,CAAC,OAAO,CAAC,CAAA;IAE3C,MAAM,SAA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EAAE,GAAG,CAAC,CAAC;SACzC,IAAI,CAAC,EAAE,CAAC,CAAA;AACjB,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,YAAY,CAAC,MAAgB;IAClC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAA;IAC5D,CAAC;IACD,OAAO,IAAA,yBAAS,EAAC,MAAM,CAAC,CAAA;AAC5B,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ProofGenerator - Client-side ZK-SNARK proof generation
|
|
3
|
+
*
|
|
4
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
5
|
+
* REVIEW: Phase 10.1 - Production Implementation (Real snarkjs proof generation)
|
|
6
|
+
* REVIEW: Phase 10.4 - CDN Integration (Production-ready with CDN URLs)
|
|
7
|
+
*
|
|
8
|
+
* Generates Groth16 ZK-SNARK proofs for identity attestations using snarkjs.
|
|
9
|
+
* Circuit artifacts (WASM, proving key, verification key) are loaded from CDN.
|
|
10
|
+
*/
|
|
11
|
+
export interface ZKProof {
|
|
12
|
+
pi_a: string[];
|
|
13
|
+
pi_b: string[][];
|
|
14
|
+
pi_c: string[];
|
|
15
|
+
protocol: string;
|
|
16
|
+
}
|
|
17
|
+
export interface ProofGenerationResult {
|
|
18
|
+
proof: ZKProof;
|
|
19
|
+
publicSignals: string[];
|
|
20
|
+
}
|
|
21
|
+
export interface MerkleProof {
|
|
22
|
+
siblings: string[][];
|
|
23
|
+
pathIndices: number[];
|
|
24
|
+
root: string;
|
|
25
|
+
leaf: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Generate a ZK-SNARK proof for identity attestation
|
|
29
|
+
*
|
|
30
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
31
|
+
* @param secret - User's secret value
|
|
32
|
+
* @param context - Context string for this attestation
|
|
33
|
+
* @param merkleProof - Merkle proof from node RPC
|
|
34
|
+
* @param merkleRoot - Current Merkle root from node RPC
|
|
35
|
+
* @returns Proof and public signals
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const result = await ProofGenerator.generateIdentityProof(
|
|
40
|
+
* "github:12345",
|
|
41
|
+
* "secret123",
|
|
42
|
+
* "dao_vote_123",
|
|
43
|
+
* merkleProof,
|
|
44
|
+
* merkleRoot
|
|
45
|
+
* )
|
|
46
|
+
* // result: { proof: {...}, publicSignals: [nullifier, merkleRoot, context] }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function generateIdentityProof(providerId: string, secret: string, context: string, merkleProof: MerkleProof, merkleRoot: string): Promise<ProofGenerationResult>;
|
|
50
|
+
/**
|
|
51
|
+
* Verify a proof locally (optional - mainly for testing)
|
|
52
|
+
*
|
|
53
|
+
* @param proof - The proof to verify
|
|
54
|
+
* @param publicSignals - Public signals for the proof
|
|
55
|
+
* @returns True if proof is valid
|
|
56
|
+
*
|
|
57
|
+
* NOTE: Node RPC will do the actual verification, this is mainly for debugging
|
|
58
|
+
* REVIEW: Phase 10.4 - Production verification implementation with CDN
|
|
59
|
+
*/
|
|
60
|
+
export declare function verifyProof(proof: ZKProof, publicSignals: string[]): Promise<boolean>;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ProofGenerator - Client-side ZK-SNARK proof generation
|
|
4
|
+
*
|
|
5
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
6
|
+
* REVIEW: Phase 10.1 - Production Implementation (Real snarkjs proof generation)
|
|
7
|
+
* REVIEW: Phase 10.4 - CDN Integration (Production-ready with CDN URLs)
|
|
8
|
+
*
|
|
9
|
+
* Generates Groth16 ZK-SNARK proofs for identity attestations using snarkjs.
|
|
10
|
+
* Circuit artifacts (WASM, proving key, verification key) are loaded from CDN.
|
|
11
|
+
*/
|
|
12
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
15
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
16
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
17
|
+
}
|
|
18
|
+
Object.defineProperty(o, k2, desc);
|
|
19
|
+
}) : (function(o, m, k, k2) {
|
|
20
|
+
if (k2 === undefined) k2 = k;
|
|
21
|
+
o[k2] = m[k];
|
|
22
|
+
}));
|
|
23
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
24
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
25
|
+
}) : function(o, v) {
|
|
26
|
+
o["default"] = v;
|
|
27
|
+
});
|
|
28
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
29
|
+
var ownKeys = function(o) {
|
|
30
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
31
|
+
var ar = [];
|
|
32
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
33
|
+
return ar;
|
|
34
|
+
};
|
|
35
|
+
return ownKeys(o);
|
|
36
|
+
};
|
|
37
|
+
return function (mod) {
|
|
38
|
+
if (mod && mod.__esModule) return mod;
|
|
39
|
+
var result = {};
|
|
40
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
41
|
+
__setModuleDefault(result, mod);
|
|
42
|
+
return result;
|
|
43
|
+
};
|
|
44
|
+
})();
|
|
45
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
+
exports.generateIdentityProof = generateIdentityProof;
|
|
47
|
+
exports.verifyProof = verifyProof;
|
|
48
|
+
// REVIEW: Phase 10.1 - Production cryptographic implementation
|
|
49
|
+
const snarkjs = __importStar(require("snarkjs"));
|
|
50
|
+
/**
|
|
51
|
+
* Generate a ZK-SNARK proof for identity attestation
|
|
52
|
+
*
|
|
53
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
54
|
+
* @param secret - User's secret value
|
|
55
|
+
* @param context - Context string for this attestation
|
|
56
|
+
* @param merkleProof - Merkle proof from node RPC
|
|
57
|
+
* @param merkleRoot - Current Merkle root from node RPC
|
|
58
|
+
* @returns Proof and public signals
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const result = await ProofGenerator.generateIdentityProof(
|
|
63
|
+
* "github:12345",
|
|
64
|
+
* "secret123",
|
|
65
|
+
* "dao_vote_123",
|
|
66
|
+
* merkleProof,
|
|
67
|
+
* merkleRoot
|
|
68
|
+
* )
|
|
69
|
+
* // result: { proof: {...}, publicSignals: [nullifier, merkleRoot, context] }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
async function generateIdentityProof(providerId, secret, context, merkleProof, merkleRoot) {
|
|
73
|
+
// Convert inputs to BigInt/field elements
|
|
74
|
+
const providerIdBigInt = stringToBigInt(providerId);
|
|
75
|
+
const secretBigInt = stringToBigInt(secret);
|
|
76
|
+
const contextBigInt = stringToBigInt(context);
|
|
77
|
+
// Prepare circuit inputs
|
|
78
|
+
const circuitInputs = {
|
|
79
|
+
// Private inputs
|
|
80
|
+
provider_id: providerIdBigInt.toString(),
|
|
81
|
+
secret: secretBigInt.toString(),
|
|
82
|
+
pathElements: merkleProof.siblings.map(s => s.map(v => v.toString())),
|
|
83
|
+
pathIndices: merkleProof.pathIndices,
|
|
84
|
+
// Public inputs
|
|
85
|
+
context: contextBigInt.toString(),
|
|
86
|
+
merkle_root: merkleRoot,
|
|
87
|
+
};
|
|
88
|
+
// REVIEW: Phase 10.4 - Production CDN URLs for circuit artifacts
|
|
89
|
+
const wasmPath = 'https://files.demos.sh/zk-circuits/v1/identity_with_merkle.wasm';
|
|
90
|
+
const zkeyPath = 'https://files.demos.sh/zk-circuits/v1/identity_with_merkle_0000.zkey';
|
|
91
|
+
// REVIEW: Phase 10.1 - Production-ready proof generation using snarkjs
|
|
92
|
+
const { proof, publicSignals } = await snarkjs.groth16.fullProve(circuitInputs, wasmPath, zkeyPath);
|
|
93
|
+
// Convert proof to our ZKProof format
|
|
94
|
+
const zkProof = {
|
|
95
|
+
pi_a: [
|
|
96
|
+
proof.pi_a[0].toString(),
|
|
97
|
+
proof.pi_a[1].toString(),
|
|
98
|
+
proof.pi_a[2].toString(),
|
|
99
|
+
],
|
|
100
|
+
pi_b: [
|
|
101
|
+
[proof.pi_b[0][0].toString(), proof.pi_b[0][1].toString()],
|
|
102
|
+
[proof.pi_b[1][0].toString(), proof.pi_b[1][1].toString()],
|
|
103
|
+
[proof.pi_b[2][0].toString(), proof.pi_b[2][1].toString()],
|
|
104
|
+
],
|
|
105
|
+
pi_c: [
|
|
106
|
+
proof.pi_c[0].toString(),
|
|
107
|
+
proof.pi_c[1].toString(),
|
|
108
|
+
proof.pi_c[2].toString(),
|
|
109
|
+
],
|
|
110
|
+
protocol: 'groth16',
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
proof: zkProof,
|
|
114
|
+
publicSignals: publicSignals.map((s) => s.toString()),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Verify a proof locally (optional - mainly for testing)
|
|
119
|
+
*
|
|
120
|
+
* @param proof - The proof to verify
|
|
121
|
+
* @param publicSignals - Public signals for the proof
|
|
122
|
+
* @returns True if proof is valid
|
|
123
|
+
*
|
|
124
|
+
* NOTE: Node RPC will do the actual verification, this is mainly for debugging
|
|
125
|
+
* REVIEW: Phase 10.4 - Production verification implementation with CDN
|
|
126
|
+
*/
|
|
127
|
+
async function verifyProof(proof, publicSignals) {
|
|
128
|
+
// REVIEW: Phase 10.4 - Load verification key from CDN
|
|
129
|
+
const vkeyUrl = 'https://files.demos.sh/zk-circuits/v1/verification_key_merkle.json';
|
|
130
|
+
try {
|
|
131
|
+
const response = await fetch(vkeyUrl);
|
|
132
|
+
if (!response.ok) {
|
|
133
|
+
throw new Error(`Failed to load verification key: ${response.status} ${response.statusText}`);
|
|
134
|
+
}
|
|
135
|
+
const vkey = await response.json();
|
|
136
|
+
// REVIEW: Phase 10.1 - Production-ready verification using snarkjs
|
|
137
|
+
// Convert our ZKProof format to snarkjs format
|
|
138
|
+
const snarkjsProof = {
|
|
139
|
+
pi_a: proof.pi_a,
|
|
140
|
+
pi_b: proof.pi_b,
|
|
141
|
+
pi_c: proof.pi_c,
|
|
142
|
+
protocol: proof.protocol,
|
|
143
|
+
};
|
|
144
|
+
return await snarkjs.groth16.verify(vkey, publicSignals, snarkjsProof);
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
throw new Error(`ZK proof verification failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// ============================================================================
|
|
151
|
+
// Helper Functions
|
|
152
|
+
// ============================================================================
|
|
153
|
+
/**
|
|
154
|
+
* Convert string to BigInt using simple hashing
|
|
155
|
+
*
|
|
156
|
+
* @param str - Input string to convert
|
|
157
|
+
* @returns BigInt representation of the string
|
|
158
|
+
*/
|
|
159
|
+
function stringToBigInt(str) {
|
|
160
|
+
const encoder = new TextEncoder();
|
|
161
|
+
const bytes = encoder.encode(str);
|
|
162
|
+
const hex = Array.from(bytes)
|
|
163
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
164
|
+
.join('');
|
|
165
|
+
return BigInt('0x' + hex);
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=ProofGenerator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProofGenerator.js","sourceRoot":"","sources":["../../../../../src/encryption/zK/identity/ProofGenerator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CH,sDA4DC;AAYD,kCA6B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/**
|
|
2
|
+
* ZKIdentity - User-facing API for ZK identity system
|
|
3
|
+
*
|
|
4
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
5
|
+
*
|
|
6
|
+
* Provides a simple interface for users to:
|
|
7
|
+
* 1. Create identity commitments
|
|
8
|
+
* 2. Generate ZK attestation proofs
|
|
9
|
+
* 3. Interact with node RPC endpoints
|
|
10
|
+
*/
|
|
11
|
+
import * as ProofGenerator from './ProofGenerator';
|
|
12
|
+
export interface IdentityCommitmentPayload {
|
|
13
|
+
commitment_hash: string;
|
|
14
|
+
provider: string;
|
|
15
|
+
timestamp: number;
|
|
16
|
+
}
|
|
17
|
+
export interface IdentityAttestationPayload {
|
|
18
|
+
nullifier_hash: string;
|
|
19
|
+
merkle_root: string;
|
|
20
|
+
proof: ProofGenerator.ZKProof;
|
|
21
|
+
public_signals: string[];
|
|
22
|
+
provider: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* ZKIdentity class for privacy-preserving identity attestations
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // Create identity
|
|
30
|
+
* const identity = new ZKIdentity('github:12345')
|
|
31
|
+
*
|
|
32
|
+
* // Get commitment (safe to share publicly)
|
|
33
|
+
* const commitment = identity.getCommitment()
|
|
34
|
+
*
|
|
35
|
+
* // Create commitment transaction
|
|
36
|
+
* const commitmentTx = await identity.createCommitmentTransaction('http://localhost:3000')
|
|
37
|
+
*
|
|
38
|
+
* // Later: Create anonymous attestation
|
|
39
|
+
* const attestationTx = await identity.createAttestationTransaction(
|
|
40
|
+
* 'http://localhost:3000',
|
|
41
|
+
* 'dao_vote_123'
|
|
42
|
+
* )
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare class ZKIdentity {
|
|
46
|
+
private providerId;
|
|
47
|
+
private secret;
|
|
48
|
+
/**
|
|
49
|
+
* Create a new ZK identity
|
|
50
|
+
*
|
|
51
|
+
* @param providerId - Provider identifier (e.g., "github:12345", "discord:67890")
|
|
52
|
+
* @param secret - Optional secret (will be generated if not provided)
|
|
53
|
+
*
|
|
54
|
+
* WARNING: Keep the secret secure! Store it encrypted in local storage.
|
|
55
|
+
* If lost, you cannot create attestations for this commitment.
|
|
56
|
+
*/
|
|
57
|
+
constructor(providerId: string, secret?: string);
|
|
58
|
+
/**
|
|
59
|
+
* Get the identity commitment hash
|
|
60
|
+
*
|
|
61
|
+
* This is safe to share publicly and will be stored in the Merkle tree.
|
|
62
|
+
*
|
|
63
|
+
* @returns Commitment hash as string
|
|
64
|
+
*/
|
|
65
|
+
getCommitment(): string;
|
|
66
|
+
/**
|
|
67
|
+
* Get the provider name from provider ID
|
|
68
|
+
*
|
|
69
|
+
* @returns Provider name (e.g., "github", "discord")
|
|
70
|
+
*/
|
|
71
|
+
getProvider(): string;
|
|
72
|
+
/**
|
|
73
|
+
* Get the secret (use with caution!)
|
|
74
|
+
*
|
|
75
|
+
* @returns The secret value
|
|
76
|
+
*
|
|
77
|
+
* WARNING: Never transmit this over the network!
|
|
78
|
+
*/
|
|
79
|
+
getSecret(): string;
|
|
80
|
+
/**
|
|
81
|
+
* Create a commitment transaction to submit to the node
|
|
82
|
+
*
|
|
83
|
+
* This adds your commitment to the global Merkle tree.
|
|
84
|
+
* You must submit this transaction before you can create attestations.
|
|
85
|
+
*
|
|
86
|
+
* @param rpcUrl - Node RPC URL
|
|
87
|
+
* @returns Commitment transaction payload
|
|
88
|
+
*/
|
|
89
|
+
createCommitmentTransaction(rpcUrl: string): Promise<IdentityCommitmentPayload>;
|
|
90
|
+
/**
|
|
91
|
+
* Create an anonymous attestation transaction
|
|
92
|
+
*
|
|
93
|
+
* This proves you have a valid identity commitment in the Merkle tree
|
|
94
|
+
* without revealing which one. Uses ZK-SNARKs for privacy.
|
|
95
|
+
*
|
|
96
|
+
* @param rpcUrl - Node RPC URL
|
|
97
|
+
* @param context - Context string for this attestation (e.g., "dao_vote_123")
|
|
98
|
+
* @returns Attestation transaction payload
|
|
99
|
+
*
|
|
100
|
+
* NOTE: Each context can only be used once per identity (nullifier prevents reuse)
|
|
101
|
+
*/
|
|
102
|
+
createAttestationTransaction(rpcUrl: string, context: string): Promise<IdentityAttestationPayload>;
|
|
103
|
+
/**
|
|
104
|
+
* Verify an attestation locally (optional - for testing)
|
|
105
|
+
*
|
|
106
|
+
* @param attestation - Attestation payload to verify
|
|
107
|
+
* @returns True if valid
|
|
108
|
+
*
|
|
109
|
+
* NOTE: Node will verify proofs server-side, this is mainly for debugging
|
|
110
|
+
*/
|
|
111
|
+
static verifyAttestation(attestation: IdentityAttestationPayload): Promise<boolean>;
|
|
112
|
+
/**
|
|
113
|
+
* Export identity for backup
|
|
114
|
+
*
|
|
115
|
+
* @returns Object containing provider ID and secret
|
|
116
|
+
*
|
|
117
|
+
* WARNING: Store this securely! Anyone with this data can create
|
|
118
|
+
* attestations as you.
|
|
119
|
+
*/
|
|
120
|
+
export(): {
|
|
121
|
+
providerId: string;
|
|
122
|
+
secret: string;
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Import identity from backup
|
|
126
|
+
*
|
|
127
|
+
* @param data - Exported identity data
|
|
128
|
+
* @returns New ZKIdentity instance
|
|
129
|
+
*/
|
|
130
|
+
static import(data: {
|
|
131
|
+
providerId: string;
|
|
132
|
+
secret: string;
|
|
133
|
+
}): ZKIdentity;
|
|
134
|
+
/**
|
|
135
|
+
* Generate a fresh identity with random secret
|
|
136
|
+
*
|
|
137
|
+
* @param providerId - Provider identifier
|
|
138
|
+
* @returns New ZKIdentity instance
|
|
139
|
+
*/
|
|
140
|
+
static generate(providerId: string): ZKIdentity;
|
|
141
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ZKIdentity - User-facing API for ZK identity system
|
|
4
|
+
*
|
|
5
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
6
|
+
*
|
|
7
|
+
* Provides a simple interface for users to:
|
|
8
|
+
* 1. Create identity commitments
|
|
9
|
+
* 2. Generate ZK attestation proofs
|
|
10
|
+
* 3. Interact with node RPC endpoints
|
|
11
|
+
*/
|
|
12
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
15
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
16
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
17
|
+
}
|
|
18
|
+
Object.defineProperty(o, k2, desc);
|
|
19
|
+
}) : (function(o, m, k, k2) {
|
|
20
|
+
if (k2 === undefined) k2 = k;
|
|
21
|
+
o[k2] = m[k];
|
|
22
|
+
}));
|
|
23
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
24
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
25
|
+
}) : function(o, v) {
|
|
26
|
+
o["default"] = v;
|
|
27
|
+
});
|
|
28
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
29
|
+
var ownKeys = function(o) {
|
|
30
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
31
|
+
var ar = [];
|
|
32
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
33
|
+
return ar;
|
|
34
|
+
};
|
|
35
|
+
return ownKeys(o);
|
|
36
|
+
};
|
|
37
|
+
return function (mod) {
|
|
38
|
+
if (mod && mod.__esModule) return mod;
|
|
39
|
+
var result = {};
|
|
40
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
41
|
+
__setModuleDefault(result, mod);
|
|
42
|
+
return result;
|
|
43
|
+
};
|
|
44
|
+
})();
|
|
45
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
+
exports.ZKIdentity = void 0;
|
|
47
|
+
const CommitmentService = __importStar(require("./CommitmentService"));
|
|
48
|
+
const ProofGenerator = __importStar(require("./ProofGenerator"));
|
|
49
|
+
/**
|
|
50
|
+
* ZKIdentity class for privacy-preserving identity attestations
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* // Create identity
|
|
55
|
+
* const identity = new ZKIdentity('github:12345')
|
|
56
|
+
*
|
|
57
|
+
* // Get commitment (safe to share publicly)
|
|
58
|
+
* const commitment = identity.getCommitment()
|
|
59
|
+
*
|
|
60
|
+
* // Create commitment transaction
|
|
61
|
+
* const commitmentTx = await identity.createCommitmentTransaction('http://localhost:3000')
|
|
62
|
+
*
|
|
63
|
+
* // Later: Create anonymous attestation
|
|
64
|
+
* const attestationTx = await identity.createAttestationTransaction(
|
|
65
|
+
* 'http://localhost:3000',
|
|
66
|
+
* 'dao_vote_123'
|
|
67
|
+
* )
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
class ZKIdentity {
|
|
71
|
+
/**
|
|
72
|
+
* Create a new ZK identity
|
|
73
|
+
*
|
|
74
|
+
* @param providerId - Provider identifier (e.g., "github:12345", "discord:67890")
|
|
75
|
+
* @param secret - Optional secret (will be generated if not provided)
|
|
76
|
+
*
|
|
77
|
+
* WARNING: Keep the secret secure! Store it encrypted in local storage.
|
|
78
|
+
* If lost, you cannot create attestations for this commitment.
|
|
79
|
+
*/
|
|
80
|
+
constructor(providerId, secret) {
|
|
81
|
+
this.providerId = providerId;
|
|
82
|
+
this.secret = secret || CommitmentService.generateSecret();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Get the identity commitment hash
|
|
86
|
+
*
|
|
87
|
+
* This is safe to share publicly and will be stored in the Merkle tree.
|
|
88
|
+
*
|
|
89
|
+
* @returns Commitment hash as string
|
|
90
|
+
*/
|
|
91
|
+
getCommitment() {
|
|
92
|
+
return CommitmentService.generateCommitment(this.providerId, this.secret);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Get the provider name from provider ID
|
|
96
|
+
*
|
|
97
|
+
* @returns Provider name (e.g., "github", "discord")
|
|
98
|
+
*/
|
|
99
|
+
getProvider() {
|
|
100
|
+
return this.providerId.split(':')[0];
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get the secret (use with caution!)
|
|
104
|
+
*
|
|
105
|
+
* @returns The secret value
|
|
106
|
+
*
|
|
107
|
+
* WARNING: Never transmit this over the network!
|
|
108
|
+
*/
|
|
109
|
+
getSecret() {
|
|
110
|
+
return this.secret;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Create a commitment transaction to submit to the node
|
|
114
|
+
*
|
|
115
|
+
* This adds your commitment to the global Merkle tree.
|
|
116
|
+
* You must submit this transaction before you can create attestations.
|
|
117
|
+
*
|
|
118
|
+
* @param rpcUrl - Node RPC URL
|
|
119
|
+
* @returns Commitment transaction payload
|
|
120
|
+
*/
|
|
121
|
+
async createCommitmentTransaction(rpcUrl) {
|
|
122
|
+
const commitment = this.getCommitment();
|
|
123
|
+
const payload = {
|
|
124
|
+
commitment_hash: commitment,
|
|
125
|
+
provider: this.getProvider(),
|
|
126
|
+
timestamp: Date.now(),
|
|
127
|
+
};
|
|
128
|
+
return payload;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Create an anonymous attestation transaction
|
|
132
|
+
*
|
|
133
|
+
* This proves you have a valid identity commitment in the Merkle tree
|
|
134
|
+
* without revealing which one. Uses ZK-SNARKs for privacy.
|
|
135
|
+
*
|
|
136
|
+
* @param rpcUrl - Node RPC URL
|
|
137
|
+
* @param context - Context string for this attestation (e.g., "dao_vote_123")
|
|
138
|
+
* @returns Attestation transaction payload
|
|
139
|
+
*
|
|
140
|
+
* NOTE: Each context can only be used once per identity (nullifier prevents reuse)
|
|
141
|
+
*/
|
|
142
|
+
async createAttestationTransaction(rpcUrl, context) {
|
|
143
|
+
// 1. Check if commitment exists in Merkle tree
|
|
144
|
+
const commitment = this.getCommitment();
|
|
145
|
+
// 2. Fetch Merkle proof for our commitment
|
|
146
|
+
const proofResponse = await fetch(`${rpcUrl}/zk/merkle/proof/${commitment}`);
|
|
147
|
+
if (!proofResponse.ok) {
|
|
148
|
+
const error = await proofResponse.json();
|
|
149
|
+
throw new Error(`Failed to get Merkle proof: ${error.error || 'Unknown error'}`);
|
|
150
|
+
}
|
|
151
|
+
const proofData = await proofResponse.json();
|
|
152
|
+
const merkleProof = {
|
|
153
|
+
siblings: proofData.proof.siblings,
|
|
154
|
+
pathIndices: proofData.proof.pathIndices,
|
|
155
|
+
root: proofData.proof.root,
|
|
156
|
+
leaf: proofData.proof.leaf || commitment,
|
|
157
|
+
};
|
|
158
|
+
// 3. Fetch current Merkle root
|
|
159
|
+
const rootResponse = await fetch(`${rpcUrl}/zk/merkle-root`);
|
|
160
|
+
if (!rootResponse.ok) {
|
|
161
|
+
throw new Error('Failed to get Merkle root');
|
|
162
|
+
}
|
|
163
|
+
const rootData = await rootResponse.json();
|
|
164
|
+
const merkleRoot = rootData.rootHash;
|
|
165
|
+
// 4. Check if nullifier has been used for this context
|
|
166
|
+
const nullifier = CommitmentService.generateNullifier(this.providerId, context);
|
|
167
|
+
const nullifierResponse = await fetch(`${rpcUrl}/zk/nullifier/${nullifier}`);
|
|
168
|
+
if (nullifierResponse.ok) {
|
|
169
|
+
const nullifierData = await nullifierResponse.json();
|
|
170
|
+
if (nullifierData.used) {
|
|
171
|
+
throw new Error(`Nullifier already used for context "${context}". Each identity can only attest once per context.`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
// 5. Generate ZK proof
|
|
175
|
+
const { proof, publicSignals } = await ProofGenerator.generateIdentityProof(this.providerId, this.secret, context, merkleProof, merkleRoot);
|
|
176
|
+
// 6. Create attestation payload
|
|
177
|
+
const payload = {
|
|
178
|
+
nullifier_hash: publicSignals[0],
|
|
179
|
+
merkle_root: publicSignals[1],
|
|
180
|
+
proof,
|
|
181
|
+
public_signals: publicSignals,
|
|
182
|
+
provider: this.getProvider(),
|
|
183
|
+
};
|
|
184
|
+
return payload;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Verify an attestation locally (optional - for testing)
|
|
188
|
+
*
|
|
189
|
+
* @param attestation - Attestation payload to verify
|
|
190
|
+
* @returns True if valid
|
|
191
|
+
*
|
|
192
|
+
* NOTE: Node will verify proofs server-side, this is mainly for debugging
|
|
193
|
+
*/
|
|
194
|
+
static async verifyAttestation(attestation) {
|
|
195
|
+
return await ProofGenerator.verifyProof(attestation.proof, attestation.public_signals);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Export identity for backup
|
|
199
|
+
*
|
|
200
|
+
* @returns Object containing provider ID and secret
|
|
201
|
+
*
|
|
202
|
+
* WARNING: Store this securely! Anyone with this data can create
|
|
203
|
+
* attestations as you.
|
|
204
|
+
*/
|
|
205
|
+
export() {
|
|
206
|
+
return {
|
|
207
|
+
providerId: this.providerId,
|
|
208
|
+
secret: this.secret,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Import identity from backup
|
|
213
|
+
*
|
|
214
|
+
* @param data - Exported identity data
|
|
215
|
+
* @returns New ZKIdentity instance
|
|
216
|
+
*/
|
|
217
|
+
static import(data) {
|
|
218
|
+
return new ZKIdentity(data.providerId, data.secret);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Generate a fresh identity with random secret
|
|
222
|
+
*
|
|
223
|
+
* @param providerId - Provider identifier
|
|
224
|
+
* @returns New ZKIdentity instance
|
|
225
|
+
*/
|
|
226
|
+
static generate(providerId) {
|
|
227
|
+
return new ZKIdentity(providerId);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
exports.ZKIdentity = ZKIdentity;
|
|
231
|
+
//# sourceMappingURL=ZKIdentity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ZKIdentity.js","sourceR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* Provides privacy-preserving identity attestations using ZK-SNARKs.
|
|
7
|
+
*/
|
|
8
|
+
export * as CommitmentService from './CommitmentService';
|
|
9
|
+
export * as ProofGenerator from './ProofGenerator';
|
|
10
|
+
export { ZKIdentity } from './ZKIdentity';
|
|
11
|
+
export type { ZKProof, ProofGenerationResult, MerkleProof, } from './ProofGenerator';
|
|
12
|
+
export type { IdentityCommitmentPayload, IdentityAttestationPayload, } from './ZKIdentity';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ZK Identity Module
|
|
4
|
+
*
|
|
5
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
6
|
+
*
|
|
7
|
+
* Provides privacy-preserving identity attestations using ZK-SNARKs.
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
12
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
13
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
14
|
+
}
|
|
15
|
+
Object.defineProperty(o, k2, desc);
|
|
16
|
+
}) : (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
o[k2] = m[k];
|
|
19
|
+
}));
|
|
20
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
21
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
22
|
+
}) : function(o, v) {
|
|
23
|
+
o["default"] = v;
|
|
24
|
+
});
|
|
25
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
26
|
+
var ownKeys = function(o) {
|
|
27
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
28
|
+
var ar = [];
|
|
29
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
30
|
+
return ar;
|
|
31
|
+
};
|
|
32
|
+
return ownKeys(o);
|
|
33
|
+
};
|
|
34
|
+
return function (mod) {
|
|
35
|
+
if (mod && mod.__esModule) return mod;
|
|
36
|
+
var result = {};
|
|
37
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
38
|
+
__setModuleDefault(result, mod);
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
})();
|
|
42
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
+
exports.ZKIdentity = exports.ProofGenerator = exports.CommitmentService = void 0;
|
|
44
|
+
exports.CommitmentService = __importStar(require("./CommitmentService"));
|
|
45
|
+
exports.ProofGenerator = __importStar(require("./ProofGenerator"));
|
|
46
|
+
var ZKIdentity_1 = require("./ZKIdentity");
|
|
47
|
+
Object.defineProperty(exports, "ZKIdentity", { enumerable: true, get: function () { return ZKIdentity_1.ZKIdentity; } });
|
|
48
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/encryption/zK/identity/index.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,yEAAwD;AACxD,mEAAkD;AAClD,2CAAyC;AAAhC,wGAAA,UAAU,OAAA"}
|
|
@@ -33,6 +33,8 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.interactive = void 0;
|
|
36
|
+
exports.identity = exports.interactive = void 0;
|
|
37
37
|
exports.interactive = __importStar(require("./interactive")); // interactive.Prover and interactive.Verifier
|
|
38
|
+
// REVIEW: Phase 9 - SDK Integration - ZK Identity attestations
|
|
39
|
+
exports.identity = __importStar(require("./identity"));
|
|
38
40
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/encryption/zK/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6DAA4C,CAAC,8CAA8C"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/encryption/zK/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6DAA4C,CAAC,8CAA8C;AAC3F,+DAA+D;AAC/D,uDAAsC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kynesyslabs/demosdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "Demosdk is a JavaScript/TypeScript SDK that provides a unified interface for interacting with Demos network",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -109,8 +109,10 @@
|
|
|
109
109
|
"node-forge": "^1.3.1",
|
|
110
110
|
"node-seal": "^5.1.7",
|
|
111
111
|
"ntru": "^4.0.4",
|
|
112
|
+
"poseidon-lite": "^0.3.0",
|
|
112
113
|
"pqcrypto": "^1.0.1",
|
|
113
114
|
"protobufjs": "^7.5.4",
|
|
115
|
+
"snarkjs": "^0.7.5",
|
|
114
116
|
"ripple-keypairs": "^2.0.0",
|
|
115
117
|
"rubic-sdk": "^5.57.4",
|
|
116
118
|
"simple-peer": "^9.11.1",
|