Commit c2c7d87c authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

chore: update multicodec usage (#2233)

parent db18b486
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') ...@@ -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()}`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment