Skip to content

Commit 17359a4

Browse files
committed
test(deps): pin compute_script_hash and hash_ip_with_salt with hardcoded expected values
Add test_compute_script_hash_p2pkh (calls compute_script_hash directly, asserts hardcoded FullHash bytes for a P2PKH scriptPubKey) and test_hash_ip_with_salt (extracts hash_ip_with_salt to a free function, tests known salt+ip → hex). These tests establish the behavioral contract before the rust-crypto dep swap, so the following commit can prove the new implementation is bit-for-bit identical.
1 parent 5b3ff8f commit 17359a4

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

src/electrum/server.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ struct Connection {
115115
salt: String,
116116
}
117117

118+
fn hash_ip_with_salt(salt: &str, ip: &str) -> String {
119+
let mut hasher: Sha256 = Sha256::new();
120+
hasher.input(salt.as_bytes());
121+
hasher.input(ip.as_bytes());
122+
hasher.result_str()
123+
}
124+
118125
impl Connection {
119126
pub fn new(
120127
query: Arc<Query>,
@@ -527,17 +534,10 @@ impl Connection {
527534
Ok(result)
528535
}
529536

530-
fn hash_ip_with_salt(&self, ip: &str) -> String {
531-
let mut hasher = Sha256::new();
532-
hasher.input(self.salt.as_bytes());
533-
hasher.input(ip.as_bytes());
534-
hasher.result_str()
535-
}
536-
537537
fn log_rpc_event(&self, mut log: Value) {
538538
let real_ip = self.addr.ip().to_string();
539539
let ip_to_log = if self.rpc_logging.anonymize_ip {
540-
self.hash_ip_with_salt(&real_ip)
540+
hash_ip_with_salt(&self.salt, &real_ip)
541541
} else {
542542
real_ip
543543
};
@@ -932,3 +932,18 @@ impl Drop for RPC {
932932
trace!("RPC server is stopped");
933933
}
934934
}
935+
936+
#[cfg(test)]
937+
mod tests {
938+
use super::*;
939+
940+
#[test]
941+
fn test_hash_ip_with_salt() {
942+
// SHA-256("test_salt" || "127.0.0.1")
943+
let result = hash_ip_with_salt("test_salt", "127.0.0.1");
944+
assert_eq!(
945+
result,
946+
"d474826bbd126d38bdfb1e61bf727a2d9a306ea1645071faf2638cc3891a2b30"
947+
);
948+
}
949+
}

src/new_index/schema.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,3 +1978,23 @@ pub mod bench {
19781978
super::add_blocks(&[data.block_entry.clone()], &data.iconfig)
19791979
}
19801980
}
1981+
1982+
#[cfg(test)]
1983+
mod tests {
1984+
use super::*;
1985+
1986+
#[test]
1987+
fn test_compute_script_hash_p2pkh() {
1988+
// P2PKH scriptPubKey for address 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
1989+
// OP_DUP OP_HASH160 <20-byte-hash> OP_EQUALVERIFY OP_CHECKSIG
1990+
let script: Script = Vec::from_hex("76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac")
1991+
.unwrap()
1992+
.into();
1993+
let expected: FullHash = [
1994+
0x61, 0x91, 0xc3, 0xb5, 0x90, 0xbf, 0xcf, 0xa0, 0x47, 0x5e, 0x87, 0x7c, 0x30, 0x2d,
1995+
0xa1, 0xe3, 0x23, 0x49, 0x7a, 0xcf, 0x3b, 0x42, 0xc0, 0x8d, 0x8f, 0xa2, 0x8e, 0x36,
1996+
0x4e, 0xdf, 0x01, 0x8b,
1997+
];
1998+
assert_eq!(compute_script_hash(&script), expected);
1999+
}
2000+
}

0 commit comments

Comments
 (0)