mining.rs (2197B)
1 use anyhow::Result; 2 3 use super::{TransactionSigningDomain, hex_hash, mine_signing_bytes}; 4 5 pub(super) fn mine_payload( 6 recipient: &str, 7 anchor: &str, 8 salt: u64, 9 nonce: u64, 10 difficulty_bits: u32, 11 ) -> String { 12 format!("iuna-mine:{recipient}:{anchor}:{salt}:{nonce}:{difficulty_bits}") 13 } 14 15 pub(super) fn mine_signature( 16 domain: &TransactionSigningDomain, 17 recipient: &str, 18 anchor: &str, 19 salt: u64, 20 nonce: u64, 21 difficulty_bits: u32, 22 ) -> Result<String> { 23 if domain.is_chain_bound() { 24 Ok(hex_hash(mine_signing_bytes( 25 domain, 26 recipient, 27 anchor, 28 salt, 29 nonce, 30 difficulty_bits, 31 )?)) 32 } else { 33 Ok(hex_hash(mine_payload( 34 recipient, 35 anchor, 36 salt, 37 nonce, 38 difficulty_bits, 39 ))) 40 } 41 } 42 43 #[cfg(test)] 44 mod tests { 45 use super::{mine_payload, mine_signature}; 46 use crate::domain::{TransactionSigningDomain, Wallet}; 47 48 #[test] 49 fn mine_signature_commits_binary_chain_domain() { 50 let payload = mine_payload("recipient", "anchor", 1, 2, 12); 51 52 assert_eq!(payload, "iuna-mine:recipient:anchor:1:2:12"); 53 let wallet = Wallet::from_seed("mine-signature-recipient"); 54 let domain = TransactionSigningDomain::new("test-chain", "0".repeat(64)); 55 assert_ne!( 56 mine_signature(&domain, wallet.address(), &"1".repeat(64), 1, 2, 12).unwrap(), 57 mine_signature( 58 &TransactionSigningDomain::new("other-chain", "0".repeat(64)), 59 wallet.address(), 60 &"1".repeat(64), 61 1, 62 2, 63 12, 64 ) 65 .unwrap() 66 ); 67 } 68 69 #[test] 70 fn legacy_mine_signature_keeps_the_original_payload_hash() { 71 assert_eq!( 72 mine_signature( 73 &TransactionSigningDomain::legacy(), 74 "recipient", 75 "anchor", 76 1, 77 2, 78 12, 79 ) 80 .unwrap(), 81 "47f9ad353685fdb9b4932cefa9dd1d27f8af70e27eaedf15c4f9ffbbb64300a3" 82 ); 83 } 84 }