Skip to content

Commit

Permalink
Add utils to generate instruction_data based on name
Browse files Browse the repository at this point in the history
Towards #55
  • Loading branch information
ksolana committed Jun 3, 2024
1 parent cfdbc3e commit 8424364
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
13 changes: 13 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.

11 changes: 11 additions & 0 deletions external-crates/move/solana/util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[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"
36 changes: 36 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,36 @@
#[test]
use binascii::{b32encode, b32decode};

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 mut b = print_as_bytes("counter__owner");
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 8424364

Please sign in to comment.