Skip to content

Commit f5d4fe7

Browse files
rphmeierpepyakin
authored andcommitted
integrate key commands into CLI
1 parent 8b2f753 commit f5d4fe7

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

sugondat-shim/src/cli.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,32 @@ pub struct Cli {
3737
pub command: Commands,
3838
}
3939

40+
/// Common parameters for key management subcommand.
41+
#[derive(clap::Args, Debug)]
42+
#[group(required = true, multiple = false)]
43+
pub struct KeyManagementParams {
44+
/// Use the Alice development key to sign blob transactions.
45+
///
46+
/// Cannot be used in conjunction with the `--submit-private-key` flag.
47+
#[arg(long)]
48+
pub submit_dev_alice: bool,
49+
50+
/// Use the keyfile at the provided path to sign blob transactions.
51+
///
52+
/// The keyfile should be 32 bytes of unencrypted, hex-encoded sr25519
53+
/// seed material.
54+
///
55+
/// Cannot be used in conjunction with the `--submit-dev-alice` flag.
56+
#[arg(long, value_name = "PATH")]
57+
pub submit_private_key: Option<std::path::PathBuf>,
58+
}
59+
4060
/// Common parameters for the adapter subcommands.
4161
#[derive(clap::Args, Debug)]
4262
pub struct AdapterServerParams {
63+
#[clap(flatten)]
64+
pub key_management: KeyManagementParams,
65+
4366
/// The address on which the shim should listen for incoming connections from the rollup nodes.
4467
#[clap(short, long, default_value = "127.0.0.1", group = "listen")]
4568
pub address: String,
@@ -53,7 +76,8 @@ pub struct AdapterServerParams {
5376
group = "listen"
5477
)]
5578
pub port: u16,
56-
// TODO: e.g. --submit-key, prometheus stuff, enabled adapters, etc.
79+
80+
// TODO: e.g. prometheus stuff, enabled adapters, etc.
5781
}
5882

5983
/// Common parameters for that commands that connect to the sugondat-node.
@@ -105,7 +129,7 @@ pub mod query {
105129
// - query blob <id> - returns the blob for a given key. The key here is the same sense as
106130
// described here https://github.com/thrumdev/sugondat/issues/9#issuecomment-1814005570.
107131

108-
use super::{SugondatRpcParams, ENV_SUGONDAT_NAMESPACE};
132+
use super::{SugondatRpcParams, KeyManagementParams, ENV_SUGONDAT_NAMESPACE};
109133
use clap::{Args, Subcommand};
110134

111135
#[derive(Debug, Args)]
@@ -123,7 +147,7 @@ pub mod query {
123147
pub mod submit {
124148
//! CLI definition for the `query submit` subcommand.
125149
126-
use super::{SugondatRpcParams, ENV_SUGONDAT_NAMESPACE};
150+
use super::{SugondatRpcParams, KeyManagementParams, ENV_SUGONDAT_NAMESPACE};
127151
use clap::Args;
128152

129153
#[derive(Debug, Args)]
@@ -141,6 +165,9 @@ pub mod query {
141165

142166
/// The file path of the blob to submit. Pass `-` to read from stdin.
143167
pub blob_path: String,
168+
169+
#[clap(flatten)]
170+
pub key_management: KeyManagementParams,
144171
}
145172
}
146173
}

sugondat-shim/src/cmd/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::cli::{Cli, Commands};
2+
use crate::key;
23
use clap::Parser;
34

45
pub mod query;
@@ -29,3 +30,16 @@ fn init_logging() -> anyhow::Result<()> {
2930
.try_init()?;
3031
Ok(())
3132
}
33+
34+
fn fetch_key(params: crate::cli::KeyManagementParams) -> anyhow::Result<key::Keypair> {
35+
if params.submit_dev_alice {
36+
return Ok(key::alice())
37+
}
38+
39+
if let Some(path) = params.submit_private_key {
40+
return key::load(path)
41+
}
42+
43+
// sanity: clap is supposed to prevent this
44+
return Err(anyhow::anyhow!("No blob submission key specified. See --help"))
45+
}

sugondat-shim/src/cmd/query/submit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub async fn run(params: Params) -> anyhow::Result<()> {
88
blob_path,
99
namespace,
1010
rpc,
11+
key_management,
1112
} = params;
1213
let blob = read_blob(&blob_path)
1314
.with_context(|| format!("cannot read blob file path '{}'", blob_path))?;

sugondat-shim/src/key.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
//! Key management: sr25519 account key used for signing blob submission
22
//! transactions.
33
4-
use subxt_signer::sr25519::{Seed, Keypair};
4+
use subxt_signer::sr25519::Seed;
55
use std::path::Path;
66

7+
pub use subxt_signer::sr25519::Keypair;
8+
79
/// Load a key from the provided file path.
810
///
911
/// The file should contain a hex-encoded 32-byte seed used to generate
1012
/// the underlying schnorrkel private key.
11-
pub fn load_key_from_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Keypair> {
13+
pub fn load<P: AsRef<Path>>(path: P) -> anyhow::Result<Keypair> {
1214
let raw = hex::decode(std::fs::read(path)?)?;
1315
let mut seed: Seed = Seed::default();
1416
if raw.len() <= seed.len() {

0 commit comments

Comments
 (0)