Skip to content

Commit 0324f6d

Browse files
authored
Merge pull request #14 from andrei-21/fix/check-bitcoin-prefix
Filter out TXT records not starting with `bitcoin:`
2 parents 74003b4 + dcedfa8 commit 0324f6d

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/dnssec_utils.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,34 @@ pub fn resolve_proof(dns_name: &Name, proof: Vec<u8>) -> Result<HrnResolution, &
2727
let mut result = None;
2828
for rr in resolved_rrs {
2929
if let RR::Txt(txt) = rr {
30-
if result.is_some() {
31-
return Err("Multiple TXT records existed for the HRN, which is invalid");
30+
let txt = txt.data.as_vec();
31+
if has_bitcoin_prefix(&txt) {
32+
if result.is_some() {
33+
return Err("Multiple TXT records existed for the HRN, which is invalid");
34+
}
35+
result = Some(txt);
3236
}
33-
result = Some(txt.data.as_vec());
3437
}
3538
}
36-
if let Some(res) = result {
37-
let result =
38-
String::from_utf8(res).map_err(|_| "TXT record contained an invalid string")?;
39-
Ok(HrnResolution::DNSSEC { proof: Some(proof), result })
40-
} else {
41-
Err("No validated TXT record found")
39+
let res = result.ok_or("No validated TXT record found")?;
40+
let result = String::from_utf8(res).map_err(|_| "TXT record contained an invalid string")?;
41+
Ok(HrnResolution::DNSSEC { proof: Some(proof), result })
42+
}
43+
44+
fn has_bitcoin_prefix(text: &[u8]) -> bool {
45+
const URI_PREFIX: &[u8] = b"bitcoin:";
46+
text.len() >= URI_PREFIX.len() && text[..URI_PREFIX.len()].eq_ignore_ascii_case(URI_PREFIX)
47+
}
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::has_bitcoin_prefix;
52+
53+
#[test]
54+
fn detects_expected_prefix() {
55+
assert!(has_bitcoin_prefix(b"bitcoin:bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty"));
56+
assert!(has_bitcoin_prefix(b"BiTcOiN:pay?amount=1000"));
57+
assert!(!has_bitcoin_prefix(b"lightning:lnurl"));
58+
assert!(!has_bitcoin_prefix(b"bitco"));
4259
}
4360
}

0 commit comments

Comments
 (0)