-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathconvert.ts
283 lines (247 loc) · 8.02 KB
/
convert.ts
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
import { IpNet } from '@chainsafe/netmask'
import { base32 } from 'multiformats/bases/base32'
import { base58btc } from 'multiformats/bases/base58'
import { bases } from 'multiformats/basics'
import { CID } from 'multiformats/cid'
import * as Digest from 'multiformats/hashes/digest'
import * as varint from 'uint8-varint'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import * as ip from './ip.js'
import { getProtocol } from './protocols-table.js'
import type { Multiaddr } from './index.js'
const ip4Protocol = getProtocol('ip4')
const ip6Protocol = getProtocol('ip6')
const ipcidrProtocol = getProtocol('ipcidr')
/**
* converts (serializes) addresses
*/
export function convert (proto: string, a: string): Uint8Array
export function convert (proto: string, a: Uint8Array): string
export function convert (proto: string, a: string | Uint8Array): Uint8Array | string {
if (a instanceof Uint8Array) {
return convertToString(proto, a)
} else {
return convertToBytes(proto, a)
}
}
/**
* Convert [code,Uint8Array] to string
*/
// eslint-disable-next-line complexity
export function convertToString (proto: number | string, buf: Uint8Array): string {
const protocol = getProtocol(proto)
switch (protocol.code) {
case 4: // ipv4
case 41: // ipv6
return bytes2ip(buf)
case 42: // ipv6zone
return bytes2str(buf)
case 43: // ipcidr
return uint8ArrayToString(buf, 'base10')
case 6: // tcp
case 273: // udp
case 33: // dccp
case 132: // sctp
return bytes2port(buf).toString()
case 53: // dns
case 54: // dns4
case 55: // dns6
case 56: // dnsaddr
case 400: // unix
case 449: // sni
case 777: // memory
return bytes2str(buf)
case 421: // ipfs
return bytes2mh(buf)
case 444: // onion
return bytes2onion(buf)
case 445: // onion3
return bytes2onion(buf)
case 466: // certhash
return bytes2mb(buf)
case 481: // http-path
return globalThis.encodeURIComponent(bytes2str(buf))
default:
return uint8ArrayToString(buf, 'base16') // no clue. convert to hex
}
}
// eslint-disable-next-line complexity
export function convertToBytes (proto: string | number, str: string): Uint8Array {
const protocol = getProtocol(proto)
switch (protocol.code) {
case 4: // ipv4
return ip2bytes(str)
case 41: // ipv6
return ip2bytes(str)
case 42: // ipv6zone
return str2bytes(str)
case 43: // ipcidr
return uint8ArrayFromString(str, 'base10')
case 6: // tcp
case 273: // udp
case 33: // dccp
case 132: // sctp
return port2bytes(parseInt(str, 10))
case 53: // dns
case 54: // dns4
case 55: // dns6
case 56: // dnsaddr
case 400: // unix
case 449: // sni
case 777: // memory
return str2bytes(str)
case 421: // ipfs
return mh2bytes(str)
case 444: // onion
return onion2bytes(str)
case 445: // onion3
return onion32bytes(str)
case 466: // certhash
return mb2bytes(str)
case 481: // http-path
return str2bytes(globalThis.decodeURIComponent(str))
default:
return uint8ArrayFromString(str, 'base16') // no clue. convert from hex
}
}
export function convertToIpNet (multiaddr: Multiaddr): IpNet {
let mask: string | undefined
let addr: string | undefined
multiaddr.stringTuples().forEach(([code, value]) => {
if (code === ip4Protocol.code || code === ip6Protocol.code) {
addr = value
}
if (code === ipcidrProtocol.code) {
mask = value
}
})
if (mask == null || addr == null) {
throw new Error('Invalid multiaddr')
}
return new IpNet(addr, mask)
}
const decoders = Object.values(bases).map((c) => c.decoder)
const anybaseDecoder = (function () {
let acc = decoders[0].or(decoders[1])
decoders.slice(2).forEach((d) => (acc = acc.or(d)))
return acc
})()
function ip2bytes (ipString: string): Uint8Array {
if (!ip.isIP(ipString)) {
throw new Error('invalid ip address')
}
return ip.toBytes(ipString)
}
function bytes2ip (ipBuff: Uint8Array): string {
const ipString = ip.toString(ipBuff, 0, ipBuff.length)
if (ipString == null) {
throw new Error('ipBuff is required')
}
if (!ip.isIP(ipString)) {
throw new Error('invalid ip address')
}
return ipString
}
function port2bytes (port: number): Uint8Array {
const buf = new ArrayBuffer(2)
const view = new DataView(buf)
view.setUint16(0, port)
return new Uint8Array(buf)
}
function bytes2port (buf: Uint8Array): number {
const view = new DataView(buf.buffer)
return view.getUint16(buf.byteOffset)
}
function str2bytes (str: string): Uint8Array {
const buf = uint8ArrayFromString(str)
const size = Uint8Array.from(varint.encode(buf.length))
return uint8ArrayConcat([size, buf], size.length + buf.length)
}
function bytes2str (buf: Uint8Array): string {
const size = varint.decode(buf)
buf = buf.slice(varint.encodingLength(size))
if (buf.length !== size) {
throw new Error('inconsistent lengths')
}
return uint8ArrayToString(buf)
}
function mh2bytes (hash: string): Uint8Array {
let mh
if (hash[0] === 'Q' || hash[0] === '1') {
mh = Digest.decode(base58btc.decode(`z${hash}`)).bytes
} else {
mh = CID.parse(hash).multihash.bytes
}
// the address is a varint prefixed multihash string representation
const size = Uint8Array.from(varint.encode(mh.length))
return uint8ArrayConcat([size, mh], size.length + mh.length)
}
function mb2bytes (mbstr: string): Uint8Array {
const mb = anybaseDecoder.decode(mbstr)
const size = Uint8Array.from(varint.encode(mb.length))
return uint8ArrayConcat([size, mb], size.length + mb.length)
}
function bytes2mb (buf: Uint8Array): string {
const size = varint.decode(buf)
const hash = buf.slice(varint.encodingLength(size))
if (hash.length !== size) {
throw new Error('inconsistent lengths')
}
return 'u' + uint8ArrayToString(hash, 'base64url')
}
/**
* Converts bytes to bas58btc string
*/
function bytes2mh (buf: Uint8Array): string {
const size = varint.decode(buf)
const address = buf.slice(varint.encodingLength(size))
if (address.length !== size) {
throw new Error('inconsistent lengths')
}
return uint8ArrayToString(address, 'base58btc')
}
function onion2bytes (str: string): Uint8Array {
const addr = str.split(':')
if (addr.length !== 2) {
throw new Error(`failed to parse onion addr: ["'${addr.join('", "')}'"]' does not contain a port number`)
}
if (addr[0].length !== 16) {
throw new Error(`failed to parse onion addr: ${addr[0]} not a Tor onion address.`)
}
// onion addresses do not include the multibase prefix, add it before decoding
const buf = base32.decode('b' + addr[0])
// onion port number
const port = parseInt(addr[1], 10)
if (port < 1 || port > 65536) {
throw new Error('Port number is not in range(1, 65536)')
}
const portBuf = port2bytes(port)
return uint8ArrayConcat([buf, portBuf], buf.length + portBuf.length)
}
function onion32bytes (str: string): Uint8Array {
const addr = str.split(':')
if (addr.length !== 2) {
throw new Error(`failed to parse onion addr: ["'${addr.join('", "')}'"]' does not contain a port number`)
}
if (addr[0].length !== 56) {
throw new Error(`failed to parse onion addr: ${addr[0]} not a Tor onion3 address.`)
}
// onion addresses do not include the multibase prefix, add it before decoding
const buf = base32.decode(`b${addr[0]}`)
// onion port number
const port = parseInt(addr[1], 10)
if (port < 1 || port > 65536) {
throw new Error('Port number is not in range(1, 65536)')
}
const portBuf = port2bytes(port)
return uint8ArrayConcat([buf, portBuf], buf.length + portBuf.length)
}
function bytes2onion (buf: Uint8Array): string {
const addrBytes = buf.slice(0, buf.length - 2)
const portBytes = buf.slice(buf.length - 2)
const addr = uint8ArrayToString(addrBytes, 'base32')
const port = bytes2port(portBytes)
return `${addr}:${port}`
}