-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
contenthashToUri.ts
43 lines (40 loc) 路 1.38 KB
/
contenthashToUri.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
import CID from 'cids'
import { getCodec, rmPrefix } from 'multicodec'
import { decode, toB58String } from 'multihashes'
export function hexToUint8Array(hex: string): Uint8Array {
hex = hex.startsWith('0x') ? hex.substr(2) : hex
if (hex.length % 2 !== 0) throw new Error('hex must have length that is multiple of 2')
const arr = new Uint8Array(hex.length / 2)
for (let i = 0; i < arr.length; i++) {
arr[i] = parseInt(hex.substr(i * 2, 2), 16)
}
return arr
}
const UTF_8_DECODER = new TextDecoder()
/**
* Returns the URI representation of the content hash for supported codecs
* @param contenthash to decode
*/
export function contenthashToUri(contenthash: string): string {
const buff = hexToUint8Array(contenthash)
const codec = getCodec(buff as Buffer) // the typing is wrong for @types/multicodec
switch (codec) {
case 'ipfs-ns': {
const data = rmPrefix(buff as Buffer)
const cid = new CID(data)
return `ipfs://${toB58String(cid.multihash)}`
}
case 'ipns-ns': {
const data = rmPrefix(buff as Buffer)
const cid = new CID(data)
const multihash = decode(cid.multihash)
if (multihash.name === 'identity') {
return `ipns://${UTF_8_DECODER.decode(multihash.digest)}`
} else {
return `ipns://${toB58String(cid.multihash)}`
}
}
default:
throw new Error(`Unrecognized codec: ${codec}`)
}
}