diff --git a/CHANGELOG.md b/CHANGELOG.md index 46046b916..e143e16ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### New - ChaCha20 encryption support `crypto.chacha20`. - `boc.parse_shardstate` function for shardstates parsing. +- `boc.get_boc_hash` function for calculating BOC root hash - `client.build_info` fully defined and documented. - `processing.wait_for_transaction` and `processing.process_message` functions execute contract locally in case if transaction waiting fails in order to resolve the contract execution error diff --git a/ton_client/client/src/boc/hash.rs b/ton_client/client/src/boc/hash.rs new file mode 100644 index 000000000..1a12f509d --- /dev/null +++ b/ton_client/client/src/boc/hash.rs @@ -0,0 +1,41 @@ +/* +* Copyright 2018-2020 TON DEV SOLUTIONS LTD. +* +* Licensed under the SOFTWARE EVALUATION License (the "License"); you may not use +* this file except in compliance with the License. +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific TON DEV software governing permissions and +* limitations under the License. +*/ + +use crate::boc::internal::deserialize_cell_from_base64; +use crate::client::ClientContext; +use crate::error::ClientResult; + +#[derive(Serialize, Deserialize, Clone, ApiType)] +pub struct ParamsOfGetBocHash { + /// BOC encoded as base64 + pub boc: String, +} + +#[derive(Serialize, Deserialize, Clone, ApiType)] +pub struct ResultOfGetBocHash { + /// BOC root hash encoded with hex + pub hash: String, +} + +/// Calculates BOC root hash +#[api_function] +pub fn get_boc_hash( + _context: std::sync::Arc, + params: ParamsOfGetBocHash, +) -> ClientResult { + let (_, cell) = deserialize_cell_from_base64(¶ms.boc, "")?; + + Ok(ResultOfGetBocHash { + hash: cell.repr_hash().to_hex_string() + }) +} diff --git a/ton_client/client/src/boc/internal.rs b/ton_client/client/src/boc/internal.rs index c66cd5e91..85fad868e 100644 --- a/ton_client/client/src/boc/internal.rs +++ b/ton_client/client/src/boc/internal.rs @@ -1,3 +1,16 @@ +/* +* Copyright 2018-2020 TON DEV SOLUTIONS LTD. +* +* Licensed under the SOFTWARE EVALUATION License (the "License"); you may not use +* this file except in compliance with the License. +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific TON DEV software governing permissions and +* limitations under the License. +*/ + use crate::boc::Error; use crate::error::ClientResult; use ton_block::{Deserializable, Serializable}; diff --git a/ton_client/client/src/boc/mod.rs b/ton_client/client/src/boc/mod.rs index 55e809d2e..1a702471f 100644 --- a/ton_client/client/src/boc/mod.rs +++ b/ton_client/client/src/boc/mod.rs @@ -14,9 +14,11 @@ pub(crate) mod blockchain_config; mod errors; pub(crate) mod parse; +pub(crate) mod internal; +pub(crate) mod hash; + #[cfg(test)] mod tests; -pub(crate) mod internal; pub use crate::boc::parse::{ source_boc, required_boc, @@ -26,4 +28,7 @@ pub use crate::boc::parse::{ pub use blockchain_config::{ get_blockchain_config, ParamsOfGetBlockchainConfig, ResultOfGetBlockchainConfig, }; +pub use hash::{ + get_boc_hash, ParamsOfGetBocHash, ResultOfGetBocHash +}; pub use errors::{Error, ErrorCode}; diff --git a/ton_client/client/src/boc/tests.rs b/ton_client/client/src/boc/tests.rs index e87d01e78..0ee3b0919 100644 --- a/ton_client/client/src/boc/tests.rs +++ b/ton_client/client/src/boc/tests.rs @@ -16,6 +16,23 @@ use crate::boc::{ParamsOfParse, ParamsOfParseShardstate, ResultOfParse}; use crate::tests::TestClient; use pretty_assertions::assert_eq; +#[test] +fn get_boc_hash() { + let client = TestClient::new(); + + let result: super::ResultOfGetBocHash = client.request( + "boc.get_boc_hash", + super::ParamsOfGetBocHash { + boc: String::from("te6ccgEBAQEAWAAAq2n+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE/zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzSsG8DgAAAAAjuOu9NAL7BxYpA") + } + ).unwrap(); + + assert_eq!( + result.hash, + "dfd47194f3058ee058bfbfad3ea40cbbd9ad17ca77cd0904d4d9f18a48c2fbca" + ); +} + #[test] fn parse_message() { let client = TestClient::new(); diff --git a/ton_client/client/src/json_interface/modules.rs b/ton_client/client/src/json_interface/modules.rs index 2f9d40f98..9762accc6 100644 --- a/ton_client/client/src/json_interface/modules.rs +++ b/ton_client/client/src/json_interface/modules.rs @@ -267,6 +267,7 @@ fn register_boc(handlers: &mut RuntimeHandlers) { crate::boc::get_blockchain_config, crate::boc::blockchain_config::get_blockchain_config_api, ); + module.register_sync_fn(crate::boc::get_boc_hash, crate::boc::hash::get_boc_hash_api); module.register(); }