Skip to content

Commit

Permalink
fix: dynamic chain support
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Mar 8, 2024
1 parent 4fa25e9 commit 1314da6
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 33 deletions.
16 changes: 14 additions & 2 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ dashmap = "5.4.0"

relay_rpc = { git = "https://github.com/WalletConnect/WalletConnectRust.git", tag = "v0.28.0", features = ["cacao"] }
relay_client = { git = "https://github.com/WalletConnect/WalletConnectRust.git", tag = "v0.28.0" }
blockchain_api = { git = "https://github.com/WalletConnect/WalletConnectRust.git", tag = "v0.28.0" }
x25519-dalek = { version = "2.0.0", features = ["static_secrets"] }
hkdf = "0.12.3"
sha2 = "0.10.6"
Expand Down Expand Up @@ -101,6 +102,7 @@ test-context = "0.1"
[build-dependencies]
build-info-build = "0.0"

# [patch.'https://github.com/WalletConnect/WalletConnectRust.git']
# relay_rpc = { path = "../WalletConnectRust/relay_rpc" }
# relay_client = { path = "../WalletConnectRust/relay_client" }
[patch.'https://github.com/WalletConnect/WalletConnectRust.git']
relay_rpc = { path = "../WalletConnectRust/relay_rpc" }
relay_client = { path = "../WalletConnectRust/relay_client" }
blockchain_api = { path = "../WalletConnectRust/blockchain_api" }
26 changes: 13 additions & 13 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ pub async fn verify_identity(
ksu: &str,
sub: &str,
redis: Option<&Arc<Redis>>,
provider: &impl GetRpcUrl,
provider: Option<&impl GetRpcUrl>,
metrics: Option<&Metrics>,
) -> Result<Authorization, IdentityVerificationError> {
let mut url = Url::parse(ksu)
Expand Down Expand Up @@ -1121,13 +1121,13 @@ pub mod test_utils {
let cacao_signature = [&signature.to_bytes()[..], &[recovery.to_byte()]].concat();
cacao.s.t = EIP191.to_owned();
cacao.s.s = hex::encode(cacao_signature);
cacao.verify(&MockGetRpcUrl).await.unwrap();
cacao.verify(Some(&MockGetRpcUrl)).await.unwrap();
cacao
}

pub struct MockGetRpcUrl;
impl GetRpcUrl for MockGetRpcUrl {
fn get_rpc_url(&self, _: String) -> Option<Url> {
async fn get_rpc_url(&self, _: String) -> Option<Url> {
None
}
}
Expand Down Expand Up @@ -1398,7 +1398,7 @@ pub mod test {
keys_server_url.as_ref(),
&account.to_did_pkh(),
None,
&MockGetRpcUrl,
Some(&MockGetRpcUrl),
None,
)
.await
Expand Down Expand Up @@ -1447,7 +1447,7 @@ pub mod test {
keys_server_url.as_ref(),
&account.to_did_pkh(),
None,
&MockGetRpcUrl,
Some(&MockGetRpcUrl),
None,
)
.await
Expand Down Expand Up @@ -1496,7 +1496,7 @@ pub mod test {
keys_server_url.as_ref(),
&account.to_did_pkh(),
None,
&MockGetRpcUrl,
Some(&MockGetRpcUrl),
None,
)
.await
Expand Down Expand Up @@ -1545,7 +1545,7 @@ pub mod test {
keys_server_url.as_ref(),
&account.to_did_pkh(),
None,
&MockGetRpcUrl,
Some(&MockGetRpcUrl),
None,
)
.await
Expand Down Expand Up @@ -1625,7 +1625,7 @@ pub mod test {
let cacao_signature = [&signature.to_bytes()[..], &[recovery.to_byte()]].concat();
cacao.s.t = EIP191.to_owned();
cacao.s.s = hex::encode(cacao_signature);
cacao.verify(&MockGetRpcUrl).await.unwrap();
cacao.verify(Some(&MockGetRpcUrl)).await.unwrap();
cacao
};

Expand All @@ -1637,7 +1637,7 @@ pub mod test {
keys_server_url.as_ref(),
&account.to_did_pkh(),
None,
&MockGetRpcUrl,
Some(&MockGetRpcUrl),
None,
)
.await;
Expand Down Expand Up @@ -1711,7 +1711,7 @@ pub mod test {
let cacao_signature = [&signature.to_bytes()[..], &[recovery.to_byte()]].concat();
cacao.s.t = EIP191.to_owned();
cacao.s.s = hex::encode(cacao_signature);
cacao.verify(&MockGetRpcUrl).await.unwrap();
cacao.verify(Some(&MockGetRpcUrl)).await.unwrap();
cacao
};

Expand All @@ -1723,7 +1723,7 @@ pub mod test {
keys_server_url.as_ref(),
&account.to_did_pkh(),
None,
&MockGetRpcUrl,
Some(&MockGetRpcUrl),
None,
)
.await;
Expand Down Expand Up @@ -1787,7 +1787,7 @@ pub mod test {
let cacao_signature = [&signature.to_bytes()[..], &[recovery.to_byte()]].concat();
cacao.s.t = EIP191.to_owned();
cacao.s.s = hex::encode(cacao_signature);
cacao.verify(&MockGetRpcUrl).await.unwrap();
cacao.verify(Some(&MockGetRpcUrl)).await.unwrap();
cacao
};

Expand All @@ -1799,7 +1799,7 @@ pub mod test {
keys_server_url.as_ref(),
&account.to_did_pkh(),
None,
&MockGetRpcUrl,
Some(&MockGetRpcUrl),
None,
)
.await;
Expand Down
7 changes: 7 additions & 0 deletions src/config/deployed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct DeployedConfiguration {
pub relay_url: Url,
pub relay_public_key: String,
pub notify_url: Url,
#[serde(default = "default_blockchain_api_endpoint")]
pub blockchain_api_endpoint: Option<String>,

pub registry_url: Url,
pub registry_auth_token: String,
Expand Down Expand Up @@ -76,6 +78,10 @@ fn default_redis_pool_size() -> u32 {
64
}

fn default_blockchain_api_endpoint() -> Option<String> {
Some("https://rpc.walletconnect.com".to_string())
}

pub fn get_configuration() -> Result<Configuration, NotifyServerError> {
let config = envy::from_env::<DeployedConfiguration>()?;

Expand All @@ -91,6 +97,7 @@ pub fn get_configuration() -> Result<Configuration, NotifyServerError> {
relay_url: config.relay_url,
relay_public_key: config.relay_public_key,
notify_url: config.notify_url,
blockchain_api_endpoint: config.blockchain_api_endpoint,
registry_url: config.registry_url,
registry_auth_token: config.registry_auth_token,
auth_redis_addr_read: config.auth_redis_addr_read,
Expand Down
7 changes: 7 additions & 0 deletions src/config/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct LocalConfiguration {
pub relay_url: Url,
#[serde(default = "default_registry_url")]
pub registry_url: Url,
#[serde(default = "default_blockchain_api_endpoint")]
pub blockchain_api_endpoint: Option<String>,
}

fn default_bind_ip() -> IpAddr {
Expand Down Expand Up @@ -76,6 +78,10 @@ fn default_registry_url() -> Url {
"https://registry.walletconnect.com".parse().unwrap()
}

fn default_blockchain_api_endpoint() -> Option<String> {
Some("https://rpc.walletconnect.com".to_string())
}

pub async fn get_configuration() -> Result<Configuration, NotifyServerError> {
load_dot_env()?;
let config = envy::from_env::<LocalConfiguration>()?;
Expand All @@ -101,6 +107,7 @@ pub async fn get_configuration() -> Result<Configuration, NotifyServerError> {
project_id: config.project_id,
relay_url: config.relay_url,
relay_public_key,
blockchain_api_endpoint: config.blockchain_api_endpoint,
registry_url: config.registry_url,
registry_auth_token: config.registry_auth_token,
auth_redis_addr_read: None,
Expand Down
1 change: 1 addition & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Configuration {
pub postgres_max_connections: u32,
pub keypair_seed: String,
pub project_id: ProjectId,
pub blockchain_api_endpoint: Option<String>,
/// Relay URL e.g. https://relay.walletconnect.com
pub relay_url: Url,
pub relay_public_key: String,
Expand Down
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ use {
},
aws_config::meta::region::RegionProviderChain,
aws_sdk_s3::{config::Region, Client as S3Client},
blockchain_api::BlockchainApiProvider,
error::NotifyServerError,
rand::{rngs::StdRng, SeedableRng},
relay_rpc::auth::{
cacao::signature::eip1271::blockchain_api::BlockchainApiProvider, ed25519_dalek::SigningKey,
},
relay_rpc::auth::ed25519_dalek::SigningKey,
sqlx::postgres::PgPoolOptions,
std::sync::Arc,
tokio::{select, sync::broadcast},
Expand Down Expand Up @@ -99,6 +98,15 @@ pub async fn bootstrap(

let (relay_mailbox_clearer_tx, relay_mailbox_clearer_rx) = tokio::sync::mpsc::channel(1000);

let provider = if let Some(blockchain_api_endpoint) = &config.blockchain_api_endpoint {
Some(
BlockchainApiProvider::new(config.project_id.clone(), blockchain_api_endpoint.parse()?)
.await?,
)
} else {
None
};

let state = Arc::new(AppState::new(
analytics.clone(),
config.clone(),
Expand All @@ -111,7 +119,7 @@ pub async fn bootstrap(
registry,
relay_mailbox_clearer_tx,
config.clock,
BlockchainApiProvider::new(config.project_id),
provider,
)?);

let relay_renewal_job = relay_renewal_job::start(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub async fn handle(msg: RelayIncomingMessage, state: &AppState) -> Result<(), R
&request_auth.ksu,
&request_auth.sub,
state.redis.as_ref(),
&state.provider,
state.provider.as_ref(),
state.metrics.as_ref(),
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub async fn handle(msg: RelayIncomingMessage, state: &AppState) -> Result<(), R
&request_auth.ksu,
&request_auth.sub,
state.redis.as_ref(),
&state.provider,
state.provider.as_ref(),
state.metrics.as_ref(),
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub async fn handle(msg: RelayIncomingMessage, state: &AppState) -> Result<(), R
&request_auth.ksu,
&request_auth.sub,
state.redis.as_ref(),
&state.provider,
state.provider.as_ref(),
state.metrics.as_ref(),
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub async fn handle(msg: RelayIncomingMessage, state: &AppState) -> Result<(), R
&request_auth.ksu,
&request_auth.sub,
state.redis.as_ref(),
&state.provider,
state.provider.as_ref(),
state.metrics.as_ref(),
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub async fn handle(msg: RelayIncomingMessage, state: &AppState) -> Result<(), R
&request_auth.ksu,
&request_auth.sub,
state.redis.as_ref(),
&state.provider,
state.provider.as_ref(),
state.metrics.as_ref(),
)
.await?
Expand Down
10 changes: 4 additions & 6 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ use {
registry::{storage::redis::Redis, Registry},
Configuration,
},
blockchain_api::BlockchainApiProvider,
build_info::BuildInfo,
relay_client::http::Client,
relay_rpc::{
auth::{
cacao::signature::eip1271::blockchain_api::BlockchainApiProvider,
ed25519_dalek::{SigningKey, VerifyingKey},
},
auth::ed25519_dalek::{SigningKey, VerifyingKey},
domain::{DecodedClientId, DidKey},
rpc::Receipt,
},
Expand All @@ -39,7 +37,7 @@ pub struct AppState {
pub notify_keys: NotifyKeys,
pub relay_mailbox_clearer_tx: Sender<Receipt>,
pub clock: Clock,
pub provider: BlockchainApiProvider,
pub provider: Option<BlockchainApiProvider>,
}

build_info::build_info!(fn build_info);
Expand All @@ -58,7 +56,7 @@ impl AppState {
registry: Arc<Registry>,
relay_mailbox_clearer_tx: Sender<Receipt>,
clock: Clock,
provider: BlockchainApiProvider,
provider: Option<BlockchainApiProvider>,
) -> Result<Self, NotifyServerError> {
let build_info: &BuildInfo = build_info();

Expand Down
1 change: 1 addition & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ impl AsyncTestContext for NotifyServerContext {
relay_url,
relay_public_key,
notify_url: notify_url.clone(),
blockchain_api_endpoint: None,
registry_auth_token: "".to_owned(),
auth_redis_addr_read: Some("redis://localhost:6378/0".to_owned()),
auth_redis_addr_write: Some("redis://localhost:6378/0".to_owned()),
Expand Down

0 comments on commit 1314da6

Please sign in to comment.