Solidity – How to Upload to IPFS or Swarm from Inside a Smart Contract

contract-developmentipfsnftsolidity

While the title pretty much sums up the question, let's go into a bit more detail.

It would be very useful for a number of use cases if a function in a contract could directly upload to IPFS, Swarm, or similar and retrieve the hash. (This is separate from asking about smart contracts having access to a local file system – we can assume here that the data is passed to the function directly as an argument.) Theoretically you could even have a gateway contract that does this as a feature – hit the function on the contract and it returns a hash.

One easy use case is in the NFT world. NFTs have metadata explaining what they are tokenizing. To date, as far as I know, those who go with storing the metadata off-chain generate the metadata off-chain, maybe just storing a provenance hash in the contract to prove the veracity of the metadata. Alternatively some projects opt to store metadata on-chain, particularly generative NFTs. (Here's a nice thread talking about different approaches to metadata storage.)

But what if an on-chain function could upload to IPFS or similar and retrieve the hash? I would think this would open up a lot of interesting options. On the other hand, I can't find anything like this. Is it impossible or prohibitively hard? What are the difficulties, and what would need to be done to make it work?

(I'll tag Solidity simply because for many Solidity is synonymous with smart contracts, but the question applies equally to any other smart contract language.)

Best Answer

It would be very useful for a number of use cases if a function in a contract could directly upload to IPFS, Swarm, or similar and retrieve the hash.

It would be useful, but what you are asking is impossible in computer science theory, and practice.

Ethereum, like all other blockchains, is deterministic. They cannot communicate with any off-chain resources, because the blockchain would be no longer deterministic - any node, miner, etc. could get a different reply from an IPFS node.

Related Topic