Skip to content

Commit 5ca8d7a

Browse files
committed
Add utils to generate instruction_data based on name
Towards #55
1 parent cfdbc3e commit 5ca8d7a

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

external-crates/move/Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "input_json"
3+
version = "1.0.0"
4+
edition = "2021"
5+
6+
[[bin]]
7+
name = "generate_input_json"
8+
path = "src/generate_input_json.rs"
9+
10+
[dependencies]
11+
binascii = "0.1"
12+
clap = { version = "3.1.8", features = ["derive"] }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#[cfg(test)]
2+
use binascii::{b32encode, b32decode};
3+
4+
use clap::Parser;
5+
6+
#[derive(Debug, Parser)]
7+
#[clap(author, version, about)]
8+
pub struct Args {
9+
/// Entry function name
10+
#[clap(short = 'f', long = "func-name")]
11+
pub entry_function_name: String,
12+
// Remaining arguments that we want to serialize as bytes
13+
#[clap(short = 's', long = "args", default_value = "")]
14+
pub argstring: String,
15+
}
16+
17+
fn print_as_bytes(data : &str) -> Vec<u8> {
18+
let length = data.len();
19+
let input = &data[..length].as_bytes();
20+
input.to_vec()
21+
}
22+
23+
#[test]
24+
fn test_encode_decode() {
25+
let data : &str = "counter__owner";
26+
let input = &data[..data.len()].as_bytes();
27+
let mut output = [0u8; 500];
28+
let mut dec_out = [0u8; 200];
29+
let encoded_output = b32encode(&input, &mut output).ok().unwrap();
30+
let decoded_output = b32decode(&encoded_output, &mut dec_out).ok().unwrap();
31+
assert_eq!(input, &decoded_output);
32+
}
33+
34+
#[test]
35+
fn test_print_as_bytes() {
36+
const ENCODED_DATA : [u8; 11] = [104, 101, 108, 108, 111, 95, 95, 109, 97, 105, 110];
37+
let b = print_as_bytes("hello__main");
38+
assert_eq!(b.len(), ENCODED_DATA.len());
39+
assert_eq!(&ENCODED_DATA, &b[..ENCODED_DATA.len()]);
40+
}
41+
42+
pub fn main() {
43+
let args = Args::parse();
44+
let mut b = print_as_bytes(args.entry_function_name.as_str());
45+
let mut blen = b.len().to_le_bytes().to_vec();
46+
println!("As bytes: {:?}", b.clone());
47+
println!("len: {:?}", blen.clone());
48+
blen.append(&mut b);
49+
println!("\"instruction_data\": {:?}", blen);
50+
}

0 commit comments

Comments
 (0)