-
Notifications
You must be signed in to change notification settings - Fork 109
feat: format-module-bytes
utility
#1236
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
andygolay
wants to merge
3
commits into
main
Choose a base branch
from
andygolay/publish-via-explorer
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,10 @@ | ||
[package] | ||
name = "publish-via-explorer" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = "1.0" | ||
serde = { version = "1", features = ["derive"] } | ||
serde_json = "1" | ||
serde_yaml = "0.9" |
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,6 @@ | ||
[package] | ||
name = "hello" | ||
version = "0.0.1" | ||
|
||
[addresses] | ||
hello = "_" |
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,62 @@ | ||
# publish-via-explorer | ||
|
||
This tool prepares a Move package for publishing using the Movement Explorer UI. | ||
|
||
It compiles the embedded Move package and outputs the required `arg0` and `arg1` values for the `publish_package_txn` function: | ||
|
||
[https://explorer.movementlabs.xyz/account/0x0000000000000000000000000000000000000000000000000000000000000001/modules/run/code/publish\_package\_txn?network=mainnet](https://explorer.movementlabs.xyz/account/0x0000000000000000000000000000000000000000000000000000000000000001/modules/run/code/publish_package_txn?network=mainnet) | ||
|
||
## Function Signature | ||
|
||
```move | ||
entry fun publish_package_txn( | ||
owner: &signer, | ||
metadata_serialized: vector<u8>, | ||
code: vector<vector<u8>> | ||
) | ||
``` | ||
|
||
## Usage | ||
|
||
1. Ensure the `movement` CLI is installed and available in your `PATH`. | ||
|
||
2. Run from the workspace root: | ||
|
||
```bash | ||
cargo run -p publish-via-explorer | ||
``` | ||
|
||
3. The tool will compile the embedded Move package and output two formatted arguments: | ||
|
||
* `arg0` (vector<u8>) | ||
* `arg1` (vector\<vector<u8>>) | ||
|
||
It will also write these to: | ||
|
||
``` | ||
publish-via-explorer/build/hello/explorer_payload.log | ||
``` | ||
|
||
## Submitting via the Explorer UI | ||
|
||
1. Open the following link (for mainnet publishing... you can switch to testnet if you prefer): | ||
|
||
[https://explorer.movementlabs.xyz/account/0x0000000000000000000000000000000000000000000000000000000000000001/modules/run/code/publish\_package\_txn?network=mainnet](https://explorer.movementlabs.xyz/account/0x0000000000000000000000000000000000000000000000000000000000000001/modules/run/code/publish_package_txn?network=mainnet) | ||
|
||
2. Input the args: | ||
|
||
* **signer**: your account address (must be funded) | ||
* **arg0**: the full vector<u8> array (surrounded by brackets) | ||
* **arg1**: the full vector\<vector<u8>> array (outer and inner brackets must be present) | ||
|
||
Example: | ||
|
||
```json | ||
arg0: [5,104,101,108,108,111,...] | ||
arg1: [[161,28,235,11,...]] | ||
``` | ||
> [!TIP] Be sure to connect to the explorer with the same wallet that you used in your local Movement config. | ||
|
||
After successful publishing via explorer, you will see a successful message as follows: | ||
|
||
<img width="1205" alt="image" src="https://github.com/user-attachments/assets/312c2c17-e164-45d8-a7ca-c379ef0f21ed" /> |
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,5 @@ | ||
module hello::hello { | ||
public entry fun hi() { | ||
// no-op | ||
} | ||
} |
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,118 @@ | ||
use anyhow::{Context, Result}; | ||
use std::{ | ||
env, | ||
fs::{self, File}, | ||
io::Write, | ||
path::{Path, PathBuf}, | ||
process::Command, | ||
}; | ||
|
||
fn read_bytes(path: &Path) -> Result<Vec<u8>> { | ||
Ok(fs::read(path)?) | ||
} | ||
|
||
fn format_vector_u8(data: &[u8]) -> String { | ||
format!("[{}]", data.iter().map(|b| b.to_string()).collect::<Vec<_>>().join(",")) | ||
} | ||
|
||
fn format_vector_vector_u8(data: &[Vec<u8>]) -> String { | ||
serde_json::to_string(data).expect("json encode failed") | ||
} | ||
|
||
fn address_from_config() -> Result<String> { | ||
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
let config_path = crate_dir.join(".movement/config.yaml"); | ||
let contents = fs::read_to_string(&config_path) | ||
.with_context(|| format!("failed to read {}", config_path.display()))?; | ||
|
||
let config: serde_yaml::Value = serde_yaml::from_str(&contents)?; | ||
let default = config | ||
.get("profiles") | ||
.and_then(|p| p.get("default")) | ||
.context("missing [profiles][default] in config")?; | ||
|
||
let addr = default | ||
.get("account") | ||
.or_else(|| default.get("address")) | ||
.context("missing 'account' or 'address' under [profiles][default]")? | ||
.as_str() | ||
.context("address is not a string")?; | ||
|
||
Ok(addr.to_string()) | ||
} | ||
|
||
pub fn run() -> Result<()> { | ||
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
let address = address_from_config()?; | ||
|
||
let move_compile = Command::new("movement") | ||
.args([ | ||
"move", | ||
"compile", | ||
"--named-addresses", | ||
&format!("hello={}", address), | ||
"--save-metadata", | ||
"--package-dir", | ||
".", | ||
]) | ||
.current_dir(&crate_dir) | ||
.status() | ||
.context("failed to run movement move compile")?; | ||
|
||
if !move_compile.success() { | ||
anyhow::bail!("Move compilation failed"); | ||
} | ||
|
||
let build_dir = crate_dir.join("build/hello"); | ||
let metadata_path = build_dir.join("package-metadata.bcs"); | ||
let modules_dir = build_dir.join("bytecode_modules"); | ||
|
||
let metadata = read_bytes(&metadata_path)?; | ||
let mut modules = Vec::new(); | ||
for entry in fs::read_dir(&modules_dir)? { | ||
let entry = entry?; | ||
if entry.path().extension().map(|ext| ext == "mv").unwrap_or(false) { | ||
modules.push(read_bytes(&entry.path())?); | ||
} | ||
} | ||
|
||
let arg0 = format_vector_u8(&metadata); | ||
let arg1 = format_vector_vector_u8(&modules); | ||
|
||
let log_path = build_dir.join("explorer_payload.log"); | ||
let mut file = File::create(&log_path)?; | ||
writeln!(file, "arg0 (vector<u8>):\n{}\n", arg0)?; | ||
writeln!(file, "arg1 (vector<vector<u8>>):\n{}\n", arg1)?; | ||
|
||
println!("\n----- COPY INTO EXPLORER -----\n"); | ||
println!("arg0 (vector<u8>):\n{}", arg0); | ||
println!("\narg1 (vector<vector<u8>>):\n{}", arg1); | ||
println!("\n(Log saved to {})", log_path.display()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_format_vector_u8() { | ||
let input = vec![1, 2, 3, 4, 255]; | ||
let output = format_vector_u8(&input); | ||
assert_eq!(output, "[1,2,3,4,255]"); | ||
} | ||
|
||
#[test] | ||
fn test_format_vector_vector_u8() { | ||
let input = vec![vec![1, 2, 3], vec![4, 5, 6]]; | ||
let output = format_vector_vector_u8(&input); | ||
assert_eq!(output, "[[1,2,3],[4,5,6]]"); | ||
} | ||
|
||
#[test] | ||
fn test_read_bytes_failure() { | ||
let result = read_bytes(Path::new("nonexistent.file")); | ||
assert!(result.is_err()); | ||
} | ||
} |
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,3 @@ | ||
fn main() -> anyhow::Result<()> { | ||
publish_via_explorer::run() | ||
} |
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.