Skip to content

Commit

Permalink
Hex encode chunk output before writing to terminal. Remove output san…
Browse files Browse the repository at this point in the history
…itization
  • Loading branch information
Marzi authored and 8marz8 committed Apr 16, 2024
1 parent 9886227 commit fbe90ed
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 38 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion stacks-signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
wsts = { workspace = true }
rand = { workspace = true }
url = "2.1.0"
regex = "1.10.3"

[dev-dependencies]
clarity = { path = "../clarity", features = ["testing"] }
Expand Down
6 changes: 3 additions & 3 deletions stacks-signer/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ pub struct Cli {
/// Subcommands for the stacks signer binary
#[derive(clap::Subcommand, Debug)]
pub enum Command {
/// Get a chunk from the stacker-db instance
/// Get a chunk from the stacker-db instance in hex encoding
GetChunk(GetChunkArgs),
/// Get the latest chunk from the stacker-db instance
/// Get the latest chunk from the stacker-db instance in hex encoding
GetLatestChunk(GetLatestChunkArgs),
/// List chunks from the stacker-db instance
/// List chunks from the stacker-db instance in hex encoding
ListChunks(StackerDBArgs),
/// Upload a chunk to the stacker-db instance
PutChunk(PutChunkArgs),
Expand Down
40 changes: 7 additions & 33 deletions stacks-signer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ fn stackerdb_session(host: &str, contract: QualifiedContractIdentifier) -> Stack
/// Write the chunk to stdout
fn write_chunk_to_stdout(chunk_opt: Option<Vec<u8>>) {
if let Some(chunk) = chunk_opt.as_ref() {
let sanitized_chunk = sanitize_chunk(chunk);
let bytes = io::stdout().write(&sanitized_chunk).unwrap();
if bytes < sanitized_chunk.len() {
let hexed_string = to_hex(chunk);
let hexed_chunk = hexed_string.as_bytes();
let bytes = io::stdout().write(&hexed_chunk).unwrap();
if bytes < hexed_chunk.len() {
print!(
"Failed to write complete chunk to stdout. Missing {} bytes",
sanitized_chunk.len() - bytes
hexed_chunk.len() - bytes
);
}
}
Expand Down Expand Up @@ -178,8 +179,8 @@ fn handle_list_chunks(args: StackerDBArgs) {
let mut session = stackerdb_session(&args.host, args.contract);
let chunk_list = session.list_chunks().unwrap();
let chunk_list_json = serde_json::to_string(&chunk_list).unwrap();
let sanitized_output = sanitize_json_output(&chunk_list_json);
println!("{}", sanitized_output);
let hexed_json = to_hex(chunk_list_json.as_bytes());
println!("{}", hexed_json);
}

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

/// Helper function for sanitizing StackerDB chunks to ensure safe terminal output
fn sanitize_chunk(data: &[u8]) -> Vec<u8> {
let mut result = Vec::new();
let mut i = 0;
while i < data.len() {
// skipping ANSI escape sequence
if i + 1 < data.len() && data[i] == 0x1B && data[i + 1] == b'[' {
i += 2;
while i < data.len() && !(data[i] >= 0x40 && data[i] <= 0x7E) {
i += 1;
}
i += 1;
} else {
result.push(data[i]);
i += 1;
}
}
result
}

/// Helper function for sanitizing JSON String to ensure safe terminal output
fn sanitize_json_output(input: &str) -> String {
// regex to remove ANSI escape sequences
let re = regex::Regex::new(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]").unwrap();
re.replace_all(input, "").to_string()
}

fn main() {
let cli = Cli::parse();

Expand Down

0 comments on commit fbe90ed

Please sign in to comment.