From 6846ce7d4d7565b87185c565f00479daad129dbd Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 22 Apr 2025 16:34:04 +0530 Subject: [PATCH 01/15] Update Cargo.lock --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 43d0cbcd56ae6..28250acc853c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3746,6 +3746,7 @@ dependencies = [ "semver 1.0.26", "serde", "serde_json", + "sha2", "thiserror 2.0.12", "toml 0.8.20", "tracing", From ea2b6b47bbd4aeb002ba10dd8574366dff1dbd8e Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 22 Apr 2025 16:35:25 +0530 Subject: [PATCH 02/15] Update Cargo.toml --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 840b4c9fa1fe1..3dd095ac4eaa7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -314,6 +314,7 @@ url = "2" vergen = { version = "8", default-features = false } yansi = { version = "1.0", features = ["detect-tty", "detect-env"] } path-slash = "0.2" +sha="0.10" [patch.crates-io] ## alloy-core From 3be499eba2dd33883a098c09d3e3d9b4bdd8318b Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 22 Apr 2025 16:36:06 +0530 Subject: [PATCH 03/15] Update Cargo.toml --- crates/cheatcodes/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/cheatcodes/Cargo.toml b/crates/cheatcodes/Cargo.toml index 645426ea8bf29..88aa290d507ce 100644 --- a/crates/cheatcodes/Cargo.toml +++ b/crates/cheatcodes/Cargo.toml @@ -62,3 +62,4 @@ tracing.workspace = true walkdir.workspace = true proptest.workspace = true serde.workspace = true +sha2.workspace=true From 05c58dfe4e13592f7a62ebefc665b6c8962da315 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 22 Apr 2025 16:37:41 +0530 Subject: [PATCH 04/15] Update cheatcodes.json --- crates/cheatcodes/assets/cheatcodes.json | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/cheatcodes/assets/cheatcodes.json b/crates/cheatcodes/assets/cheatcodes.json index 7cce924b5f60c..994b4db66657b 100644 --- a/crates/cheatcodes/assets/cheatcodes.json +++ b/crates/cheatcodes/assets/cheatcodes.json @@ -8096,6 +8096,26 @@ "status": "stable", "safety": "safe" }, + { + "func": { + "id": "ipfsCidV0", + "description": "Calculates the IPFS CID V0 of a file's content.", + "declaration": "function ipfsCidV0(string calldata filepath) external returns (bytes32 cid);", + "visibility": "external", + "mutability": "", + "signature": "ipfsCidV0(string)", + "selector": "0xcac92fa5", + "selectorBytes": [ + 202, + 201, + 47, + 165 + ] + }, + "group": "filesystem", + "status": "stable", + "safety": "safe" + }, { "func": { "id": "publicKeyP256", @@ -11023,4 +11043,4 @@ "safety": "safe" } ] -} \ No newline at end of file +} From dbae68544f8bf1fd0b39911be024001c8781085c Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 22 Apr 2025 16:41:57 +0530 Subject: [PATCH 05/15] Update vm.rs --- crates/cheatcodes/spec/src/vm.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/cheatcodes/spec/src/vm.rs b/crates/cheatcodes/spec/src/vm.rs index c0481569af296..29844d10c6d56 100644 --- a/crates/cheatcodes/spec/src/vm.rs +++ b/crates/cheatcodes/spec/src/vm.rs @@ -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); + // ======== Environment Variables ======== /// Sets environment variables. From 52e187ec50f1085189efbf75c7f4e79decd6a726 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 22 Apr 2025 16:42:42 +0530 Subject: [PATCH 06/15] Update fs.rs --- crates/cheatcodes/src/fs.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/cheatcodes/src/fs.rs b/crates/cheatcodes/src/fs.rs index 940d5f6d8ef3c..1e5711b6f6b99 100644 --- a/crates/cheatcodes/src/fs.rs +++ b/crates/cheatcodes/src/fs.rs @@ -15,6 +15,7 @@ use foundry_config::fs_permissions::FsAccessKind; use revm::interpreter::CreateInputs; use revm_inspectors::tracing::types::CallKind; use semver::Version; +use sha2::{Sha256,Digest}; use std::{ io::{BufRead, BufReader, Write}, path::{Path, PathBuf}, From 11043b07313dbe54d36d1fdce8466022d8866045 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 22 Apr 2025 16:58:21 +0530 Subject: [PATCH 07/15] Update fs.rs --- crates/cheatcodes/src/fs.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/cheatcodes/src/fs.rs b/crates/cheatcodes/src/fs.rs index 1e5711b6f6b99..36c56b8301650 100644 --- a/crates/cheatcodes/src/fs.rs +++ b/crates/cheatcodes/src/fs.rs @@ -588,6 +588,19 @@ impl Cheatcode for promptUintCall { } } +impl Cheatcode for ipfsCidV0Call { + fn apply(&self, _state: &mut Cheatcodes) -> Result { + let contents = fs::read(&self.filepath).unwrap(); + let hash = Sha256::digest(&contents); + let mut multihash = Vec::with_capacity(2 + hash.len()); + multihash.push(0x12); + multihash.push(0x20); + multihash.extend_from_slice(&hash); + + Ok(multihash) + } +} + pub(super) fn write_file(state: &Cheatcodes, path: &Path, contents: &[u8]) -> Result { let path = state.config.ensure_path_allowed(path, FsAccessKind::Write)?; // write access to foundry.toml is not allowed From d8b9e54589fd5f9ea00e7af793a61c17ef1954f5 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 22 Apr 2025 17:24:10 +0530 Subject: [PATCH 08/15] Update Vm.sol --- testdata/cheats/Vm.sol | 1 + 1 file changed, 1 insertion(+) diff --git a/testdata/cheats/Vm.sol b/testdata/cheats/Vm.sol index b3a447cc5e1dc..f077053ca1748 100644 --- a/testdata/cheats/Vm.sol +++ b/testdata/cheats/Vm.sol @@ -311,6 +311,7 @@ interface Vm { function getWallets() external returns (address[] memory wallets); function indexOf(string calldata input, string calldata key) external pure returns (uint256); function interceptInitcode() external; + function ipfsCidV0(string calldata filepath) external returns (bytes32 cid); function isContext(ForgeContext context) external view returns (bool result); function isDir(string calldata path) external view returns (bool result); function isFile(string calldata path) external view returns (bool result); From f020635a4324128799b3048263a74cb276071567 Mon Sep 17 00:00:00 2001 From: zerosnacks <95942363+zerosnacks@users.noreply.github.com> Date: Tue, 22 Apr 2025 14:59:53 +0200 Subject: [PATCH 09/15] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3dd095ac4eaa7..15ad5900de671 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -314,7 +314,7 @@ url = "2" vergen = { version = "8", default-features = false } yansi = { version = "1.0", features = ["detect-tty", "detect-env"] } path-slash = "0.2" -sha="0.10" +sha2 = "0.10" [patch.crates-io] ## alloy-core From e2c069027d4c06232d2c94d9fe661b6c8ab1d76a Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 22 Apr 2025 19:22:16 +0530 Subject: [PATCH 10/15] fmt From 52d707b271a65287de1ab832a64d46227f463fed Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 22 Apr 2025 19:22:45 +0530 Subject: [PATCH 11/15] Update fs.rs --- crates/cheatcodes/src/fs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cheatcodes/src/fs.rs b/crates/cheatcodes/src/fs.rs index 36c56b8301650..ca76fb164302c 100644 --- a/crates/cheatcodes/src/fs.rs +++ b/crates/cheatcodes/src/fs.rs @@ -15,7 +15,7 @@ use foundry_config::fs_permissions::FsAccessKind; use revm::interpreter::CreateInputs; use revm_inspectors::tracing::types::CallKind; use semver::Version; -use sha2::{Sha256,Digest}; +use sha2::{Digest, Sha256}; use std::{ io::{BufRead, BufReader, Write}, path::{Path, PathBuf}, From 6247228eba75e1cb9fb54c948d077d41c1368708 Mon Sep 17 00:00:00 2001 From: grandizzy Date: Tue, 22 Apr 2025 16:57:56 +0300 Subject: [PATCH 12/15] Fix cargo cheats and fmt --- crates/cheatcodes/assets/cheatcodes.json | 42 ++++++++++++------------ crates/cheatcodes/spec/src/vm.rs | 2 +- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/crates/cheatcodes/assets/cheatcodes.json b/crates/cheatcodes/assets/cheatcodes.json index 994b4db66657b..1c0f14de1d3f8 100644 --- a/crates/cheatcodes/assets/cheatcodes.json +++ b/crates/cheatcodes/assets/cheatcodes.json @@ -6354,6 +6354,26 @@ "status": "stable", "safety": "unsafe" }, + { + "func": { + "id": "ipfsCidV0", + "description": "Calculates the IPFS CID V0 of a file's content.", + "declaration": "function ipfsCidV0(string calldata filepath) external returns (bytes32 cid);", + "visibility": "external", + "mutability": "", + "signature": "ipfsCidV0(string)", + "selector": "0xcac92fa5", + "selectorBytes": [ + 202, + 201, + 47, + 165 + ] + }, + "group": "filesystem", + "status": "stable", + "safety": "safe" + }, { "func": { "id": "isContext", @@ -8096,26 +8116,6 @@ "status": "stable", "safety": "safe" }, - { - "func": { - "id": "ipfsCidV0", - "description": "Calculates the IPFS CID V0 of a file's content.", - "declaration": "function ipfsCidV0(string calldata filepath) external returns (bytes32 cid);", - "visibility": "external", - "mutability": "", - "signature": "ipfsCidV0(string)", - "selector": "0xcac92fa5", - "selectorBytes": [ - 202, - 201, - 47, - 165 - ] - }, - "group": "filesystem", - "status": "stable", - "safety": "safe" - }, { "func": { "id": "publicKeyP256", @@ -11043,4 +11043,4 @@ "safety": "safe" } ] -} +} \ No newline at end of file diff --git a/crates/cheatcodes/spec/src/vm.rs b/crates/cheatcodes/spec/src/vm.rs index 29844d10c6d56..378d06d4ef82f 100644 --- a/crates/cheatcodes/spec/src/vm.rs +++ b/crates/cheatcodes/spec/src/vm.rs @@ -2011,7 +2011,7 @@ interface Vm { /// Calculates the IPFS CID V0 of a file's content. #[cheatcode(group = Filesystem)] function ipfsCidV0(string calldata filepath) external returns (bytes32 cid); - + // ======== Environment Variables ======== /// Sets environment variables. From ca89033f6eabd2b08fc7e13f18f5b8a8b8efb35b Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 23 Apr 2025 20:54:22 +0530 Subject: [PATCH 13/15] test case --- testdata/default/cheats/IpfsCidV0.t.sol | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 testdata/default/cheats/IpfsCidV0.t.sol diff --git a/testdata/default/cheats/IpfsCidV0.t.sol b/testdata/default/cheats/IpfsCidV0.t.sol new file mode 100644 index 0000000000000..d35a19904c141 --- /dev/null +++ b/testdata/default/cheats/IpfsCidV0.t.sol @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +pragma solidity ^0.8.18; + +import "ds-test/test.sol"; +import "cheats/Vm.sol"; + +contract IpfsCidV0Test is DSTest { + Vm constant vm = Vm(HEVM_ADDRESS); + + function testIpfsCidV0() public { + string memory filePath = "./testdata/test.txt"; + string memory fileContents = "---\ntitle: TestTitle\ndiscussions: TestDiscussion\nauthor: TestAuthor\n---"; + + vm.writeFile(filePath, fileContents); + + bytes32 cid = vm.ipfsCidV0(filePath); + + bytes32 expectedCid = 0x94da694df5cf2e139206cddcdd6f855baa45e519c5fdbc2e6aa1cf803cfd65d5; + + assertEq(cid, expectedCid); + } +} From 7a1dd8fd9ce964559e92302df92a21aec22f642e Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 23 Apr 2025 21:12:14 +0530 Subject: [PATCH 14/15] test file --- testdata/fixtures/File/test.txt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 testdata/fixtures/File/test.txt diff --git a/testdata/fixtures/File/test.txt b/testdata/fixtures/File/test.txt new file mode 100644 index 0000000000000..eb3a0e33b9df9 --- /dev/null +++ b/testdata/fixtures/File/test.txt @@ -0,0 +1,5 @@ +--- +title: TestTitle +discussions: TestDiscussion +author: TestAuthor +--- \ No newline at end of file From bd557a4e56f65c227d9fd098fd8b9b0c5a6e305a Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 23 Apr 2025 21:18:04 +0530 Subject: [PATCH 15/15] test case changes --- testdata/default/cheats/IpfsCidV0.t.sol | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/testdata/default/cheats/IpfsCidV0.t.sol b/testdata/default/cheats/IpfsCidV0.t.sol index d35a19904c141..df72b42f1acfa 100644 --- a/testdata/default/cheats/IpfsCidV0.t.sol +++ b/testdata/default/cheats/IpfsCidV0.t.sol @@ -8,10 +8,7 @@ contract IpfsCidV0Test is DSTest { Vm constant vm = Vm(HEVM_ADDRESS); function testIpfsCidV0() public { - string memory filePath = "./testdata/test.txt"; - string memory fileContents = "---\ntitle: TestTitle\ndiscussions: TestDiscussion\nauthor: TestAuthor\n---"; - - vm.writeFile(filePath, fileContents); + string memory filePath = "testdata/fixtures/File/test.txt"; bytes32 cid = vm.ipfsCidV0(filePath);