-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Feat (cheatcodes): Introducing ipfs cid v0 to calculate file content #10348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Feat (cheatcodes): Introducing ipfs cid v0 to calculate file content #10348
Conversation
@Ayushdubey86 you need to run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ayushdubey86 thank you, mind to add a test for this new cheatcode similar with tests within https://github.com/foundry-rs/foundry/tree/master/testdata/default/cheats?
I am lacking slight bandwidth, I'll raise pr for tests within a day or 2!:) Hope this is fine |
@sakulstra do you have a test handy to include for |
On out ffi-js tooling test we got this: const content = `---
title: TestTitle
discussions: TestDiscussion
author: TestAuthor
---`;
expect(await Hash.of(content)).toBe('QmYMiDJUYXGsUng5rAgwgLvZzLEMjbhaWALFbQJjC6saPv');
const bs58Hex = `0x${Buffer.from(bs58.decode(hash)).slice(2).toString('hex')}`
expect(bs58Hex).toBe('0x94da694df5cf2e139206cddcdd6f855baa45e519c5fdbc2e6aa1cf803cfd65d5'); So i guess |
hey @sakulstra when I try to hash same content with |
@grandizzy i tried via
(no new line at eof), and it emits the same hash:
|
@sakulstra interesting, I get different (maybe due to OS?)
Mind to attach your test file to give it a try? |
Here it is: Pretty sure is not os related as we use the tooling accross oses. |
got it, yeah, get the same hash with your file |
…to-calculate-file-content
Have pushed test cases, do check it out!:) @grandizzy @sakulstra |
@Ayushdubey86 for test you could add the file directly under https://github.com/foundry-rs/foundry/tree/master/testdata/fixtures/File without the need of test to create it |
have pushed and modified test case! |
thanks! The path to file in test should not contain |
@@ -2008,6 +2008,10 @@ interface Vm { | |||
#[cheatcode(group = Filesystem)] | |||
function promptUint(string calldata promptText) external returns (uint256); | |||
|
|||
/// Calculates the IPFS CID V0 of a file's content. | |||
#[cheatcode(group = Filesystem)] | |||
function ipfsCidV0(string calldata filepath) external returns (bytes32 cid); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this have to be a path? It just reads the file then hashes it, we could just accept a bytes
directly.
Also, sha256 exists in the EVM itself (precompile 0x00...02), why can this not be a simple utility function written in Solidity where it is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point — accepting bytes directly would give more flexibility.
It depends on the user though — some might prefer to pass a filepath (especially for testing existing files).
Maybe we can support both?
I'm fine with either approach for now — happy to go with what you think makes the most sense! @sakulstra @grandizzy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, too strong opinion.
If it accepted string or similar, we would build our own wrapped which is perfectly fine either.
For us the main usecase is hashing content in external files which is why i suggested path, but i guess there could be usecases that would require hashing strings from solidity. So perhaps string input is more flexible as we could just use vm.ipfsCidV0 (vm.readFile(path))
and others can skip the file reading and hash content immediately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DaniPopes for sha256,
That’s a good point, and normally sha256() in Solidity would be sufficient — but this cheatcode targets a different use case.
We need to compute the SHA-256 hash of an off-chain file in the testing environment, prefix it per the multihash spec (IPFS CIDv0), and then pass that result into the EVM. Since Solidity's sha256() only operates on in-memory bytes at runtime, it can’t handle file reads or multihash formatting.
So this utility is kinda necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks to be more complex than initial draft, the only way I was able to get same CID v0 as with npx ipfs-only-hash
was to use https://crates.io/crates/ipfs-cid crate which chunks it and hash last chunk https://github.com/omarabid/ipfs-cid/blob/e60fae3ee3222916da0c1dfec5eda1a4c26dec5b/src/lib.rs#L8 This uses an archived / unmaintained crate https://github.com/rs-ipfs/rust-ipfs and adds in bunch of other dependencies which would like to avoid. If we cannot get to a clean impl (that doesn't require pulling in so many deps) we should hold off on this for now.
I'll look into this test case failure post final design discussion! |
Hello @jenpaff ,it should close #9862
locally everything seems to be passing

can you rerun pipeline, saw in documentation it wont pass through first attempt