-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
254 lines (226 loc) · 8.9 KB
/
index.html
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>zk p2p demo </title>
<style>
div { padding: 0.2rem; }
#controller {
height: 2rem;
width: 10rem;
}
.container {
display: flex;
flex-direction: column;
}
@media (max-width: 600px) {
.container {
flex-direction: column;
}
#io-panel {
width: auto;
}
}
#console {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 200px;
overflow-y: scroll;
background-color: #f0f0f0;
border-top: 2px solid #000;
box-sizing: border-box;
padding: 10px;
font-family: monospace;
}
</style>
<script>
function openWindows(n) {
for (let i = 1; i <= n; i++) {
const currentUrl = window.location.href;
let windowRef = window.open(currentUrl, `_blank${i}`);
if (windowRef) {
windowRef.addEventListener('load', () => {
windowRef.document.title = `zk p2p demo -- #${i}`;
}, { once: false });
} else {
alert(`Window #${i} was blocked by the browser.`);
break; // Stop the loop if a window was blocked
}
}
}
</script>
</head>
<body>
<style>#forkongithub a{background:#000;color:#fff;text-decoration:none;font-family:arial,sans-serif;text-align:center;font-weight:bold;padding:5px 40px;font-size:1rem;line-height:2rem;position:relative;transition:0.5s;}#forkongithub a:hover{background:#c11;color:#fff;}#forkongithub a::before,#forkongithub a::after{content:"";width:100%;display:block;position:absolute;top:1px;left:0;height:1px;background:#fff;}#forkongithub a::after{bottom:1px;top:auto;}@media screen and (min-width:800px){#forkongithub{position:absolute;display:block;top:0;right:0;width:200px;overflow:hidden;height:200px;z-index:9999;}#forkongithub a{width:200px;position:absolute;top:60px;right:-60px;transform:rotate(45deg);-webkit-transform:rotate(45deg);-ms-transform:rotate(45deg);-moz-transform:rotate(45deg);-o-transform:rotate(45deg);box-shadow:4px 4px 10px rgba(0,0,0,0.8);}}</style>
<span id="forkongithub"><a href="https://github.com/RingsNetwork/rings-wasm-p2p">Fork me on GitHub</a></span>
<div class="container">
<div id="panel">
<button onclick="openWindows(1)">Create 1 node</button>
<button onclick="openWindows(2);">Create 2 nodes</button>
<button onclick="openWindows(3);">Create 3 nodes</button>
</div>
<h2 id="did"></h2>
<p id="loading" style="color:red;visibility: hidden;">
executing
</p>
<script>
function start() {
document.getElementById("loading").style.visibility="visible";
}
function done() {
document.getElementById("loading").style.visibility="hidden";
}
</script>
<script type="module">
import init, { debug, Provider,
BackendBehaviour, SNARKTaskBuilder, SNARKBehaviour,
SupportedPrimeField,
Input
} from './node_modules/@ringsnetwork/rings-node/dist/rings_node.js';
import { Wallet } from "./node_modules/ethers/dist/ethers.js"
await init();
debug(true)
const account = Wallet.createRandom();
document.getElementById("did").innerText = account.address;
// init signer
// Utility function to convert hex string to Uint8Array
function hexToBytes(hexString) {
// Remove the "0x" prefix if it exists
if (hexString.startsWith('0x')) {
hexString = hexString.slice(2);
}
const byteArray = [];
for (let i = 0; i < hexString.length; i+=2) {
byteArray.push(parseInt(hexString.substr(i, 2), 16));
}
return new Uint8Array(byteArray);
}
const signer = async function(proof) {
const signed = await account.signMessage(proof)
return new Uint8Array(hexToBytes(signed));
}
const behaviour = new BackendBehaviour()
const snark = new SNARKBehaviour()
window.snark = snark
behaviour.extend(snark.clone().as_dyn_obj())
window.client = await new Provider(
// ice_servers
// 'stun://stun.l.google.com:19302',
'stun://stun.qq.com:3478',
// stable_timeout
BigInt(5),
// account
account.address,
// account type
"eip191",
// signer
signer,
// callback
behaviour
)
window.load_r1cs = async function(r1cs_url, wasm_url) {
const snarkBuilder = await new SNARKTaskBuilder(r1cs_url, wasm_url);
const F = SupportedPrimeField.Vesta
const publicInputData = [["inp", new Array(2).fill(BigInt(0))]]
const input = Input.from_array(publicInputData, F)
console.log("init input DONE")
console.log(input)
console.log("gen circuit START")
const circuits = snarkBuilder.gen_circuits(
input, [], 6
)
console.log("gen circuit DONE")
const task = await SNARKBehaviour.gen_proof_task_ref(circuits)
window.task = task;
}
await window.client.listen()
</script>
<div id="panel">
<!-- <button onclick="openWindows(1)">Create 1 node</button> -->
<!-- <button onclick="openWindows(2);">Create 2 nodes</button> -->
<!-- <button onclick="openWindows(3);">Create 3 nodes</button> -->
</div>
<div id="handshake">
<button
onclick="start();window.client.request('createOffer', [document.getElementById('input').innerText]).then((res) => { console.log(res);document.getElementById('output').innerText=res.offer;done()})"
>Create Offer SDP</button>
<button
onclick="start();window.client.request('answerOffer', [getElementById('input').innerText]).then((res)=>{console.log(res);document.getElementById('output').innerText=res.answer;done()})"
>Accept Offer [SDP] and Create answer SDP</button>
<button
onclick="start();window.client.request('acceptAnswer', [getElementById('input').innerText]).then((res)=>{console.log(res);done()})"
>Accept Answer [SDP]</button>
<button
onclick="start();window.client.request('connectWithDid', [document.getElementById('input').innerText]).then((ret)=>{console.log(ret);done()})"
>connect with [DID]</button>
<button
onclick="start();window.client.request('nodeInfo', []).then((ret)=>{console.log(ret);done()})"
>node info</button></br>
<button
onclick="start();let input = document.getElementById('input').innerText.split(';');window.load_r1cs(input[0], input[1]).then(function(circuits) {window.circuits = circuits;done()});"
>generate ZKP Task [R1CSURL;WASMURL]</button>
<button
onclick="start();let input = document.getElementById('input').innerText.split(';');
let splitedTask = window.task.split(input.length);
for (let i = 0; i < input.length; i++) {
window.snark.send_proof_task_to(window.client.as_ref(), splitedTask[i], input[i]).then(function(ret) {
console.log(ret);
done()
})}">split and send ZKP Task to [DID;DID;DID...]</button>
</div>
<div id="io-panel">
<h3>Input / Output Panel</h3>
<h4>input</h4>
<p id="input" style="font-size:0.80rem;height:2.5rem; background-color:#eee; overflow:scroll;" contenteditable=true></p><button onclick="document.getElementById('input').innerText=''">clear</button></br>
<h4>output</h4>
<p id="output" style="font-size:0.80rem;height:2.5rem; background-color:#eee; overflow:scroll; word-wrap: break-word;"></p>
</div>
</div>
<footer>
<span>console</span>
<div id="console"></div>
<script>
(function() {
var consoleContainer = document.getElementById('console');
function formatTime(date) {
return date.getHours().toString().padStart(2, '0') + ':' +
date.getMinutes().toString().padStart(2, '0') + ':' +
date.getSeconds().toString().padStart(2, '0');
}
function updateConsole(method, args) {
var currentTime = formatTime(new Date());
var message = currentTime + ' - ' + Array.from(args).map(function(arg) {
return typeof arg === 'object' ? JSON.stringify(arg, null, 2) : arg;
}).join(' ');
var messageElement = document.createElement('div');
messageElement.textContent = message;
consoleContainer.appendChild(messageElement);
consoleContainer.scrollTop = consoleContainer.scrollHeight; // 滚动到最新信息
}
var originalLog = console.log;
console.log = function() {
originalLog.apply(console, arguments);
updateConsole('log', arguments);
};
var originalInfo = console.info;
console.info = function() {
originalInfo.apply(console, arguments);
updateConsole('info', arguments);
};
var originalDebug = console.debug;
console.debug = function() {
originalDebug.apply(console, arguments);
updateConsole('debug', arguments);
};
var originalError = console.error;
console.error = function() {
originalError.apply(console, arguments);
updateConsole('error', arguments);
};
})();
</script>
</footer>
</body>