verifyIPFS icon indicating copy to clipboard operation
verifyIPFS copied to clipboard

base 58 decode function?

Open 0x6080 opened this issue 2 years ago • 1 comments

The b58 encode function is awesome, would there be a way to make a similar one that decodes instead?

0x6080 avatar May 01 '22 15:05 0x6080

Hi @MrChico , thank you so much I'm using your code in production for Ninfa.io marketplace nft contracts, you're leet!

I'm currently using javascript for encoding ipfs CID to bytes32, do you have any pointers for doing the same in Solidity? For writing some scripts in Foundry!

For the record @hen-hao

const bs58 = require("bs58");

const getBytes32FromCID = (str) => {
  // remove leading 0x
  const remove0x = str.slice(2, str.length);
  // add back the multihash id
  const bytes = Buffer.from(`1220${remove0x}`, "hex");
  const hash = bs58.encode(bytes);
  return hash;
};

const getCIDFromBytes32 = (hash) => {
  let bytes = bs58.decode(hash);
  const multiHashId = 2;
  // remove the multihash hash id
  bytes = bytes.slice(multiHashId, bytes.length);
  return web3.utils.bytesToHex(bytes);
};

codemedici avatar Oct 19 '23 06:10 codemedici