Skip to content

Commit 650efe9

Browse files
committed
added tests
1 parent ed88ed7 commit 650efe9

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

wasm/src/ledger/transition.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ impl Transition {
207207
compute_function_id(&U16Native::new(CurrentNetwork::ID), self.0.program_id(), self.0.function_name())
208208
.map_err(|e| e.to_string())?;
209209

210-
// Create a vector that will be populated with decrypted private inputs and
211-
// non-private inputs.
210+
// Create a vector that will be populated with decrypted private inputs and
211+
// non-private inputs.
212212
let mut decrypted_inputs = Vec::with_capacity(self.0.inputs().len());
213213

214214
// Iterate over the inputs and decrypt if they are private. Non-private inputs
@@ -229,7 +229,7 @@ impl Transition {
229229
let outputs = self.0.outputs();
230230
let num_inputs = self.0.inputs().len();
231231

232-
// Create a vector that will be populated with decrypted private outputs and
232+
// Create a vector that will be populated with decrypted private outputs and
233233
// non-private outputs.
234234
let mut decrypted_outputs = Vec::with_capacity(outputs.len());
235235

@@ -259,7 +259,7 @@ impl Transition {
259259
*self.0.tcm(),
260260
*self.0.scm(),
261261
)
262-
.map_err(||"failed to construct decrypted transition".to_string()),
262+
.map_err(|_| "failed to construct decrypted transition".to_string())?,
263263
))
264264
}
265265
}
@@ -520,4 +520,40 @@ mod tests {
520520

521521
assert_eq!(decrypted_transition.to_string(), TRANSITION_MAINNET_DECRYPTED);
522522
}
523+
524+
#[wasm_bindgen_test]
525+
#[cfg(feature = "testnet")]
526+
fn test_transition_decrypt_testnet_invalid_vk() {
527+
// Create a view key from the test private key.
528+
let private_key = PrivateKey::from_str(TEST_PRIVATE_KEY).unwrap();
529+
let view_key = ViewKey::from_private_key(&private_key);
530+
531+
// Get a transition with records.
532+
let transition = Transition::from_string(TRANSITION_TESTNET).unwrap();
533+
534+
// Create an invalid transition view key.
535+
let invalid_tvk = transition.tvk(&view_key);
536+
537+
// Attempt to decrypt the transition using the invalid transition view key.
538+
let result = transition.decrypt_transition(&invalid_tvk);
539+
assert!(result.is_err());
540+
}
541+
542+
#[wasm_bindgen_test]
543+
#[cfg(feature = "mainnet")]
544+
fn test_transition_decrypt_testnet_invalid_vk() {
545+
// Create a view key from the test private key.
546+
let private_key = PrivateKey::from_str(TEST_PRIVATE_KEY).unwrap();
547+
let view_key = ViewKey::from_private_key(&private_key);
548+
549+
// Get a transition with records.
550+
let transition = Transition::from_string(TRANSITION_MAINNET).unwrap();
551+
552+
// Create an invalid transition view key.
553+
let invalid_tvk = transition.tvk(&view_key);
554+
555+
// Attempt to decrypt the transition using the invalid transition view key.
556+
let result = transition.decrypt_transition(&invalid_tvk);
557+
assert!(result.is_err());
558+
}
523559
}

wasm/src/record/record_ciphertext.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
js_array_from_fields,
2424
to_bits_array_le,
2525
types::native::{CurrentNetwork, RecordCiphertextNative},
26-
utilities::encrypt::EncryptionToolkilt,
26+
utilities::encrypt::EncryptionToolkit,
2727
};
2828
use snarkvm_console::prelude::{FromBytes, Network, ToBits, ToBytes, ToFields};
2929

@@ -233,7 +233,8 @@ mod tests {
233233
let view_key = ViewKey::from_string(OWNER_VIEW_KEY);
234234
let graph_key = GraphKey::from_view_key(&view_key);
235235
let plaintext = record.decrypt(&view_key).unwrap();
236-
assert_eq!(plaintext.to_string(), OWNER_PLAINTEXT);
236+
let owner_plaintext = RecordPlaintext::from_string(OWNER_PLAINTEXT).unwrap();
237+
assert_eq!(plaintext.to_string(), owner_plaintext.to_string());
237238
let incorrect_view_key = ViewKey::from_string(NON_OWNER_VIEW_KEY);
238239
assert!(record.decrypt(&incorrect_view_key).is_err());
239240

@@ -270,4 +271,12 @@ mod tests {
270271
let decrypted_plaintext = record.decrypt_with_record_view_key(record_vk).unwrap();
271272
assert_eq!(record_plaintext.to_string(), decrypted_plaintext.to_string());
272273
}
274+
275+
#[wasm_bindgen_test]
276+
fn test_decrypt_with_record_vk_invalid() {
277+
let record = RecordCiphertext::from_string(OWNER_CIPHERTEXT).unwrap();
278+
let view_key_non_owner = ViewKey::from_string(NON_OWNER_VIEW_KEY);
279+
let invalid_record_vk = record.record_view_key(&view_key_non_owner);
280+
assert!(record.decrypt_with_record_view_key(invalid_record_vk).is_err());
281+
}
273282
}

wasm/src/utilities/encrypt.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ mod tests {
186186
use super::*;
187187

188188
use std::str::FromStr;
189-
use wasm_bindgen_test::wasm_bindgen_test;
189+
use wasm_bindgen_test::{console_log, wasm_bindgen_test};
190190

191191
const NON_OWNER_VIEW_KEY: &str = "AViewKey1e2WyreaH5H4RBcioLL2GnxvHk5Ud46EtwycnhTdXLmXp";
192192
const OWNER_CIPHERTEXT: &str = "record1qyqsqpe2szk2wwwq56akkwx586hkndl3r8vzdwve32lm7elvphh37rsyqyxx66trwfhkxun9v35hguerqqpqzqrtjzeu6vah9x2me2exkgege824sd8x2379scspmrmtvczs0d93qttl7y92ga0k0rsexu409hu3vlehe3yxjhmey3frh2z5pxm5cmxsv4un97q";
@@ -196,7 +196,37 @@ mod tests {
196196
_nonce: 3077450429259593211617823051143573281856129402760267155982965992208217472983group.public
197197
}";
198198
const OWNER_VIEW_KEY: &str = "AViewKey1ccEt8A2Ryva5rxnKcAbn7wgTaTsb79tzkKHFpeKsm9NX";
199+
const RECORD_VIEW_KEY: &str = "4445718830394614891114647247073357094867447866913203502139893824059966201724field";
199200
const RECORD_TAG: &str = "1796466189545157638691489609907096471289658804813960182690905095269699169603field";
201+
const TRANSITION_PUBLIC_KEY: &str =
202+
"7532444547840484531569841377269810017844130178606467837628364672670182422388group";
203+
const TRANSITION_VIEW_KEY: &str =
204+
"5089075468761042335883809641276568724009791331127957254389204093712358605127field";
205+
206+
#[wasm_bindgen_test]
207+
fn test_record_view_key_generation() {
208+
let owner_view_key = ViewKey::from_str(OWNER_VIEW_KEY).unwrap();
209+
let owner_ciphertext = RecordCiphertext::from_str(OWNER_CIPHERTEXT).unwrap();
210+
211+
// Generate the record view key
212+
let record_vk = EncryptionToolkit::generate_record_view_key(&owner_view_key, &owner_ciphertext).unwrap();
213+
214+
// Verify the record view key matches the expected value
215+
assert_eq!(record_vk.to_string(), RECORD_VIEW_KEY);
216+
}
217+
218+
#[wasm_bindgen_test]
219+
fn test_tvk_generation() {
220+
let view_key = ViewKey::from_str(OWNER_VIEW_KEY).unwrap();
221+
let tpk = Group::from_string(TRANSITION_PUBLIC_KEY).unwrap();
222+
223+
// Generate the transition view key
224+
let tvk = EncryptionToolkit::generate_tvk(&view_key, &tpk);
225+
226+
// Verify the transition view key matches the expected value
227+
// console_log!("Transition View Key: {}", tvk.to_string());
228+
assert_eq!(tvk.to_string(), TRANSITION_VIEW_KEY);
229+
}
200230

201231
#[wasm_bindgen_test]
202232
fn test_decrypt_record_symmetric() {

0 commit comments

Comments
 (0)