Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion proto/parser/parser.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ enum Chain {
CHAIN_SOLANA = 3;
CHAIN_SUI = 4;
CHAIN_TRON = 5;
CHAIN_NEAR = 6;

// Reserve space for future chains
reserved 6 to 998;
reserved 7 to 998;

// Custom for extensibility
CHAIN_CUSTOM = 999;
Expand Down
3 changes: 3 additions & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/chain_parsers/visualsign-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ base64 = "0.22.1"
borsh = "1.5"
bs58 = "0.5.1"
chrono = { version = "0.4", default-features = false, features = ["std", "clock"] }
clap = { version = "4.0", features = ["derive"], optional = true }
generated = { path = "../../generated" }
hex = "0.4.3"
# Pulls in ~34 transitive crates as mandatory (non-dev) dependencies, including
# serde_yaml 0.9 (unsafe-libyaml) and the arbitrary fuzzing derive.
near-primitives = "0.35.1"
parser_cli_core = { path = "../../parser/cli-core", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "2.0.12"
Expand All @@ -30,6 +32,8 @@ k256 = "0.13"
near-crypto = "0.35.1"

[features]
default = ["cli-plugin"]
cli-plugin = ["dep:clap", "dep:parser_cli_core"]
diagnostics = ["visualsign/diagnostics"]

[lints]
Expand Down
92 changes: 92 additions & 0 deletions src/chain_parsers/visualsign-near/src/cli_plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use clap::Args as ClapArgs;
use generated::parser::{ChainMetadata, NearMetadata, chain_metadata};
use visualsign::registry::{Chain, TransactionConverterRegistry};

use crate::networks::NearNetwork;

/// CLI arguments specific to NEAR.
///
/// Currently no NEAR-specific args are needed beyond the global `--network`
/// flag, which `create_metadata` below turns into `NearMetadata`.
#[derive(ClapArgs, Debug, Default, Clone)]
pub struct NearArgs {}

/// [`parser_cli_core::ChainPlugin`] implementation for NEAR.
pub struct NearPlugin {
#[allow(dead_code)]
args: NearArgs,
}

impl NearPlugin {
/// Creates a new `NearPlugin` with the given CLI args.
#[must_use]
pub fn new(args: NearArgs) -> Self {
Self { args }
}
}

impl parser_cli_core::ChainPlugin for NearPlugin {
fn chain(&self) -> Chain {
Chain::Near
}

fn register(&self, registry: &mut TransactionConverterRegistry) {
registry.register::<crate::NearTransaction, _>(
Chain::Near,
crate::NearVisualSignConverter::new(),
);
}

fn create_metadata(&self, network: Option<String>) -> Result<Option<ChainMetadata>, String> {
let Some(network) = network else {
return Ok(None);
};
if NearNetwork::from_network_id(&network).is_none() {
return Err(format!(
"Invalid network '{network}'. Supported: NEAR_MAINNET, NEAR_TESTNET"
));
}
Ok(Some(ChainMetadata {
metadata: Some(chain_metadata::Metadata::Near(NearMetadata {
network_id: Some(network.to_uppercase()),
})),
}))
}
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
use parser_cli_core::ChainPlugin;

fn plugin() -> NearPlugin {
NearPlugin::new(NearArgs {})
}

#[test]
fn create_metadata_defaults_to_none_without_a_network_flag() {
assert_eq!(plugin().create_metadata(None).unwrap(), None);
}

#[test]
fn create_metadata_builds_near_metadata_for_a_valid_network() {
let metadata = plugin()
.create_metadata(Some("near_testnet".to_string()))
.unwrap()
.expect("Some(ChainMetadata)");
assert_eq!(
metadata,
ChainMetadata {
metadata: Some(chain_metadata::Metadata::Near(NearMetadata {
network_id: Some("NEAR_TESTNET".to_string()),
})),
}
);
}

#[test]
fn create_metadata_rejects_an_invalid_network() {
assert!(plugin().create_metadata(Some("bogus".to_string())).is_err());
}
}
4 changes: 4 additions & 0 deletions src/chain_parsers/visualsign-near/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
//! human-readable [`visualsign::SignablePayload`] fields.

pub mod actions;
#[cfg(feature = "cli-plugin")]
pub mod cli_plugin;
pub mod convert;
pub mod fmt;
pub mod networks;
pub mod presets;
pub mod tx;

#[cfg(feature = "cli-plugin")]
pub use cli_plugin::{NearArgs, NearPlugin};
pub use convert::NearVisualSignConverter;
pub use tx::NearTransaction;
Binary file modified src/generated/src/generated/descriptor.bin
Binary file not shown.
3 changes: 3 additions & 0 deletions src/generated/src/generated/parser.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading