Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base 58 decode function? #4

Open
decafboba opened this issue May 1, 2022 · 1 comment
Open

base 58 decode function? #4

decafboba opened this issue May 1, 2022 · 1 comment

Comments

@decafboba
Copy link

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

@codemedici
Copy link

codemedici commented Oct 19, 2023

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);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants