Base58 <->Bytes32

Storing a string in solidity is quite expensive. So we use the string hash(base58) to bytes32 conversion function. And we store the data as bytes32.

Below are the implementations of the convert function in two directions.

import bs58 from 'bs58'

getBytes32FromIpfsHash(ipfsListing) {
  return "0x"+bs58.decode(ipfsListing).slice(2).toString('hex')
}

getIpfsHashFromBytes32(bytes32Hex) {
  const hashHex = "1220" + bytes32Hex.slice(2)
  const hashBytes = Buffer.from(hashHex, 'hex');
  const hashStr = bs58.encode(hashBytes)
  return hashStr
}

Last updated