@kynesyslabs/demosdk 2.4.26 → 2.5.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/build/encryption/zK/identity/CommitmentService.d.ts +48 -0
- package/build/encryption/zK/identity/CommitmentService.js +120 -0
- package/build/encryption/zK/identity/CommitmentService.js.map +1 -0
- package/build/encryption/zK/identity/ProofGenerator.d.ts +57 -0
- package/build/encryption/zK/identity/ProofGenerator.js +151 -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 +1 -1
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CommitmentService - Generate identity commitments and nullifiers
|
|
3
|
+
*
|
|
4
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
5
|
+
*
|
|
6
|
+
* Provides methods for generating cryptographic commitments and nullifiers
|
|
7
|
+
* for the ZK identity system using Poseidon hash.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Generate a Poseidon hash commitment from provider ID and secret
|
|
11
|
+
*
|
|
12
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
13
|
+
* @param secret - User's secret value (generated client-side)
|
|
14
|
+
* @returns Commitment hash as string
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const commitment = CommitmentService.generateCommitment("github:12345", "secret123")
|
|
19
|
+
* // commitment: "1234567890..."
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function generateCommitment(providerId: string, secret: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Generate a nullifier from provider ID and context
|
|
25
|
+
*
|
|
26
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
27
|
+
* @param context - Context string (e.g., "dao_vote_123")
|
|
28
|
+
* @returns Nullifier hash as string
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const nullifier = CommitmentService.generateNullifier("github:12345", "dao_vote_123")
|
|
33
|
+
* // nullifier: "9876543210..."
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function generateNullifier(providerId: string, context: string): string;
|
|
37
|
+
/**
|
|
38
|
+
* Generate a cryptographically secure random secret
|
|
39
|
+
*
|
|
40
|
+
* @returns Random secret as hex string
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const secret = CommitmentService.generateSecret()
|
|
45
|
+
* // secret: "a1b2c3d4e5f6..."
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function generateSecret(): string;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* CommitmentService - Generate identity commitments and nullifiers
|
|
4
|
+
*
|
|
5
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
6
|
+
*
|
|
7
|
+
* Provides methods for generating cryptographic commitments and nullifiers
|
|
8
|
+
* for the ZK identity system using Poseidon hash.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.generateCommitment = generateCommitment;
|
|
12
|
+
exports.generateNullifier = generateNullifier;
|
|
13
|
+
exports.generateSecret = generateSecret;
|
|
14
|
+
/**
|
|
15
|
+
* Generate a Poseidon hash commitment from provider ID and secret
|
|
16
|
+
*
|
|
17
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
18
|
+
* @param secret - User's secret value (generated client-side)
|
|
19
|
+
* @returns Commitment hash as string
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const commitment = CommitmentService.generateCommitment("github:12345", "secret123")
|
|
24
|
+
* // commitment: "1234567890..."
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
function generateCommitment(providerId, secret) {
|
|
28
|
+
// Convert strings to BigInt for Poseidon hashing
|
|
29
|
+
const providerHash = stringToBigInt(providerId);
|
|
30
|
+
const secretHash = stringToBigInt(secret);
|
|
31
|
+
// Use Poseidon hash (ZK-friendly)
|
|
32
|
+
// NOTE: This will be implemented using poseidon-lite or browser-compatible alternative
|
|
33
|
+
// For now, using a placeholder that will be replaced with actual Poseidon implementation
|
|
34
|
+
const commitment = poseidonHash([providerHash, secretHash]);
|
|
35
|
+
return commitment.toString();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Generate a nullifier from provider ID and context
|
|
39
|
+
*
|
|
40
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
41
|
+
* @param context - Context string (e.g., "dao_vote_123")
|
|
42
|
+
* @returns Nullifier hash as string
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const nullifier = CommitmentService.generateNullifier("github:12345", "dao_vote_123")
|
|
47
|
+
* // nullifier: "9876543210..."
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
function generateNullifier(providerId, context) {
|
|
51
|
+
const providerHash = stringToBigInt(providerId);
|
|
52
|
+
const contextHash = stringToBigInt(context);
|
|
53
|
+
const nullifier = poseidonHash([providerHash, contextHash]);
|
|
54
|
+
return nullifier.toString();
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Generate a cryptographically secure random secret
|
|
58
|
+
*
|
|
59
|
+
* @returns Random secret as hex string
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const secret = CommitmentService.generateSecret()
|
|
64
|
+
* // secret: "a1b2c3d4e5f6..."
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
function generateSecret() {
|
|
68
|
+
// Use Web Crypto API for secure random generation
|
|
69
|
+
if (typeof window !== 'undefined' && window.crypto) {
|
|
70
|
+
const array = new Uint8Array(32);
|
|
71
|
+
window.crypto.getRandomValues(array);
|
|
72
|
+
return uint8ArrayToHex(array);
|
|
73
|
+
}
|
|
74
|
+
// Node.js environment
|
|
75
|
+
if (typeof require !== 'undefined') {
|
|
76
|
+
const crypto = require('crypto');
|
|
77
|
+
return crypto.randomBytes(32).toString('hex');
|
|
78
|
+
}
|
|
79
|
+
throw new Error('No secure random number generator available');
|
|
80
|
+
}
|
|
81
|
+
// ============================================================================
|
|
82
|
+
// Helper Functions
|
|
83
|
+
// ============================================================================
|
|
84
|
+
/**
|
|
85
|
+
* Convert string to BigInt using simple hashing
|
|
86
|
+
* NOTE: For production, should use more robust hashing
|
|
87
|
+
*/
|
|
88
|
+
function stringToBigInt(str) {
|
|
89
|
+
// Simple conversion: encode to UTF-8 bytes then to hex
|
|
90
|
+
const encoder = new TextEncoder();
|
|
91
|
+
const bytes = encoder.encode(str);
|
|
92
|
+
const hex = uint8ArrayToHex(bytes);
|
|
93
|
+
return BigInt('0x' + hex);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Convert Uint8Array to hex string
|
|
97
|
+
*/
|
|
98
|
+
function uint8ArrayToHex(array) {
|
|
99
|
+
return Array.from(array)
|
|
100
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
101
|
+
.join('');
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Poseidon hash implementation
|
|
105
|
+
*
|
|
106
|
+
* TODO: Replace with actual poseidon-lite or circomlibjs implementation
|
|
107
|
+
* This is a placeholder for testing purposes
|
|
108
|
+
*/
|
|
109
|
+
function poseidonHash(inputs) {
|
|
110
|
+
// TEMPORARY: Simple XOR-based hash for testing
|
|
111
|
+
// MUST be replaced with real Poseidon hash from poseidon-lite
|
|
112
|
+
console.warn('WARNING: Using placeholder hash - replace with real Poseidon');
|
|
113
|
+
let result = BigInt(0);
|
|
114
|
+
for (const input of inputs) {
|
|
115
|
+
result ^= input;
|
|
116
|
+
}
|
|
117
|
+
// Ensure positive result
|
|
118
|
+
return result > 0 ? result : -result;
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=CommitmentService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CommitmentService.js","sourceRoot":"","sources":["../../../../../src/encryption/zK/identity/CommitmentService.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AAeH,gDAWC;AAeD,8CAOC;AAaD,wCAeC;AA1ED;;;;;;;;;;;;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,SAAS,GAAG,YAAY,CAAC,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC,CAAA;IAE3D,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAA;AAC/B,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,cAAc;IAC1B,kDAAkD;IAClD,IAAI,OAAO,MAAM,KA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E5E,IAAI,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,CAAA;IACtB,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAA;IACnB,CAAC;IAED,yBAAyB;IACzB,OAAO,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;AACxC,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ProofGenerator - Client-side ZK-SNARK proof generation
|
|
3
|
+
*
|
|
4
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
5
|
+
*
|
|
6
|
+
* Generates Groth16 ZK-SNARK proofs for identity attestations using snarkjs.
|
|
7
|
+
* Requires the circuit's proving key (WASM) and witness calculator.
|
|
8
|
+
*/
|
|
9
|
+
export interface ZKProof {
|
|
10
|
+
pi_a: string[];
|
|
11
|
+
pi_b: string[][];
|
|
12
|
+
pi_c: string[];
|
|
13
|
+
protocol: string;
|
|
14
|
+
}
|
|
15
|
+
export interface ProofGenerationResult {
|
|
16
|
+
proof: ZKProof;
|
|
17
|
+
publicSignals: string[];
|
|
18
|
+
}
|
|
19
|
+
export interface MerkleProof {
|
|
20
|
+
siblings: string[][];
|
|
21
|
+
pathIndices: number[];
|
|
22
|
+
root: string;
|
|
23
|
+
leaf: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generate a ZK-SNARK proof for identity attestation
|
|
27
|
+
*
|
|
28
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
29
|
+
* @param secret - User's secret value
|
|
30
|
+
* @param context - Context string for this attestation
|
|
31
|
+
* @param merkleProof - Merkle proof from node RPC
|
|
32
|
+
* @param merkleRoot - Current Merkle root from node RPC
|
|
33
|
+
* @returns Proof and public signals
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const result = await ProofGenerator.generateIdentityProof(
|
|
38
|
+
* "github:12345",
|
|
39
|
+
* "secret123",
|
|
40
|
+
* "dao_vote_123",
|
|
41
|
+
* merkleProof,
|
|
42
|
+
* merkleRoot
|
|
43
|
+
* )
|
|
44
|
+
* // result: { proof: {...}, publicSignals: [nullifier, merkleRoot, context] }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function generateIdentityProof(providerId: string, secret: string, context: string, merkleProof: MerkleProof, merkleRoot: string): Promise<ProofGenerationResult>;
|
|
48
|
+
/**
|
|
49
|
+
* Verify a proof locally (optional - mainly for testing)
|
|
50
|
+
*
|
|
51
|
+
* @param proof - The proof to verify
|
|
52
|
+
* @param publicSignals - Public signals for the proof
|
|
53
|
+
* @returns True if proof is valid
|
|
54
|
+
*
|
|
55
|
+
* NOTE: Node RPC will do the actual verification, this is mainly for debugging
|
|
56
|
+
*/
|
|
57
|
+
export declare function verifyProof(proof: ZKProof, publicSignals: string[]): Promise<boolean>;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ProofGenerator - Client-side ZK-SNARK proof generation
|
|
4
|
+
*
|
|
5
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
6
|
+
*
|
|
7
|
+
* Generates Groth16 ZK-SNARK proofs for identity attestations using snarkjs.
|
|
8
|
+
* Requires the circuit's proving key (WASM) and witness calculator.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.generateIdentityProof = generateIdentityProof;
|
|
12
|
+
exports.verifyProof = verifyProof;
|
|
13
|
+
/**
|
|
14
|
+
* Generate a ZK-SNARK proof for identity attestation
|
|
15
|
+
*
|
|
16
|
+
* @param providerId - Provider identifier (e.g., "github:12345")
|
|
17
|
+
* @param secret - User's secret value
|
|
18
|
+
* @param context - Context string for this attestation
|
|
19
|
+
* @param merkleProof - Merkle proof from node RPC
|
|
20
|
+
* @param merkleRoot - Current Merkle root from node RPC
|
|
21
|
+
* @returns Proof and public signals
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const result = await ProofGenerator.generateIdentityProof(
|
|
26
|
+
* "github:12345",
|
|
27
|
+
* "secret123",
|
|
28
|
+
* "dao_vote_123",
|
|
29
|
+
* merkleProof,
|
|
30
|
+
* merkleRoot
|
|
31
|
+
* )
|
|
32
|
+
* // result: { proof: {...}, publicSignals: [nullifier, merkleRoot, context] }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
async function generateIdentityProof(providerId, secret, context, merkleProof, merkleRoot) {
|
|
36
|
+
// Convert inputs to BigInt/field elements
|
|
37
|
+
const providerIdBigInt = stringToBigInt(providerId);
|
|
38
|
+
const secretBigInt = stringToBigInt(secret);
|
|
39
|
+
const contextBigInt = stringToBigInt(context);
|
|
40
|
+
// Prepare circuit inputs
|
|
41
|
+
const circuitInputs = {
|
|
42
|
+
// Private inputs
|
|
43
|
+
provider_id: providerIdBigInt.toString(),
|
|
44
|
+
secret: secretBigInt.toString(),
|
|
45
|
+
pathElements: merkleProof.siblings.map(s => s.map(v => v.toString())),
|
|
46
|
+
pathIndices: merkleProof.pathIndices,
|
|
47
|
+
// Public inputs
|
|
48
|
+
context: contextBigInt.toString(),
|
|
49
|
+
merkle_root: merkleRoot,
|
|
50
|
+
};
|
|
51
|
+
// TODO: Load proving key (WASM) from CDN or local storage
|
|
52
|
+
// const wasmPath = '/zk/identity_with_merkle.wasm'
|
|
53
|
+
// const zkeyPath = '/zk/proving_key_merkle.zkey'
|
|
54
|
+
// TODO: Generate witness using snarkjs
|
|
55
|
+
// const { proof, publicSignals } = await snarkjs.groth16.fullProve(
|
|
56
|
+
// circuitInputs,
|
|
57
|
+
// wasmPath,
|
|
58
|
+
// zkeyPath
|
|
59
|
+
// )
|
|
60
|
+
// TEMPORARY: Return mock proof for testing
|
|
61
|
+
console.warn('WARNING: ProofGenerator not yet implemented - using mock proof');
|
|
62
|
+
const mockProof = {
|
|
63
|
+
pi_a: ['1', '2', '1'],
|
|
64
|
+
pi_b: [
|
|
65
|
+
['1', '2'],
|
|
66
|
+
['3', '4'],
|
|
67
|
+
['1', '0'],
|
|
68
|
+
],
|
|
69
|
+
pi_c: ['1', '2', '1'],
|
|
70
|
+
protocol: 'groth16',
|
|
71
|
+
};
|
|
72
|
+
const mockPublicSignals = [
|
|
73
|
+
computeNullifier(providerId, context),
|
|
74
|
+
merkleRoot,
|
|
75
|
+
contextBigInt.toString(),
|
|
76
|
+
];
|
|
77
|
+
return {
|
|
78
|
+
proof: mockProof,
|
|
79
|
+
publicSignals: mockPublicSignals,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Verify a proof locally (optional - mainly for testing)
|
|
84
|
+
*
|
|
85
|
+
* @param proof - The proof to verify
|
|
86
|
+
* @param publicSignals - Public signals for the proof
|
|
87
|
+
* @returns True if proof is valid
|
|
88
|
+
*
|
|
89
|
+
* NOTE: Node RPC will do the actual verification, this is mainly for debugging
|
|
90
|
+
*/
|
|
91
|
+
async function verifyProof(proof, publicSignals) {
|
|
92
|
+
// TODO: Load verification key
|
|
93
|
+
// const vkey = await loadVerificationKey()
|
|
94
|
+
// TODO: Verify using snarkjs
|
|
95
|
+
// return await snarkjs.groth16.verify(vkey, publicSignals, proof)
|
|
96
|
+
console.warn('WARNING: Local proof verification not yet implemented');
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
// ============================================================================
|
|
100
|
+
// Helper Functions
|
|
101
|
+
// ============================================================================
|
|
102
|
+
/**
|
|
103
|
+
* Compute nullifier from provider ID and context
|
|
104
|
+
* (Same logic as CommitmentService.generateNullifier)
|
|
105
|
+
*/
|
|
106
|
+
function computeNullifier(providerId, context) {
|
|
107
|
+
const providerHash = stringToBigInt(providerId);
|
|
108
|
+
const contextHash = stringToBigInt(context);
|
|
109
|
+
// TODO: Use real Poseidon hash
|
|
110
|
+
const nullifier = providerHash ^ contextHash;
|
|
111
|
+
return (nullifier > BigInt(0) ? nullifier : -nullifier).toString();
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Convert string to BigInt using simple hashing
|
|
115
|
+
*/
|
|
116
|
+
function stringToBigInt(str) {
|
|
117
|
+
const encoder = new TextEncoder();
|
|
118
|
+
const bytes = encoder.encode(str);
|
|
119
|
+
const hex = Array.from(bytes)
|
|
120
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
121
|
+
.join('');
|
|
122
|
+
return BigInt('0x' + hex);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Load circuit WASM file
|
|
126
|
+
*
|
|
127
|
+
* TODO: Implement loading from CDN or local storage
|
|
128
|
+
*/
|
|
129
|
+
async function loadCircuitWasm(url) {
|
|
130
|
+
const response = await fetch(url);
|
|
131
|
+
return await response.arrayBuffer();
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Load proving key
|
|
135
|
+
*
|
|
136
|
+
* TODO: Implement loading from CDN or local storage
|
|
137
|
+
*/
|
|
138
|
+
async function loadProvingKey(url) {
|
|
139
|
+
const response = await fetch(url);
|
|
140
|
+
return await response.arrayBuffer();
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Load verification key for local verification
|
|
144
|
+
*
|
|
145
|
+
* TODO: Implement loading verification key
|
|
146
|
+
*/
|
|
147
|
+
async function loadVerificationKey() {
|
|
148
|
+
// Load from CDN or local storage
|
|
149
|
+
throw new Error('Not implemented');
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=ProofGenerator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProofGenerator.js","sourceRoot":"","sources":["../../../../../src/encryption/zK/identity/ProofGenerator.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AA2CH,sDA2DC;AAWD,kCAYC;AAxGD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACI,KAAK,UAAU,qBAAqB,CACvC,UAAkB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AC7B,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,eAAe,CAAC,GAAW;IACtC,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAA;IACjC,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAA;AACvC,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,cAAc,CAAC,GAAW;IACrC,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAA;IACjC,OAAO,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAA;AACvC,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,mBAAmB;IAC9B,iCAAiC;IACjC,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAA;AACtC,CAAC"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
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","sourceRoot":"","sources":["../../../../../src/encryption/zK/identity/ZKIdentity.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,uEAAwD;AACxD,iEAAkD;AAgBlD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,UAAU;IAInB;;;;;;;;OAQG;IACH,YAAY,UAAkB,EAAE,MAAe;QAC3C,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,iBAAiB,CAAC,cAAc,EAAE,CAAA;IAC9D,CAAC;IAED;;;;;;OAMG;IACH,aAAa;QACT,OAAO,iBAAiB,CAAC,kBAAkB,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;IAC7E,CAAC;IAED;;;;OAIG;IACH,WAAW;QACP,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IACxC,CAAC;IAED;;;;;;OAMG;IACH,SAAS;QACL,OAAO,IAAI,CAAC,MAAM,CAAA;IACtB,CAAC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,EAAE,IAAI,CAAC,UAAU;YAC3B,MAAM,EAAE,IAAI,CAAC,MAAM;SACtB,CAAA;IACL,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,IAA4C;QACtD,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;IACvD,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,CAAC,UAAkB;QAC9B,OAAO,IAAI,UAAU,CAAC,UAAU,CAAC,CAAA;IACrC,CAAC;CACJ;AAhND,gCAgNC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ZK Identity Module
|
|
3
|
+
*
|
|
4
|
+
* REVIEW: Phase 9 - SDK Integration
|
|
5
|
+
*
|
|
6
|
+
* 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