coinbase: lossy to latin

This commit is contained in:
nym21
2026-04-01 21:54:02 +02:00
parent d4936d889a
commit 83edef4806
2 changed files with 19 additions and 5 deletions

View File

@@ -463,7 +463,7 @@ impl Query {
let coinbase_signature_ascii = tx
.input
.first()
.map(|input| String::from_utf8_lossy(input.script_sig.as_bytes()).to_string())
.map(|input| input.script_sig.as_bytes().iter().map(|&b| b as char).collect::<String>())
.unwrap_or_default();
let coinbase_addresses: Vec<String> = tx

View File

@@ -33,11 +33,11 @@ impl Bytes for CoinbaseTag {
}
impl CoinbaseTag {
/// Returns the tag as a UTF-8 string (lossy).
/// Returns the tag as a string, decoding each byte as its latin-1/Unicode codepoint.
#[inline]
pub fn as_str(&self) -> std::borrow::Cow<'_, str> {
pub fn as_str(&self) -> String {
let len = (self.0[0] as usize).min(100);
String::from_utf8_lossy(&self.0[1..1 + len])
self.0[1..1 + len].iter().map(|&b| b as char).collect()
}
}
@@ -73,7 +73,21 @@ impl Formattable for CoinbaseTag {
fn fmt_json(&self, buf: &mut Vec<u8>) {
buf.push(b'"');
buf.extend_from_slice(self.as_str().as_bytes());
for &b in self.as_str().as_bytes() {
match b {
b'"' => buf.extend_from_slice(b"\\\""),
b'\\' => buf.extend_from_slice(b"\\\\"),
b'\n' => buf.extend_from_slice(b"\\n"),
b'\r' => buf.extend_from_slice(b"\\r"),
b'\t' => buf.extend_from_slice(b"\\t"),
0x00..=0x1f => {
buf.extend_from_slice(b"\\u00");
buf.push(b"0123456789abcdef"[(b >> 4) as usize]);
buf.push(b"0123456789abcdef"[(b & 0xf) as usize]);
}
_ => buf.push(b),
}
}
buf.push(b'"');
}
}