intent.rs (2960B)
1 #![cfg(feature = "intent")] 2 use core::num::NonZeroU64; 3 use radroots_domain::{OperationId, PublicKey}; 4 use radroots_protocol::intent::{ArtifactRole, IntentBinding}; 5 6 #[test] 7 fn exact_v1_commitments_match_independent_ecmascript_vectors() { 8 let vectors: serde_json::Value = 9 serde_json::from_str(include_str!("fixtures/exact_intent.v1.json")).unwrap(); 10 for row in vectors["vectors"].as_array().unwrap() { 11 let role = match row["role"].as_str().unwrap() { 12 "public_event" => ArtifactRole::PublicEvent, 13 "business_seal" => ArtifactRole::BusinessSeal, 14 "gift_wrap" => ArtifactRole::GiftWrap, 15 _ => unreachable!(), 16 }; 17 let peer = row["recipient"] 18 .as_str() 19 .map(|value| PublicKey::from_hex(value).unwrap()); 20 let binding = IntentBinding::from_components( 21 OperationId::from_bytes([0x11; 32]), 22 [0x22; 32], 23 [0x33; 32], 24 role, 25 peer, 26 NonZeroU64::new(row["deadline"].as_str().unwrap().parse().unwrap()).unwrap(), 27 ) 28 .unwrap(); 29 let expected: OperationId = row["digest"].as_str().unwrap().parse().unwrap(); 30 assert_eq!(binding.digest(), expected.as_bytes()); 31 assert_eq!(binding.operation_id().as_bytes(), &[0x11; 32]); 32 assert_eq!(binding.command_digest(), &[0x22; 32]); 33 for (operation, command, plan) in [ 34 (OperationId::from_bytes([0x12; 32]), [0x22; 32], [0x33; 32]), 35 (OperationId::from_bytes([0x11; 32]), [0x23; 32], [0x33; 32]), 36 (OperationId::from_bytes([0x11; 32]), [0x22; 32], [0x34; 32]), 37 ] { 38 assert_ne!( 39 IntentBinding::from_components( 40 operation, 41 command, 42 plan, 43 role, 44 peer, 45 NonZeroU64::new(row["deadline"].as_str().unwrap().parse().unwrap()).unwrap() 46 ) 47 .unwrap(), 48 binding 49 ); 50 } 51 } 52 } 53 #[test] 54 fn binding_data_rejects_invalid_role_recipient_and_time_without_authority() { 55 let peer = 56 PublicKey::from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798") 57 .unwrap(); 58 for (role, recipient, deadline) in [ 59 (ArtifactRole::PublicEvent, Some(peer), 1), 60 (ArtifactRole::BusinessSeal, None, 1), 61 (ArtifactRole::GiftWrap, None, 1), 62 (ArtifactRole::BusinessSeal, Some(peer), 253_402_300_800_000), 63 ] { 64 let error = IntentBinding::from_components( 65 OperationId::from_bytes([1; 32]), 66 [2; 32], 67 [3; 32], 68 role, 69 recipient, 70 NonZeroU64::new(deadline).unwrap(), 71 ) 72 .unwrap_err(); 73 assert_eq!(error.to_string(), "invalid exact intent binding"); 74 assert!(core::error::Error::source(&error).is_none()); 75 } 76 }