Skip to content
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

Add utils to generate instruction_data based on name #57

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions external-crates/move/Cargo.lock

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

12 changes: 12 additions & 0 deletions external-crates/move/solana/util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "input_json"
version = "1.0.0"
edition = "2021"

[[bin]]
name = "generate_input_json"
path = "src/generate_input_json.rs"

[dependencies]
binascii = "0.1"
clap = { version = "3.1.8", features = ["derive"] }
50 changes: 50 additions & 0 deletions external-crates/move/solana/util/src/generate_input_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#[cfg(test)]
use binascii::{b32encode, b32decode};

use clap::Parser;

#[derive(Debug, Parser)]
#[clap(author, version, about)]
pub struct Args {
/// Entry function name
#[clap(short = 'f', long = "func-name")]
pub entry_function_name: String,
// Remaining arguments that we want to serialize as bytes
#[clap(short = 's', long = "args", default_value = "")]
pub argstring: String,
}

fn print_as_bytes(data : &str) -> Vec<u8> {
let length = data.len();
let input = &data[..length].as_bytes();
input.to_vec()
}

#[test]
fn test_encode_decode() {
let data : &str = "counter__owner";
let input = &data[..data.len()].as_bytes();
let mut output = [0u8; 500];
let mut dec_out = [0u8; 200];
let encoded_output = b32encode(&input, &mut output).ok().unwrap();
let decoded_output = b32decode(&encoded_output, &mut dec_out).ok().unwrap();
assert_eq!(input, &decoded_output);
}

#[test]
fn test_print_as_bytes() {
const ENCODED_DATA : [u8; 11] = [104, 101, 108, 108, 111, 95, 95, 109, 97, 105, 110];
let b = print_as_bytes("hello__main");
assert_eq!(b.len(), ENCODED_DATA.len());
assert_eq!(&ENCODED_DATA, &b[..ENCODED_DATA.len()]);
}

pub fn main() {
let args = Args::parse();
let mut b = print_as_bytes(args.entry_function_name.as_str());
let mut blen = b.len().to_le_bytes().to_vec();
println!("As bytes: {:?}", b.clone());
println!("len: {:?}", blen.clone());
blen.append(&mut b);
println!("\"instruction_data\": {:?}", blen);
}
Loading