refactor: allow ethereum node to be a signer

buddies-main-deployment
James Prestwich 4 years ago committed by James Prestwich
parent 999dde9fd2
commit 9951a90c29
No known key found for this signature in database
GPG Key ID: 7CC174C250AD83AD
  1. 4
      rust/optics-base/Cargo.toml
  2. 2
      rust/optics-base/bin/example.rs
  3. 2
      rust/optics-base/config/default.toml
  4. 3
      rust/optics-base/src/lib.rs
  5. 27
      rust/optics-base/src/settings/ethereum.rs

@ -21,3 +21,7 @@ tracing-futures = "0.2.4"
tracing-subscriber = "0.2.15"
optics-core = { path = "../optics-core" }
[[example]]
name = "example"
path = "./bin/example.rs"

@ -1,6 +1,6 @@
use color_eyre::{eyre::eyre, Result};
use crate::{
use optics_base::{
agent::OpticsAgent,
settings::{log::Style, Settings},
};

@ -3,7 +3,6 @@ slip44 = 52752
address = "0x0000000000000000000000000000000000000000"
rpc-style = "ethereum"
[home.config]
signer = "380eb0f3d505f087e438eca80bc4df9a7faa24f868e69fc0440261a0fc0567dc"
connection = { type = "http", url = "http://localhost:8545" }
[replicas]
@ -12,6 +11,7 @@ rpc-style = "ethereum"
address = "0x0000000000000000000000000000000000000000"
rpc-style = "ethereum"
[replicas.EthereumMainnet.config]
signer = {type = "hexKey", key = "380eb0f3d505f087e438eca80bc4df9a7faa24f868e69fc0440261a0fc0567dc"}
connection = { type = "ws", url = "ws://localhost:8545"}
[replicas.EthereumClassic]

@ -19,6 +19,3 @@ pub mod settings;
/// Base trait for an agent
pub mod agent;
// example main
mod sample_main;

@ -1,6 +1,6 @@
use std::convert::TryFrom;
use color_eyre::{Report, Result};
use color_eyre::{eyre::eyre, Report, Result};
use ethers::core::types::Address;
use optics_core::traits::{Home, Replica};
@ -26,7 +26,6 @@ pub enum EthereumConnection {
macro_rules! construct_box_contract {
($contract:ident, $name:expr, $slip44:expr, $address:expr, $provider:expr, $signer:expr) => {{
if let Some(signer) = $signer {
let signer = signer?;
let provider = ethers::middleware::SignerMiddleware::new($provider, signer);
Box::new(crate::abis::$contract::new(
$name,
@ -64,18 +63,26 @@ macro_rules! construct_http_box_contract {
/// Ethereum signer types
#[derive(Debug, Clone, serde::Deserialize)]
#[serde(untagged)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum EthereumSigner {
/// Hex string of private key
HexKey(String),
/// A local hex key
HexKey {
/// Hex string of private key, without 0x prefix
key: String,
},
#[serde(other)]
/// Node will sign on RPC calls
Node,
}
impl EthereumSigner {
// TODO: allow ledger or other signer traits?
/// Try to conver the ethereum signer to a local wallet
#[tracing::instrument(err)]
pub fn try_into_wallet(&self) -> Result<ethers::signers::LocalWallet> {
match self {
EthereumSigner::HexKey(s) => Ok(s.parse()?),
EthereumSigner::HexKey { key } => Ok(key.parse()?),
EthereumSigner::Node => Err(eyre!("Node signer")),
}
}
}
@ -84,15 +91,16 @@ impl EthereumSigner {
#[derive(Debug, serde::Deserialize)]
pub struct EthereumConf {
connection: EthereumConnection,
signer: Option<EthereumSigner>,
signer: EthereumSigner,
}
impl EthereumConf {
fn signer(&self) -> Option<Result<ethers::signers::LocalWallet>> {
self.signer.clone().map(|s| s.try_into_wallet())
fn signer(&self) -> Option<ethers::signers::LocalWallet> {
self.signer.try_into_wallet().ok()
}
/// Try to convert this into a home contract
#[tracing::instrument(err)]
pub async fn try_into_home(
&self,
name: &str,
@ -118,6 +126,7 @@ impl EthereumConf {
}
/// Try to convert this into a replica contract
#[tracing::instrument(err)]
pub async fn try_into_replica(
&self,
name: &str,

Loading…
Cancel
Save