diff --git a/Cargo.toml b/Cargo.toml index 4f14ae013..037202b12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ cw-orch-fns-derive = { path = "packages/cw-orch-fns-derive", version = "0.18.0" cw-orch-networks = { path = "packages/cw-orch-networks", version = "0.20.0" } thiserror = { version = "1.0.21" } -sha256 = { version = "1" } +sha2 = { version = "0.10.8" } serde_json = "1.0.79" tonic = { version = "0.10.2" } prost-types = "0.12.3" diff --git a/cw-orch-daemon/Cargo.toml b/cw-orch-daemon/Cargo.toml index 893dc7be2..530356184 100644 --- a/cw-orch-daemon/Cargo.toml +++ b/cw-orch-daemon/Cargo.toml @@ -38,7 +38,7 @@ thiserror = { workspace = true } prost-types = { workspace = true } # Daemon deps -sha256 = { workspace = true } +sha2 = { workspace = true } prost = { version = "0.12.3" } bitcoin = { version = "0.30.0" } hex = { version = "0.4.3" } diff --git a/cw-orch-daemon/src/queriers/cosmwasm.rs b/cw-orch-daemon/src/queriers/cosmwasm.rs index 77f3bae51..0b75b6d1d 100644 --- a/cw-orch-daemon/src/queriers/cosmwasm.rs +++ b/cw-orch-daemon/src/queriers/cosmwasm.rs @@ -5,7 +5,7 @@ use cosmrs::proto::cosmos::base::query::v1beta1::PageRequest; use cosmrs::AccountId; use cosmwasm_std::{ from_json, instantiate2_address, to_json_binary, CanonicalAddr, CodeInfoResponse, - ContractInfoResponse, + ContractInfoResponse, HexBinary, }; use cw_orch_core::environment::{Querier, QuerierGetter, WasmQuerier}; use tokio::runtime::Handle; @@ -45,14 +45,13 @@ impl Querier for CosmWasm { impl CosmWasm { /// Query code_id by hash - pub async fn _code_id_hash(&self, code_id: u64) -> Result { + pub async fn _code_id_hash(&self, code_id: u64) -> Result { use cosmos_modules::cosmwasm::{query_client::*, QueryCodeRequest}; let mut client: QueryClient = QueryClient::new(self.channel.clone()); let request = QueryCodeRequest { code_id }; let resp = client.code(request).await?.into_inner(); let contract_hash = resp.code_info.unwrap().data_hash; - let on_chain_hash = base16::encode_lower(&contract_hash); - Ok(on_chain_hash) + Ok(contract_hash.into()) } /// Query contract info @@ -199,7 +198,7 @@ impl CosmWasm { } impl WasmQuerier for CosmWasm { - fn code_id_hash(&self, code_id: u64) -> Result { + fn code_id_hash(&self, code_id: u64) -> Result { self.rt_handle .as_ref() .ok_or(DaemonError::QuerierNeedRuntime)? @@ -287,7 +286,7 @@ impl WasmQuerier for CosmWasm { let prefix = account_id.prefix(); let canon = account_id.to_bytes(); let checksum = self.code_id_hash(code_id)?; - let addr = instantiate2_address(checksum.as_bytes(), &CanonicalAddr(canon.into()), &salt)?; + let addr = instantiate2_address(checksum.as_slice(), &CanonicalAddr(canon.into()), &salt)?; Ok(AccountId::new(prefix, &addr.0)?.to_string()) } diff --git a/cw-orch/src/osmosis_test_tube/queriers/wasm.rs b/cw-orch/src/osmosis_test_tube/queriers/wasm.rs index c2bf27a66..16e179aa3 100644 --- a/cw-orch/src/osmosis_test_tube/queriers/wasm.rs +++ b/cw-orch/src/osmosis_test_tube/queriers/wasm.rs @@ -3,7 +3,7 @@ use std::{cell::RefCell, rc::Rc, str::FromStr}; use cosmrs::AccountId; use cosmwasm_std::{ from_json, instantiate2_address, to_json_vec, CanonicalAddr, CodeInfoResponse, - ContractInfoResponse, + ContractInfoResponse, HexBinary, }; use cw_orch_core::{ environment::{Querier, QuerierGetter, StateInterface, WasmQuerier}, @@ -41,7 +41,7 @@ impl QuerierGetter for OsmosisTes } impl WasmQuerier for OsmosisTestTubeWasmQuerier { - fn code_id_hash(&self, code_id: u64) -> Result { + fn code_id_hash(&self, code_id: u64) -> Result { let code_info_result: QueryCodeResponse = self .app .borrow() @@ -51,12 +51,11 @@ impl WasmQuerier for OsmosisTestTubeWasmQuerier { ) .map_err(map_err)?; - Ok(hex::encode( - code_info_result - .code_info - .ok_or(CwEnvError::CodeIdNotInStore(code_id.to_string()))? - .data_hash, - )) + Ok(code_info_result + .code_info + .ok_or(CwEnvError::CodeIdNotInStore(code_id.to_string()))? + .data_hash + .into()) } fn contract_info( @@ -170,7 +169,7 @@ impl WasmQuerier for OsmosisTestTubeWasmQuerier { let prefix = account_id.prefix(); let canon = account_id.to_bytes(); let addr = - instantiate2_address(checksum.as_bytes(), &CanonicalAddr(canon.into()), &salt).unwrap(); + instantiate2_address(checksum.as_slice(), &CanonicalAddr(canon.into()), &salt).unwrap(); Ok(AccountId::new(prefix, &addr.0).unwrap().to_string()) } diff --git a/packages/cw-orch-core/Cargo.toml b/packages/cw-orch-core/Cargo.toml index e8a751d4c..bcecc9400 100644 --- a/packages/cw-orch-core/Cargo.toml +++ b/packages/cw-orch-core/Cargo.toml @@ -30,7 +30,7 @@ serde = { workspace = true } cw-multi-test = { workspace = true } log = { workspace = true } -sha256 = { workspace = true } +sha2 = { workspace = true } anyhow = { workspace = true } serde_json = { workspace = true } diff --git a/packages/cw-orch-core/src/contract/paths.rs b/packages/cw-orch-core/src/contract/paths.rs index 5042aa28f..e85249a48 100644 --- a/packages/cw-orch-core/src/contract/paths.rs +++ b/packages/cw-orch-core/src/contract/paths.rs @@ -4,9 +4,12 @@ pub use wasm_path::WasmPath; mod wasm_path { use crate::error::CwEnvError; - use cosmwasm_std::ensure_eq; - use sha256::TrySha256Digest; - use std::path::{Path, PathBuf}; + use cosmwasm_std::{ensure_eq, HexBinary}; + use sha2::{Digest, Sha256}; + use std::{ + io::Read, + path::{Path, PathBuf}, + }; /// Direct path to a `.wasm` file /// Stored as `PathBuf` to avoid lifetimes. @@ -48,9 +51,12 @@ mod wasm_path { } /// Calculate the checksum of the WASM file. - pub fn checksum(&self) -> Result { - let checksum = self.path().digest()?; - Ok(checksum) + pub fn checksum(&self) -> Result { + let mut file = std::fs::File::open(self.path())?; + let mut wasm = Vec::::new(); + file.read_to_end(&mut wasm)?; + let checksum: [u8; 32] = Sha256::digest(wasm).into(); + Ok(checksum.into()) } } } diff --git a/packages/cw-orch-core/src/environment/queriers/wasm.rs b/packages/cw-orch-core/src/environment/queriers/wasm.rs index de593d23e..d7b5cd138 100644 --- a/packages/cw-orch-core/src/environment/queriers/wasm.rs +++ b/packages/cw-orch-core/src/environment/queriers/wasm.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::{CodeInfoResponse, ContractInfoResponse}; +use cosmwasm_std::{CodeInfoResponse, ContractInfoResponse, HexBinary}; use serde::{de::DeserializeOwned, Serialize}; use crate::{ @@ -10,7 +10,7 @@ use crate::{ use super::{Querier, QueryHandler}; pub trait WasmQuerier: Querier { - fn code_id_hash(&self, code_id: u64) -> Result; + fn code_id_hash(&self, code_id: u64) -> Result; /// Query contract info fn contract_info( @@ -37,7 +37,7 @@ pub trait WasmQuerier: Querier { /// Returns the checksum of the WASM file if the env supports it. Will re-upload every time if not supported. fn local_hash>( contract: &T, - ) -> Result { + ) -> Result { contract.wasm().checksum() } diff --git a/packages/cw-orch-mock/Cargo.toml b/packages/cw-orch-mock/Cargo.toml index 42f7fef80..f039ce50d 100644 --- a/packages/cw-orch-mock/Cargo.toml +++ b/packages/cw-orch-mock/Cargo.toml @@ -15,7 +15,7 @@ cosmwasm-std = { workspace = true } cw-multi-test = { workspace = true } cw-utils = { workspace = true } serde = { workspace = true } -sha256 = { workspace = true } +sha2 = { workspace = true } log.workspace = true [dev-dependencies] diff --git a/packages/cw-orch-mock/src/queriers/wasm.rs b/packages/cw-orch-mock/src/queriers/wasm.rs index 7c105dbf0..fe6974399 100644 --- a/packages/cw-orch-mock/src/queriers/wasm.rs +++ b/packages/cw-orch-mock/src/queriers/wasm.rs @@ -9,6 +9,7 @@ use cw_orch_core::{ CwEnvError, }; use serde::{de::DeserializeOwned, Serialize}; +use sha2::{Digest, Sha256}; use crate::{core::MockApp, MockBase}; @@ -34,9 +35,12 @@ impl QuerierGetter> for MockBase(querier: &MockWasmQuerier, code_id: u64) -> Result { +fn code_id_hash( + querier: &MockWasmQuerier, + code_id: u64, +) -> Result { let code_info = querier.app.borrow().wrap().query_wasm_code_info(code_id)?; - Ok(code_info.checksum.to_hex()) + Ok(code_info.checksum) } fn contract_info( @@ -53,10 +57,11 @@ fn contract_info( fn local_hash>( contract: &T, -) -> Result { +) -> Result { // We return the hashed contract-id. // This will cause the logic to never re-upload a contract if it has the same contract-id. - Ok(sha256::digest(contract.id().as_bytes())) + let hash: [u8; 32] = Sha256::digest(contract.id()).into(); + Ok(hash.into()) } fn raw_query( @@ -112,7 +117,7 @@ fn code( impl WasmQuerier for MockWasmQuerier { /// Returns the hex-encoded checksum of the code. - fn code_id_hash(&self, code_id: u64) -> Result { + fn code_id_hash(&self, code_id: u64) -> Result { code_id_hash(self, code_id) } @@ -126,7 +131,7 @@ impl WasmQuerier for MockWasmQuerier { fn local_hash>( contract: &T, - ) -> Result { + ) -> Result { local_hash(contract) } @@ -172,7 +177,7 @@ impl WasmQuerier for MockWasmQuerier { )) } else { // if bech32 mock - let checksum = HexBinary::from_hex(&self.code_id_hash(code_id)?)?; + let checksum = self.code_id_hash(code_id)?; let canon_creator = self.app.borrow().api().addr_canonicalize(&creator.into())?; let canonical_addr = instantiate2_address(checksum.as_slice(), &canon_creator, &salt)?; Ok(self