Here is an IPFS hash: QmTfCejgo2wTwqnDJs8Lu1pCNeCrCDuE4GAwkna93zdd7d. According to IPFS docs, they are base58 encoded, and I assume the Qm is metadata (because they all share this).

What is the most effective datatype to store one of these?

Edit: they do not all start with Qm


2 Answers 2


bytes because they are more than 32 bytes. An IPFS hash:

itself specifies the hash function and length of the hash in the first two bytes of the multihash. In the examples above the first two bytes in hex is 1220, where 12 denotes that this is the SHA256 hash function and 20 is the length of the hash in bytes - 32 bytes.


Although SHA2-256 is 32 bytes and currently the most common IPFS hash, other content could use a hash function that is larger than 32 bytes.

  • 1
    I'm sure this is the right answer for the general case but if the hash is created by your own software (ie you don't have to support arbitrary IPFS content) there may be an argument for committing to using 32-byte SHA2-256 hashes and stripping the multihash-identifying 1220, since arrays are a PITA to work with in Solidity, and in practice everyone is just going to be filling the blockchain up with 1220s. Jan 25, 2017 at 23:40
  • 1
    Certainly agree about PITA with some types. We should remember though IPFS highly values future-proofing (like github.com/multiformats/multihash). Maybe a library could help, as "it is also possible to extend elementary types".
    – eth
    Jan 29, 2017 at 8:16

You can store any IPFS hash in bytes32.
You will need cids npm package.


export function IpfsHashToBytes32(hash: string)
    return new CID(hash).toV0().multihash.subarray(2)

export function bytes32ToIpfsHash(bytes: Uint8Array)
    return new CID(new Uint8Array([18, 32, ...bytes])).toString()

First two bytes are hash version prefixes, so you don't need them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.