chore: update multicodec usage (#2233)
This commit is contained in:
parent
db18b486b4
commit
c2c7d87ca3
@ -1,5 +1,5 @@
|
|||||||
import CID from 'cids'
|
import CID from 'cids'
|
||||||
import { getCodec, rmPrefix } from 'multicodec'
|
import { getNameFromData, rmPrefix } from 'multicodec'
|
||||||
import { decode, toB58String } from 'multihashes'
|
import { decode, toB58String } from 'multihashes'
|
||||||
|
|
||||||
export function hexToUint8Array(hex: string): Uint8Array {
|
export function hexToUint8Array(hex: string): Uint8Array {
|
||||||
@ -19,17 +19,17 @@ const UTF_8_DECODER = new TextDecoder('utf-8')
|
|||||||
* @param contenthash to decode
|
* @param contenthash to decode
|
||||||
*/
|
*/
|
||||||
export default function contenthashToUri(contenthash: string): string {
|
export default function contenthashToUri(contenthash: string): string {
|
||||||
const buff = hexToUint8Array(contenthash)
|
const data = hexToUint8Array(contenthash)
|
||||||
const codec = getCodec(buff as Buffer) // the typing is wrong for @types/multicodec
|
const codec = getNameFromData(data)
|
||||||
switch (codec) {
|
switch (codec) {
|
||||||
case 'ipfs-ns': {
|
case 'ipfs-ns': {
|
||||||
const data = rmPrefix(buff as Buffer)
|
const unprefixedData = rmPrefix(data)
|
||||||
const cid = new CID(data)
|
const cid = new CID(unprefixedData)
|
||||||
return `ipfs://${toB58String(cid.multihash)}`
|
return `ipfs://${toB58String(cid.multihash)}`
|
||||||
}
|
}
|
||||||
case 'ipns-ns': {
|
case 'ipns-ns': {
|
||||||
const data = rmPrefix(buff as Buffer)
|
const unprefixedData = rmPrefix(data)
|
||||||
const cid = new CID(data)
|
const cid = new CID(unprefixedData)
|
||||||
const multihash = decode(cid.multihash)
|
const multihash = decode(cid.multihash)
|
||||||
if (multihash.name === 'identity') {
|
if (multihash.name === 'identity') {
|
||||||
return `ipns://${UTF_8_DECODER.decode(multihash.digest).trim()}`
|
return `ipns://${UTF_8_DECODER.decode(multihash.digest).trim()}`
|
||||||
|
Loading…
Reference in New Issue
Block a user