Skip to content

Commit 9d5b843

Browse files
Add program checksum to deploy process
1 parent 201e581 commit 9d5b843

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ version = "1.8"
8888

8989
[dependencies.reqwest]
9090
version = "0.11.18"
91+
features = [ "json" ]
9192

9293
[dependencies.serde]
9394
version = "1.0.183"

wasm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ pub use types::{Field, Group, Scalar};
176176
mod thread_pool;
177177

178178
mod utilities;
179-
pub use utilities::EncryptionToolkit;
180179
#[cfg(test)]
181180
pub use utilities::test;
181+
pub use utilities::{EncryptionToolkit, get, get_network, latest_block_height};
182182

183183
#[cfg(test)]
184184
mod thread_pool {

wasm/src/programs/manager/deploy.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ use crate::{
2222
RecordPlaintext,
2323
Transaction,
2424
execute_fee,
25+
latest_block_height,
2526
log,
2627
types::native::{
28+
AddressNative,
2729
CurrentAleo,
2830
CurrentNetwork,
31+
PrivateKeyNative,
2932
ProcessNative,
3033
ProgramIDNative,
3134
ProgramNative,
@@ -34,6 +37,7 @@ use crate::{
3437
TransactionNative,
3538
},
3639
};
40+
use snarkvm_console::prelude::{ConsensusVersion, Network};
3741

3842
use js_sys::Object;
3943
use rand::{SeedableRng, rngs::StdRng};
@@ -84,11 +88,24 @@ impl ProgramManager {
8488

8589
log("Creating deployment");
8690
let node_url = url.as_deref().unwrap_or(DEFAULT_URL);
87-
let deployment = process.deploy::<CurrentAleo, _>(&program, rng).map_err(|err| err.to_string())?;
91+
let mut deployment = process.deploy::<CurrentAleo, _>(&program, rng).map_err(|err| err.to_string())?;
8892
if deployment.program().functions().is_empty() {
8993
return Err("Attempted to create an empty transaction deployment".to_string());
9094
}
9195

96+
log("Setting program checksum and owner");
97+
let latest_height = latest_block_height(node_url).await?;
98+
let consensus_version = CurrentNetwork::CONSENSUS_VERSION(latest_height).map_err(|err| err.to_string())?;
99+
let private_key_native = PrivateKeyNative::from(private_key);
100+
if consensus_version < ConsensusVersion::V9 {
101+
deployment.set_program_checksum_raw(None);
102+
deployment.set_program_owner_raw(None)
103+
} else {
104+
deployment.set_program_checksum_raw(Some(deployment.program().to_checksum()));
105+
deployment
106+
.set_program_owner_raw(Some(AddressNative::try_from(private_key_native).map_err(|e| e.to_string())?));
107+
}
108+
92109
log("Ensuring the fee is sufficient to pay for the deployment");
93110
let (minimum_deployment_cost, (_, _, _, _)) =
94111
deployment_cost::<CurrentNetwork>(&process, &deployment).map_err(|err| err.to_string())?;

wasm/src/utilities/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ pub mod test;
2121

2222
pub mod encrypt;
2323
pub use encrypt::EncryptionToolkit;
24+
25+
pub mod network;
26+
pub use network::{get, get_network, latest_block_height};

wasm/src/utilities/network.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (C) 2019-2025 Provable Inc.
2+
// This file is part of the Provable SDK library.
3+
4+
// The Provable SDK library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// The Provable SDK library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with the Provable SDK library. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use crate::types::native::CurrentNetwork;
18+
19+
use snarkvm_console::network::Network;
20+
21+
/// Get the current network name.
22+
pub fn get_network() -> &'static str {
23+
match CurrentNetwork::ID {
24+
snarkvm_console::network::MainnetV0::ID => "mainnet",
25+
snarkvm_console::network::TestnetV0::ID => "testnet",
26+
snarkvm_console::network::CanaryV0::ID => "canary",
27+
_ => panic!("Invalid network"),
28+
}
29+
}
30+
31+
/// Get the latest block height.
32+
pub async fn latest_block_height(base_url: &str) -> Result<u32, String> {
33+
let url = format!("{base_url}/{}/block/height/latest", get_network());
34+
let res = get(&url).await?;
35+
Ok(res)
36+
}
37+
38+
/// Make a GET request to the service.
39+
pub async fn get<T>(url: &str) -> Result<T, String>
40+
where
41+
T: serde::de::DeserializeOwned,
42+
{
43+
let client = reqwest::Client::new();
44+
let res = client.get(url).send().await.map_err(|e| e.to_string())?.json().await.map_err(|e| e.to_string())?;
45+
Ok(res)
46+
}

0 commit comments

Comments
 (0)