0

I want to save multiple files in Ipfs and store its hashes mapped to an user address. I thought of having an array mapped to address and pushing to it, but does not seem to be the right approach. Please advise.

2

You basically have to use an array. You could use a mapping inside a mapping of some sorts but the problem is that you can't iterate over a mapping so you would have no way of knowing what hashes you have stored - unless you already know the hashes in which case storing them here is pointless. So you need something like mapping (address => string[]) private hashes;.

Depending on your real use case there might not even be anything wrong with using an array; in some cases deletions and inserts may pose problems but as there's generally no reason to delete an IPFS hash (the IPFS network doesn't guarantee deletion of a file if I remember correctly) and as the order of the hashes doesn't probably make a difference for you you can just always append to the array. And iterating over the array is trivial.

  • 1
    Instead of string[] you can use bytes32[] since it will be a ipfs hash – Sanjay S B Aug 8 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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