Skip to content

Commit

Permalink
Add utils to generate instruction_data based on name (anza-xyz#57)
Browse files Browse the repository at this point in the history
Towards anza-xyz#55
  • Loading branch information
ksolana committed Jun 14, 2024
1 parent 9bfd5f2 commit dcb7252
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
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);
}

0 comments on commit dcb7252

Please sign in to comment.