-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathG1PUB2IPNS.html
399 lines (354 loc) · 15 KB
/
G1PUB2IPNS.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
<!DOCTYPE html>
<html>
<head>
<title>Key Conversion Tool</title>
<style>
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.key-section {
width: 45%;
margin-bottom: 20px;
padding: 15px;
border: 1px solid #ccc;
box-sizing: border-box;
}
.key-section h2 {
margin-top: 0;
}
.key-section label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.key-section input[type="text"] {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ddd;
box-sizing: border-box;
}
.key-section button {
padding: 8px 15px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
}
.key-section p {
margin-top: 5px;
margin-bottom: 10px;
word-wrap: break-word;
}
.key-section .ipns-key {
font-family: monospace;
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
</head>
<body>
<h1>Key Conversion Tool</h1>
<div class="container">
<div class="key-section">
<h2>G1 to IPNS</h2>
<label for="g1PublicKey">G1 Public Key (Base58):</label>
<input type="text" id="g1PublicKey" placeholder="Enter G1 Public Key (Base58)">
<button onclick="convertG1PublicKey()">Convert to IPNS</button>
<p>IPNS Public Key:</p>
<p id="ipnsG1PublicKey" class="ipns-key"></p>
</div>
<div class="key-section">
<h2>Nostr to IPNS</h2>
<label for="nostrPublicKey">Nostr Public Key (Hex or npub):</label>
<input type="text" id="nostrPublicKey" placeholder="Enter Nostr Public Key (Hex or npub)">
<button onclick="convertNostrPublicKey()">Convert to IPNS</button>
<p>IPNS Public Key:</p>
<p id="ipnsNostrPublicKey" class="ipns-key"></p>
</div>
<div class="key-section">
<h2>Bitcoin to IPNS</h2>
<label for="bitcoinPublicKey">Bitcoin Public Key (Base58):</label>
<input type="text" id="bitcoinPublicKey" placeholder="Enter Bitcoin Public Key (Base58)">
<button onclick="convertBitcoinPublicKey()">Convert to IPNS</button>
<p>IPNS Public Key:</p>
<p id="ipnsBitcoinPublicKey" class="ipns-key"></p>
</div>
<div class="key-section">
<h2>Monero to IPNS</h2>
<label for="moneroPublicKey">Monero Public Address (Base58):</label>
<input type="text" id="moneroPublicKey" placeholder="Enter Monero Public Address">
<button onclick="convertMoneroPublicKey()">Convert to IPNS</button>
<p>IPNS Public Key:</p>
<p id="ipnsMoneroPublicKey" class="ipns-key"></p>
</div>
<div class="key-section">
<h2>IPNS to Bitcoin</h2>
<label for="ipnsBitcoin">IPNS Public Key:</label>
<input type="text" id="ipnsBitcoin" placeholder="Enter IPNS Public Key">
<button onclick="convertIPNSToBitcoin()">Convert to Bitcoin</button>
<p>Bitcoin Public Key:</p>
<p id="bitcoinFromIpns" class="ipns-key"></p>
</div>
<div class="key-section">
<h2>IPNS to Monero</h2>
<label for="ipnsMonero">IPNS Public Key:</label>
<input type="text" id="ipnsMonero" placeholder="Enter IPNS Public Key">
<button onclick="convertIPNSToMonero()">Convert to Monero</button>
<p>Monero Public Address:</p>
<p id="moneroFromIpns" class="ipns-key"></p>
</div>
</div>
<!-- Include the tweetnacl library for Ed25519 operations -->
<script src="./nacl.min.js"></script>
<script>
// Custom Base58 encoding and decoding functions
function base58Encode(bytes) {
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
const base = BigInt(ALPHABET.length);
let encoded = '';
let value = BigInt('0');
for (let i = 0; i < bytes.length; i++) {
value = value * BigInt(256) + BigInt(bytes[i]);
}
while (value > BigInt(0)) {
const remainder = value % base;
value = value / base;
encoded = ALPHABET[Number(remainder)] + encoded;
}
return encoded;
}
function base58Decode(encoded) {
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
const base = BigInt(ALPHABET.length);
let value = BigInt('0');
for (let i = 0; i < encoded.length; i++) {
const char = encoded[i];
const charValue = BigInt(ALPHABET.indexOf(char));
value = value * base + charValue;
}
const valueBytes = [];
while (value > BigInt(0)) {
const byteValue = Number(value % BigInt(256));
value = value / BigInt(256);
valueBytes.unshift(byteValue);
}
return new Uint8Array(valueBytes);
}
function hexToBytes(hex) {
let bytes = [];
for (let c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return new Uint8Array(bytes);
}
function bech32Decode(bech32String) {
const ALPHABET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
const GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
function polymod(values) {
let chk = 1;
for (let v of values) {
let top = chk >> 25;
chk = ((chk & 0x1ffffff) << 5) ^ v;
for (let i = 0; i < 5; i++) {
if ((top >> i) & 1) {
chk ^= GENERATOR[i];
}
}
}
return chk;
}
function hrpExpand(hrp) {
let result = [];
for (let i = 0; i < hrp.length; i++) {
result.push(hrp.charCodeAt(i) >> 5);
}
result.push(0);
for (let i = 0; i < hrp.length; i++) {
result.push(hrp.charCodeAt(i) & 31);
}
return result;
}
function convertBits(data, fromBits, toBits, pad) {
let acc = 0;
let bits = 0;
let result = [];
let maxv = (1 << toBits) - 1;
for (let i = 0; i < data.length; i++) {
acc = (acc << fromBits) | data[i];
bits += fromBits;
while (bits >= toBits) {
bits -= toBits;
result.push((acc >> bits) & maxv);
}
}
if (pad) {
if (bits > 0) {
result.push((acc << (toBits - bits)) & maxv);
}
} else if (bits >= fromBits || ((acc << (toBits - bits)) & maxv)) {
return null;
}
return result;
}
try {
let lower = bech32String.toLowerCase();
let parts = lower.split('1');
if (parts.length !== 2) {
return null; // Invalid format
}
let hrp = parts[0];
let data = parts[1];
let values = [];
for (let i = 0; i < data.length; i++) {
let charIndex = ALPHABET.indexOf(data[i]);
if (charIndex === -1) {
return null; // Invalid char in data
}
values.push(charIndex);
}
let checksum = values.slice(-6);
values = values.slice(0, -6);
let fullValues = hrpExpand(hrp).concat(values);
if (polymod(fullValues.concat(checksum)) !== 1) {
return null; // Invalid checksum
}
// Convert 5-bit data to 8-bit
let convertedData = convertBits(values, 5, 8, false);
if (!convertedData) {
return null; // Conversion failed
}
return new Uint8Array(convertedData);
} catch (error) {
return null; // Indicate invalid Bech32 string
}
}
function convertG1PublicKey() {
const g1PublicKey = document.getElementById("g1PublicKey").value;
if (!g1PublicKey) {
document.getElementById("ipnsG1PublicKey").innerText = "Error: Please enter a G1 public key.";
return;
}
try {
const decodedShared = base58Decode(g1PublicKey);
const ipnsPublicKeyBytes = new Uint8Array(decodedShared.length + 6);
const prefixBytes = new Uint8Array([0, 36, 8, 1, 18, 32]);
ipnsPublicKeyBytes.set(prefixBytes, 0);
ipnsPublicKeyBytes.set(decodedShared, prefixBytes.length);
const ipnsPublicKey = '1' + base58Encode(ipnsPublicKeyBytes);
document.getElementById("ipnsG1PublicKey").innerText = ipnsPublicKey;
} catch (error) {
document.getElementById("ipnsG1PublicKey").innerText = "Error: Invalid G1 Public Key";
}
}
function convertNostrPublicKey() {
const nostrPublicKeyInput = document.getElementById("nostrPublicKey").value;
if (!nostrPublicKeyInput) {
document.getElementById("ipnsNostrPublicKey").innerText = "Error: Please enter a Nostr public key.";
return;
}
try {
let nostrPublicKeyBytes;
if (nostrPublicKeyInput.startsWith("npub")) {
const decodedNpub = bech32Decode(nostrPublicKeyInput);
if (!decodedNpub || decodedNpub.length !== 32) {
document.getElementById("ipnsNostrPublicKey").innerText = "Error: Invalid npub format";
return;
}
nostrPublicKeyBytes = decodedNpub;
} else {
nostrPublicKeyBytes = hexToBytes(nostrPublicKeyInput);
if (nostrPublicKeyBytes.length !== 32) {
document.getElementById("ipnsNostrPublicKey").innerText = "Error: Nostr public key must be 32 bytes (64 hex characters)";
return;
}
}
const ipnsPublicKeyBytes = new Uint8Array(nostrPublicKeyBytes.length + 6);
const prefixBytes = new Uint8Array([0, 36, 8, 1, 18, 32]);
ipnsPublicKeyBytes.set(prefixBytes, 0);
ipnsPublicKeyBytes.set(nostrPublicKeyBytes, prefixBytes.length);
const ipnsPublicKey = '1' + base58Encode(ipnsPublicKeyBytes);
document.getElementById("ipnsNostrPublicKey").innerText = ipnsPublicKey;
} catch (error) {
document.getElementById("ipnsNostrPublicKey").innerText = "Error: Invalid Nostr Public Key";
}
}
function convertBitcoinPublicKey() {
const bitcoinPublicKey = document.getElementById("bitcoinPublicKey").value;
if (!bitcoinPublicKey) {
document.getElementById("ipnsBitcoinPublicKey").innerText = "Error: Please enter a Bitcoin public key.";
return;
}
try {
const decodedShared = base58Decode(bitcoinPublicKey);
const ipnsPublicKeyBytes = new Uint8Array(decodedShared.length + 6);
const prefixBytes = new Uint8Array([0, 36, 8, 1, 18, 32]);
ipnsPublicKeyBytes.set(prefixBytes, 0);
ipnsPublicKeyBytes.set(decodedShared, prefixBytes.length);
const ipnsPublicKey = '1' + base58Encode(ipnsPublicKeyBytes);
document.getElementById("ipnsBitcoinPublicKey").innerText = ipnsPublicKey;
} catch (error) {
document.getElementById("ipnsBitcoinPublicKey").innerText = "Error: Invalid Bitcoin Public Key";
}
}
function convertMoneroPublicKey() {
const moneroPublicKey = document.getElementById("moneroPublicKey").value;
if (!moneroPublicKey) {
document.getElementById("ipnsMoneroPublicKey").innerText = "Error: Please enter a Monero public address.";
return;
}
try {
const decodedShared = base58Decode(moneroPublicKey);
const ipnsPublicKeyBytes = new Uint8Array(decodedShared.length + 6);
const prefixBytes = new Uint8Array([0, 36, 8, 1, 18, 32]);
ipnsPublicKeyBytes.set(prefixBytes, 0);
ipnsPublicKeyBytes.set(decodedShared, prefixBytes.length);
const ipnsPublicKey = '1' + base58Encode(ipnsPublicKeyBytes);
document.getElementById("ipnsMoneroPublicKey").innerText = ipnsPublicKey;
} catch (error) {
document.getElementById("ipnsMoneroPublicKey").innerText = "Error: Invalid Monero Public Address";
}
}
function convertIPNSToBitcoin() {
const ipnsBitcoin = document.getElementById("ipnsBitcoin").value;
if (!ipnsBitcoin) {
document.getElementById("bitcoinFromIpns").innerText = "Error: Please enter an IPNS public key.";
return;
}
try {
const decodedIPNS = base58Decode(ipnsBitcoin.substring(1));
if (decodedIPNS.length <= 6) {
document.getElementById("bitcoinFromIpns").innerText = "Error: Invalid IPNS Public Key";
return;
}
const bitcoinBytes = decodedIPNS.slice(6);
const bitcoinPublicKey = base58Encode(bitcoinBytes);
document.getElementById("bitcoinFromIpns").innerText = bitcoinPublicKey;
} catch (error) {
document.getElementById("bitcoinFromIpns").innerText = "Error: Invalid IPNS Public Key";
}
}
function convertIPNSToMonero() {
const ipnsMonero = document.getElementById("ipnsMonero").value;
if (!ipnsMonero) {
document.getElementById("moneroFromIpns").innerText = "Error: Please enter an IPNS public key.";
return;
}
try {
const decodedIPNS = base58Decode(ipnsMonero.substring(1));
if (decodedIPNS.length <= 6) {
document.getElementById("moneroFromIpns").innerText = "Error: Invalid IPNS Public Key";
return;
}
const moneroBytes = decodedIPNS.slice(6);
const moneroPublicKey = base58Encode(moneroBytes);
document.getElementById("moneroFromIpns").innerText = moneroPublicKey;
} catch (error) {
document.getElementById("moneroFromIpns").innerText = "Error: Invalid IPNS Public Key";
}
}
</script>
</body>
</html>