casper_rust_wasm_sdk/sdk/transaction_utils/
sign_transaction.rs1use crate::{types::transaction::Transaction, SDK};
2#[cfg(target_arch = "wasm32")]
3use wasm_bindgen::prelude::*;
4
5#[cfg(target_arch = "wasm32")]
7#[wasm_bindgen]
8impl SDK {
9 #[wasm_bindgen(js_name = "sign_transaction")]
20 pub fn sign_transaction_js_alias(
21 &self,
22 transaction: Transaction,
23 secret_key: &str,
24 ) -> Transaction {
25 sign_transaction(transaction, secret_key)
26 }
27}
28
29impl SDK {
30 pub fn sign_transaction(&self, transaction: Transaction, secret_key: &str) -> Transaction {
41 sign_transaction(transaction, secret_key)
42 }
43}
44
45pub(crate) fn sign_transaction(mut transaction: Transaction, secret_key: &str) -> Transaction {
47 transaction.sign(secret_key)
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54 use crate::{
55 helpers::public_key_from_secret_key,
56 types::{
57 addr::entity_addr::EntityAddr,
58 transaction_params::{
59 transaction_builder_params::TransactionBuilderParams,
60 transaction_str_params::TransactionStrParams,
61 },
62 },
63 };
64 use sdk_tests::{
65 config::{ENTRYPOINT_MINT, PAYMENT_AMOUNT},
66 tests::helpers::{get_network_constants, get_user_secret_key},
67 };
68
69 #[tokio::test]
70 async fn test_sign_transaction_with_valid_params() {
71 let sdk = SDK::new(None, None, None);
73 let (_, _, _, _, chain_name) = get_network_constants();
74
75 let secret_key = get_user_secret_key(None).unwrap();
76 let initiator_addr = public_key_from_secret_key(&secret_key).unwrap();
77
78 let transaction_params = TransactionStrParams::default();
79 transaction_params.set_secret_key(&secret_key);
80 transaction_params.set_chain_name(&chain_name);
81 transaction_params.set_payment_amount(PAYMENT_AMOUNT);
82
83 let entity_addr = EntityAddr::from_formatted_str(
84 "entity-contract-cfa781f5eb69c3eee952c2944ce9670a049f88c5e46b83fb5881ebe13fb98e6d",
85 )
86 .unwrap();
87
88 let builder_params =
89 TransactionBuilderParams::new_invocable_entity(entity_addr.into(), ENTRYPOINT_MINT);
90
91 let transaction = sdk
93 .make_transaction(builder_params, transaction_params)
94 .unwrap();
95
96 let signed_transaction = sdk.sign_transaction(transaction, &secret_key);
97
98 assert!(signed_transaction.verify());
100 assert!(!signed_transaction
102 .compute_approvals_hash()
103 .unwrap()
104 .to_string()
105 .is_empty());
106 assert_eq!(signed_transaction.initiator_addr(), initiator_addr);
107 }
108
109 #[tokio::test]
110 async fn test_sign_transaction_with_invalid_signature() {
111 let sdk = SDK::new(None, None, None);
113 let (_, _, _, _, chain_name) = get_network_constants();
114
115 let secret_key = get_user_secret_key(None).unwrap();
116 let initiator_addr = public_key_from_secret_key(&secret_key).unwrap();
117
118 let transaction_params = TransactionStrParams::default();
119 transaction_params.set_secret_key(&secret_key);
120 transaction_params.set_chain_name(&chain_name);
121 transaction_params.set_payment_amount(PAYMENT_AMOUNT);
122
123 let entity_addr = EntityAddr::from_formatted_str(
124 "entity-contract-cfa781f5eb69c3eee952c2944ce9670a049f88c5e46b83fb5881ebe13fb98e6d",
125 )
126 .unwrap();
127
128 let builder_params =
129 TransactionBuilderParams::new_invocable_entity(entity_addr.into(), ENTRYPOINT_MINT);
130
131 let transaction = sdk
133 .make_transaction(builder_params, transaction_params)
134 .unwrap();
135
136 let signed_transaction = sdk.sign_transaction(transaction, "test_wrong_signature");
137
138 assert!(signed_transaction.verify());
141 assert!(!signed_transaction
142 .compute_approvals_hash()
143 .unwrap()
144 .to_string()
145 .is_empty());
146 assert_eq!(signed_transaction.initiator_addr(), initiator_addr);
147 }
148}