-
Notifications
You must be signed in to change notification settings - Fork 8
Support base64 input too #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
77f984d
f03929f
b6117dc
8eb95ec
2152c88
a76635e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ solana-sdk = "2.0.3" | |
| solana-program = "2.0.3" | ||
| bincode = "1.3.3" | ||
| hex = "0.4.3" | ||
| base64 = "0.13.0" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,40 +7,60 @@ use crate::solana::structs::SolanaParsedTransactionPayload; | |
|
|
||
| fn main() { | ||
| let args: Vec<String> = env::args().collect(); | ||
| if args.len() != 4 { | ||
| println!("Usage: `cargo run parse <unsigned transaction> --message OR cargo run parse <unsinged transaction> --transaction`"); | ||
|
|
||
| if !(args.len() == 4 || args.len() == 6) { | ||
| println!("Usage: `cargo run parse <unsigned transaction> --message OR cargo run parse <unsigned transaction> --transaction`. You can optionally include the format flag (base64 or hex) after the flag."); | ||
| return; | ||
| } | ||
|
|
||
| let command = &args[1]; | ||
| match command.as_str() { | ||
| "parse" => { | ||
| let unsigned_tx = &args[3]; | ||
| let flag = if args.len() > 3 { Some(&args[2]) } else { None }; | ||
| match flag { | ||
| Some(flag) if flag == "--message" || flag == "--transaction" => { | ||
| let is_transaction = flag == "--transaction"; | ||
| let result = parse_transaction(unsigned_tx.to_string(), is_transaction); | ||
| match result { | ||
| Ok(response) => { | ||
| print_parsed_transaction(response.solana_parsed_transaction.payload.unwrap()); | ||
| }, | ||
| Err(e) => println!("Error: {}", e), | ||
| match command.as_str() { | ||
| "parse" => { | ||
| let unsigned_tx = &args[3]; | ||
| let flag = if args.len() > 3 { Some(&args[2]) } else { None }; | ||
| println!("Parsing transaction: {}", unsigned_tx); | ||
|
||
| println!("Flag: {:?}", flag); | ||
| let format_flag = if args.len() > 4 { | ||
| Some(args[5].as_str()) | ||
| } else { | ||
| Some("hex") | ||
| }; // Default to "hex" | ||
| println!("Format Flag: {:?}", format_flag); | ||
| match (flag, format_flag) { | ||
| (Some(flag), Some(format_flag)) | ||
| if (flag == "--message" || flag == "--transaction") | ||
| && (format_flag == "base64" || format_flag == "hex") => | ||
| { | ||
| let is_transaction = flag == "--transaction"; | ||
| let result = parse_transaction( | ||
| unsigned_tx.to_string(), | ||
| is_transaction, | ||
| format_flag.to_string(), | ||
| ); | ||
| match result { | ||
| Ok(response) => { | ||
| print_parsed_transaction( | ||
| response.solana_parsed_transaction.payload.unwrap(), | ||
| ); | ||
| } | ||
| } | ||
| _ => { | ||
| println!("Invalid or missing flag. Use either --message or --transaction."); | ||
| Err(e) => println!("Error: {}", e), | ||
| } | ||
| } | ||
| _ => { | ||
| println!("Invalid or missing flag. Use either --message or --transaction followed by format flag (base64 or hex)."); | ||
| } | ||
| } | ||
| _ => println!("Unknown command: {}", command), | ||
| } | ||
| _ => println!("Unknown command: {}", command), | ||
| } | ||
| } | ||
|
|
||
| fn print_parsed_transaction(transaction_payload: SolanaParsedTransactionPayload) { | ||
| println!("Solana Parsed Transaction Payload:"); | ||
| println!(" Unsigned Payload: {}", transaction_payload.unsigned_payload); | ||
| println!( | ||
|
||
| " Unsigned Payload: {}", | ||
| transaction_payload.unsigned_payload | ||
| ); | ||
| if let Some(metadata) = transaction_payload.transaction_metadata { | ||
| println!(" Transaction Metadata:"); | ||
| println!(" Signatures: {:?}", metadata.signatures); | ||
|
|
@@ -52,8 +72,14 @@ fn print_parsed_transaction(transaction_payload: SolanaParsedTransactionPayload) | |
| println!(" Instruction {}:", i + 1); | ||
| println!(" Program Key: {}", instruction.program_key); | ||
| println!(" Accounts: {:?}", instruction.accounts); | ||
| println!(" Instruction Data (hex): {}", instruction.instruction_data_hex); | ||
| println!(" Address Table Lookups: {:?}", instruction.address_table_lookups); | ||
| println!( | ||
| " Instruction Data (hex): {}", | ||
| instruction.instruction_data_hex | ||
| ); | ||
| println!( | ||
| " Address Table Lookups: {:?}", | ||
| instruction.address_table_lookups | ||
| ); | ||
| } | ||
| println!(" Transfers:"); | ||
| for (i, transfer) in metadata.transfers.iter().enumerate() { | ||
|
|
@@ -82,6 +108,9 @@ fn print_parsed_transaction(transaction_payload: SolanaParsedTransactionPayload) | |
| println!(" Fee: {}", fee); | ||
| } | ||
| } | ||
| println!(" Address Table Lookups: {:?}", metadata.address_table_lookups); | ||
| println!( | ||
| " Address Table Lookups: {:?}", | ||
| metadata.address_table_lookups | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now with the introduction of
format flag, I'd like to rename thisflagto be a bit more specific.As I was thinking about it, I was thinking that
--messageand--transactionare both formats, and--hexand--base64are types of encodings. So maybe we can rename these variables as such?format_flagandencoding_flagrespectivelyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason not to use
clapfor more consistent parsing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't a
messagea component of a transaction? I'd call something aformatif it's representation of same thing. I agree on encodingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you feel strongly about not adding dependency or have a lighterweight suggestion I can use that, but this is much cleaner to read with clap than manually accounting for indices b6117dc