-
Notifications
You must be signed in to change notification settings - Fork 1
/
base58.html
executable file
·189 lines (165 loc) · 4.84 KB
/
base58.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
<!DOCTYPE html>
<html>
<head>
<style>
main {
width: 80%; /* or any specific width according to your design */
max-width: 800px; /* prevents the content from stretching too wide on large screens */
margin: 50px auto; /* top/bottom margin and auto left/right margin for centering */
}
label {
display: block;
font-size: 18px;
margin-bottom: 10px;
color: #555;
}
input {
width: calc(100% - 20px);
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid #ddd;
outline: none;
box-sizing: border-box;
}
input:focus {
border-color: #66afe9;
box-shadow: 0 0 5px rgba(102, 175, 233, 0.5);
}
/* Navbar styles */
nav {
display: flex;
justify-content: space-between;
padding: 10px 50px;
background-color: #f8f9fa;
border-bottom: 1px solid #e5e5e5;
box-sizing: border-box;
flex-wrap: wrap;
}
nav a {
color: #6c757d;
text-decoration: none;
margin-left: 140px;
padding: 0 15px;
}
nav button {
color: #6c757d;
text-decoration: none;
padding: 0 15px;
border: none;
background: none;
}
</style>
</head>
<body>
<script type="module">
import { html, render, Component } from './js/standalone.module.js'
import Navbar from './js/navbar2.js'
import base58 from 'https://esm.sh/base58check'
const links = [
{
'@id': 'https://github.com/ursuscamp/nomen/blob/master/docs/SPEC.md',
label: 'Base58'
}
]
function isValidHex(hex) {
const regex = /^[0-9a-fA-F]+$/
return regex.test(hex)
}
function hexToUint8Array(hexString) {
const bytes = new Uint8Array(Math.ceil(hexString.length / 2))
for (let i = 0; i < bytes.length; i++) {
bytes[i] = parseInt(hexString.substr(i * 2, 2), 16)
}
return bytes
}
function uint8ArrayToHex(byteArray) {
return Array.from(byteArray)
.map(byte => byte.toString(16).padStart(2, '0'))
.join('')
}
class Converter extends Component {
constructor(props) {
super(props)
this.state = {
b58: '',
hex: ''
}
}
handleBase58Change = e => {
const b58 = e.target.value
try {
console.log(`Input Base58: ${b58}`)
// Decode Base58 to Uint8Array and log the resulting array
const byteArray = base58.decode(b58)
console.log(`Decoded Byte Array:`, byteArray)
// Convert Uint8Array to hex and log the resulting string
const hex = uint8ArrayToHex(byteArray.data)
console.log(`Converted Hex: ${hex}`)
// Ensure that the hex string is valid
if (!isValidHex(hex)) {
console.error('Invalid hex after conversion')
return
}
this.setState({ b58, hex })
} catch (error) {
console.error('Invalid Base58:', error)
}
}
handleHexChange = e => {
const hex = e.target.value
// Validate the hex input
if (!isValidHex(hex)) {
console.error('Invalid hex input')
return
}
try {
// Ensure byte length is even for encoding
const adjustedHex = hex.length % 2 === 0 ? hex : `0${hex}`
console.log(`Adjusted Hex: ${adjustedHex}`)
// Convert hex to Uint8Array and log the resulting array
const byteArray = hexToUint8Array(adjustedHex)
console.log(`Byte Array:`, byteArray)
// Encode to Base58 and log the result
const b58 = base58.encode(adjustedHex)
console.log(`Base58: ${b58}`)
this.setState({ b58, hex })
} catch (error) {
console.error('Invalid hex:', error)
}
}
render() {
return html`
<main>
<label>Hex: </label>
<input
type="text"
value=${this.state.hex}
onInput=${this.handleHexChange}
/>
</main>
<main>
<label>Base58: </label>
<input
type="text"
value=${this.state.b58}
onInput=${this.handleBase58Change}
/>
</main>
`
}
}
const renderApp = () => {
render(
html`
<${Navbar} links=${links} />
<${Converter} />
`,
document.body
)
}
// Initial render
renderApp()
</script>
</body>
</html>