-
Notifications
You must be signed in to change notification settings - Fork 18
feat(initia move cli): add decode cli & add docker image to use cli in docker container #208
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
Open
djm07073
wants to merge
6
commits into
main
Choose a base branch
from
feat(initia-move-cli)/decoder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5d2eda6
feat(initia-move-cli): decoder
djm07073 94322ec
fix: docker image push
djm07073 8def282
chore(fix): move-cli.yml
djm07073 b86cedb
fmt
djm07073 8cb4952
chore: update readme
djm07073 b461ee2
fix: add package path option in decode
djm07073 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM debian:bullseye-slim | ||
|
||
RUN apt-get update && \ | ||
apt-get install -y --no-install-recommends git ca-certificates && \ | ||
rm -rf /var/lib/apt/lists/* | ||
|
||
WORKDIR /usr/local/bin | ||
COPY initia-move-cli . | ||
RUN chmod +x initia-move-cli | ||
|
||
ENTRYPOINT ["initia-move-cli"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
use crate::{InitiaCLI, InitiaCommand}; | ||
use anyhow::Context; | ||
use clap::{Parser, Subcommand}; | ||
use movevm::move_api::handler::{decode_module_bytes, decode_script_bytes, read_module_info}; | ||
use std::{fs, path::PathBuf}; | ||
|
||
#[derive(Parser)] | ||
#[command( | ||
name = "decode", | ||
about = "Read or Decode Move modules and scripts", | ||
long_about = "Read or Decode Move modules and prints the result in JSON format" | ||
)] | ||
pub struct Decode { | ||
#[command(subcommand)] | ||
pub command: DecodeCommands, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
pub enum DecodeCommands { | ||
#[command( | ||
name = "read", | ||
about = "Read Move module info from bytecode", | ||
long_about = "Read and display basic information about a Move module from its bytecode file.\n\ | ||
Example: initia-move decode read ./build/package/bytecode_modules/my_module.mv" | ||
)] | ||
Read { | ||
#[arg(value_name = "PACKAGE_NAME")] | ||
package_name: String, | ||
#[arg(value_name = "MODULE_NAME")] | ||
module_name: String, | ||
#[clap( | ||
long = "path", | ||
short = 'p', | ||
value_name = "PACKAGE_PATH", | ||
help = "Path to the package directory" | ||
)] | ||
package_path: Option<PathBuf>, | ||
}, | ||
|
||
#[command( | ||
name = "script", | ||
about = "Decode Move script bytecode", | ||
long_about = "Decode Move script bytecode and display its ABI (Application Binary Interface).\n\ | ||
Example: initia-move decode script ./build/package/scripts/my_script.mv" | ||
)] | ||
Script { | ||
#[arg(value_name = "PACKAGE_NAME")] | ||
package_name: String, | ||
#[arg(value_name = "SCRIPT_NAME")] | ||
script_name: String, | ||
#[clap( | ||
long = "path", | ||
short = 'p', | ||
value_name = "PACKAGE_PATH", | ||
help = "Path to the package directory" | ||
)] | ||
package_path: Option<PathBuf>, | ||
}, | ||
|
||
#[command( | ||
name = "module", | ||
about = "Decode Move module bytecode", | ||
long_about = "Decode Move module bytecode and display its ABI (Application Binary Interface).\n\ | ||
Example: initia-move decode module ./build/package/bytecode_modules/my_module.mv" | ||
)] | ||
Module { | ||
#[arg(value_name = "PACKAGE_NAME")] | ||
package_name: String, | ||
#[arg(value_name = "MODULE_NAME")] | ||
module_name: String, | ||
#[clap( | ||
long = "path", | ||
short = 'p', | ||
value_name = "PACKAGE_PATH", | ||
help = "Path to the package directory" | ||
)] | ||
package_path: Option<PathBuf>, | ||
}, | ||
} | ||
|
||
pub trait Decoder { | ||
fn decode(self) -> anyhow::Result<()>; | ||
} | ||
|
||
fn read_file(package_path: &Option<PathBuf>, path: &str) -> anyhow::Result<Vec<u8>> { | ||
let current_dir = package_path | ||
.clone() | ||
.unwrap_or_else(|| std::env::current_dir().expect("Failed to get current directory")); | ||
let file_path = current_dir.join(PathBuf::from(path)); | ||
fs::read(&file_path).with_context(|| format!("Failed to read file: {}", file_path.display())) | ||
} | ||
|
||
impl Decoder for InitiaCLI { | ||
fn decode(self) -> anyhow::Result<()> { | ||
match &self.cmd { | ||
InitiaCommand::Decode(cmd) => { | ||
match &cmd.command { | ||
DecodeCommands::Read { | ||
package_name, | ||
module_name, | ||
package_path, | ||
} => { | ||
let path = | ||
format!("build/{}/bytecode_modules/{}.mv", package_name, module_name); | ||
let bytes = read_file(package_path, &path)?; | ||
let result = read_module_info(&bytes)?; | ||
let mut json: serde_json::Value = serde_json::from_slice(&result)?; | ||
|
||
if let Some(address) = json.get_mut("address") { | ||
if let serde_json::Value::Array(bytes) = address { | ||
let hex = format!( | ||
"0x{}", | ||
bytes | ||
.iter() | ||
.filter_map(|b| b.as_u64()) | ||
.map(|b| format!("{:02x}", b)) | ||
.collect::<String>() | ||
); | ||
*address = serde_json::json!(hex); | ||
} | ||
} | ||
println!("{}", serde_json::to_string_pretty(&json)?); | ||
} | ||
DecodeCommands::Script { | ||
package_name, | ||
script_name, | ||
package_path, | ||
} => { | ||
let path = format!( | ||
"build/{}/scripts/bytecode_scripts/{}.mv", | ||
package_name, script_name | ||
); | ||
let bytes = read_file(package_path, &path)?; | ||
let result = decode_script_bytes(bytes)?; | ||
let json: serde_json::Value = serde_json::from_slice(&result)?; | ||
println!("{}", serde_json::to_string_pretty(&json)?); | ||
} | ||
DecodeCommands::Module { | ||
package_name, | ||
module_name, | ||
package_path, | ||
} => { | ||
let path = | ||
format!("build/{}/bytecode_modules/{}.mv", package_name, module_name); | ||
let bytes = read_file(package_path, &path)?; | ||
let result = decode_module_bytes(bytes)?; | ||
let json: serde_json::Value = serde_json::from_slice(&result)?; | ||
println!("{}", serde_json::to_string_pretty(&json)?); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.