Let's build the Bitcoin File System (BFS), a decentralized, open, and permissionless file storage on Bitcoin.
write a file to Bitcoin
read a file from Bitcoin
support large files
Write the BFS smart contract
It turns out that writing the Bitcoin File System smart contract is very simple. Here is a basic contract to provide decentralized file storage on Bitcoin Virtual Machine.
The store() function saves a chunk of a file. The load() function returns the data of a specific chunk. And the getId() function returns the ID of the file.
You can save a large file onto Bitcoin by splitting it into smaller chunks, committing them to Bitcoin, and retrieving & merging them back as needed.
BFS files are immutable.
Clone the smart contract examples
We've prepared a few different examples for you to get started. The BNS example is located at smart-contract-examples/contracts/BNS.sol.
Review config file hardhat.config.ts. The network configs should look like this.
networks: { mynw: { url:"http://localhost:10002", accounts: { mnemonic:"<your mnemonic with funds>" }, timeout:100_000, }, blockscoutVerify: { blockscoutURL:"http://localhost:4000",// your explorer URL... } }
Run the deploy scripts using hardhat-deploy.
npxhardhatdeploy--tagsBFS
Make sure the accounts in hardhat.config.ts have some $BVM.
Interact with the contracts
Once the contracts are deployed, you can interact with them. We've prepared a few hardhat tasks to make it easy for you to interact with the contracts.
# store dataecho"this is some text">data.txtnpxhardhatwrite-storage--filename./data.txtnpxhardhatread-storage--filename./data.txt# read data, print it as readable text