@@ -4,6 +4,7 @@ import { getProtocol } from './protocols-table.js'
4
4
import { CID } from 'multiformats/cid'
5
5
import { base32 } from 'multiformats/bases/base32'
6
6
import { base58btc } from 'multiformats/bases/base58'
7
+ import { bases } from 'multiformats/basics'
7
8
import * as Digest from 'multiformats/hashes/digest'
8
9
import varint from 'varint'
9
10
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
@@ -51,6 +52,8 @@ export function convertToString (proto: number | string, buf: Uint8Array) {
51
52
return bytes2onion ( buf )
52
53
case 445 : // onion3
53
54
return bytes2onion ( buf )
55
+ case 466 : // certhash
56
+ return bytes2mb ( buf )
54
57
default :
55
58
return uint8ArrayToString ( buf , 'base16' ) // no clue. convert to hex
56
59
}
@@ -84,11 +87,20 @@ export function convertToBytes (proto: string | number, str: string) {
84
87
return onion2bytes ( str )
85
88
case 445 : // onion3
86
89
return onion32bytes ( str )
90
+ case 466 : // certhash
91
+ return mb2bytes ( str )
87
92
default :
88
93
return uint8ArrayFromString ( str , 'base16' ) // no clue. convert from hex
89
94
}
90
95
}
91
96
97
+ const decoders = Object . values ( bases ) . map ( ( c ) => c . decoder )
98
+ const anybaseDecoder = ( function ( ) {
99
+ let acc = decoders [ 0 ] . or ( decoders [ 1 ] )
100
+ decoders . slice ( 2 ) . forEach ( ( d ) => ( acc = acc . or ( d ) ) )
101
+ return acc
102
+ } ) ( )
103
+
92
104
function ip2bytes ( ipString : string ) {
93
105
if ( ! ip . isIP ( ipString ) ) {
94
106
throw new Error ( 'invalid ip address' )
@@ -148,6 +160,22 @@ function mh2bytes (hash: string) {
148
160
return uint8ArrayConcat ( [ size , mh ] , size . length + mh . length )
149
161
}
150
162
163
+ function mb2bytes ( mbstr : string ) {
164
+ const mb = anybaseDecoder . decode ( mbstr )
165
+ const size = Uint8Array . from ( varint . encode ( mb . length ) )
166
+ return uint8ArrayConcat ( [ size , mb ] , size . length + mb . length )
167
+ }
168
+ function bytes2mb ( buf : Uint8Array ) {
169
+ const size = varint . decode ( buf )
170
+ const hash = buf . slice ( varint . decode . bytes )
171
+
172
+ if ( hash . length !== size ) {
173
+ throw new Error ( 'inconsistent lengths' )
174
+ }
175
+
176
+ return 'u' + uint8ArrayToString ( hash , 'base64url' )
177
+ }
178
+
151
179
/**
152
180
* Converts bytes to bas58btc string
153
181
*/
0 commit comments