Skip to content

Commit 8874fd2

Browse files
Marzi8marz8
authored andcommitted
Hex encode chunk output before writing to terminal. Remove output sanitization
1 parent aaec563 commit 8874fd2

File tree

4 files changed

+10
-38
lines changed

4 files changed

+10
-38
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stacks-signer/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
4343
wsts = { workspace = true }
4444
rand = { workspace = true }
4545
url = "2.1.0"
46-
regex = "1.10.3"
4746

4847
[dev-dependencies]
4948
clarity = { path = "../clarity", features = ["testing"] }

stacks-signer/src/cli.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ pub struct Cli {
4444
/// Subcommands for the stacks signer binary
4545
#[derive(clap::Subcommand, Debug)]
4646
pub enum Command {
47-
/// Get a chunk from the stacker-db instance
47+
/// Get a chunk from the stacker-db instance in hex encoding
4848
GetChunk(GetChunkArgs),
49-
/// Get the latest chunk from the stacker-db instance
49+
/// Get the latest chunk from the stacker-db instance in hex encoding
5050
GetLatestChunk(GetLatestChunkArgs),
51-
/// List chunks from the stacker-db instance
51+
/// List chunks from the stacker-db instance in hex encoding
5252
ListChunks(StackerDBArgs),
5353
/// Upload a chunk to the stacker-db instance
5454
PutChunk(PutChunkArgs),

stacks-signer/src/main.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ fn stackerdb_session(host: &str, contract: QualifiedContractIdentifier) -> Stack
7171
/// Write the chunk to stdout
7272
fn write_chunk_to_stdout(chunk_opt: Option<Vec<u8>>) {
7373
if let Some(chunk) = chunk_opt.as_ref() {
74-
let sanitized_chunk = sanitize_chunk(chunk);
75-
let bytes = io::stdout().write(&sanitized_chunk).unwrap();
76-
if bytes < sanitized_chunk.len() {
74+
let hexed_string = to_hex(chunk);
75+
let hexed_chunk = hexed_string.as_bytes();
76+
let bytes = io::stdout().write(&hexed_chunk).unwrap();
77+
if bytes < hexed_chunk.len() {
7778
print!(
7879
"Failed to write complete chunk to stdout. Missing {} bytes",
79-
sanitized_chunk.len() - bytes
80+
hexed_chunk.len() - bytes
8081
);
8182
}
8283
}
@@ -178,8 +179,8 @@ fn handle_list_chunks(args: StackerDBArgs) {
178179
let mut session = stackerdb_session(&args.host, args.contract);
179180
let chunk_list = session.list_chunks().unwrap();
180181
let chunk_list_json = serde_json::to_string(&chunk_list).unwrap();
181-
let sanitized_output = sanitize_json_output(&chunk_list_json);
182-
println!("{}", sanitized_output);
182+
let hexed_json = to_hex(chunk_list_json.as_bytes());
183+
println!("{}", hexed_json);
183184
}
184185

185186
fn handle_put_chunk(args: PutChunkArgs) {
@@ -369,33 +370,6 @@ fn write_file(dir: &Path, filename: &str, contents: &str) {
369370
println!("Created file: {}", filename);
370371
}
371372

372-
/// Helper function for sanitizing StackerDB chunks to ensure safe terminal output
373-
fn sanitize_chunk(data: &[u8]) -> Vec<u8> {
374-
let mut result = Vec::new();
375-
let mut i = 0;
376-
while i < data.len() {
377-
// skipping ANSI escape sequence
378-
if i + 1 < data.len() && data[i] == 0x1B && data[i + 1] == b'[' {
379-
i += 2;
380-
while i < data.len() && !(data[i] >= 0x40 && data[i] <= 0x7E) {
381-
i += 1;
382-
}
383-
i += 1;
384-
} else {
385-
result.push(data[i]);
386-
i += 1;
387-
}
388-
}
389-
result
390-
}
391-
392-
/// Helper function for sanitizing JSON String to ensure safe terminal output
393-
fn sanitize_json_output(input: &str) -> String {
394-
// regex to remove ANSI escape sequences
395-
let re = regex::Regex::new(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]").unwrap();
396-
re.replace_all(input, "").to_string()
397-
}
398-
399373
fn main() {
400374
let cli = Cli::parse();
401375

0 commit comments

Comments
 (0)