Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
chore: update multicodec usage (#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp authored Aug 23, 2021
1 parent db18b48 commit c2c7d87
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/utils/contenthashToUri.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CID from 'cids'
import { getCodec, rmPrefix } from 'multicodec'
import { getNameFromData, rmPrefix } from 'multicodec'
import { decode, toB58String } from 'multihashes'

export function hexToUint8Array(hex: string): Uint8Array {
Expand All @@ -19,17 +19,17 @@ const UTF_8_DECODER = new TextDecoder('utf-8')
* @param contenthash to decode
*/
export default function contenthashToUri(contenthash: string): string {
const buff = hexToUint8Array(contenthash)
const codec = getCodec(buff as Buffer) // the typing is wrong for @types/multicodec
const data = hexToUint8Array(contenthash)
const codec = getNameFromData(data)
switch (codec) {
case 'ipfs-ns': {
const data = rmPrefix(buff as Buffer)
const cid = new CID(data)
const unprefixedData = rmPrefix(data)
const cid = new CID(unprefixedData)
return `ipfs://${toB58String(cid.multihash)}`
}
case 'ipns-ns': {
const data = rmPrefix(buff as Buffer)
const cid = new CID(data)
const unprefixedData = rmPrefix(data)
const cid = new CID(unprefixedData)
const multihash = decode(cid.multihash)
if (multihash.name === 'identity') {
return `ipns://${UTF_8_DECODER.decode(multihash.digest).trim()}`
Expand Down

0 comments on commit c2c7d87

Please sign in to comment.