Skip to content

Commit

Permalink
add testkey admin function
Browse files Browse the repository at this point in the history
  • Loading branch information
musitdev committed Feb 19, 2025
1 parent 70ca6ea commit c5fcb38
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions 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 networks/movement/movement-full-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ movement-signer = { workspace = true }
movement-signer-loader = { workspace = true }
syncador = { workspace = true }
syncup = { workspace = true }
aptos-crypto = { workspace = true }
aptos-sdk = { workspace = true }
k256 = { workspace = true }
alloy-signer = { workspace = true }

[features]
default = []
Expand Down
5 changes: 5 additions & 0 deletions networks/movement/movement-full-node/src/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub mod governed_gas_pool;
pub mod mcr;
pub mod ops;
pub mod rotate_key;
pub mod testkey;

use clap::Subcommand;

#[derive(Subcommand, Debug)]
Expand All @@ -24,6 +26,8 @@ pub enum Admin {
Framework(framework::Framework),
#[clap(subcommand)]
Config(config::Config),
#[clap(subcommand)]
TestKey(testkey::TestKey),
}

impl Admin {
Expand All @@ -36,6 +40,7 @@ impl Admin {
Admin::Ops(ops) => ops.execute().await,
Admin::Framework(framework) => framework.execute().await,
Admin::Config(config) => config.execute().await,
Admin::TestKey(key) => key.execute().await,
}
}
}
77 changes: 77 additions & 0 deletions networks/movement/movement-full-node/src/admin/testkey/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use aptos_crypto::ed25519::Ed25519PublicKey;
use aptos_sdk::types::transaction::authenticator::AuthenticationKey;
use clap::Parser;
use clap::Subcommand;
use k256::ecdsa::VerifyingKey;
use movement_signer::cryptography::ed25519::Ed25519;
use movement_signer::cryptography::secp256k1::Secp256k1;
use movement_signer::key::TryFromCanonicalString;
use movement_signer::Signing;
use movement_signer::Verify;
use movement_signer_loader::identifiers::SignerIdentifier;
use movement_signer_loader::{Load, LoadedSigner};

#[derive(Subcommand, Debug)]
#[clap(rename_all = "kebab-case", about = "Commands to test key name")]
pub enum TestKey {
Ed25519(TestKeyParam),
Secp256k1(TestKeyParam),
}

impl TestKey {
pub async fn execute(&self) -> Result<(), anyhow::Error> {
match self {
TestKey::Ed25519(param) => param.execute_ed25519().await,
TestKey::Secp256k1(param) => param.execute_secp256k1().await,
}
}
}

#[derive(Debug, Parser, Clone)]
#[clap(rename_all = "kebab-case", about = "Key to test.")]
pub struct TestKeyParam {
#[clap(default_value = "{maptos,maptos-storage,movement-da-db}/**", value_name = "DB PATTERN")]
pub name: String,
}

impl TestKeyParam {
pub async fn execute_ed25519(&self) -> Result<(), anyhow::Error> {
let signer_identifier = SignerIdentifier::try_from_canonical_string(&self.name)
.map_err(|err| anyhow::anyhow!(err))?;
let loader: LoadedSigner<Ed25519> = signer_identifier.load().await?;

let public_key = Ed25519PublicKey::try_from(loader.public_key().await?.as_bytes())?;
let account_address = AuthenticationKey::ed25519(&public_key).account_address();

tracing::info!("Key loaded, account address:{account_address}");
tracing::info!("Try to sign a message ...");

let message = b"Hello, world!";
let signature = loader.sign(message).await?;
assert!(Ed25519::verify(message, &signature, &loader.public_key().await?)?);

tracing::info!("Message sign verify pass");

Ok(())
}
pub async fn execute_secp256k1(&self) -> Result<(), anyhow::Error> {
let signer_identifier = SignerIdentifier::try_from_canonical_string(&self.name)
.map_err(|err| anyhow::anyhow!(err))?;
let loader: LoadedSigner<Secp256k1> = signer_identifier.load().await?;
let pub_key = loader.public_key().await?;
let verify_key = VerifyingKey::from_sec1_bytes(pub_key.as_bytes())?;

let account_address = alloy_signer::utils::public_key_to_address(&verify_key);

tracing::info!("Key loaded, account address:{account_address}");
tracing::info!("Try to sign a message ...");

let message = b"Hello, world!";
let signature = loader.sign(message).await?;
assert!(Secp256k1::verify(message, &signature, &loader.public_key().await?)?);

tracing::info!("Message sign verify pass");

Ok(())
}
}

0 comments on commit c5fcb38

Please sign in to comment.