Skip to content
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

feat: add relayer key management support #38

Merged
merged 8 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
68 changes: 68 additions & 0 deletions src/cli/simple/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use aurora_engine_types::parameters::engine::{
};
use aurora_engine_types::types::Address;
use aurora_engine_types::{types::Wei, H256, U256};
use near_crypto::{KeyType, PublicKey};
use near_primitives::views::{CallResult, FinalExecutionStatus};
use serde_json::Value;
use std::fmt::{Display, Formatter};
Expand Down Expand Up @@ -459,6 +460,23 @@ pub fn key_pair(random: bool, seed: Option<u64>) -> anyhow::Result<()> {
Ok(())
}

/// Return randomly generated content of the key file for `AccountId`.
pub fn gen_near_key(account_id: &str, key_type: KeyType) -> anyhow::Result<()> {
let secret_key = near_crypto::SecretKey::from_random(key_type);
let public_key = secret_key.public_key();

println!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({
"account_id": account_id,
"public_key": public_key,
"secret_key": secret_key
}))?
);

Ok(())
}

/// Pause precompiles with mask.
pub async fn pause_precompiles(client: Client, mask: u32) -> anyhow::Result<()> {
let args = PausePrecompilesCallArgs { paused_mask: mask }.try_to_vec()?;
Expand Down Expand Up @@ -490,6 +508,56 @@ pub async fn paused_precompiles(client: Client) -> anyhow::Result<()> {
get_value::<u32>(client, "paused_precompiles", None).await
}

/// Set relayer key manager.
pub async fn set_key_manager(client: Client, manager: Option<AccountId>) -> anyhow::Result<()> {
let message = manager.as_ref().map_or_else(
|| "has been removed".to_string(),
|account_id| format!("{account_id} has been set"),
);
// TODO: Use RelayerKeyManagerArgs from engine-types instead.
aleksuss marked this conversation as resolved.
Show resolved Hide resolved
let args = serde_json::to_vec(&serde_json::json!({ "key_manager": manager }))?;

contract_call!(
"set_key_manager",
"The key manager {message} successfully",
aleksuss marked this conversation as resolved.
Show resolved Hide resolved
"Error while setting key manager"
)
.proceed(client, args)
.await
}

/// Add relayer public key.
pub async fn add_relayer_key(
client: Client,
public_key: PublicKey,
allowance: f64,
) -> anyhow::Result<()> {
// TODO: Use RelayerKeyArgs from engine-types instead.
let args = serde_json::to_vec(&serde_json::json!({ "public_key": public_key }))?;

contract_call!(
"add_relayer_key",
"The public key: {public_key} has been added successfully",
"Error while adding public key"
)
.proceed_with_deposit(client, args, allowance)
.await
}

/// Remove relayer public key.
pub async fn remove_relayer_key(client: Client, public_key: PublicKey) -> anyhow::Result<()> {
// TODO: Use RelayerKeyArgs from engine-types instead.
let args = serde_json::to_vec(&serde_json::json!({ "public_key": public_key }))?;

contract_call!(
"remove_relayer_key",
"The public key: {public_key} has been removed successfully",
"Error while removing public key"
)
.proceed(client, args)
.await
}

async fn get_value<T: FromCallResult + Display>(
client: Client,
method_name: &str,
Expand Down
50 changes: 50 additions & 0 deletions src/cli/simple/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use aurora_engine_types::account_id::AccountId;
use clap::{Parser, Subcommand};
use lazy_static::lazy_static;
use near_crypto::{KeyType, PublicKey};
use shadow_rs::shadow;
use std::str::FromStr;

Expand Down Expand Up @@ -190,6 +192,13 @@ pub enum Command {
#[arg(long)]
seed: Option<u64>,
},
/// Return randomly generated NEAR key for AccountId
GenerateNearKey {
/// AccountId
account_id: String,
aleksuss marked this conversation as resolved.
Show resolved Hide resolved
/// Key type: ed25519 or secp256k1
key_type: KeyType,
},
/// Return fixed gas cost
GetFixedGasCost,
/// Set fixed gas cost
Expand Down Expand Up @@ -234,6 +243,26 @@ pub enum Command {
#[arg(long)]
entry: String,
},
/// Set relayer key manager
SetKeyManager {
/// AccountId of the key manager
#[arg(value_parser = parse_account_id)]
account_id: Option<AccountId>,
},
/// Add relayer public key
AddRelayerKey {
/// Public key
#[arg(long)]
public_key: PublicKey,
/// Allowance
#[arg(long)]
allowance: f64,
},
/// Remove relayer public key
RemoveRelayerKey {
/// Public key
public_key: PublicKey,
},
}

#[derive(Clone)]
Expand Down Expand Up @@ -355,6 +384,10 @@ pub async fn run(args: Cli) -> anyhow::Result<()> {
}
Command::EncodeAddress { account } => command::encode_address(&account),
Command::KeyPair { random, seed } => command::key_pair(random, seed)?,
Command::GenerateNearKey {
account_id,
key_type,
} => command::gen_near_key(&account_id, key_type)?,
// Silo Specific Methods
Command::GetFixedGasCost => command::silo::get_fixed_gas_cost(client).await?,
Command::SetFixedGasCost { cost } => {
Expand All @@ -375,7 +408,24 @@ pub async fn run(args: Cli) -> anyhow::Result<()> {
Command::RemoveEntryFromWhitelist { kind, entry } => {
command::silo::remove_entry_from_whitelist(client, kind, entry).await?;
}
Command::SetKeyManager { account_id } => {
// command::set_key_manager(client, account_id.map(|a| a.parse().unwrap())).await?;
command::set_key_manager(client, account_id).await?;
}
Command::AddRelayerKey {
public_key,
allowance,
} => {
command::add_relayer_key(client, public_key, allowance).await?;
}
Command::RemoveRelayerKey { public_key } => {
command::remove_relayer_key(client, public_key).await?;
}
}

Ok(())
}

fn parse_account_id(arg: &str) -> anyhow::Result<AccountId> {
arg.parse().map_err(|e| anyhow::anyhow!("{e}"))
}
16 changes: 15 additions & 1 deletion src/client/near.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::utils;

// The maximum amount of prepaid NEAR gas required for paying for a transaction.
const NEAR_GAS: u64 = 300_000_000_000_000;
const TIMEOUT: u64 = 20;

pub struct NearClient {
client: JsonRpcClient,
Expand All @@ -36,7 +37,20 @@ pub struct NearClient {

impl NearClient {
pub fn new<U: AsUrl>(url: U, engine_account_id: &str, signer_key_path: Option<String>) -> Self {
let client = JsonRpcClient::connect(url);
let mut headers = reqwest::header::HeaderMap::with_capacity(2);
headers.insert(
reqwest::header::CONTENT_TYPE,
reqwest::header::HeaderValue::from_static("application/json"),
);
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(TIMEOUT))
.connect_timeout(std::time::Duration::from_secs(TIMEOUT))
.default_headers(headers)
.build()
.map(JsonRpcClient::with)
.expect("couldn't create json rpc client");
let client = client.connect(url);

Self {
client,
engine_account_id: engine_account_id.parse().unwrap(),
Expand Down