casper_rust_wasm_sdk/sdk/deploy_utils/
sign_deploy.rsuse crate::types::deploy::Deploy;
use crate::SDK;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
impl SDK {
#[wasm_bindgen(js_name = "sign_deploy")]
pub fn sign_deploy_js_alias(&self, deploy: Deploy, secret_key: &str) -> Deploy {
sign_deploy(deploy, secret_key)
}
}
impl SDK {
pub fn sign_deploy(&self, deploy: Deploy, secret_key: &str) -> Deploy {
sign_deploy(deploy, secret_key)
}
}
pub(crate) fn sign_deploy(mut deploy: Deploy, secret_key: &str) -> Deploy {
deploy.sign(secret_key)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
helpers::public_key_from_secret_key,
types::deploy_params::{
deploy_str_params::DeployStrParams, payment_str_params::PaymentStrParams,
session_str_params::SessionStrParams,
},
};
use sdk_tests::{
config::{ENTRYPOINT_MINT, PAYMENT_AMOUNT},
tests::helpers::{get_network_constants, get_user_secret_key},
};
use serde_json::Value;
#[tokio::test]
async fn test_sign_deploy_with_valid_params() {
let sdk = SDK::new(None, None);
let (_, _, chain_name) = get_network_constants();
let secret_key = get_user_secret_key(None).unwrap();
let account = public_key_from_secret_key(&secret_key).unwrap();
let deploy_params = DeployStrParams::new(&chain_name, &account, None, None, None);
let session_params = SessionStrParams::default();
session_params.set_session_hash(
"hash-cfa781f5eb69c3eee952c2944ce9670a049f88c5e46b83fb5881ebe13fb98e6d",
);
session_params.set_session_entry_point(ENTRYPOINT_MINT);
let payment_params = PaymentStrParams::default();
payment_params.set_payment_amount(PAYMENT_AMOUNT);
let deploy = sdk
.make_deploy(deploy_params, session_params, payment_params)
.unwrap();
let signed_deploy = sdk.sign_deploy(deploy, &secret_key);
let parsed_json: Value =
serde_json::from_str(&signed_deploy.to_json_string().unwrap()).unwrap();
let cl_value_as_value = &parsed_json["approvals"][0]["signer"];
assert_eq!(*cl_value_as_value, Value::String(account.to_string()));
let cl_value_as_value = &parsed_json["approvals"][0]["signature"];
assert!(cl_value_as_value.is_string());
}
#[tokio::test]
async fn test_sign_deploy_with_invalid_signature() {
let sdk = SDK::new(None, None);
let (_, _, chain_name) = get_network_constants();
let secret_key = get_user_secret_key(None).unwrap();
let account = public_key_from_secret_key(&secret_key).unwrap();
let deploy_params = DeployStrParams::new(&chain_name, &account, None, None, None);
let session_params = SessionStrParams::default();
session_params.set_session_hash(
"hash-cfa781f5eb69c3eee952c2944ce9670a049f88c5e46b83fb5881ebe13fb98e6d",
);
session_params.set_session_entry_point(ENTRYPOINT_MINT);
let payment_params = PaymentStrParams::default();
payment_params.set_payment_amount(PAYMENT_AMOUNT);
let deploy = sdk
.make_deploy(deploy_params, session_params, payment_params)
.unwrap();
let signed_deploy = sdk.sign_deploy(deploy, "test_wrong_signature");
let parsed_json: Value =
serde_json::from_str(&signed_deploy.to_json_string().unwrap()).unwrap();
let cl_value_as_value = &parsed_json["approvals"][0]["signer"];
assert_eq!(*cl_value_as_value, Value::Null);
}
}