-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
68 lines (53 loc) · 2.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {
withZKCWeb3MetaMaskProvider,
ZKCWasmService,
ZKCProveService,
} from 'zkc-sdk';
// Get the URL of the wasm file for initializing the WebAssembly instance.
const zkHelloWorldURL = new URL(
'./wasmsrc/c/zk-hello-world.wasm',
import.meta.url,
);
// Application image id that has been created and can be used for task proofing
const ZK_HELLO_WORLD_MD5 = process.env.MD5 || '4470FD5212FCDCAA5B50F3DC538FCDAE';
// get DOM Node
const luckyNumberNode = document.querySelector('#lucky-number'),
luckyButtonNode = document.querySelector('#get-lucky'),
submitProofNode = document.querySelector('#submit-proof'),
proofMessageNode = document.querySelector('#proof-message');
luckyButtonNode.addEventListener('click', getRandomNumber);
submitProofNode.addEventListener('click', submitProof);
async function getRandomNumber() {
const randomNumber = Math.ceil(Math.random() * 10);
luckyNumberNode.textContent = randomNumber;
// load wasm module instance
const { exports } = await ZKCWasmService.loadWasm(zkHelloWorldURL);
// Call the checkLucky function export from zk-hello-world.wasm
const isLucky = exports.checkLucky(randomNumber);
return alert(
isLucky
? 'Congratulations! Please submit your proof on-chain!'
: 'Not Lucky, pleasy try again',
);
}
async function submitProof() {
withZKCWeb3MetaMaskProvider(async provider => {
const luckyNumberValue = +luckyNumberNode.textContent || 0;
if (luckyNumberValue === 0) return alert('Get your lucky number first!');
// Information to be signed
const taskInfo = {
md5: ZK_HELLO_WORLD_MD5,
public_inputs: [],
private_inputs: [`0x${luckyNumberValue.toString(16)}:i64`],
};
try {
var response = await new ZKCProveService().settlement(provider, taskInfo);
} catch (error) {
console.dir(error);
return alert(error.message ? error.message : 'Prove failed!');
}
alert('Add proving task success!');
// Set the result onto the body
proofMessageNode.textContent = `Hello World! ZK Proof: https://scan.zkcross.org/request/${response.body?.application?.uuid}`;
});
}