@kynesyslabs/demosdk 2.4.25 → 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/abstraction/index.d.ts +2 -2
- package/build/abstraction/index.js.map +1 -1
- package/build/d402/client/D402Client.d.ts +47 -0
- package/build/d402/client/D402Client.js +159 -0
- package/build/d402/client/D402Client.js.map +1 -0
- package/build/d402/client/index.d.ts +6 -0
- package/build/d402/client/index.js +10 -0
- package/build/d402/client/index.js.map +1 -0
- package/build/d402/client/types.d.ts +30 -0
- package/build/d402/client/types.js +7 -0
- package/build/d402/client/types.js.map +1 -0
- package/build/d402/index.d.ts +7 -0
- package/build/d402/index.js +27 -0
- package/build/d402/index.js.map +1 -0
- package/build/d402/server/D402Server.d.ts +42 -0
- package/build/d402/server/D402Server.js +156 -0
- package/build/d402/server/D402Server.js.map +1 -0
- package/build/d402/server/index.d.ts +8 -0
- package/build/d402/server/index.js +12 -0
- package/build/d402/server/index.js.map +1 -0
- package/build/d402/server/middleware.d.ts +54 -0
- package/build/d402/server/middleware.js +97 -0
- package/build/d402/server/middleware.js.map +1 -0
- package/build/d402/server/types.d.ts +49 -0
- package/build/d402/server/types.js +7 -0
- package/build/d402/server/types.js.map +1 -0
- 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 +5 -2
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* D402 Express Middleware
|
|
4
|
+
* Express-compatible middleware for HTTP 402 payment gating
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.d402Required = d402Required;
|
|
8
|
+
const D402Server_1 = require("./D402Server");
|
|
9
|
+
/**
|
|
10
|
+
* Create Express middleware for D402 payment gating
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* app.get('/premium-article',
|
|
15
|
+
* d402Required({
|
|
16
|
+
* amount: 5000000000000000000, // 5 DEM in smallest unit
|
|
17
|
+
* resourceId: 'article-123',
|
|
18
|
+
* rpcUrl: 'https://node2.demos.sh',
|
|
19
|
+
* recipient: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb'
|
|
20
|
+
* }),
|
|
21
|
+
* (req, res) => {
|
|
22
|
+
* res.json({ article: "Premium content" })
|
|
23
|
+
* }
|
|
24
|
+
* )
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
function d402Required(options) {
|
|
28
|
+
const server = new D402Server_1.D402Server({
|
|
29
|
+
rpcUrl: options.rpcUrl,
|
|
30
|
+
cacheTTL: options.cacheTTL
|
|
31
|
+
});
|
|
32
|
+
return async (req, res, next) => {
|
|
33
|
+
try {
|
|
34
|
+
// Get payment proof from header
|
|
35
|
+
const paymentProof = req.headers['x-payment-proof'];
|
|
36
|
+
// Determine recipient address
|
|
37
|
+
const recipient = options.recipient || req.d402Recipient;
|
|
38
|
+
if (!recipient) {
|
|
39
|
+
return res.status(500).json({
|
|
40
|
+
error: 'Server misconfiguration: recipient address not provided'
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
// If no payment proof, return 402 with requirements
|
|
44
|
+
if (!paymentProof) {
|
|
45
|
+
const requirement = {
|
|
46
|
+
amount: options.amount,
|
|
47
|
+
recipient: recipient,
|
|
48
|
+
resourceId: options.resourceId,
|
|
49
|
+
description: options.description
|
|
50
|
+
};
|
|
51
|
+
const response = server.require(requirement);
|
|
52
|
+
return res.status(response.status).json(response.body);
|
|
53
|
+
}
|
|
54
|
+
// Verify payment
|
|
55
|
+
const verification = await server.verify(paymentProof);
|
|
56
|
+
if (!verification.valid) {
|
|
57
|
+
return res.status(403).json({
|
|
58
|
+
error: 'Invalid payment proof'
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
// Validate payment matches requirements
|
|
62
|
+
const requirement = {
|
|
63
|
+
amount: options.amount,
|
|
64
|
+
recipient: recipient,
|
|
65
|
+
resourceId: options.resourceId,
|
|
66
|
+
description: options.description
|
|
67
|
+
};
|
|
68
|
+
const isValid = server.validatePayment(verification, requirement);
|
|
69
|
+
if (!isValid) {
|
|
70
|
+
return res.status(403).json({
|
|
71
|
+
error: 'Payment does not match requirements',
|
|
72
|
+
details: {
|
|
73
|
+
expected_recipient: recipient,
|
|
74
|
+
expected_amount: options.amount,
|
|
75
|
+
expected_resource: `resourceId:${options.resourceId}`
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
// Attach payment details to request for downstream handlers
|
|
80
|
+
req.d402Payment = {
|
|
81
|
+
from: verification.verified_from,
|
|
82
|
+
to: verification.verified_to,
|
|
83
|
+
amount: verification.verified_amount,
|
|
84
|
+
txHash: paymentProof
|
|
85
|
+
};
|
|
86
|
+
// Payment valid, proceed
|
|
87
|
+
next();
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
console.error('D402 middleware error:', error);
|
|
91
|
+
return res.status(500).json({
|
|
92
|
+
error: 'Payment verification failed'
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,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;QACN,CAAC;IACL,CAAC,CAAA;AACL,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* D402 Server Types
|
|
3
|
+
* Server-side types for HTTP 402 payment protocol implementation
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Payment requirements sent in 402 response
|
|
7
|
+
*/
|
|
8
|
+
export interface D402PaymentRequirement {
|
|
9
|
+
/** Payment amount in smallest unit */
|
|
10
|
+
amount: number;
|
|
11
|
+
/** Merchant/recipient address */
|
|
12
|
+
recipient: string;
|
|
13
|
+
/** Resource identifier (used in memo validation) */
|
|
14
|
+
resourceId: string;
|
|
15
|
+
/** Optional payment description */
|
|
16
|
+
description?: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Payment verification result from RPC
|
|
20
|
+
*/
|
|
21
|
+
export interface D402VerificationResult {
|
|
22
|
+
valid: boolean;
|
|
23
|
+
verified_from?: string;
|
|
24
|
+
verified_to?: string;
|
|
25
|
+
verified_amount?: number;
|
|
26
|
+
verified_memo?: string;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* D402Server configuration options
|
|
31
|
+
*/
|
|
32
|
+
export interface D402ServerConfig {
|
|
33
|
+
/** Demos Network RPC URL */
|
|
34
|
+
rpcUrl: string;
|
|
35
|
+
/** Payment cache TTL in seconds (default: 300 = 5 minutes) */
|
|
36
|
+
cacheTTL?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Cached payment data
|
|
40
|
+
*/
|
|
41
|
+
export interface CachedPayment {
|
|
42
|
+
txHash: string;
|
|
43
|
+
from: string;
|
|
44
|
+
to: string;
|
|
45
|
+
amount: number;
|
|
46
|
+
memo: string;
|
|
47
|
+
timestamp: number;
|
|
48
|
+
expiresAt: number;
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/d402/server/types.ts"],"names":[],"mappings":";AAAA;;;GAGG"}
|
|
@@ -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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;IACvD,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;IACjC,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACjC,MAAM,GAAG,GAAG,eAAe,CAAC,KAAK,CAAC,CAAA;IAClC,OAAO,MAAM,CAAC,IAAI,GAAG,GAAG,CAAC,CAAA;AAC7B,CAAC;AAED;;GAEG;AACH,SAAS,eAAe,CAAC,KAAiB;IACtC,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;SACnB,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;SACzC,IAAI,CAAC,EAAE,CAAC,CAAA;AACjB,CAAC;AAED;;;;;GAKG;AACH,SAAS,YAAY,CAAC,MAAgB;IAClC,+CAA+C;IAC/C,8DAA8D;IAC9D,OAAO,CAAC,IAAI,CAAC,8DAA8D,CAAC,CAAA;IAE5E,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,EAClB,MAAc,EACd,OAAe,EACf,WAAwB,EACxB,UAAkB;IAElB,0CAA0C;IAC1C,MAAM,gBAAgB,GAAG,cAAc,CAAC,UAAU,CAAC,CAAA;IACnD,MAAM,YAAY,GAAG,cAAc,CAAC,MAAM,CAAC,CAAA;IAC3C,MAAM,aAAa,GAAG,cAAc,CAAC,OAAO,CAAC,CAAA;IAE7C,yBAAyB;IACzB,MAAM,aAAa,GAAG;QAClB,iBAAiB;QACjB,WAAW,EAAE,gBAAgB,CAAC,QAAQ,EAAE;QACxC,MAAM,EAAE,YAAY,CAAC,QAAQ,EAAE;QAC/B,YAAY,EAAE,WAAW,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;QACrE,WAAW,EAAE,WAAW,CAAC,WAAW;QAEpC,gBAAgB;QAChB,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