-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
449 lines (378 loc) · 15.4 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DOME Key Generation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Blinker:wght@400;700&display=swap">
<script src="https://unpkg.com/[email protected]/lib/bn.js" type="text/javascript"></script>
<script src="https://unpkg.com/@lionello/[email protected]/src/secp256k1.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/keccak256@latest/keccak256.js"></script>
<style>
body {
font-family: "Blinker", sans-serif;
margin: 0;
padding: 0;
box-sizing: border-box;
}
.disclaimer {
background-color: #e0e0e0;
color: #333;
padding: 20px;
text-align: justify;
font-size: 14px;
border: 1px solid #ccc;
width: 100%;
top: 0;
left: 0;
z-index: 1000;
box-sizing: border-box;
}
.disclaimer p {
margin: 0;
margin-left: 5rem;
margin-right: 5rem;
}
.login-pf-page {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #dde6f6;
padding: 1rem;
}
.login-pf-page-header {
text-align: center;
font-size: 2rem;
font-weight: bold;
margin-bottom: 1.5rem;
}
.card-pf {
background: white;
padding: 2rem;
border-radius: 5px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
max-width: 400px;
width: 100%;
transition: box-shadow 0.3s;
margin: 1rem;
min-width: 650px;
}
.card-pf:hover {
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.pf-c-button {
width: 100%;
padding: 0.75rem;
color: white;
background-color: #2d58a7 !important;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1rem;
transition: transform 0.1s;
margin-top: 1rem;
}
.pf-c-button:active {
transform: scale(0.95);
}
.pf-c-button:hover {
background-color: #cadcee;
}
.output {
margin-top: 1rem;
font-family: monospace;
width: 100%;
}
.key-box {
border: 1px solid #ccc;
padding: 10px;
margin: 10px 0;
border-radius: 5px;
background-color: #f9f9f9;
position: relative;
font-family: monospace;
word-wrap: break-word;
flex-grow: 1;
display: flex;
justify-content: space-between;
}
.key-box p {
margin: 0;
max-width: 550px;
white-space: normal;
word-wrap: break-word;
}
.copy-btn {
cursor: pointer;
background-color: #e0e0e0;
color: #333;
border: none;
padding: 5px 10px;
border-radius: 5px;
}
.copy-btn:hover {
background-color: #b0b0b0;
}
@media (max-width: 768px) {
.login-pf-page-header {
font-size: 1.5rem;
margin-bottom: 1rem;
}
.card-pf {
padding: 1.5rem;
max-width: 90%;
}
.pf-c-button {
padding: 0.5rem;
font-size: 0.9rem;
}
}
@media (max-width: 480px) {
.login-pf-page-header {
font-size: 1.25rem;
}
.card-pf {
padding: 1rem;
max-width: 100%;
}
.pf-c-button {
padding: 0.5rem;
font-size: 0.8rem;
}
.output {
font-size: 0.9rem;
}
}
</style>
</head>
<body>
<header>
<div class="disclaimer">
<p>In our service, the security and privacy of your data are our top priority.
That's why we have designed our system in such a way that all keys are
generated entirely on the client side, meaning on your own device.
This means they are neither sent nor stored on any server, ensuring that no one but you
have access to them.
Thanks to this approach, your keys remain under your
exclusive control at all times, protecting your information with the highest
level of privacy.</p>
</div>
</header>
<div class="login-pf-page">
<div class="login-pf-page-header">DOME Access Node Key Generator
</div>
<div class="card-pf">
<button class="pf-c-button"
onclick="generateP256(); generateSecp256k1();document.getElementById('didKeyOutput').style.display='block'">
Generate
Keys
</button>
<div id="didKeyOutput" style="display: none;">
<h2>Desmos keys</h2>
<div>
<div class="output">
<h3 style="margin-right: 10px;">Private Key</h3>
<div class="key-box" style="flex-grow: 1;">
<p id="desmosPrivateKeyValue"></p>
<button class="copy-btn" onclick="copyToClipboard('desmosPrivateKeyValue')">Copy</button>
</div>
</div>
<div class="output">
<h3 style="margin-right: 10px;">Public Key</h3>
<div class="key-box" style="flex-grow: 1;">
<p id="desmosPublicKeyValue"></p>
<button class="copy-btn" onclick="copyToClipboard('desmosPublicKeyValue')">Copy</button>
</div>
</div>
<div class="output">
<h3 style="margin-right: 10px;">DID</h3>
<div class="key-box" style="flex-grow: 1;">
<p id="desmosDidKeyValue"></p>
<button class="copy-btn" onclick="copyToClipboard('desmosDidKeyValue')">Copy</button>
</div>
</div>
<br>
<hr>
<h2>DLT keys</h2>
<div class="output">
<h3 style="margin-right: 10px;">Private Key</h3>
<div class="key-box" style="flex-grow: 1;">
<p id="dltPrivateKeyValue"></p>
<button class="copy-btn" onclick="copyToClipboard('dltPrivateKeyValue')">Copy</button>
</div>
</div>
<div class="output">
<h3 style="margin-right: 10px;">DLT Address</h3>
<div class="key-box" style="flex-grow: 1;">
<p id="dltAddressValue"></p>
<button class="copy-btn" onclick="copyToClipboard('dltAddressValue')">Copy</button>
</div>
</div>
<div class="output">
<h3 style="margin-right: 10px;">ISS</h3>
<div class="key-box" style="flex-grow: 1;">
<p id="issValue"></p>
<button class="copy-btn" onclick="copyToClipboard('issValue')">Copy</button>
</div>
</div>
</div>
</div>
</div>
<br>
<br>
<footer>
<p><a href="https://github.com/DOME-Marketplace/access-node/blob/main/README.md">Access Node Guide</a></p>
</footer>
</div>
<script>
function copyToClipboard(elementId) {
const text = document.getElementById(elementId).innerText;
navigator.clipboard.writeText(text);
}
async function generateP256() {
const keyPair = await generateP256KeyPair();
const privateKeyHex = await generateP256PrivateKeyHex(keyPair);
const publicKeyHex = await generateP256PublicKeyHex(keyPair)
const didKey = await generateDidKey(publicKeyHex)
const iss = await sha256(didKey)
document.getElementById("desmosPrivateKeyValue").innerText = privateKeyHex;
document.getElementById("desmosPublicKeyValue").innerText = publicKeyHex;
document.getElementById("desmosDidKeyValue").innerText = didKey;
document.getElementById("issValue").innerText = "0x" + iss;
}
async function generateP256KeyPair() {
return await window.crypto.subtle.generateKey(
{
name: "ECDSA",
namedCurve: "P-256"
},
true,
["sign", "verify"]
);
}
async function generateP256PrivateKeyHex(keyPair){
const privateKeyPkcs8 = await window.crypto.subtle.exportKey("pkcs8", keyPair.privateKey);
const privateKeyPkcs8Bytes = new Uint8Array(privateKeyPkcs8);
const privateKeyPkcs8Hex = bytesToHexString(privateKeyPkcs8Bytes);
console.log("Private Key P-256 (Secp256r1) PKCS#8 (HEX): ", privateKeyPkcs8Hex);
const privateKeyBytes = privateKeyPkcs8Bytes.slice(36, 36 + 32);
const privateKeyHexBytes = bytesToHexString(privateKeyBytes);
return privateKeyHexBytes;
}
async function generateP256PublicKeyHex(keyPair){
const publicKey = await window.crypto.subtle.exportKey("raw", keyPair.publicKey);
const publicKeyBytes = new Uint8Array(publicKey);
return bytesToHexString(publicKeyBytes)
}
async function generateDidKey(publicKeyHex){
const publicKeyHexWithout0xAndPrefix = publicKeyHex.slice(4)
const publicKeyX = publicKeyHexWithout0xAndPrefix.slice(0, 64)
const publicKeyY = publicKeyHexWithout0xAndPrefix.slice(64)
const isPublicKeyYEven = isHexNumberEven(publicKeyY)
const compressedPublicKeyX = (isPublicKeyYEven ? "02" : "03") + publicKeyX;
const multicodecHex = "8024" + compressedPublicKeyX
const multicodecBytes = multicodecHex.match(/.{1,2}/g).map(byte => parseInt(byte, 16));
var MAP = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const multicodecBase58 = base58encode(multicodecBytes, MAP);
return 'did:key:z' + multicodecBase58;
}
function bytesToHexString(bytesToTransform){
return `0x${Array.from(bytesToTransform).map(b => b.toString(16).padStart(2, '0')).join('')}`;
}
function isHexNumberEven(hexNumber){
const decimalNumber = BigInt("0x" + hexNumber)
const stringNumber = decimalNumber.toString()
const lastNumPosition = stringNumber.length - 1
const lastNumDecimal = parseInt(stringNumber[lastNumPosition])
const isEven = lastNumDecimal % 2 === 0;
return isEven;
}
// https://gist.github.com/diafygi/90a3e80ca1c2793220e5/
function base58encode(
B, //Uint8Array raw byte input
A //Base58 characters (i.e. "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
) {
var d = [], //the array for storing the stream of base58 digits
s = "", //the result string variable that will be returned
i, //the iterator variable for the byte input
j, //the iterator variable for the base58 digit array (d)
c, //the carry amount variable that is used to overflow from the current base58 digit to the next base58 digit
n; //a temporary placeholder variable for the current base58 digit
for(i in B) { //loop through each byte in the input stream
j = 0, //reset the base58 digit iterator
c = B[i]; //set the initial carry amount equal to the current byte amount
s += c || s.length ^ i ? "" : 1; //prepend the result string with a "1" (0 in base58) if the byte stream is zero and non-zero bytes haven't been seen yet (to ensure correct decode length)
while(j in d || c) { //start looping through the digits until there are no more digits and no carry amount
n = d[j]; //set the placeholder for the current base58 digit
n = n ? n * 256 + c : c; //shift the current base58 one byte and add the carry amount (or just add the carry amount if this is a new digit)
c = n / 58 | 0; //find the new carry amount (floored integer of current digit divided by 58)
d[j] = n % 58; //reset the current base58 digit to the remainder (the carry amount will pass on the overflow)
j++ //iterate to the next base58 digit
}
}
while(j--) //since the base58 digits are backwards, loop through them in reverse order
s += A[d[j]]; //lookup the character associated with each base58 digit
return s //return the final base58 string
}
// https://stackoverflow.com/a/48161723
async function sha256(message) {
// encode as UTF-8
const msgBuffer = new TextEncoder().encode(message);
// hash the message
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
// convert ArrayBuffer to Array
const hashArray = Array.from(new Uint8Array(hashBuffer));
// convert bytes to hex string
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
// To test it: https://www.rfctools.com/ethereum-address-test-tool/
function generateSecp256k1(){
const privateKey = generateSecp256k1PrivateKey()
const ethereumAddress = generateEthereumAddressFromPrivateKey(privateKey)
const privateKeyHexWithout0x = privateKey.toString(16)
const privateKeyHex = "0x" + privateKey.toString(16)
document.getElementById("dltPrivateKeyValue").innerText = privateKeyHex;
document.getElementById("dltAddressValue").innerText = ethereumAddress;
}
function generateSecp256k1PrivateKey(){
const privateKeyBuf = window.crypto.getRandomValues(new Uint8Array(32))
return Secp256k1.uint256(privateKeyBuf, 16)
}
function generateEthereumAddressFromPrivateKey(privateKey){
const publicKey = Secp256k1.generatePublicKeyFromPrivateKeyData(privateKey)
const publicKeyXHex = Secp256k1.uint256(publicKey.x, 16).toString(16)
const publicKeyYHex = Secp256k1.uint256(publicKey.y, 16).toString(16)
const publicKeyHexWithout0xAndPreffix = publicKeyXHex + publicKeyYHex
console.log("Public Key Secp256k1 (HEX): ", "0x04" + publicKeyHexWithout0xAndPreffix);
var address = generateEthereumAddressFromPublicKey(publicKeyHexWithout0xAndPreffix)
console.log("address: ", address);
var addressChecksum = calculateEthereumAddressChecksum(address)
return "0x" + addressChecksum
}
// https://stackoverflow.com/a/73606400
function generateEthereumAddressFromPublicKey(publicKeyHexWithout0xAndPreffix){
console.log("publicKeyHexWithout0xAndPreffix: ", publicKeyHexWithout0xAndPreffix);
var hashXY = keccak256("0x" + publicKeyHexWithout0xAndPreffix);
console.log("hashXY: ", hashXY);
console.log("hashXY: ", hashXY.toString('hex'));
return hashXY.subarray(-20).toString('hex').toLowerCase();
}
// https://stackoverflow.com/a/73606400
function calculateEthereumAddressChecksum(address){
var addressHash = keccak256(address).toString('hex');
var addressChecksum = '';
for (var i = 0; i < address.length; i++){
if (parseInt(addressHash[i], 16) > 7) {
addressChecksum += address[i].toUpperCase();
} else {
addressChecksum += address[i];
}
}
return addressChecksum
}
</script>
</body>
</html>