-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f10962
commit df2952b
Showing
8 changed files
with
185 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Generate Proof</title> | ||
<script type="module" src="proof.js"></script> | ||
</head> | ||
<body> | ||
<h1>Generate Proof</h1> | ||
<div> | ||
<label for="minAge">Minimum Age:</label> | ||
<input type="number" id="minAge" name="minAge" required /> | ||
<input type="number" id="minAge" min="0" /> | ||
</div> | ||
<br /> | ||
<button id="generateProofBtn">Generate Proof</button> | ||
<div id="proofResult"></div> | ||
<script type="module" src="proof.js"></script> | ||
|
||
<iframe | ||
id="sandboxFrame" | ||
sandbox="allow-scripts" | ||
src="sandbox.html" | ||
style="display: none" | ||
></iframe> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta | ||
http-equiv="Content-Security-Policy" | ||
content="script-src 'self' 'wasm-eval';" | ||
/> | ||
<script type="module" src="sandbox.js"></script> | ||
</head> | ||
<body></body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { Field, PublicKey, Signature, ZkProgram } from 'o1js'; | ||
|
||
console.log('sandbox.js loaded'); | ||
|
||
let compiledZkProgram; | ||
|
||
console.log('Defining ZkProgram'); | ||
const zkProgram = ZkProgram({ | ||
name: 'AgeCheck', | ||
publicInput: Field, | ||
publicOutput: PublicKey, | ||
methods: { | ||
verifyAge: { | ||
privateInputs: [Field, PublicKey, Signature], | ||
async method(minAge, age, issuerPubKey, signature) { | ||
console.log('verifyAge method called with:', { | ||
minAge, | ||
age, | ||
issuerPubKey, | ||
signature, | ||
}); | ||
const validSignature = signature.verify(issuerPubKey, [age]); | ||
console.log('Signature verification result:', validSignature); | ||
validSignature.assertTrue(); | ||
console.log('Asserting age >= minAge'); | ||
age.assertGreaterThanOrEqual(minAge); | ||
return issuerPubKey; | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
async function compileProgram() { | ||
console.log('Compiling ZkProgram'); | ||
try { | ||
compiledZkProgram = await zkProgram.compile(); | ||
console.log('ZkProgram compiled successfully'); | ||
window.parent.postMessage( | ||
{ | ||
type: 'compilationResult', | ||
success: true, | ||
}, | ||
'*' | ||
); | ||
} catch (error) { | ||
console.error('Error compiling ZK program:', error); | ||
window.parent.postMessage( | ||
{ | ||
type: 'compilationResult', | ||
success: false, | ||
}, | ||
'*' | ||
); | ||
} | ||
} | ||
|
||
window.addEventListener('message', async (event) => { | ||
console.log('Received message in sandbox:', event.data); | ||
|
||
if (event.data.type === 'compile') { | ||
console.log('Compile message received, starting compilation'); | ||
await compileProgram(); | ||
} else if (event.data.type === 'generateProof') { | ||
console.log('Generate proof message received'); | ||
const { age, minAgeField, signature, issuerPublicKey } = event.data.data; | ||
console.log('Proof generation data:', { | ||
age, | ||
minAgeField, | ||
signature, | ||
issuerPublicKey, | ||
}); | ||
|
||
try { | ||
if (!compiledZkProgram) { | ||
throw new Error('ZkProgram not compiled yet'); | ||
} | ||
|
||
console.log('Generating proof'); | ||
const proof = await zkProgram.verifyAge( | ||
minAgeField, | ||
age, | ||
issuerPublicKey, | ||
signature | ||
); | ||
|
||
console.log('Proof generated successfully'); | ||
window.parent.postMessage( | ||
{ | ||
type: 'proofResult', | ||
result: `Proof generated successfully. The stored age (${age.toString()}) is at least ${minAgeField.toString()}.`, | ||
}, | ||
'*' | ||
); | ||
|
||
console.log('Generated proof:', proof.toJSON()); | ||
} catch (error) { | ||
console.error('Error generating proof:', error); | ||
window.parent.postMessage( | ||
{ | ||
type: 'proofResult', | ||
result: | ||
'An error occurred while generating the proof. Please check the console for details.', | ||
}, | ||
'*' | ||
); | ||
} | ||
} | ||
}); | ||
|
||
console.log('sandbox.js event listener set up'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters