merge: @main into client-integrity-verification
Some checks failed
ci/woodpecker/push/server-vet Pipeline failed
ci/woodpecker/push/server-lint Pipeline failed
ci/woodpecker/pr/useragent-analyze Pipeline failed
ci/woodpecker/pr/server-lint Pipeline failed
ci/woodpecker/push/useragent-analyze Pipeline failed
ci/woodpecker/push/server-test Pipeline failed
ci/woodpecker/pr/server-test Pipeline failed
ci/woodpecker/pr/server-vet Pipeline failed
ci/woodpecker/push/server-audit Pipeline was successful
ci/woodpecker/pr/server-audit Pipeline was successful
Some checks failed
ci/woodpecker/push/server-vet Pipeline failed
ci/woodpecker/push/server-lint Pipeline failed
ci/woodpecker/pr/useragent-analyze Pipeline failed
ci/woodpecker/pr/server-lint Pipeline failed
ci/woodpecker/push/useragent-analyze Pipeline failed
ci/woodpecker/push/server-test Pipeline failed
ci/woodpecker/pr/server-test Pipeline failed
ci/woodpecker/pr/server-vet Pipeline failed
ci/woodpecker/push/server-audit Pipeline was successful
ci/woodpecker/pr/server-audit Pipeline was successful
This commit was merged in pull request #43.
This commit is contained in:
2
server/Cargo.lock
generated
2
server/Cargo.lock
generated
@@ -724,6 +724,7 @@ name = "arbiter-server"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"alloy",
|
||||
"anyhow",
|
||||
"arbiter-proto",
|
||||
"arbiter-tokens-registry",
|
||||
"argon2",
|
||||
@@ -742,7 +743,6 @@ dependencies = [
|
||||
"k256",
|
||||
"kameo",
|
||||
"memsafe",
|
||||
"miette",
|
||||
"pem",
|
||||
"prost-types",
|
||||
"rand 0.10.0",
|
||||
|
||||
@@ -22,7 +22,6 @@ chrono = { version = "0.4.44", features = ["serde"] }
|
||||
rand = "0.10.0"
|
||||
rustls = { version = "0.23.37", features = ["aws-lc-rs"] }
|
||||
smlang = "0.8.0"
|
||||
miette = { version = "7.6.0", features = ["fancy", "serde"] }
|
||||
thiserror = "2.0.18"
|
||||
async-trait = "0.1.89"
|
||||
futures = "0.3.32"
|
||||
@@ -43,3 +42,4 @@ k256 = { version = "0.13.4", features = ["ecdsa", "pkcs8"] }
|
||||
rsa = { version = "0.9", features = ["sha2"] }
|
||||
sha2 = "0.10"
|
||||
spki = "0.7"
|
||||
miette = { version = "7.6.0", features = ["fancy", "serde"] }
|
||||
@@ -1,9 +1,17 @@
|
||||
use arbiter_proto::{
|
||||
ClientMetadata, format_challenge,
|
||||
proto::client::{
|
||||
AuthChallengeRequest, AuthChallengeSolution, AuthResult, ClientInfo as ProtoClientInfo,
|
||||
ClientRequest, client_request::Payload as ClientRequestPayload,
|
||||
client_response::Payload as ClientResponsePayload,
|
||||
proto::{
|
||||
client::{
|
||||
ClientRequest,
|
||||
auth::{
|
||||
self as proto_auth, AuthChallenge, AuthChallengeRequest, AuthChallengeSolution,
|
||||
AuthResult, request::Payload as AuthRequestPayload,
|
||||
response::Payload as AuthResponsePayload,
|
||||
},
|
||||
client_request::Payload as ClientRequestPayload,
|
||||
client_response::Payload as ClientResponsePayload,
|
||||
},
|
||||
shared::ClientInfo as ProtoClientInfo,
|
||||
},
|
||||
};
|
||||
use ed25519_dalek::Signer as _;
|
||||
@@ -51,16 +59,16 @@ async fn send_auth_challenge_request(
|
||||
transport
|
||||
.send(ClientRequest {
|
||||
request_id: next_request_id(),
|
||||
payload: Some(ClientRequestPayload::AuthChallengeRequest(
|
||||
AuthChallengeRequest {
|
||||
payload: Some(ClientRequestPayload::Auth(proto_auth::Request {
|
||||
payload: Some(AuthRequestPayload::ChallengeRequest(AuthChallengeRequest {
|
||||
pubkey: key.verifying_key().to_bytes().to_vec(),
|
||||
client_info: Some(ProtoClientInfo {
|
||||
name: metadata.name,
|
||||
description: metadata.description,
|
||||
version: metadata.version,
|
||||
}),
|
||||
},
|
||||
)),
|
||||
})),
|
||||
})),
|
||||
})
|
||||
.await
|
||||
.map_err(|_| AuthError::UnexpectedAuthResponse)
|
||||
@@ -68,7 +76,7 @@ async fn send_auth_challenge_request(
|
||||
|
||||
async fn receive_auth_challenge(
|
||||
transport: &mut ClientTransport,
|
||||
) -> std::result::Result<arbiter_proto::proto::client::AuthChallenge, AuthError> {
|
||||
) -> std::result::Result<AuthChallenge, AuthError> {
|
||||
let response = transport
|
||||
.recv()
|
||||
.await
|
||||
@@ -76,8 +84,11 @@ async fn receive_auth_challenge(
|
||||
|
||||
let payload = response.payload.ok_or(AuthError::MissingAuthChallenge)?;
|
||||
match payload {
|
||||
ClientResponsePayload::AuthChallenge(challenge) => Ok(challenge),
|
||||
ClientResponsePayload::AuthResult(result) => Err(map_auth_result(result)),
|
||||
ClientResponsePayload::Auth(response) => match response.payload {
|
||||
Some(AuthResponsePayload::Challenge(challenge)) => Ok(challenge),
|
||||
Some(AuthResponsePayload::Result(result)) => Err(map_auth_result(result)),
|
||||
None => Err(AuthError::MissingAuthChallenge),
|
||||
},
|
||||
_ => Err(AuthError::UnexpectedAuthResponse),
|
||||
}
|
||||
}
|
||||
@@ -85,7 +96,7 @@ async fn receive_auth_challenge(
|
||||
async fn send_auth_challenge_solution(
|
||||
transport: &mut ClientTransport,
|
||||
key: &ed25519_dalek::SigningKey,
|
||||
challenge: arbiter_proto::proto::client::AuthChallenge,
|
||||
challenge: AuthChallenge,
|
||||
) -> std::result::Result<(), AuthError> {
|
||||
let challenge_payload = format_challenge(challenge.nonce, &challenge.pubkey);
|
||||
let signature = key.sign(&challenge_payload).to_bytes().to_vec();
|
||||
@@ -93,9 +104,11 @@ async fn send_auth_challenge_solution(
|
||||
transport
|
||||
.send(ClientRequest {
|
||||
request_id: next_request_id(),
|
||||
payload: Some(ClientRequestPayload::AuthChallengeSolution(
|
||||
AuthChallengeSolution { signature },
|
||||
)),
|
||||
payload: Some(ClientRequestPayload::Auth(proto_auth::Request {
|
||||
payload: Some(AuthRequestPayload::ChallengeSolution(
|
||||
AuthChallengeSolution { signature },
|
||||
)),
|
||||
})),
|
||||
})
|
||||
.await
|
||||
.map_err(|_| AuthError::UnexpectedAuthResponse)
|
||||
@@ -111,12 +124,15 @@ async fn receive_auth_confirmation(
|
||||
|
||||
let payload = response.payload.ok_or(AuthError::UnexpectedAuthResponse)?;
|
||||
match payload {
|
||||
ClientResponsePayload::AuthResult(result)
|
||||
if AuthResult::try_from(result).ok() == Some(AuthResult::Success) =>
|
||||
{
|
||||
Ok(())
|
||||
}
|
||||
ClientResponsePayload::AuthResult(result) => Err(map_auth_result(result)),
|
||||
ClientResponsePayload::Auth(response) => match response.payload {
|
||||
Some(AuthResponsePayload::Result(result))
|
||||
if AuthResult::try_from(result).ok() == Some(AuthResult::Success) =>
|
||||
{
|
||||
Ok(())
|
||||
}
|
||||
Some(AuthResponsePayload::Result(result)) => Err(map_auth_result(result)),
|
||||
_ => Err(AuthError::UnexpectedAuthResponse),
|
||||
},
|
||||
_ => Err(AuthError::UnexpectedAuthResponse),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,4 @@ pub use client::{ArbiterClient, Error};
|
||||
pub use storage::{FileSigningKeyStorage, SigningKeyStorage, StorageError};
|
||||
|
||||
#[cfg(feature = "evm")]
|
||||
pub use wallets::evm::ArbiterEvmWallet;
|
||||
pub use wallets::evm::{ArbiterEvmSignTransactionError, ArbiterEvmWallet};
|
||||
|
||||
@@ -8,7 +8,49 @@ use async_trait::async_trait;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::transport::ClientTransport;
|
||||
use arbiter_proto::proto::{
|
||||
client::{
|
||||
ClientRequest,
|
||||
client_request::Payload as ClientRequestPayload,
|
||||
client_response::Payload as ClientResponsePayload,
|
||||
evm::{
|
||||
self as proto_evm, request::Payload as EvmRequestPayload,
|
||||
response::Payload as EvmResponsePayload,
|
||||
},
|
||||
},
|
||||
evm::{
|
||||
EvmSignTransactionRequest,
|
||||
evm_sign_transaction_response::Result as EvmSignTransactionResult,
|
||||
},
|
||||
shared::evm::TransactionEvalError,
|
||||
};
|
||||
|
||||
use crate::transport::{ClientTransport, next_request_id};
|
||||
|
||||
/// A typed error payload returned by [`ArbiterEvmWallet`] transaction signing.
|
||||
///
|
||||
/// This is wrapped into `alloy::signers::Error::Other`, so consumers can downcast by [`TryFrom`] and
|
||||
/// interpret the concrete policy evaluation failure instead of parsing strings.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[non_exhaustive]
|
||||
pub enum ArbiterEvmSignTransactionError {
|
||||
#[error("transaction rejected by policy: {0:?}")]
|
||||
PolicyEval(TransactionEvalError),
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a Error> for &'a ArbiterEvmSignTransactionError {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: &'a Error) -> Result<Self, Self::Error> {
|
||||
if let Error::Other(inner) = value
|
||||
&& let Some(eval_error) = inner.downcast_ref()
|
||||
{
|
||||
Ok(eval_error)
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ArbiterEvmWallet {
|
||||
transport: Arc<Mutex<ClientTransport>>,
|
||||
@@ -79,11 +121,72 @@ impl TxSigner<Signature> for ArbiterEvmWallet {
|
||||
&self,
|
||||
tx: &mut dyn SignableTransaction<Signature>,
|
||||
) -> Result<Signature> {
|
||||
let _transport = self.transport.lock().await;
|
||||
self.validate_chain_id(tx)?;
|
||||
|
||||
Err(Error::other(
|
||||
"transaction signing is not supported by current arbiter.client protocol",
|
||||
))
|
||||
let mut transport = self.transport.lock().await;
|
||||
let request_id = next_request_id();
|
||||
let rlp_transaction = tx.encoded_for_signing();
|
||||
|
||||
transport
|
||||
.send(ClientRequest {
|
||||
request_id,
|
||||
payload: Some(ClientRequestPayload::Evm(proto_evm::Request {
|
||||
payload: Some(EvmRequestPayload::SignTransaction(
|
||||
EvmSignTransactionRequest {
|
||||
wallet_address: self.address.to_vec(),
|
||||
rlp_transaction,
|
||||
},
|
||||
)),
|
||||
})),
|
||||
})
|
||||
.await
|
||||
.map_err(|_| Error::other("failed to send evm sign transaction request"))?;
|
||||
|
||||
let response = transport
|
||||
.recv()
|
||||
.await
|
||||
.map_err(|_| Error::other("failed to receive evm sign transaction response"))?;
|
||||
|
||||
if response.request_id != Some(request_id) {
|
||||
return Err(Error::other(
|
||||
"received mismatched response id for evm sign transaction",
|
||||
));
|
||||
}
|
||||
|
||||
let payload = response
|
||||
.payload
|
||||
.ok_or_else(|| Error::other("missing evm sign transaction response payload"))?;
|
||||
|
||||
let ClientResponsePayload::Evm(proto_evm::Response {
|
||||
payload: Some(payload),
|
||||
}) = payload
|
||||
else {
|
||||
return Err(Error::other(
|
||||
"unexpected response payload for evm sign transaction request",
|
||||
));
|
||||
};
|
||||
|
||||
let EvmResponsePayload::SignTransaction(response) = payload else {
|
||||
return Err(Error::other(
|
||||
"unexpected evm response payload for sign transaction request",
|
||||
));
|
||||
};
|
||||
|
||||
let result = response
|
||||
.result
|
||||
.ok_or_else(|| Error::other("missing evm sign transaction result"))?;
|
||||
|
||||
match result {
|
||||
EvmSignTransactionResult::Signature(signature) => {
|
||||
Signature::try_from(signature.as_slice())
|
||||
.map_err(|_| Error::other("invalid signature returned by server"))
|
||||
}
|
||||
EvmSignTransactionResult::EvalError(eval_error) => Err(Error::other(
|
||||
ArbiterEvmSignTransactionError::PolicyEval(eval_error),
|
||||
)),
|
||||
EvmSignTransactionResult::Error(code) => Err(Error::other(format!(
|
||||
"server failed to sign transaction with error code {code}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,56 @@ use base64::{Engine, prelude::BASE64_STANDARD};
|
||||
pub mod proto {
|
||||
tonic::include_proto!("arbiter");
|
||||
|
||||
pub mod shared {
|
||||
tonic::include_proto!("arbiter.shared");
|
||||
|
||||
pub mod evm {
|
||||
tonic::include_proto!("arbiter.shared.evm");
|
||||
}
|
||||
}
|
||||
|
||||
pub mod user_agent {
|
||||
tonic::include_proto!("arbiter.user_agent");
|
||||
|
||||
pub mod auth {
|
||||
tonic::include_proto!("arbiter.user_agent.auth");
|
||||
}
|
||||
|
||||
pub mod evm {
|
||||
tonic::include_proto!("arbiter.user_agent.evm");
|
||||
}
|
||||
|
||||
pub mod sdk_client {
|
||||
tonic::include_proto!("arbiter.user_agent.sdk_client");
|
||||
}
|
||||
|
||||
pub mod vault {
|
||||
tonic::include_proto!("arbiter.user_agent.vault");
|
||||
|
||||
pub mod bootstrap {
|
||||
tonic::include_proto!("arbiter.user_agent.vault.bootstrap");
|
||||
}
|
||||
|
||||
pub mod unseal {
|
||||
tonic::include_proto!("arbiter.user_agent.vault.unseal");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod client {
|
||||
tonic::include_proto!("arbiter.client");
|
||||
|
||||
pub mod auth {
|
||||
tonic::include_proto!("arbiter.client.auth");
|
||||
}
|
||||
|
||||
pub mod evm {
|
||||
tonic::include_proto!("arbiter.client.evm");
|
||||
}
|
||||
|
||||
pub mod vault {
|
||||
tonic::include_proto!("arbiter.client.vault");
|
||||
}
|
||||
}
|
||||
|
||||
pub mod evm {
|
||||
|
||||
@@ -25,7 +25,6 @@ tonic.features = ["tls-aws-lc"]
|
||||
tokio.workspace = true
|
||||
rustls.workspace = true
|
||||
smlang.workspace = true
|
||||
miette.workspace = true
|
||||
thiserror.workspace = true
|
||||
fatality = "0.1.1"
|
||||
diesel_migrations = { version = "2.3.1", features = ["sqlite"] }
|
||||
@@ -54,6 +53,7 @@ spki.workspace = true
|
||||
alloy.workspace = true
|
||||
prost-types.workspace = true
|
||||
arbiter-tokens-registry.path = "../arbiter-tokens-registry"
|
||||
anyhow = "1.0.102"
|
||||
|
||||
[dev-dependencies]
|
||||
insta = "1.46.3"
|
||||
|
||||
@@ -2,7 +2,7 @@ use arbiter_proto::{BOOTSTRAP_PATH, home_path};
|
||||
use diesel::QueryDsl;
|
||||
use diesel_async::RunQueryDsl;
|
||||
use kameo::{Actor, messages};
|
||||
use miette::Diagnostic;
|
||||
|
||||
use rand::{RngExt, distr::Alphanumeric, make_rng, rngs::StdRng};
|
||||
use thiserror::Error;
|
||||
|
||||
@@ -25,18 +25,15 @@ pub async fn generate_token() -> Result<String, std::io::Error> {
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
#[derive(Error, Debug, Diagnostic)]
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Database error: {0}")]
|
||||
#[diagnostic(code(arbiter_server::bootstrap::database))]
|
||||
Database(#[from] db::PoolError),
|
||||
|
||||
#[error("Database query error: {0}")]
|
||||
#[diagnostic(code(arbiter_server::bootstrap::database_query))]
|
||||
Query(#[from] diesel::result::Error),
|
||||
|
||||
#[error("I/O error: {0}")]
|
||||
#[diagnostic(code(arbiter_server::bootstrap::io))]
|
||||
Io(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
|
||||
@@ -84,7 +84,6 @@ async fn get_client_and_nonce(
|
||||
})?;
|
||||
|
||||
conn.exclusive_transaction(|conn| {
|
||||
let pubkey_bytes = pubkey_bytes.clone();
|
||||
Box::pin(async move {
|
||||
let Some((client_id, current_nonce)) = program_client::table
|
||||
.filter(program_client::public_key.eq(&pubkey_bytes))
|
||||
@@ -288,10 +287,7 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn authenticate<T>(
|
||||
props: &mut ClientConnection,
|
||||
transport: &mut T,
|
||||
) -> Result<VerifyingKey, Error>
|
||||
pub async fn authenticate<T>(props: &mut ClientConnection, transport: &mut T) -> Result<i32, Error>
|
||||
where
|
||||
T: Bi<Inbound, Result<Outbound, Error>> + Send + ?Sized,
|
||||
{
|
||||
@@ -319,7 +315,6 @@ where
|
||||
};
|
||||
|
||||
sync_client_metadata(&props.db, info.id, &metadata).await?;
|
||||
|
||||
challenge_client(transport, pubkey, info.current_nonce).await?;
|
||||
|
||||
transport
|
||||
@@ -330,5 +325,5 @@ where
|
||||
Error::Transport
|
||||
})?;
|
||||
|
||||
Ok(pubkey)
|
||||
Ok(info.id)
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ where
|
||||
T: Bi<auth::Inbound, Result<auth::Outbound, auth::Error>> + Send + ?Sized,
|
||||
{
|
||||
match auth::authenticate(&mut props, transport).await {
|
||||
Ok(_pubkey) => {
|
||||
ClientSession::spawn(ClientSession::new(props));
|
||||
Ok(client_id) => {
|
||||
ClientSession::spawn(ClientSession::new(props, client_id));
|
||||
info!("Client authenticated, session started");
|
||||
}
|
||||
Err(err) => {
|
||||
|
||||
@@ -1,21 +1,28 @@
|
||||
use kameo::{Actor, messages};
|
||||
use tracing::error;
|
||||
|
||||
use alloy::{consensus::TxEip1559, primitives::Address, signers::Signature};
|
||||
|
||||
use crate::{
|
||||
actors::{
|
||||
GlobalActors, client::ClientConnection, flow_coordinator::RegisterClient,
|
||||
GlobalActors,
|
||||
client::ClientConnection,
|
||||
evm::{ClientSignTransaction, SignTransactionError},
|
||||
flow_coordinator::RegisterClient,
|
||||
keyholder::KeyHolderState,
|
||||
},
|
||||
db,
|
||||
evm::VetError,
|
||||
};
|
||||
|
||||
pub struct ClientSession {
|
||||
props: ClientConnection,
|
||||
client_id: i32,
|
||||
}
|
||||
|
||||
impl ClientSession {
|
||||
pub(crate) fn new(props: ClientConnection) -> Self {
|
||||
Self { props }
|
||||
pub(crate) fn new(props: ClientConnection, client_id: i32) -> Self {
|
||||
Self { props, client_id }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +42,34 @@ impl ClientSession {
|
||||
|
||||
Ok(vault_state)
|
||||
}
|
||||
|
||||
#[message]
|
||||
pub(crate) async fn handle_sign_transaction(
|
||||
&mut self,
|
||||
wallet_address: Address,
|
||||
transaction: TxEip1559,
|
||||
) -> Result<Signature, SignTransactionRpcError> {
|
||||
match self
|
||||
.props
|
||||
.actors
|
||||
.evm
|
||||
.ask(ClientSignTransaction {
|
||||
client_id: self.client_id,
|
||||
wallet_address,
|
||||
transaction,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(signature) => Ok(signature),
|
||||
Err(kameo::error::SendError::HandlerError(SignTransactionError::Vet(vet_error))) => {
|
||||
Err(SignTransactionRpcError::Vet(vet_error))
|
||||
}
|
||||
Err(err) => {
|
||||
error!(?err, "Failed to sign EVM transaction in client session");
|
||||
Err(SignTransactionRpcError::Internal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Actor for ClientSession {
|
||||
@@ -59,7 +94,10 @@ impl Actor for ClientSession {
|
||||
impl ClientSession {
|
||||
pub fn new_test(db: db::DatabasePool, actors: GlobalActors) -> Self {
|
||||
let props = ClientConnection::new(db, actors);
|
||||
Self { props }
|
||||
Self {
|
||||
props,
|
||||
client_id: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,3 +108,12 @@ pub enum Error {
|
||||
#[error("Internal error")]
|
||||
Internal,
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum SignTransactionRpcError {
|
||||
#[error("Policy evaluation failed")]
|
||||
Vet(#[from] VetError),
|
||||
|
||||
#[error("Internal error")]
|
||||
Internal,
|
||||
}
|
||||
|
||||
@@ -25,45 +25,36 @@ use crate::{
|
||||
|
||||
pub use crate::evm::safe_signer;
|
||||
|
||||
#[derive(Debug, thiserror::Error, miette::Diagnostic)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum SignTransactionError {
|
||||
#[error("Wallet not found")]
|
||||
#[diagnostic(code(arbiter::evm::sign::wallet_not_found))]
|
||||
WalletNotFound,
|
||||
|
||||
#[error("Database error: {0}")]
|
||||
#[diagnostic(code(arbiter::evm::sign::database))]
|
||||
Database(#[from] DatabaseError),
|
||||
|
||||
#[error("Keyholder error: {0}")]
|
||||
#[diagnostic(code(arbiter::evm::sign::keyholder))]
|
||||
Keyholder(#[from] crate::actors::keyholder::Error),
|
||||
|
||||
#[error("Keyholder mailbox error")]
|
||||
#[diagnostic(code(arbiter::evm::sign::keyholder_send))]
|
||||
KeyholderSend,
|
||||
|
||||
#[error("Signing error: {0}")]
|
||||
#[diagnostic(code(arbiter::evm::sign::signing))]
|
||||
Signing(#[from] alloy::signers::Error),
|
||||
|
||||
#[error("Policy error: {0}")]
|
||||
#[diagnostic(code(arbiter::evm::sign::vet))]
|
||||
Vet(#[from] evm::VetError),
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error, miette::Diagnostic)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("Keyholder error: {0}")]
|
||||
#[diagnostic(code(arbiter::evm::keyholder))]
|
||||
Keyholder(#[from] crate::actors::keyholder::Error),
|
||||
|
||||
#[error("Keyholder mailbox error")]
|
||||
#[diagnostic(code(arbiter::evm::keyholder_send))]
|
||||
KeyholderSend,
|
||||
|
||||
#[error("Database error: {0}")]
|
||||
#[diagnostic(code(arbiter::evm::database))]
|
||||
Database(#[from] DatabaseError),
|
||||
}
|
||||
|
||||
|
||||
@@ -39,36 +39,28 @@ enum State {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error, miette::Diagnostic)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("Keyholder is already bootstrapped")]
|
||||
#[diagnostic(code(arbiter::keyholder::already_bootstrapped))]
|
||||
AlreadyBootstrapped,
|
||||
#[error("Keyholder is not bootstrapped")]
|
||||
#[diagnostic(code(arbiter::keyholder::not_bootstrapped))]
|
||||
NotBootstrapped,
|
||||
#[error("Invalid key provided")]
|
||||
#[diagnostic(code(arbiter::keyholder::invalid_key))]
|
||||
InvalidKey,
|
||||
|
||||
#[error("Requested aead entry not found")]
|
||||
#[diagnostic(code(arbiter::keyholder::aead_not_found))]
|
||||
NotFound,
|
||||
|
||||
#[error("Encryption error: {0}")]
|
||||
#[diagnostic(code(arbiter::keyholder::encryption_error))]
|
||||
Encryption(#[from] chacha20poly1305::aead::Error),
|
||||
|
||||
#[error("Database error: {0}")]
|
||||
#[diagnostic(code(arbiter::keyholder::database_error))]
|
||||
DatabaseConnection(#[from] db::PoolError),
|
||||
|
||||
#[error("Database transaction error: {0}")]
|
||||
#[diagnostic(code(arbiter::keyholder::database_transaction_error))]
|
||||
DatabaseTransaction(#[from] diesel::result::Error),
|
||||
|
||||
#[error("Broken database")]
|
||||
#[diagnostic(code(arbiter::keyholder::broken_database))]
|
||||
BrokenDatabase,
|
||||
}
|
||||
|
||||
@@ -217,7 +209,6 @@ impl KeyHolder {
|
||||
let mut conn = self.db.get().await?;
|
||||
schema::root_key_history::table
|
||||
.filter(schema::root_key_history::id.eq(*root_key_history_id))
|
||||
.select(schema::root_key_history::data_encryption_nonce)
|
||||
.select(RootKeyHistory::as_select())
|
||||
.first(&mut conn)
|
||||
.await?
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use kameo::actor::{ActorRef, Spawn};
|
||||
use miette::Diagnostic;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
@@ -17,14 +16,12 @@ pub mod flow_coordinator;
|
||||
pub mod keyholder;
|
||||
pub mod user_agent;
|
||||
|
||||
#[derive(Error, Debug, Diagnostic)]
|
||||
#[derive(Error, Debug)]
|
||||
pub enum SpawnError {
|
||||
#[error("Failed to spawn Bootstrapper actor")]
|
||||
#[diagnostic(code(SpawnError::Bootstrapper))]
|
||||
Bootstrapper(#[from] bootstrap::Error),
|
||||
|
||||
#[error("Failed to spawn KeyHolder actor")]
|
||||
#[diagnostic(code(SpawnError::KeyHolder))]
|
||||
KeyHolder(#[from] keyholder::Error),
|
||||
}
|
||||
|
||||
|
||||
@@ -338,7 +338,6 @@ where
|
||||
};
|
||||
|
||||
let Some(expected_tag) = self.try_sign_pubkey_integrity_tag(pubkey).await? else {
|
||||
// Vault sealed/unbootstrapped: cannot verify integrity yet.
|
||||
return Ok(AttestationStatus::Unavailable);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::sync::Mutex;
|
||||
|
||||
use alloy::primitives::Address;
|
||||
use alloy::{consensus::TxEip1559, primitives::Address, signers::Signature};
|
||||
use chacha20poly1305::{AeadInPlace, XChaCha20Poly1305, XNonce, aead::KeyInit};
|
||||
use diesel::{ExpressionMethods as _, QueryDsl as _, SelectableHelper};
|
||||
use diesel_async::{AsyncConnection, RunQueryDsl};
|
||||
@@ -21,7 +21,8 @@ use crate::safe_cell::SafeCell;
|
||||
use crate::{
|
||||
actors::{
|
||||
evm::{
|
||||
Generate, ListWallets, UseragentCreateGrant, UseragentDeleteGrant, UseragentListGrants,
|
||||
ClientSignTransaction, Generate, ListWallets, SignTransactionError as EvmSignError,
|
||||
UseragentCreateGrant, UseragentDeleteGrant, UseragentListGrants,
|
||||
},
|
||||
keyholder::{self, Bootstrap, TryUnseal},
|
||||
user_agent::session::{
|
||||
@@ -110,6 +111,15 @@ pub enum BootstrapError {
|
||||
General(#[from] super::Error),
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SignTransactionError {
|
||||
#[error("Policy evaluation failed")]
|
||||
Vet(#[from] crate::evm::VetError),
|
||||
|
||||
#[error("Internal signing error")]
|
||||
Internal,
|
||||
}
|
||||
|
||||
#[messages]
|
||||
impl UserAgentSession {
|
||||
#[message]
|
||||
@@ -354,6 +364,35 @@ impl UserAgentSession {
|
||||
}
|
||||
}
|
||||
|
||||
#[message]
|
||||
pub(crate) async fn handle_sign_transaction(
|
||||
&mut self,
|
||||
client_id: i32,
|
||||
wallet_address: Address,
|
||||
transaction: TxEip1559,
|
||||
) -> Result<Signature, SignTransactionError> {
|
||||
match self
|
||||
.props
|
||||
.actors
|
||||
.evm
|
||||
.ask(ClientSignTransaction {
|
||||
client_id,
|
||||
wallet_address,
|
||||
transaction,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(signature) => Ok(signature),
|
||||
Err(SendError::HandlerError(EvmSignError::Vet(vet_error))) => {
|
||||
Err(SignTransactionError::Vet(vet_error))
|
||||
}
|
||||
Err(err) => {
|
||||
error!(?err, "EVM sign transaction failed in user-agent session");
|
||||
Err(SignTransactionError::Internal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[message]
|
||||
pub(crate) async fn handle_grant_evm_wallet_access(
|
||||
&mut self,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use miette::Diagnostic;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
@@ -11,30 +10,24 @@ use crate::{
|
||||
|
||||
pub mod tls;
|
||||
|
||||
#[derive(Error, Debug, Diagnostic)]
|
||||
#[derive(Error, Debug)]
|
||||
pub enum InitError {
|
||||
#[error("Database setup failed: {0}")]
|
||||
#[diagnostic(code(arbiter_server::init::database_setup))]
|
||||
DatabaseSetup(#[from] db::DatabaseSetupError),
|
||||
|
||||
#[error("Connection acquire failed: {0}")]
|
||||
#[diagnostic(code(arbiter_server::init::database_pool))]
|
||||
DatabasePool(#[from] db::PoolError),
|
||||
|
||||
#[error("Database query error: {0}")]
|
||||
#[diagnostic(code(arbiter_server::init::database_query))]
|
||||
DatabaseQuery(#[from] diesel::result::Error),
|
||||
|
||||
#[error("TLS initialization failed: {0}")]
|
||||
#[diagnostic(code(arbiter_server::init::tls_init))]
|
||||
Tls(#[from] tls::InitError),
|
||||
|
||||
#[error("Actor spawn failed: {0}")]
|
||||
#[diagnostic(code(arbiter_server::init::actor_spawn))]
|
||||
ActorSpawn(#[from] crate::actors::SpawnError),
|
||||
|
||||
#[error("I/O Error: {0}")]
|
||||
#[diagnostic(code(arbiter_server::init::io))]
|
||||
Io(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::{net::IpAddr, string::FromUtf8Error};
|
||||
use std::{net::Ipv4Addr, string::FromUtf8Error};
|
||||
|
||||
use diesel::{ExpressionMethods as _, QueryDsl, SelectableHelper as _};
|
||||
use diesel_async::{AsyncConnection, RunQueryDsl};
|
||||
use miette::Diagnostic;
|
||||
|
||||
use pem::Pem;
|
||||
use rcgen::{
|
||||
BasicConstraints, Certificate, CertificateParams, CertifiedIssuer, DistinguishedName, DnType,
|
||||
@@ -29,30 +29,24 @@ const ENCODE_CONFIG: pem::EncodeConfig = {
|
||||
pem::EncodeConfig::new().set_line_ending(line_ending)
|
||||
};
|
||||
|
||||
#[derive(Error, Debug, Diagnostic)]
|
||||
#[derive(Error, Debug)]
|
||||
pub enum InitError {
|
||||
#[error("Key generation error during TLS initialization: {0}")]
|
||||
#[diagnostic(code(arbiter_server::tls_init::key_generation))]
|
||||
KeyGeneration(#[from] rcgen::Error),
|
||||
|
||||
#[error("Key invalid format: {0}")]
|
||||
#[diagnostic(code(arbiter_server::tls_init::key_invalid_format))]
|
||||
KeyInvalidFormat(#[from] FromUtf8Error),
|
||||
|
||||
#[error("Key deserialization error: {0}")]
|
||||
#[diagnostic(code(arbiter_server::tls_init::key_deserialization))]
|
||||
KeyDeserializationError(rcgen::Error),
|
||||
|
||||
#[error("Database error during TLS initialization: {0}")]
|
||||
#[diagnostic(code(arbiter_server::tls_init::database_error))]
|
||||
DatabaseError(#[from] diesel::result::Error),
|
||||
|
||||
#[error("Pem deserialization error during TLS initialization: {0}")]
|
||||
#[diagnostic(code(arbiter_server::tls_init::pem_deserialization))]
|
||||
PemDeserializationError(#[from] rustls::pki_types::pem::Error),
|
||||
|
||||
#[error("Database pool acquire error during TLS initialization: {0}")]
|
||||
#[diagnostic(code(arbiter_server::tls_init::database_pool_acquire))]
|
||||
DatabasePoolAcquire(#[from] db::PoolError),
|
||||
}
|
||||
|
||||
@@ -116,7 +110,7 @@ impl TlsCa {
|
||||
];
|
||||
params
|
||||
.subject_alt_names
|
||||
.push(SanType::IpAddress(IpAddr::from([127, 0, 0, 1])));
|
||||
.push(SanType::IpAddress(Ipv4Addr::LOCALHOST.into()));
|
||||
|
||||
let mut dn = DistinguishedName::new();
|
||||
dn.push(DnType::CommonName, "Arbiter Instance Leaf");
|
||||
|
||||
@@ -5,7 +5,7 @@ use diesel_async::{
|
||||
sync_connection_wrapper::SyncConnectionWrapper,
|
||||
};
|
||||
use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations};
|
||||
use miette::Diagnostic;
|
||||
|
||||
use thiserror::Error;
|
||||
use tracing::info;
|
||||
|
||||
@@ -21,26 +21,21 @@ static DB_FILE: &str = "arbiter.sqlite";
|
||||
|
||||
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
|
||||
|
||||
#[derive(Error, Diagnostic, Debug)]
|
||||
#[derive(Error, Debug)]
|
||||
pub enum DatabaseSetupError {
|
||||
#[error("Failed to determine home directory")]
|
||||
#[diagnostic(code(arbiter::db::home_dir))]
|
||||
HomeDir(std::io::Error),
|
||||
|
||||
#[error(transparent)]
|
||||
#[diagnostic(code(arbiter::db::connection))]
|
||||
Connection(diesel::ConnectionError),
|
||||
|
||||
#[error(transparent)]
|
||||
#[diagnostic(code(arbiter::db::concurrency))]
|
||||
ConcurrencySetup(diesel::result::Error),
|
||||
|
||||
#[error(transparent)]
|
||||
#[diagnostic(code(arbiter::db::migration))]
|
||||
Migration(Box<dyn std::error::Error + Send + Sync>),
|
||||
|
||||
#[error(transparent)]
|
||||
#[diagnostic(code(arbiter::db::pool))]
|
||||
Pool(#[from] PoolInitError),
|
||||
}
|
||||
|
||||
|
||||
@@ -28,39 +28,32 @@ pub mod policies;
|
||||
mod utils;
|
||||
|
||||
/// Errors that can only occur once the transaction meaning is known (during policy evaluation)
|
||||
#[derive(Debug, thiserror::Error, miette::Diagnostic)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum PolicyError {
|
||||
#[error("Database error")]
|
||||
Error(#[from] crate::db::DatabaseError),
|
||||
Database(#[from] crate::db::DatabaseError),
|
||||
#[error("Transaction violates policy: {0:?}")]
|
||||
#[diagnostic(code(arbiter_server::evm::policy_error::violation))]
|
||||
Violations(Vec<EvalViolation>),
|
||||
#[error("No matching grant found")]
|
||||
#[diagnostic(code(arbiter_server::evm::policy_error::no_matching_grant))]
|
||||
NoMatchingGrant,
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error, miette::Diagnostic)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum VetError {
|
||||
#[error("Contract creation transactions are not supported")]
|
||||
#[diagnostic(code(arbiter_server::evm::vet_error::contract_creation_unsupported))]
|
||||
ContractCreationNotSupported,
|
||||
#[error("Engine can't classify this transaction")]
|
||||
#[diagnostic(code(arbiter_server::evm::vet_error::unsupported))]
|
||||
UnsupportedTransactionType,
|
||||
#[error("Policy evaluation failed: {1}")]
|
||||
#[diagnostic(code(arbiter_server::evm::vet_error::evaluated))]
|
||||
Evaluated(SpecificMeaning, #[source] PolicyError),
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error, miette::Diagnostic)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum AnalyzeError {
|
||||
#[error("Engine doesn't support granting permissions for contract creation")]
|
||||
#[diagnostic(code(arbiter_server::evm::analyze_error::contract_creation_not_supported))]
|
||||
ContractCreationNotSupported,
|
||||
|
||||
#[error("Unsupported transaction type")]
|
||||
#[diagnostic(code(arbiter_server::evm::analyze_error::unsupported_transaction_type))]
|
||||
UnsupportedTransactionType,
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ use diesel::{
|
||||
ExpressionMethods as _, QueryDsl, SelectableHelper, result::QueryResult, sqlite::Sqlite,
|
||||
};
|
||||
use diesel_async::{AsyncConnection, RunQueryDsl};
|
||||
use miette::Diagnostic;
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
@@ -33,33 +33,27 @@ pub struct EvalContext {
|
||||
pub max_priority_fee_per_gas: u128,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
#[derive(Debug, Error)]
|
||||
pub enum EvalViolation {
|
||||
#[error("This grant doesn't allow transactions to the target address {target}")]
|
||||
#[diagnostic(code(arbiter_server::evm::eval_violation::invalid_target))]
|
||||
InvalidTarget { target: Address },
|
||||
|
||||
#[error("Gas limit exceeded for this grant")]
|
||||
#[diagnostic(code(arbiter_server::evm::eval_violation::gas_limit_exceeded))]
|
||||
GasLimitExceeded {
|
||||
max_gas_fee_per_gas: Option<U256>,
|
||||
max_priority_fee_per_gas: Option<U256>,
|
||||
},
|
||||
|
||||
#[error("Rate limit exceeded for this grant")]
|
||||
#[diagnostic(code(arbiter_server::evm::eval_violation::rate_limit_exceeded))]
|
||||
RateLimitExceeded,
|
||||
|
||||
#[error("Transaction exceeds volumetric limits of the grant")]
|
||||
#[diagnostic(code(arbiter_server::evm::eval_violation::volumetric_limit_exceeded))]
|
||||
VolumetricLimitExceeded,
|
||||
|
||||
#[error("Transaction is outside of the grant's validity period")]
|
||||
#[diagnostic(code(arbiter_server::evm::eval_violation::invalid_time))]
|
||||
InvalidTime,
|
||||
|
||||
#[error("Transaction type is not allowed by this grant")]
|
||||
#[diagnostic(code(arbiter_server::evm::eval_violation::invalid_transaction_type))]
|
||||
InvalidTransactionType,
|
||||
}
|
||||
|
||||
|
||||
@@ -36,8 +36,8 @@ use super::{DatabaseID, EvalContext, EvalViolation};
|
||||
// Plain ether transfer
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Meaning {
|
||||
to: Address,
|
||||
value: U256,
|
||||
pub(crate) to: Address,
|
||||
pub(crate) value: U256,
|
||||
}
|
||||
impl Display for Meaning {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
@@ -91,6 +91,7 @@ async fn query_relevant_past_transaction(
|
||||
|
||||
async fn check_rate_limits(
|
||||
grant: &Grant<Settings>,
|
||||
current_transfer_value: U256,
|
||||
db: &mut impl AsyncConnection<Backend = Sqlite>,
|
||||
) -> QueryResult<Vec<EvalViolation>> {
|
||||
let mut violations = Vec::new();
|
||||
@@ -99,12 +100,12 @@ async fn check_rate_limits(
|
||||
let past_transaction = query_relevant_past_transaction(grant.id, window, db).await?;
|
||||
|
||||
let window_start = chrono::Utc::now() - grant.settings.limit.window;
|
||||
let cumulative_volume: U256 = past_transaction
|
||||
let prospective_cumulative_volume: U256 = past_transaction
|
||||
.iter()
|
||||
.filter(|(_, timestamp)| timestamp >= &window_start)
|
||||
.fold(U256::default(), |acc, (value, _)| acc + *value);
|
||||
.fold(current_transfer_value, |acc, (value, _)| acc + *value);
|
||||
|
||||
if cumulative_volume > grant.settings.limit.max_volume {
|
||||
if prospective_cumulative_volume > grant.settings.limit.max_volume {
|
||||
violations.push(EvalViolation::VolumetricLimitExceeded);
|
||||
}
|
||||
|
||||
@@ -141,7 +142,7 @@ impl Policy for EtherTransfer {
|
||||
violations.push(EvalViolation::InvalidTarget { target: meaning.to });
|
||||
}
|
||||
|
||||
let rate_violations = check_rate_limits(grant, db).await?;
|
||||
let rate_violations = check_rate_limits(grant, meaning.value, db).await?;
|
||||
violations.extend(rate_violations);
|
||||
|
||||
Ok(violations)
|
||||
|
||||
@@ -198,7 +198,7 @@ async fn evaluate_rejects_volume_over_limit() {
|
||||
grant_id,
|
||||
wallet_access_id: WALLET_ACCESS_ID,
|
||||
chain_id: CHAIN_ID as i32,
|
||||
eth_value: utils::u256_to_bytes(U256::from(1_001u64)).to_vec(),
|
||||
eth_value: utils::u256_to_bytes(U256::from(1_000u64)).to_vec(),
|
||||
signed_at: SqliteTimestamp(Utc::now()),
|
||||
})
|
||||
.execute(&mut *conn)
|
||||
@@ -211,7 +211,7 @@ async fn evaluate_rejects_volume_over_limit() {
|
||||
shared: shared(),
|
||||
settings,
|
||||
};
|
||||
let context = ctx(ALLOWED, U256::from(100u64));
|
||||
let context = ctx(ALLOWED, U256::from(1u64));
|
||||
let m = EtherTransfer::analyze(&context).unwrap();
|
||||
let v = EtherTransfer::evaluate(&context, &m, &grant, &mut *conn)
|
||||
.await
|
||||
@@ -233,13 +233,13 @@ async fn evaluate_passes_at_exactly_volume_limit() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Exactly at the limit — the check is `>`, so this should not violate
|
||||
// Exactly at the limit including current transfer — check is `>`, so this should not violate
|
||||
insert_into(evm_transaction_log::table)
|
||||
.values(NewEvmTransactionLog {
|
||||
grant_id,
|
||||
wallet_access_id: WALLET_ACCESS_ID,
|
||||
chain_id: CHAIN_ID as i32,
|
||||
eth_value: utils::u256_to_bytes(U256::from(1_000u64)).to_vec(),
|
||||
eth_value: utils::u256_to_bytes(U256::from(900u64)).to_vec(),
|
||||
signed_at: SqliteTimestamp(Utc::now()),
|
||||
})
|
||||
.execute(&mut *conn)
|
||||
|
||||
@@ -38,9 +38,9 @@ fn grant_join() -> _ {
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Meaning {
|
||||
token: &'static TokenInfo,
|
||||
to: Address,
|
||||
value: U256,
|
||||
pub(crate) token: &'static TokenInfo,
|
||||
pub(crate) to: Address,
|
||||
pub(crate) value: U256,
|
||||
}
|
||||
impl std::fmt::Display for Meaning {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
@@ -101,6 +101,7 @@ async fn query_relevant_past_transfers(
|
||||
|
||||
async fn check_volume_rate_limits(
|
||||
grant: &Grant<Settings>,
|
||||
current_transfer_value: U256,
|
||||
db: &mut impl AsyncConnection<Backend = Sqlite>,
|
||||
) -> QueryResult<Vec<EvalViolation>> {
|
||||
let mut violations = Vec::new();
|
||||
@@ -113,12 +114,12 @@ async fn check_volume_rate_limits(
|
||||
|
||||
for limit in &grant.settings.volume_limits {
|
||||
let window_start = chrono::Utc::now() - limit.window;
|
||||
let cumulative_volume: U256 = past_transfers
|
||||
let prospective_cumulative_volume: U256 = past_transfers
|
||||
.iter()
|
||||
.filter(|(_, timestamp)| timestamp >= &window_start)
|
||||
.fold(U256::default(), |acc, (value, _)| acc + *value);
|
||||
.fold(current_transfer_value, |acc, (value, _)| acc + *value);
|
||||
|
||||
if cumulative_volume > limit.max_volume {
|
||||
if prospective_cumulative_volume > limit.max_volume {
|
||||
violations.push(EvalViolation::VolumetricLimitExceeded);
|
||||
break;
|
||||
}
|
||||
@@ -163,7 +164,7 @@ impl Policy for TokenTransfer {
|
||||
violations.push(EvalViolation::InvalidTarget { target: meaning.to });
|
||||
}
|
||||
|
||||
let rate_violations = check_volume_rate_limits(grant, db).await?;
|
||||
let rate_violations = check_volume_rate_limits(grant, meaning.value, db).await?;
|
||||
violations.extend(rate_violations);
|
||||
|
||||
Ok(violations)
|
||||
|
||||
@@ -220,7 +220,7 @@ async fn evaluate_rejects_wrong_restricted_recipient() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn evaluate_passes_volume_within_limit() {
|
||||
async fn evaluate_passes_volume_at_exact_limit() {
|
||||
let db = db::create_test_pool().await;
|
||||
let mut conn = db.get().await.unwrap();
|
||||
|
||||
@@ -230,7 +230,7 @@ async fn evaluate_passes_volume_within_limit() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Record a past transfer of 500 (within 1000 limit)
|
||||
// Record a past transfer of 900, with current transfer 100 => exactly 1000 limit
|
||||
use crate::db::{models::NewEvmTokenTransferLog, schema::evm_token_transfer_log};
|
||||
insert_into(evm_token_transfer_log::table)
|
||||
.values(NewEvmTokenTransferLog {
|
||||
@@ -239,7 +239,7 @@ async fn evaluate_passes_volume_within_limit() {
|
||||
chain_id: CHAIN_ID as i32,
|
||||
token_contract: DAI.to_vec(),
|
||||
recipient_address: RECIPIENT.to_vec(),
|
||||
value: utils::u256_to_bytes(U256::from(500u64)).to_vec(),
|
||||
value: utils::u256_to_bytes(U256::from(900u64)).to_vec(),
|
||||
})
|
||||
.execute(&mut *conn)
|
||||
.await
|
||||
@@ -282,7 +282,7 @@ async fn evaluate_rejects_volume_over_limit() {
|
||||
chain_id: CHAIN_ID as i32,
|
||||
token_contract: DAI.to_vec(),
|
||||
recipient_address: RECIPIENT.to_vec(),
|
||||
value: utils::u256_to_bytes(U256::from(1_001u64)).to_vec(),
|
||||
value: utils::u256_to_bytes(U256::from(1_000u64)).to_vec(),
|
||||
})
|
||||
.execute(&mut *conn)
|
||||
.await
|
||||
@@ -294,7 +294,7 @@ async fn evaluate_rejects_volume_over_limit() {
|
||||
shared: shared(),
|
||||
settings,
|
||||
};
|
||||
let calldata = transfer_calldata(RECIPIENT, U256::from(100u64));
|
||||
let calldata = transfer_calldata(RECIPIENT, U256::from(1u64));
|
||||
let context = ctx(DAI, calldata);
|
||||
let m = TokenTransfer::analyze(&context).unwrap();
|
||||
let v = TokenTransfer::evaluate(&context, &m, &grant, &mut *conn)
|
||||
|
||||
@@ -1,32 +1,24 @@
|
||||
use arbiter_proto::{
|
||||
proto::client::{
|
||||
ClientRequest, ClientResponse, VaultState as ProtoVaultState,
|
||||
client_request::Payload as ClientRequestPayload,
|
||||
ClientRequest, ClientResponse, client_request::Payload as ClientRequestPayload,
|
||||
client_response::Payload as ClientResponsePayload,
|
||||
},
|
||||
transport::{Receiver, Sender, grpc::GrpcBi},
|
||||
};
|
||||
use kameo::{
|
||||
actor::{ActorRef, Spawn as _},
|
||||
error::SendError,
|
||||
};
|
||||
use kameo::actor::{ActorRef, Spawn as _};
|
||||
use tonic::Status;
|
||||
use tracing::{info, warn};
|
||||
|
||||
use crate::{
|
||||
actors::{
|
||||
client::{
|
||||
self, ClientConnection,
|
||||
session::{ClientSession, Error, HandleQueryVaultState},
|
||||
},
|
||||
keyholder::KeyHolderState,
|
||||
},
|
||||
actors::client::{ClientConnection, session::ClientSession},
|
||||
grpc::request_tracker::RequestTracker,
|
||||
};
|
||||
|
||||
mod auth;
|
||||
mod evm;
|
||||
mod inbound;
|
||||
mod outbound;
|
||||
mod vault;
|
||||
|
||||
async fn dispatch_loop(
|
||||
mut bi: GrpcBi<ClientRequest, ClientResponse>,
|
||||
@@ -89,21 +81,10 @@ async fn dispatch_inner(
|
||||
payload: ClientRequestPayload,
|
||||
) -> Result<ClientResponsePayload, Status> {
|
||||
match payload {
|
||||
ClientRequestPayload::QueryVaultState(_) => {
|
||||
let state = match actor.ask(HandleQueryVaultState {}).await {
|
||||
Ok(KeyHolderState::Unbootstrapped) => ProtoVaultState::Unbootstrapped,
|
||||
Ok(KeyHolderState::Sealed) => ProtoVaultState::Sealed,
|
||||
Ok(KeyHolderState::Unsealed) => ProtoVaultState::Unsealed,
|
||||
Err(SendError::HandlerError(Error::Internal)) => ProtoVaultState::Error,
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to query vault state");
|
||||
ProtoVaultState::Error
|
||||
}
|
||||
};
|
||||
Ok(ClientResponsePayload::VaultState(state.into()))
|
||||
}
|
||||
payload => {
|
||||
warn!(?payload, "Unsupported post-auth client request");
|
||||
ClientRequestPayload::Vault(req) => vault::dispatch(actor, req).await,
|
||||
ClientRequestPayload::Evm(req) => evm::dispatch(actor, req).await,
|
||||
ClientRequestPayload::Auth(..) => {
|
||||
warn!("Unsupported post-auth client auth request");
|
||||
Err(Status::invalid_argument("Unsupported client request"))
|
||||
}
|
||||
}
|
||||
@@ -112,14 +93,21 @@ async fn dispatch_inner(
|
||||
pub async fn start(mut conn: ClientConnection, mut bi: GrpcBi<ClientRequest, ClientResponse>) {
|
||||
let mut request_tracker = RequestTracker::default();
|
||||
|
||||
if let Err(e) = auth::start(&mut conn, &mut bi, &mut request_tracker).await {
|
||||
let mut transport = auth::AuthTransportAdapter::new(&mut bi, &mut request_tracker);
|
||||
let _ = transport.send(Err(e.clone())).await;
|
||||
warn!(error = ?e, "Client authentication failed");
|
||||
return;
|
||||
let client_id = match auth::start(&mut conn, &mut bi, &mut request_tracker).await {
|
||||
Ok(id) => id,
|
||||
Err(err) => {
|
||||
let _ = bi
|
||||
.send(Err(Status::unauthenticated(format!(
|
||||
"Authentication failed: {}",
|
||||
err
|
||||
))))
|
||||
.await;
|
||||
warn!(error = ?err, "Client authentication failed");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let actor = client::session::ClientSession::spawn(client::session::ClientSession::new(conn));
|
||||
let actor = ClientSession::spawn(ClientSession::new(conn, client_id));
|
||||
let actor_for_cleanup = actor.clone();
|
||||
|
||||
info!("Client authenticated successfully");
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
use arbiter_proto::{
|
||||
ClientMetadata,
|
||||
proto::client::{
|
||||
AuthChallenge as ProtoAuthChallenge, AuthChallengeRequest as ProtoAuthChallengeRequest,
|
||||
AuthChallengeSolution as ProtoAuthChallengeSolution, AuthResult as ProtoAuthResult,
|
||||
ClientInfo as ProtoClientInfo, ClientRequest, ClientResponse,
|
||||
client_request::Payload as ClientRequestPayload,
|
||||
client_response::Payload as ClientResponsePayload,
|
||||
proto::{
|
||||
client::{
|
||||
ClientRequest, ClientResponse,
|
||||
auth::{
|
||||
self as proto_auth, AuthChallenge as ProtoAuthChallenge,
|
||||
AuthChallengeRequest as ProtoAuthChallengeRequest,
|
||||
AuthChallengeSolution as ProtoAuthChallengeSolution, AuthResult as ProtoAuthResult,
|
||||
request::Payload as AuthRequestPayload, response::Payload as AuthResponsePayload,
|
||||
},
|
||||
client_request::Payload as ClientRequestPayload,
|
||||
client_response::Payload as ClientResponsePayload,
|
||||
},
|
||||
shared::ClientInfo as ProtoClientInfo,
|
||||
},
|
||||
transport::{Bi, Error as TransportError, Receiver, Sender, grpc::GrpcBi},
|
||||
};
|
||||
@@ -34,22 +41,22 @@ impl<'a> AuthTransportAdapter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn response_to_proto(response: auth::Outbound) -> ClientResponsePayload {
|
||||
fn response_to_proto(response: auth::Outbound) -> AuthResponsePayload {
|
||||
match response {
|
||||
auth::Outbound::AuthChallenge { pubkey, nonce } => {
|
||||
ClientResponsePayload::AuthChallenge(ProtoAuthChallenge {
|
||||
AuthResponsePayload::Challenge(ProtoAuthChallenge {
|
||||
pubkey: pubkey.to_bytes().to_vec(),
|
||||
nonce,
|
||||
})
|
||||
}
|
||||
auth::Outbound::AuthSuccess => {
|
||||
ClientResponsePayload::AuthResult(ProtoAuthResult::Success.into())
|
||||
AuthResponsePayload::Result(ProtoAuthResult::Success.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn error_to_proto(error: auth::Error) -> ClientResponsePayload {
|
||||
ClientResponsePayload::AuthResult(
|
||||
fn error_to_proto(error: auth::Error) -> AuthResponsePayload {
|
||||
AuthResponsePayload::Result(
|
||||
match error {
|
||||
auth::Error::InvalidChallengeSolution => ProtoAuthResult::InvalidSignature,
|
||||
auth::Error::ApproveError(auth::ApproveError::Denied) => {
|
||||
@@ -69,18 +76,20 @@ impl<'a> AuthTransportAdapter<'a> {
|
||||
|
||||
async fn send_client_response(
|
||||
&mut self,
|
||||
payload: ClientResponsePayload,
|
||||
payload: AuthResponsePayload,
|
||||
) -> Result<(), TransportError> {
|
||||
self.bi
|
||||
.send(Ok(ClientResponse {
|
||||
request_id: Some(self.request_tracker.current_request_id()),
|
||||
payload: Some(payload),
|
||||
payload: Some(ClientResponsePayload::Auth(proto_auth::Response {
|
||||
payload: Some(payload),
|
||||
})),
|
||||
}))
|
||||
.await
|
||||
}
|
||||
|
||||
async fn send_auth_result(&mut self, result: ProtoAuthResult) -> Result<(), TransportError> {
|
||||
self.send_client_response(ClientResponsePayload::AuthResult(result.into()))
|
||||
self.send_client_response(AuthResponsePayload::Result(result.into()))
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -119,9 +128,27 @@ impl Receiver<auth::Inbound> for AuthTransportAdapter<'_> {
|
||||
}
|
||||
};
|
||||
let payload = request.payload?;
|
||||
let ClientRequestPayload::Auth(auth_request) = payload else {
|
||||
let _ = self
|
||||
.bi
|
||||
.send(Err(Status::invalid_argument(
|
||||
"Unsupported client auth request",
|
||||
)))
|
||||
.await;
|
||||
return None;
|
||||
};
|
||||
let Some(payload) = auth_request.payload else {
|
||||
let _ = self
|
||||
.bi
|
||||
.send(Err(Status::invalid_argument(
|
||||
"Missing client auth request payload",
|
||||
)))
|
||||
.await;
|
||||
return None;
|
||||
};
|
||||
|
||||
match payload {
|
||||
ClientRequestPayload::AuthChallengeRequest(ProtoAuthChallengeRequest {
|
||||
AuthRequestPayload::ChallengeRequest(ProtoAuthChallengeRequest {
|
||||
pubkey,
|
||||
client_info,
|
||||
}) => {
|
||||
@@ -145,9 +172,7 @@ impl Receiver<auth::Inbound> for AuthTransportAdapter<'_> {
|
||||
metadata: client_metadata_from_proto(client_info),
|
||||
})
|
||||
}
|
||||
ClientRequestPayload::AuthChallengeSolution(ProtoAuthChallengeSolution {
|
||||
signature,
|
||||
}) => {
|
||||
AuthRequestPayload::ChallengeSolution(ProtoAuthChallengeSolution { signature }) => {
|
||||
let Ok(signature) = ed25519_dalek::Signature::try_from(signature.as_slice()) else {
|
||||
let _ = self
|
||||
.send_auth_result(ProtoAuthResult::InvalidSignature)
|
||||
@@ -156,15 +181,6 @@ impl Receiver<auth::Inbound> for AuthTransportAdapter<'_> {
|
||||
};
|
||||
Some(auth::Inbound::AuthChallengeSolution { signature })
|
||||
}
|
||||
_ => {
|
||||
let _ = self
|
||||
.bi
|
||||
.send(Err(Status::invalid_argument(
|
||||
"Unsupported client auth request",
|
||||
)))
|
||||
.await;
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,8 +199,7 @@ pub async fn start(
|
||||
conn: &mut ClientConnection,
|
||||
bi: &mut GrpcBi<ClientRequest, ClientResponse>,
|
||||
request_tracker: &mut RequestTracker,
|
||||
) -> Result<(), auth::Error> {
|
||||
) -> Result<i32, auth::Error> {
|
||||
let mut transport = AuthTransportAdapter::new(bi, request_tracker);
|
||||
client::auth::authenticate(conn, &mut transport).await?;
|
||||
Ok(())
|
||||
client::auth::authenticate(conn, &mut transport).await
|
||||
}
|
||||
|
||||
87
server/crates/arbiter-server/src/grpc/client/evm.rs
Normal file
87
server/crates/arbiter-server/src/grpc/client/evm.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use arbiter_proto::proto::{
|
||||
client::{
|
||||
client_response::Payload as ClientResponsePayload,
|
||||
evm::{
|
||||
self as proto_evm, request::Payload as EvmRequestPayload,
|
||||
response::Payload as EvmResponsePayload,
|
||||
},
|
||||
},
|
||||
evm::{
|
||||
EvmError as ProtoEvmError, EvmSignTransactionResponse,
|
||||
evm_sign_transaction_response::Result as EvmSignTransactionResult,
|
||||
},
|
||||
};
|
||||
use kameo::actor::ActorRef;
|
||||
use tonic::Status;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::{
|
||||
actors::client::session::{ClientSession, HandleSignTransaction, SignTransactionRpcError},
|
||||
grpc::{
|
||||
Convert, TryConvert,
|
||||
common::inbound::{RawEvmAddress, RawEvmTransaction},
|
||||
},
|
||||
};
|
||||
|
||||
fn wrap_response(payload: EvmResponsePayload) -> ClientResponsePayload {
|
||||
ClientResponsePayload::Evm(proto_evm::Response {
|
||||
payload: Some(payload),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) async fn dispatch(
|
||||
actor: &ActorRef<ClientSession>,
|
||||
req: proto_evm::Request,
|
||||
) -> Result<ClientResponsePayload, Status> {
|
||||
let Some(payload) = req.payload else {
|
||||
return Err(Status::invalid_argument(
|
||||
"Missing client EVM request payload",
|
||||
));
|
||||
};
|
||||
|
||||
match payload {
|
||||
EvmRequestPayload::SignTransaction(request) => {
|
||||
let address = RawEvmAddress(request.wallet_address).try_convert()?;
|
||||
let transaction = RawEvmTransaction(request.rlp_transaction).try_convert()?;
|
||||
|
||||
let response = match actor
|
||||
.ask(HandleSignTransaction {
|
||||
wallet_address: address,
|
||||
transaction,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(signature) => EvmSignTransactionResponse {
|
||||
result: Some(EvmSignTransactionResult::Signature(
|
||||
signature.as_bytes().to_vec(),
|
||||
)),
|
||||
},
|
||||
Err(kameo::error::SendError::HandlerError(SignTransactionRpcError::Vet(
|
||||
vet_error,
|
||||
))) => EvmSignTransactionResponse {
|
||||
result: Some(vet_error.convert()),
|
||||
},
|
||||
Err(kameo::error::SendError::HandlerError(SignTransactionRpcError::Internal)) => {
|
||||
EvmSignTransactionResponse {
|
||||
result: Some(EvmSignTransactionResult::Error(
|
||||
ProtoEvmError::Internal.into(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to sign EVM transaction");
|
||||
EvmSignTransactionResponse {
|
||||
result: Some(EvmSignTransactionResult::Error(
|
||||
ProtoEvmError::Internal.into(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(wrap_response(EvmResponsePayload::SignTransaction(response)))
|
||||
}
|
||||
EvmRequestPayload::AnalyzeTransaction(_) => Err(Status::unimplemented(
|
||||
"EVM transaction analysis is not yet implemented",
|
||||
)),
|
||||
}
|
||||
}
|
||||
47
server/crates/arbiter-server/src/grpc/client/vault.rs
Normal file
47
server/crates/arbiter-server/src/grpc/client/vault.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use arbiter_proto::proto::{
|
||||
client::{
|
||||
client_response::Payload as ClientResponsePayload,
|
||||
vault::{
|
||||
self as proto_vault, request::Payload as VaultRequestPayload,
|
||||
response::Payload as VaultResponsePayload,
|
||||
},
|
||||
},
|
||||
shared::VaultState as ProtoVaultState,
|
||||
};
|
||||
use kameo::{actor::ActorRef, error::SendError};
|
||||
use tonic::Status;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::actors::{
|
||||
client::session::{ClientSession, Error, HandleQueryVaultState},
|
||||
keyholder::KeyHolderState,
|
||||
};
|
||||
|
||||
pub(super) async fn dispatch(
|
||||
actor: &ActorRef<ClientSession>,
|
||||
req: proto_vault::Request,
|
||||
) -> Result<ClientResponsePayload, Status> {
|
||||
let Some(payload) = req.payload else {
|
||||
return Err(Status::invalid_argument(
|
||||
"Missing client vault request payload",
|
||||
));
|
||||
};
|
||||
|
||||
match payload {
|
||||
VaultRequestPayload::QueryState(_) => {
|
||||
let state = match actor.ask(HandleQueryVaultState {}).await {
|
||||
Ok(KeyHolderState::Unbootstrapped) => ProtoVaultState::Unbootstrapped,
|
||||
Ok(KeyHolderState::Sealed) => ProtoVaultState::Sealed,
|
||||
Ok(KeyHolderState::Unsealed) => ProtoVaultState::Unsealed,
|
||||
Err(SendError::HandlerError(Error::Internal)) => ProtoVaultState::Error,
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to query vault state");
|
||||
ProtoVaultState::Error
|
||||
}
|
||||
};
|
||||
Ok(ClientResponsePayload::Vault(proto_vault::Response {
|
||||
payload: Some(VaultResponsePayload::State(state.into())),
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
2
server/crates/arbiter-server/src/grpc/common.rs
Normal file
2
server/crates/arbiter-server/src/grpc/common.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod inbound;
|
||||
pub mod outbound;
|
||||
35
server/crates/arbiter-server/src/grpc/common/inbound.rs
Normal file
35
server/crates/arbiter-server/src/grpc/common/inbound.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use alloy::{consensus::TxEip1559, primitives::Address, rlp::Decodable as _};
|
||||
|
||||
use crate::grpc::TryConvert;
|
||||
|
||||
pub struct RawEvmAddress(pub Vec<u8>);
|
||||
impl TryConvert for RawEvmAddress {
|
||||
type Output = Address;
|
||||
|
||||
type Error = tonic::Status;
|
||||
|
||||
fn try_convert(self) -> Result<Self::Output, Self::Error> {
|
||||
let wallet_address = match <[u8; 20]>::try_from(self.0.as_slice()) {
|
||||
Ok(address) => Address::from(address),
|
||||
Err(_) => {
|
||||
return Err(tonic::Status::invalid_argument(
|
||||
"Invalid EVM wallet address",
|
||||
));
|
||||
}
|
||||
};
|
||||
Ok(wallet_address)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RawEvmTransaction(pub Vec<u8>);
|
||||
impl TryConvert for RawEvmTransaction {
|
||||
type Output = TxEip1559;
|
||||
|
||||
type Error = tonic::Status;
|
||||
|
||||
fn try_convert(self) -> Result<Self::Output, Self::Error> {
|
||||
let tx = TxEip1559::decode(&mut self.0.as_slice())
|
||||
.map_err(|_| tonic::Status::invalid_argument("Invalid EVM transaction format"))?;
|
||||
Ok(tx)
|
||||
}
|
||||
}
|
||||
119
server/crates/arbiter-server/src/grpc/common/outbound.rs
Normal file
119
server/crates/arbiter-server/src/grpc/common/outbound.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
use alloy::primitives::U256;
|
||||
use arbiter_proto::proto::{
|
||||
evm::{
|
||||
EvmError as ProtoEvmError,
|
||||
evm_sign_transaction_response::Result as EvmSignTransactionResult,
|
||||
},
|
||||
shared::evm::{
|
||||
EvalViolation as ProtoEvalViolation, GasLimitExceededViolation, NoMatchingGrantError,
|
||||
PolicyViolationsError, SpecificMeaning as ProtoSpecificMeaning,
|
||||
TokenInfo as ProtoTokenInfo, TransactionEvalError as ProtoTransactionEvalError,
|
||||
eval_violation::Kind as ProtoEvalViolationKind,
|
||||
specific_meaning::Meaning as ProtoSpecificMeaningKind,
|
||||
transaction_eval_error::Kind as ProtoTransactionEvalErrorKind,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
evm::{
|
||||
PolicyError, VetError,
|
||||
policies::{EvalViolation, SpecificMeaning},
|
||||
},
|
||||
grpc::Convert,
|
||||
};
|
||||
|
||||
fn u256_to_proto_bytes(value: U256) -> Vec<u8> {
|
||||
value.to_be_bytes::<32>().to_vec()
|
||||
}
|
||||
|
||||
impl Convert for SpecificMeaning {
|
||||
type Output = ProtoSpecificMeaning;
|
||||
|
||||
fn convert(self) -> Self::Output {
|
||||
let kind = match self {
|
||||
SpecificMeaning::EtherTransfer(meaning) => ProtoSpecificMeaningKind::EtherTransfer(
|
||||
arbiter_proto::proto::shared::evm::EtherTransferMeaning {
|
||||
to: meaning.to.to_vec(),
|
||||
value: u256_to_proto_bytes(meaning.value),
|
||||
},
|
||||
),
|
||||
SpecificMeaning::TokenTransfer(meaning) => ProtoSpecificMeaningKind::TokenTransfer(
|
||||
arbiter_proto::proto::shared::evm::TokenTransferMeaning {
|
||||
token: Some(ProtoTokenInfo {
|
||||
symbol: meaning.token.symbol.to_string(),
|
||||
address: meaning.token.contract.to_vec(),
|
||||
chain_id: meaning.token.chain,
|
||||
}),
|
||||
to: meaning.to.to_vec(),
|
||||
value: u256_to_proto_bytes(meaning.value),
|
||||
},
|
||||
),
|
||||
};
|
||||
|
||||
ProtoSpecificMeaning {
|
||||
meaning: Some(kind),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Convert for EvalViolation {
|
||||
type Output = ProtoEvalViolation;
|
||||
|
||||
fn convert(self) -> Self::Output {
|
||||
let kind = match self {
|
||||
EvalViolation::InvalidTarget { target } => {
|
||||
ProtoEvalViolationKind::InvalidTarget(target.to_vec())
|
||||
}
|
||||
EvalViolation::GasLimitExceeded {
|
||||
max_gas_fee_per_gas,
|
||||
max_priority_fee_per_gas,
|
||||
} => ProtoEvalViolationKind::GasLimitExceeded(GasLimitExceededViolation {
|
||||
max_gas_fee_per_gas: max_gas_fee_per_gas.map(u256_to_proto_bytes),
|
||||
max_priority_fee_per_gas: max_priority_fee_per_gas.map(u256_to_proto_bytes),
|
||||
}),
|
||||
EvalViolation::RateLimitExceeded => ProtoEvalViolationKind::RateLimitExceeded(()),
|
||||
EvalViolation::VolumetricLimitExceeded => {
|
||||
ProtoEvalViolationKind::VolumetricLimitExceeded(())
|
||||
}
|
||||
EvalViolation::InvalidTime => ProtoEvalViolationKind::InvalidTime(()),
|
||||
EvalViolation::InvalidTransactionType => {
|
||||
ProtoEvalViolationKind::InvalidTransactionType(())
|
||||
}
|
||||
};
|
||||
|
||||
ProtoEvalViolation { kind: Some(kind) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Convert for VetError {
|
||||
type Output = EvmSignTransactionResult;
|
||||
|
||||
fn convert(self) -> Self::Output {
|
||||
let kind = match self {
|
||||
VetError::ContractCreationNotSupported => {
|
||||
ProtoTransactionEvalErrorKind::ContractCreationNotSupported(())
|
||||
}
|
||||
VetError::UnsupportedTransactionType => {
|
||||
ProtoTransactionEvalErrorKind::UnsupportedTransactionType(())
|
||||
}
|
||||
VetError::Evaluated(meaning, policy_error) => match policy_error {
|
||||
PolicyError::NoMatchingGrant => {
|
||||
ProtoTransactionEvalErrorKind::NoMatchingGrant(NoMatchingGrantError {
|
||||
meaning: Some(meaning.convert()),
|
||||
})
|
||||
}
|
||||
PolicyError::Violations(violations) => {
|
||||
ProtoTransactionEvalErrorKind::PolicyViolations(PolicyViolationsError {
|
||||
meaning: Some(meaning.convert()),
|
||||
violations: violations.into_iter().map(Convert::convert).collect(),
|
||||
})
|
||||
}
|
||||
PolicyError::Database(_) => {
|
||||
return EvmSignTransactionResult::Error(ProtoEvmError::Internal.into());
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
EvmSignTransactionResult::EvalError(ProtoTransactionEvalError { kind: Some(kind) }.into())
|
||||
}
|
||||
}
|
||||
@@ -14,10 +14,13 @@ use crate::{
|
||||
grpc::user_agent::start,
|
||||
};
|
||||
|
||||
pub mod client;
|
||||
mod request_tracker;
|
||||
|
||||
pub mod client;
|
||||
pub mod user_agent;
|
||||
|
||||
mod common;
|
||||
|
||||
pub trait Convert {
|
||||
type Output;
|
||||
|
||||
|
||||
@@ -1,63 +1,29 @@
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use arbiter_proto::{
|
||||
proto::{
|
||||
client::ClientInfo as ProtoClientMetadata,
|
||||
evm::{
|
||||
EvmError as ProtoEvmError, EvmGrantCreateRequest, EvmGrantCreateResponse,
|
||||
EvmGrantDeleteRequest, EvmGrantDeleteResponse, EvmGrantList, EvmGrantListResponse,
|
||||
GrantEntry, WalletCreateResponse, WalletEntry, WalletList, WalletListResponse,
|
||||
evm_grant_create_response::Result as EvmGrantCreateResult,
|
||||
evm_grant_delete_response::Result as EvmGrantDeleteResult,
|
||||
evm_grant_list_response::Result as EvmGrantListResult,
|
||||
wallet_create_response::Result as WalletCreateResult,
|
||||
wallet_list_response::Result as WalletListResult,
|
||||
},
|
||||
user_agent::{
|
||||
BootstrapEncryptedKey as ProtoBootstrapEncryptedKey,
|
||||
BootstrapResult as ProtoBootstrapResult, ListWalletAccessResponse,
|
||||
SdkClientConnectionCancel as ProtoSdkClientConnectionCancel,
|
||||
SdkClientConnectionRequest as ProtoSdkClientConnectionRequest,
|
||||
SdkClientEntry as ProtoSdkClientEntry, SdkClientError as ProtoSdkClientError,
|
||||
SdkClientGrantWalletAccess, SdkClientList as ProtoSdkClientList,
|
||||
SdkClientListResponse as ProtoSdkClientListResponse, SdkClientRevokeWalletAccess, UnsealEncryptedKey as ProtoUnsealEncryptedKey,
|
||||
UnsealResult as ProtoUnsealResult, UnsealStart, UserAgentRequest, UserAgentResponse,
|
||||
VaultState as ProtoVaultState,
|
||||
sdk_client_list_response::Result as ProtoSdkClientListResult,
|
||||
user_agent_request::Payload as UserAgentRequestPayload,
|
||||
user_agent_response::Payload as UserAgentResponsePayload,
|
||||
},
|
||||
proto::user_agent::{
|
||||
UserAgentRequest, UserAgentResponse,
|
||||
user_agent_request::Payload as UserAgentRequestPayload,
|
||||
user_agent_response::Payload as UserAgentResponsePayload,
|
||||
},
|
||||
transport::{Error as TransportError, Receiver, Sender, grpc::GrpcBi},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use kameo::{
|
||||
actor::{ActorRef, Spawn as _},
|
||||
error::SendError,
|
||||
};
|
||||
use kameo::actor::{ActorRef, Spawn as _};
|
||||
use tonic::Status;
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
use crate::{
|
||||
actors::{
|
||||
keyholder::KeyHolderState,
|
||||
user_agent::{
|
||||
OutOfBand, UserAgentConnection, UserAgentSession,
|
||||
session::connection::{
|
||||
BootstrapError, HandleBootstrapEncryptedKey, HandleEvmWalletCreate,
|
||||
HandleEvmWalletList, HandleGrantCreate, HandleGrantDelete,
|
||||
HandleGrantEvmWalletAccess, HandleGrantList, HandleListWalletAccess,
|
||||
HandleNewClientApprove, HandleQueryVaultState, HandleRevokeEvmWalletAccess,
|
||||
HandleSdkClientList, HandleUnsealEncryptedKey, HandleUnsealRequest, UnsealError,
|
||||
},
|
||||
},
|
||||
},
|
||||
db::models::NewEvmWalletAccess,
|
||||
grpc::{Convert, TryConvert, request_tracker::RequestTracker},
|
||||
actors::user_agent::{OutOfBand, UserAgentConnection, UserAgentSession},
|
||||
grpc::request_tracker::RequestTracker,
|
||||
};
|
||||
|
||||
mod auth;
|
||||
mod evm;
|
||||
mod inbound;
|
||||
mod outbound;
|
||||
mod sdk_client;
|
||||
mod vault;
|
||||
|
||||
pub struct OutOfBandAdapter(mpsc::Sender<OutOfBand>);
|
||||
|
||||
@@ -85,23 +51,7 @@ async fn dispatch_loop(
|
||||
return;
|
||||
};
|
||||
|
||||
let payload = match oob {
|
||||
OutOfBand::ClientConnectionRequest { profile } => {
|
||||
UserAgentResponsePayload::SdkClientConnectionRequest(ProtoSdkClientConnectionRequest {
|
||||
pubkey: profile.pubkey.to_bytes().to_vec(),
|
||||
info: Some(ProtoClientMetadata {
|
||||
name: profile.metadata.name,
|
||||
description: profile.metadata.description,
|
||||
version: profile.metadata.version,
|
||||
}),
|
||||
})
|
||||
}
|
||||
OutOfBand::ClientConnectionCancel { pubkey } => {
|
||||
UserAgentResponsePayload::SdkClientConnectionCancel(ProtoSdkClientConnectionCancel {
|
||||
pubkey: pubkey.to_bytes().to_vec(),
|
||||
})
|
||||
}
|
||||
};
|
||||
let payload = sdk_client::out_of_band_payload(oob);
|
||||
|
||||
if bi.send(Ok(UserAgentResponse { id: None, payload: Some(payload) })).await.is_err() {
|
||||
return;
|
||||
@@ -143,7 +93,7 @@ async fn dispatch_loop(
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(status) => {
|
||||
error!(?status, "Failed to process user agent request");
|
||||
error!(?status, "Failed to process user agent request");
|
||||
let _ = bi.send(Err(status)).await;
|
||||
return;
|
||||
}
|
||||
@@ -157,288 +107,15 @@ async fn dispatch_inner(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
payload: UserAgentRequestPayload,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let response = match payload {
|
||||
UserAgentRequestPayload::UnsealStart(UnsealStart { client_pubkey }) => {
|
||||
let client_pubkey = <[u8; 32]>::try_from(client_pubkey)
|
||||
.map(x25519_dalek::PublicKey::from)
|
||||
.map_err(|_| Status::invalid_argument("Invalid X25519 public key"))?;
|
||||
|
||||
let response = actor
|
||||
.ask(HandleUnsealRequest { client_pubkey })
|
||||
.await
|
||||
.map_err(|err| {
|
||||
warn!(error = ?err, "Failed to handle unseal start request");
|
||||
Status::internal("Failed to start unseal flow")
|
||||
})?;
|
||||
|
||||
UserAgentResponsePayload::UnsealStartResponse(
|
||||
arbiter_proto::proto::user_agent::UnsealStartResponse {
|
||||
server_pubkey: response.server_pubkey.as_bytes().to_vec(),
|
||||
},
|
||||
)
|
||||
match payload {
|
||||
UserAgentRequestPayload::Vault(req) => vault::dispatch(actor, req).await,
|
||||
UserAgentRequestPayload::Evm(req) => evm::dispatch(actor, req).await,
|
||||
UserAgentRequestPayload::SdkClient(req) => sdk_client::dispatch(actor, req).await,
|
||||
UserAgentRequestPayload::Auth(..) => {
|
||||
warn!("Unsupported post-auth user agent auth request");
|
||||
Err(Status::invalid_argument("Unsupported user-agent request"))
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::UnsealEncryptedKey(ProtoUnsealEncryptedKey {
|
||||
nonce,
|
||||
ciphertext,
|
||||
associated_data,
|
||||
}) => {
|
||||
let result = match actor
|
||||
.ask(HandleUnsealEncryptedKey {
|
||||
nonce,
|
||||
ciphertext,
|
||||
associated_data,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(()) => ProtoUnsealResult::Success,
|
||||
Err(SendError::HandlerError(UnsealError::InvalidKey)) => {
|
||||
ProtoUnsealResult::InvalidKey
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to handle unseal request");
|
||||
return Err(Status::internal("Failed to unseal vault"));
|
||||
}
|
||||
};
|
||||
UserAgentResponsePayload::UnsealResult(result.into())
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::BootstrapEncryptedKey(ProtoBootstrapEncryptedKey {
|
||||
nonce,
|
||||
ciphertext,
|
||||
associated_data,
|
||||
}) => {
|
||||
let result = match actor
|
||||
.ask(HandleBootstrapEncryptedKey {
|
||||
nonce,
|
||||
ciphertext,
|
||||
associated_data,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(()) => ProtoBootstrapResult::Success,
|
||||
Err(SendError::HandlerError(BootstrapError::InvalidKey)) => {
|
||||
ProtoBootstrapResult::InvalidKey
|
||||
}
|
||||
Err(SendError::HandlerError(BootstrapError::AlreadyBootstrapped)) => {
|
||||
ProtoBootstrapResult::AlreadyBootstrapped
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to handle bootstrap request");
|
||||
return Err(Status::internal("Failed to bootstrap vault"));
|
||||
}
|
||||
};
|
||||
UserAgentResponsePayload::BootstrapResult(result.into())
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::QueryVaultState(_) => {
|
||||
let state = match actor.ask(HandleQueryVaultState {}).await {
|
||||
Ok(KeyHolderState::Unbootstrapped) => ProtoVaultState::Unbootstrapped,
|
||||
Ok(KeyHolderState::Sealed) => ProtoVaultState::Sealed,
|
||||
Ok(KeyHolderState::Unsealed) => ProtoVaultState::Unsealed,
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to query vault state");
|
||||
ProtoVaultState::Error
|
||||
}
|
||||
};
|
||||
UserAgentResponsePayload::VaultState(state.into())
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::EvmWalletCreate(_) => {
|
||||
let result = match actor.ask(HandleEvmWalletCreate {}).await {
|
||||
Ok((wallet_id, address)) => WalletCreateResult::Wallet(WalletEntry {
|
||||
id: wallet_id,
|
||||
address: address.to_vec(),
|
||||
}),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to create EVM wallet");
|
||||
WalletCreateResult::Error(ProtoEvmError::Internal.into())
|
||||
}
|
||||
};
|
||||
UserAgentResponsePayload::EvmWalletCreate(WalletCreateResponse {
|
||||
result: Some(result),
|
||||
})
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::EvmWalletList(_) => {
|
||||
let result = match actor.ask(HandleEvmWalletList {}).await {
|
||||
Ok(wallets) => WalletListResult::Wallets(WalletList {
|
||||
wallets: wallets
|
||||
.into_iter()
|
||||
.map(|(id, address)| WalletEntry {
|
||||
address: address.to_vec(),
|
||||
id,
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to list EVM wallets");
|
||||
WalletListResult::Error(ProtoEvmError::Internal.into())
|
||||
}
|
||||
};
|
||||
UserAgentResponsePayload::EvmWalletList(WalletListResponse {
|
||||
result: Some(result),
|
||||
})
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::EvmGrantList(_) => {
|
||||
let result = match actor.ask(HandleGrantList {}).await {
|
||||
Ok(grants) => EvmGrantListResult::Grants(EvmGrantList {
|
||||
grants: grants
|
||||
.into_iter()
|
||||
.map(|grant| GrantEntry {
|
||||
id: grant.id,
|
||||
wallet_access_id: grant.shared.wallet_access_id,
|
||||
shared: Some(grant.shared.convert()),
|
||||
specific: Some(grant.settings.convert()),
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to list EVM grants");
|
||||
EvmGrantListResult::Error(ProtoEvmError::Internal.into())
|
||||
}
|
||||
};
|
||||
UserAgentResponsePayload::EvmGrantList(EvmGrantListResponse {
|
||||
result: Some(result),
|
||||
})
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::EvmGrantCreate(EvmGrantCreateRequest { shared, specific }) => {
|
||||
let basic = shared
|
||||
.ok_or_else(|| Status::invalid_argument("Missing shared grant settings"))?
|
||||
.try_convert()?;
|
||||
let grant = specific
|
||||
.ok_or_else(|| Status::invalid_argument("Missing specific grant settings"))?
|
||||
.try_convert()?;
|
||||
|
||||
let result = match actor.ask(HandleGrantCreate { basic, grant }).await {
|
||||
Ok(grant_id) => EvmGrantCreateResult::GrantId(grant_id),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to create EVM grant");
|
||||
EvmGrantCreateResult::Error(ProtoEvmError::Internal.into())
|
||||
}
|
||||
};
|
||||
UserAgentResponsePayload::EvmGrantCreate(EvmGrantCreateResponse {
|
||||
result: Some(result),
|
||||
})
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::EvmGrantDelete(EvmGrantDeleteRequest { grant_id }) => {
|
||||
let result = match actor.ask(HandleGrantDelete { grant_id }).await {
|
||||
Ok(()) => EvmGrantDeleteResult::Ok(()),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to delete EVM grant");
|
||||
EvmGrantDeleteResult::Error(ProtoEvmError::Internal.into())
|
||||
}
|
||||
};
|
||||
UserAgentResponsePayload::EvmGrantDelete(EvmGrantDeleteResponse {
|
||||
result: Some(result),
|
||||
})
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::SdkClientConnectionResponse(resp) => {
|
||||
let pubkey_bytes = <[u8; 32]>::try_from(resp.pubkey)
|
||||
.map_err(|_| Status::invalid_argument("Invalid Ed25519 public key length"))?;
|
||||
let pubkey = ed25519_dalek::VerifyingKey::from_bytes(&pubkey_bytes)
|
||||
.map_err(|_| Status::invalid_argument("Invalid Ed25519 public key"))?;
|
||||
|
||||
actor
|
||||
.ask(HandleNewClientApprove {
|
||||
approved: resp.approved,
|
||||
pubkey,
|
||||
})
|
||||
.await
|
||||
.map_err(|err| {
|
||||
warn!(?err, "Failed to process client connection response");
|
||||
Status::internal("Failed to process response")
|
||||
})?;
|
||||
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::SdkClientRevoke(_) => todo!(),
|
||||
|
||||
UserAgentRequestPayload::SdkClientList(_) => {
|
||||
let result = match actor.ask(HandleSdkClientList {}).await {
|
||||
Ok(clients) => ProtoSdkClientListResult::Clients(ProtoSdkClientList {
|
||||
clients: clients
|
||||
.into_iter()
|
||||
.map(|(client, metadata)| ProtoSdkClientEntry {
|
||||
id: client.id,
|
||||
pubkey: client.public_key,
|
||||
info: Some(ProtoClientMetadata {
|
||||
name: metadata.name,
|
||||
description: metadata.description,
|
||||
version: metadata.version,
|
||||
}),
|
||||
created_at: client.created_at.0.timestamp() as i32,
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to list SDK clients");
|
||||
ProtoSdkClientListResult::Error(ProtoSdkClientError::Internal.into())
|
||||
}
|
||||
};
|
||||
UserAgentResponsePayload::SdkClientListResponse(ProtoSdkClientListResponse {
|
||||
result: Some(result),
|
||||
})
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::GrantWalletAccess(SdkClientGrantWalletAccess { accesses }) => {
|
||||
let entries: Vec<NewEvmWalletAccess> =
|
||||
accesses.into_iter().map(|a| a.convert()).collect();
|
||||
|
||||
match actor.ask(HandleGrantEvmWalletAccess { entries }).await {
|
||||
Ok(()) => {
|
||||
info!("Successfully granted wallet access");
|
||||
return Ok(None);
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to grant wallet access");
|
||||
return Err(Status::internal("Failed to grant wallet access"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::RevokeWalletAccess(SdkClientRevokeWalletAccess { accesses }) => {
|
||||
match actor
|
||||
.ask(HandleRevokeEvmWalletAccess { entries: accesses })
|
||||
.await
|
||||
{
|
||||
Ok(()) => {
|
||||
info!("Successfully revoked wallet access");
|
||||
return Ok(None);
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to revoke wallet access");
|
||||
return Err(Status::internal("Failed to revoke wallet access"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::ListWalletAccess(_) => {
|
||||
let result = match actor.ask(HandleListWalletAccess {}).await {
|
||||
Ok(accesses) => ListWalletAccessResponse {
|
||||
accesses: accesses.into_iter().map(|a| a.convert()).collect(),
|
||||
},
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to list wallet access");
|
||||
return Err(Status::internal("Failed to list wallet access"));
|
||||
}
|
||||
};
|
||||
UserAgentResponsePayload::ListWalletAccessResponse(result)
|
||||
}
|
||||
|
||||
UserAgentRequestPayload::AuthChallengeRequest(..)
|
||||
| UserAgentRequestPayload::AuthChallengeSolution(..) => {
|
||||
warn!(?payload, "Unsupported post-auth user agent request");
|
||||
return Err(Status::invalid_argument("Unsupported user-agent request"));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Some(response))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start(
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
use arbiter_proto::{
|
||||
proto::user_agent::{
|
||||
AuthChallenge as ProtoAuthChallenge, AuthChallengeRequest as ProtoAuthChallengeRequest,
|
||||
AuthChallengeSolution as ProtoAuthChallengeSolution, AuthResult as ProtoAuthResult,
|
||||
KeyType as ProtoKeyType, UserAgentRequest, UserAgentResponse,
|
||||
UserAgentRequest, UserAgentResponse,
|
||||
auth::{
|
||||
self as proto_auth, AuthChallenge as ProtoAuthChallenge,
|
||||
AuthChallengeRequest as ProtoAuthChallengeRequest,
|
||||
AuthChallengeSolution as ProtoAuthChallengeSolution, AuthResult as ProtoAuthResult,
|
||||
KeyType as ProtoKeyType, request::Payload as AuthRequestPayload,
|
||||
response::Payload as AuthResponsePayload,
|
||||
},
|
||||
user_agent_request::Payload as UserAgentRequestPayload,
|
||||
user_agent_response::Payload as UserAgentResponsePayload,
|
||||
},
|
||||
@@ -36,12 +41,14 @@ impl<'a> AuthTransportAdapter<'a> {
|
||||
|
||||
async fn send_user_agent_response(
|
||||
&mut self,
|
||||
payload: UserAgentResponsePayload,
|
||||
payload: AuthResponsePayload,
|
||||
) -> Result<(), TransportError> {
|
||||
self.bi
|
||||
.send(Ok(UserAgentResponse {
|
||||
id: Some(self.request_tracker.current_request_id()),
|
||||
payload: Some(payload),
|
||||
payload: Some(UserAgentResponsePayload::Auth(proto_auth::Response {
|
||||
payload: Some(payload),
|
||||
})),
|
||||
}))
|
||||
.await
|
||||
}
|
||||
@@ -56,19 +63,19 @@ impl Sender<Result<auth::Outbound, auth::Error>> for AuthTransportAdapter<'_> {
|
||||
use auth::{Error, Outbound};
|
||||
let payload = match item {
|
||||
Ok(Outbound::AuthChallenge { nonce }) => {
|
||||
UserAgentResponsePayload::AuthChallenge(ProtoAuthChallenge { nonce })
|
||||
AuthResponsePayload::Challenge(ProtoAuthChallenge { nonce })
|
||||
}
|
||||
Ok(Outbound::AuthSuccess) => {
|
||||
UserAgentResponsePayload::AuthResult(ProtoAuthResult::Success.into())
|
||||
AuthResponsePayload::Result(ProtoAuthResult::Success.into())
|
||||
}
|
||||
Err(Error::UnregisteredPublicKey) => {
|
||||
UserAgentResponsePayload::AuthResult(ProtoAuthResult::InvalidKey.into())
|
||||
AuthResponsePayload::Result(ProtoAuthResult::InvalidKey.into())
|
||||
}
|
||||
Err(Error::InvalidChallengeSolution) => {
|
||||
UserAgentResponsePayload::AuthResult(ProtoAuthResult::InvalidSignature.into())
|
||||
AuthResponsePayload::Result(ProtoAuthResult::InvalidSignature.into())
|
||||
}
|
||||
Err(Error::InvalidBootstrapToken) => {
|
||||
UserAgentResponsePayload::AuthResult(ProtoAuthResult::TokenInvalid.into())
|
||||
AuthResponsePayload::Result(ProtoAuthResult::TokenInvalid.into())
|
||||
}
|
||||
Err(Error::Internal { details }) => {
|
||||
return self.bi.send(Err(Status::internal(details))).await;
|
||||
@@ -112,8 +119,26 @@ impl Receiver<auth::Inbound> for AuthTransportAdapter<'_> {
|
||||
return None;
|
||||
};
|
||||
|
||||
let UserAgentRequestPayload::Auth(auth_request) = payload else {
|
||||
let _ = self
|
||||
.bi
|
||||
.send(Err(Status::invalid_argument(
|
||||
"Unsupported user-agent auth request",
|
||||
)))
|
||||
.await;
|
||||
return None;
|
||||
};
|
||||
|
||||
let Some(payload) = auth_request.payload else {
|
||||
warn!(
|
||||
event = "received auth request with empty payload",
|
||||
"grpc.useragent.auth_adapter"
|
||||
);
|
||||
return None;
|
||||
};
|
||||
|
||||
match payload {
|
||||
UserAgentRequestPayload::AuthChallengeRequest(ProtoAuthChallengeRequest {
|
||||
AuthRequestPayload::ChallengeRequest(ProtoAuthChallengeRequest {
|
||||
pubkey,
|
||||
bootstrap_token,
|
||||
key_type,
|
||||
@@ -150,17 +175,8 @@ impl Receiver<auth::Inbound> for AuthTransportAdapter<'_> {
|
||||
bootstrap_token,
|
||||
})
|
||||
}
|
||||
UserAgentRequestPayload::AuthChallengeSolution(ProtoAuthChallengeSolution {
|
||||
signature,
|
||||
}) => Some(auth::Inbound::AuthChallengeSolution { signature }),
|
||||
_ => {
|
||||
let _ = self
|
||||
.bi
|
||||
.send(Err(Status::invalid_argument(
|
||||
"Unsupported user-agent auth request",
|
||||
)))
|
||||
.await;
|
||||
None
|
||||
AuthRequestPayload::ChallengeSolution(ProtoAuthChallengeSolution { signature }) => {
|
||||
Some(auth::Inbound::AuthChallengeSolution { signature })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
234
server/crates/arbiter-server/src/grpc/user_agent/evm.rs
Normal file
234
server/crates/arbiter-server/src/grpc/user_agent/evm.rs
Normal file
@@ -0,0 +1,234 @@
|
||||
use arbiter_proto::proto::{
|
||||
evm::{
|
||||
EvmError as ProtoEvmError, EvmGrantCreateRequest, EvmGrantCreateResponse,
|
||||
EvmGrantDeleteRequest, EvmGrantDeleteResponse, EvmGrantList, EvmGrantListResponse,
|
||||
EvmSignTransactionResponse, GrantEntry, WalletCreateResponse, WalletEntry, WalletList,
|
||||
WalletListResponse, evm_grant_create_response::Result as EvmGrantCreateResult,
|
||||
evm_grant_delete_response::Result as EvmGrantDeleteResult,
|
||||
evm_grant_list_response::Result as EvmGrantListResult,
|
||||
evm_sign_transaction_response::Result as EvmSignTransactionResult,
|
||||
wallet_create_response::Result as WalletCreateResult,
|
||||
wallet_list_response::Result as WalletListResult,
|
||||
},
|
||||
user_agent::{
|
||||
evm::{
|
||||
self as proto_evm, SignTransactionRequest as ProtoSignTransactionRequest,
|
||||
request::Payload as EvmRequestPayload, response::Payload as EvmResponsePayload,
|
||||
},
|
||||
user_agent_response::Payload as UserAgentResponsePayload,
|
||||
},
|
||||
};
|
||||
use kameo::actor::ActorRef;
|
||||
use tonic::Status;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::{
|
||||
actors::user_agent::{
|
||||
UserAgentSession,
|
||||
session::connection::{
|
||||
HandleEvmWalletCreate, HandleEvmWalletList, HandleGrantCreate, HandleGrantDelete,
|
||||
HandleGrantList, HandleSignTransaction,
|
||||
SignTransactionError as SessionSignTransactionError,
|
||||
},
|
||||
},
|
||||
grpc::{
|
||||
Convert, TryConvert,
|
||||
common::inbound::{RawEvmAddress, RawEvmTransaction},
|
||||
},
|
||||
};
|
||||
|
||||
fn wrap_evm_response(payload: EvmResponsePayload) -> UserAgentResponsePayload {
|
||||
UserAgentResponsePayload::Evm(proto_evm::Response {
|
||||
payload: Some(payload),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) async fn dispatch(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: proto_evm::Request,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let Some(payload) = req.payload else {
|
||||
return Err(Status::invalid_argument("Missing EVM request payload"));
|
||||
};
|
||||
|
||||
match payload {
|
||||
EvmRequestPayload::WalletCreate(_) => handle_wallet_create(actor).await,
|
||||
EvmRequestPayload::WalletList(_) => handle_wallet_list(actor).await,
|
||||
EvmRequestPayload::GrantCreate(req) => handle_grant_create(actor, req).await,
|
||||
EvmRequestPayload::GrantDelete(req) => handle_grant_delete(actor, req).await,
|
||||
EvmRequestPayload::GrantList(_) => handle_grant_list(actor).await,
|
||||
EvmRequestPayload::SignTransaction(req) => handle_sign_transaction(actor, req).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_wallet_create(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let result = match actor.ask(HandleEvmWalletCreate {}).await {
|
||||
Ok((wallet_id, address)) => WalletCreateResult::Wallet(WalletEntry {
|
||||
id: wallet_id,
|
||||
address: address.to_vec(),
|
||||
}),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to create EVM wallet");
|
||||
WalletCreateResult::Error(ProtoEvmError::Internal.into())
|
||||
}
|
||||
};
|
||||
Ok(Some(wrap_evm_response(EvmResponsePayload::WalletCreate(
|
||||
WalletCreateResponse {
|
||||
result: Some(result),
|
||||
},
|
||||
))))
|
||||
}
|
||||
|
||||
async fn handle_wallet_list(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let result = match actor.ask(HandleEvmWalletList {}).await {
|
||||
Ok(wallets) => WalletListResult::Wallets(WalletList {
|
||||
wallets: wallets
|
||||
.into_iter()
|
||||
.map(|(id, address)| WalletEntry {
|
||||
address: address.to_vec(),
|
||||
id,
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to list EVM wallets");
|
||||
WalletListResult::Error(ProtoEvmError::Internal.into())
|
||||
}
|
||||
};
|
||||
Ok(Some(wrap_evm_response(EvmResponsePayload::WalletList(
|
||||
WalletListResponse {
|
||||
result: Some(result),
|
||||
},
|
||||
))))
|
||||
}
|
||||
|
||||
async fn handle_grant_list(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let result = match actor.ask(HandleGrantList {}).await {
|
||||
Ok(grants) => EvmGrantListResult::Grants(EvmGrantList {
|
||||
grants: grants
|
||||
.into_iter()
|
||||
.map(|grant| GrantEntry {
|
||||
id: grant.id,
|
||||
wallet_access_id: grant.shared.wallet_access_id,
|
||||
shared: Some(grant.shared.convert()),
|
||||
specific: Some(grant.settings.convert()),
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to list EVM grants");
|
||||
EvmGrantListResult::Error(ProtoEvmError::Internal.into())
|
||||
}
|
||||
};
|
||||
Ok(Some(wrap_evm_response(EvmResponsePayload::GrantList(
|
||||
EvmGrantListResponse {
|
||||
result: Some(result),
|
||||
},
|
||||
))))
|
||||
}
|
||||
|
||||
async fn handle_grant_create(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: EvmGrantCreateRequest,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let basic = req
|
||||
.shared
|
||||
.ok_or_else(|| Status::invalid_argument("Missing shared grant settings"))?
|
||||
.try_convert()?;
|
||||
let grant = req
|
||||
.specific
|
||||
.ok_or_else(|| Status::invalid_argument("Missing specific grant settings"))?
|
||||
.try_convert()?;
|
||||
|
||||
let result = match actor.ask(HandleGrantCreate { basic, grant }).await {
|
||||
Ok(grant_id) => EvmGrantCreateResult::GrantId(grant_id),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to create EVM grant");
|
||||
EvmGrantCreateResult::Error(ProtoEvmError::Internal.into())
|
||||
}
|
||||
};
|
||||
Ok(Some(wrap_evm_response(EvmResponsePayload::GrantCreate(
|
||||
EvmGrantCreateResponse {
|
||||
result: Some(result),
|
||||
},
|
||||
))))
|
||||
}
|
||||
|
||||
async fn handle_grant_delete(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: EvmGrantDeleteRequest,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let result = match actor
|
||||
.ask(HandleGrantDelete {
|
||||
grant_id: req.grant_id,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(()) => EvmGrantDeleteResult::Ok(()),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to delete EVM grant");
|
||||
EvmGrantDeleteResult::Error(ProtoEvmError::Internal.into())
|
||||
}
|
||||
};
|
||||
Ok(Some(wrap_evm_response(EvmResponsePayload::GrantDelete(
|
||||
EvmGrantDeleteResponse {
|
||||
result: Some(result),
|
||||
},
|
||||
))))
|
||||
}
|
||||
|
||||
async fn handle_sign_transaction(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: ProtoSignTransactionRequest,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let request = req
|
||||
.request
|
||||
.ok_or_else(|| Status::invalid_argument("Missing sign transaction request"))?;
|
||||
let wallet_address = RawEvmAddress(request.wallet_address).try_convert()?;
|
||||
let transaction = RawEvmTransaction(request.rlp_transaction).try_convert()?;
|
||||
|
||||
let response = match actor
|
||||
.ask(HandleSignTransaction {
|
||||
client_id: req.client_id,
|
||||
wallet_address,
|
||||
transaction,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(signature) => EvmSignTransactionResponse {
|
||||
result: Some(EvmSignTransactionResult::Signature(
|
||||
signature.as_bytes().to_vec(),
|
||||
)),
|
||||
},
|
||||
Err(kameo::error::SendError::HandlerError(SessionSignTransactionError::Vet(vet_error))) => {
|
||||
EvmSignTransactionResponse {
|
||||
result: Some(vet_error.convert()),
|
||||
}
|
||||
}
|
||||
Err(kameo::error::SendError::HandlerError(SessionSignTransactionError::Internal)) => {
|
||||
EvmSignTransactionResponse {
|
||||
result: Some(EvmSignTransactionResult::Error(
|
||||
ProtoEvmError::Internal.into(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to sign EVM transaction");
|
||||
EvmSignTransactionResponse {
|
||||
result: Some(EvmSignTransactionResult::Error(
|
||||
ProtoEvmError::Internal.into(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Some(wrap_evm_response(
|
||||
EvmResponsePayload::SignTransaction(response),
|
||||
)))
|
||||
}
|
||||
@@ -5,7 +5,9 @@ use arbiter_proto::proto::evm::{
|
||||
TransactionRateLimit as ProtoTransactionRateLimit, VolumeRateLimit as ProtoVolumeRateLimit,
|
||||
specific_grant::Grant as ProtoSpecificGrantType,
|
||||
};
|
||||
use arbiter_proto::proto::user_agent::{SdkClientWalletAccess, WalletAccess};
|
||||
use arbiter_proto::proto::user_agent::sdk_client::{
|
||||
WalletAccess, WalletAccessEntry as SdkClientWalletAccess,
|
||||
};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use prost_types::Timestamp as ProtoTimestamp;
|
||||
use tonic::Status;
|
||||
|
||||
@@ -5,7 +5,7 @@ use arbiter_proto::proto::{
|
||||
TransactionRateLimit as ProtoTransactionRateLimit, VolumeRateLimit as ProtoVolumeRateLimit,
|
||||
specific_grant::Grant as ProtoSpecificGrantType,
|
||||
},
|
||||
user_agent::{SdkClientWalletAccess as ProtoSdkClientWalletAccess, WalletAccess},
|
||||
user_agent::sdk_client::{WalletAccess, WalletAccessEntry as ProtoSdkClientWalletAccess},
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
use prost_types::Timestamp as ProtoTimestamp;
|
||||
|
||||
194
server/crates/arbiter-server/src/grpc/user_agent/sdk_client.rs
Normal file
194
server/crates/arbiter-server/src/grpc/user_agent/sdk_client.rs
Normal file
@@ -0,0 +1,194 @@
|
||||
use arbiter_proto::proto::{
|
||||
shared::ClientInfo as ProtoClientMetadata,
|
||||
user_agent::{
|
||||
sdk_client::{
|
||||
self as proto_sdk_client, ConnectionCancel as ProtoSdkClientConnectionCancel,
|
||||
ConnectionRequest as ProtoSdkClientConnectionRequest,
|
||||
ConnectionResponse as ProtoSdkClientConnectionResponse, Entry as ProtoSdkClientEntry,
|
||||
Error as ProtoSdkClientError, GrantWalletAccess as ProtoSdkClientGrantWalletAccess,
|
||||
List as ProtoSdkClientList, ListResponse as ProtoSdkClientListResponse,
|
||||
ListWalletAccessResponse, RevokeWalletAccess as ProtoSdkClientRevokeWalletAccess,
|
||||
list_response::Result as ProtoSdkClientListResult,
|
||||
request::Payload as SdkClientRequestPayload,
|
||||
response::Payload as SdkClientResponsePayload,
|
||||
},
|
||||
user_agent_response::Payload as UserAgentResponsePayload,
|
||||
},
|
||||
};
|
||||
use kameo::actor::ActorRef;
|
||||
use tonic::Status;
|
||||
use tracing::{info, warn};
|
||||
|
||||
use crate::{
|
||||
actors::user_agent::{
|
||||
OutOfBand, UserAgentSession,
|
||||
session::connection::{
|
||||
HandleGrantEvmWalletAccess, HandleListWalletAccess, HandleNewClientApprove,
|
||||
HandleRevokeEvmWalletAccess, HandleSdkClientList,
|
||||
},
|
||||
},
|
||||
db::models::NewEvmWalletAccess,
|
||||
grpc::Convert,
|
||||
};
|
||||
|
||||
fn wrap_sdk_client_response(payload: SdkClientResponsePayload) -> UserAgentResponsePayload {
|
||||
UserAgentResponsePayload::SdkClient(proto_sdk_client::Response {
|
||||
payload: Some(payload),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn out_of_band_payload(oob: OutOfBand) -> UserAgentResponsePayload {
|
||||
match oob {
|
||||
OutOfBand::ClientConnectionRequest { profile } => wrap_sdk_client_response(
|
||||
SdkClientResponsePayload::ConnectionRequest(ProtoSdkClientConnectionRequest {
|
||||
pubkey: profile.pubkey.to_bytes().to_vec(),
|
||||
info: Some(ProtoClientMetadata {
|
||||
name: profile.metadata.name,
|
||||
description: profile.metadata.description,
|
||||
version: profile.metadata.version,
|
||||
}),
|
||||
}),
|
||||
),
|
||||
OutOfBand::ClientConnectionCancel { pubkey } => wrap_sdk_client_response(
|
||||
SdkClientResponsePayload::ConnectionCancel(ProtoSdkClientConnectionCancel {
|
||||
pubkey: pubkey.to_bytes().to_vec(),
|
||||
}),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn dispatch(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: proto_sdk_client::Request,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let Some(payload) = req.payload else {
|
||||
return Err(Status::invalid_argument(
|
||||
"Missing SDK client request payload",
|
||||
));
|
||||
};
|
||||
|
||||
match payload {
|
||||
SdkClientRequestPayload::ConnectionResponse(resp) => {
|
||||
handle_connection_response(actor, resp).await
|
||||
}
|
||||
SdkClientRequestPayload::Revoke(_) => Err(Status::unimplemented(
|
||||
"SdkClientRevoke is not yet implemented",
|
||||
)),
|
||||
SdkClientRequestPayload::List(_) => handle_list(actor).await,
|
||||
SdkClientRequestPayload::GrantWalletAccess(req) => {
|
||||
handle_grant_wallet_access(actor, req).await
|
||||
}
|
||||
SdkClientRequestPayload::RevokeWalletAccess(req) => {
|
||||
handle_revoke_wallet_access(actor, req).await
|
||||
}
|
||||
SdkClientRequestPayload::ListWalletAccess(_) => handle_list_wallet_access(actor).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_connection_response(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
resp: ProtoSdkClientConnectionResponse,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let pubkey_bytes = <[u8; 32]>::try_from(resp.pubkey)
|
||||
.map_err(|_| Status::invalid_argument("Invalid Ed25519 public key length"))?;
|
||||
let pubkey = ed25519_dalek::VerifyingKey::from_bytes(&pubkey_bytes)
|
||||
.map_err(|_| Status::invalid_argument("Invalid Ed25519 public key"))?;
|
||||
|
||||
actor
|
||||
.ask(HandleNewClientApprove {
|
||||
approved: resp.approved,
|
||||
pubkey,
|
||||
})
|
||||
.await
|
||||
.map_err(|err| {
|
||||
warn!(?err, "Failed to process client connection response");
|
||||
Status::internal("Failed to process response")
|
||||
})?;
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
async fn handle_list(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let result = match actor.ask(HandleSdkClientList {}).await {
|
||||
Ok(clients) => ProtoSdkClientListResult::Clients(ProtoSdkClientList {
|
||||
clients: clients
|
||||
.into_iter()
|
||||
.map(|(client, metadata)| ProtoSdkClientEntry {
|
||||
id: client.id,
|
||||
pubkey: client.public_key,
|
||||
info: Some(ProtoClientMetadata {
|
||||
name: metadata.name,
|
||||
description: metadata.description,
|
||||
version: metadata.version,
|
||||
}),
|
||||
created_at: client.created_at.0.timestamp() as i32,
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to list SDK clients");
|
||||
ProtoSdkClientListResult::Error(ProtoSdkClientError::Internal.into())
|
||||
}
|
||||
};
|
||||
Ok(Some(wrap_sdk_client_response(
|
||||
SdkClientResponsePayload::List(ProtoSdkClientListResponse {
|
||||
result: Some(result),
|
||||
}),
|
||||
)))
|
||||
}
|
||||
|
||||
async fn handle_grant_wallet_access(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: ProtoSdkClientGrantWalletAccess,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let entries: Vec<NewEvmWalletAccess> = req.accesses.into_iter().map(|a| a.convert()).collect();
|
||||
match actor.ask(HandleGrantEvmWalletAccess { entries }).await {
|
||||
Ok(()) => {
|
||||
info!("Successfully granted wallet access");
|
||||
Ok(None)
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to grant wallet access");
|
||||
Err(Status::internal("Failed to grant wallet access"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_revoke_wallet_access(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: ProtoSdkClientRevokeWalletAccess,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
match actor
|
||||
.ask(HandleRevokeEvmWalletAccess {
|
||||
entries: req.accesses,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(()) => {
|
||||
info!("Successfully revoked wallet access");
|
||||
Ok(None)
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to revoke wallet access");
|
||||
Err(Status::internal("Failed to revoke wallet access"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_list_wallet_access(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
match actor.ask(HandleListWalletAccess {}).await {
|
||||
Ok(accesses) => Ok(Some(wrap_sdk_client_response(
|
||||
SdkClientResponsePayload::ListWalletAccess(ListWalletAccessResponse {
|
||||
accesses: accesses.into_iter().map(|a| a.convert()).collect(),
|
||||
}),
|
||||
))),
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to list wallet access");
|
||||
Err(Status::internal("Failed to list wallet access"))
|
||||
}
|
||||
}
|
||||
}
|
||||
180
server/crates/arbiter-server/src/grpc/user_agent/vault.rs
Normal file
180
server/crates/arbiter-server/src/grpc/user_agent/vault.rs
Normal file
@@ -0,0 +1,180 @@
|
||||
use arbiter_proto::proto::shared::VaultState as ProtoVaultState;
|
||||
use arbiter_proto::proto::user_agent::{
|
||||
user_agent_response::Payload as UserAgentResponsePayload,
|
||||
vault::{
|
||||
self as proto_vault,
|
||||
bootstrap::{
|
||||
self as proto_bootstrap, BootstrapEncryptedKey as ProtoBootstrapEncryptedKey,
|
||||
BootstrapResult as ProtoBootstrapResult,
|
||||
},
|
||||
request::Payload as VaultRequestPayload,
|
||||
response::Payload as VaultResponsePayload,
|
||||
unseal::{
|
||||
self as proto_unseal, UnsealEncryptedKey as ProtoUnsealEncryptedKey,
|
||||
UnsealResult as ProtoUnsealResult, UnsealStart,
|
||||
request::Payload as UnsealRequestPayload, response::Payload as UnsealResponsePayload,
|
||||
},
|
||||
},
|
||||
};
|
||||
use kameo::{actor::ActorRef, error::SendError};
|
||||
use tonic::Status;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::actors::{
|
||||
keyholder::KeyHolderState,
|
||||
user_agent::{
|
||||
UserAgentSession,
|
||||
session::connection::{
|
||||
BootstrapError, HandleBootstrapEncryptedKey, HandleQueryVaultState,
|
||||
HandleUnsealEncryptedKey, HandleUnsealRequest, UnsealError,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
fn wrap_vault_response(payload: VaultResponsePayload) -> UserAgentResponsePayload {
|
||||
UserAgentResponsePayload::Vault(proto_vault::Response {
|
||||
payload: Some(payload),
|
||||
})
|
||||
}
|
||||
|
||||
fn wrap_unseal_response(payload: UnsealResponsePayload) -> UserAgentResponsePayload {
|
||||
wrap_vault_response(VaultResponsePayload::Unseal(proto_unseal::Response {
|
||||
payload: Some(payload),
|
||||
}))
|
||||
}
|
||||
|
||||
fn wrap_bootstrap_response(result: ProtoBootstrapResult) -> UserAgentResponsePayload {
|
||||
wrap_vault_response(VaultResponsePayload::Bootstrap(proto_bootstrap::Response {
|
||||
result: result.into(),
|
||||
}))
|
||||
}
|
||||
|
||||
pub(super) async fn dispatch(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: proto_vault::Request,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let Some(payload) = req.payload else {
|
||||
return Err(Status::invalid_argument("Missing vault request payload"));
|
||||
};
|
||||
|
||||
match payload {
|
||||
VaultRequestPayload::QueryState(_) => handle_query_vault_state(actor).await,
|
||||
VaultRequestPayload::Unseal(req) => dispatch_unseal_request(actor, req).await,
|
||||
VaultRequestPayload::Bootstrap(req) => handle_bootstrap_request(actor, req).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn dispatch_unseal_request(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: proto_unseal::Request,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let Some(payload) = req.payload else {
|
||||
return Err(Status::invalid_argument("Missing unseal request payload"));
|
||||
};
|
||||
|
||||
match payload {
|
||||
UnsealRequestPayload::Start(req) => handle_unseal_start(actor, req).await,
|
||||
UnsealRequestPayload::EncryptedKey(req) => handle_unseal_encrypted_key(actor, req).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_unseal_start(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: UnsealStart,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let client_pubkey = <[u8; 32]>::try_from(req.client_pubkey)
|
||||
.map(x25519_dalek::PublicKey::from)
|
||||
.map_err(|_| Status::invalid_argument("Invalid X25519 public key"))?;
|
||||
|
||||
let response = actor
|
||||
.ask(HandleUnsealRequest { client_pubkey })
|
||||
.await
|
||||
.map_err(|err| {
|
||||
warn!(error = ?err, "Failed to handle unseal start request");
|
||||
Status::internal("Failed to start unseal flow")
|
||||
})?;
|
||||
|
||||
Ok(Some(wrap_unseal_response(UnsealResponsePayload::Start(
|
||||
proto_unseal::UnsealStartResponse {
|
||||
server_pubkey: response.server_pubkey.as_bytes().to_vec(),
|
||||
},
|
||||
))))
|
||||
}
|
||||
|
||||
async fn handle_unseal_encrypted_key(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: ProtoUnsealEncryptedKey,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let result = match actor
|
||||
.ask(HandleUnsealEncryptedKey {
|
||||
nonce: req.nonce,
|
||||
ciphertext: req.ciphertext,
|
||||
associated_data: req.associated_data,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(()) => ProtoUnsealResult::Success,
|
||||
Err(SendError::HandlerError(UnsealError::InvalidKey)) => ProtoUnsealResult::InvalidKey,
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to handle unseal request");
|
||||
return Err(Status::internal("Failed to unseal vault"));
|
||||
}
|
||||
};
|
||||
Ok(Some(wrap_unseal_response(UnsealResponsePayload::Result(
|
||||
result.into(),
|
||||
))))
|
||||
}
|
||||
|
||||
async fn handle_bootstrap_request(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: proto_bootstrap::Request,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let encrypted_key = req
|
||||
.encrypted_key
|
||||
.ok_or_else(|| Status::invalid_argument("Missing bootstrap encrypted key"))?;
|
||||
handle_bootstrap_encrypted_key(actor, encrypted_key).await
|
||||
}
|
||||
|
||||
async fn handle_bootstrap_encrypted_key(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
req: ProtoBootstrapEncryptedKey,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let result = match actor
|
||||
.ask(HandleBootstrapEncryptedKey {
|
||||
nonce: req.nonce,
|
||||
ciphertext: req.ciphertext,
|
||||
associated_data: req.associated_data,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(()) => ProtoBootstrapResult::Success,
|
||||
Err(SendError::HandlerError(BootstrapError::InvalidKey)) => {
|
||||
ProtoBootstrapResult::InvalidKey
|
||||
}
|
||||
Err(SendError::HandlerError(BootstrapError::AlreadyBootstrapped)) => {
|
||||
ProtoBootstrapResult::AlreadyBootstrapped
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to handle bootstrap request");
|
||||
return Err(Status::internal("Failed to bootstrap vault"));
|
||||
}
|
||||
};
|
||||
Ok(Some(wrap_bootstrap_response(result)))
|
||||
}
|
||||
|
||||
async fn handle_query_vault_state(
|
||||
actor: &ActorRef<UserAgentSession>,
|
||||
) -> Result<Option<UserAgentResponsePayload>, Status> {
|
||||
let state = match actor.ask(HandleQueryVaultState {}).await {
|
||||
Ok(KeyHolderState::Unbootstrapped) => ProtoVaultState::Unbootstrapped,
|
||||
Ok(KeyHolderState::Sealed) => ProtoVaultState::Sealed,
|
||||
Ok(KeyHolderState::Unsealed) => ProtoVaultState::Unsealed,
|
||||
Err(err) => {
|
||||
warn!(error = ?err, "Failed to query vault state");
|
||||
ProtoVaultState::Error
|
||||
}
|
||||
};
|
||||
Ok(Some(wrap_vault_response(VaultResponsePayload::State(
|
||||
state.into(),
|
||||
))))
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use arbiter_proto::{proto::arbiter_service_server::ArbiterServiceServer, url::ArbiterUrl};
|
||||
use arbiter_server::{Server, actors::bootstrap::GetToken, context::ServerContext, db};
|
||||
use miette::miette;
|
||||
use rustls::crypto::aws_lc_rs;
|
||||
use tonic::transport::{Identity, ServerTlsConfig};
|
||||
use tracing::info;
|
||||
@@ -10,7 +10,7 @@ use tracing::info;
|
||||
const PORT: u16 = 50051;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> miette::Result<()> {
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
aws_lc_rs::default_provider().install_default().unwrap();
|
||||
|
||||
tracing_subscriber::fmt()
|
||||
@@ -46,11 +46,11 @@ async fn main() -> miette::Result<()> {
|
||||
|
||||
tonic::transport::Server::builder()
|
||||
.tls_config(tls)
|
||||
.map_err(|err| miette!("Faild to setup TLS: {err}"))?
|
||||
.map_err(|err| anyhow!("Failed to setup TLS: {err}"))?
|
||||
.add_service(ArbiterServiceServer::new(Server::new(context)))
|
||||
.serve(addr)
|
||||
.await
|
||||
.map_err(|e| miette::miette!("gRPC server error: {e}"))?;
|
||||
.map_err(|e| anyhow!("gRPC server error: {e}"))?;
|
||||
|
||||
unreachable!("gRPC server should run indefinitely");
|
||||
}
|
||||
|
||||
@@ -85,7 +85,6 @@ pub async fn test_bootstrap_invalid_token_auth() {
|
||||
Err(auth::Error::InvalidBootstrapToken)
|
||||
));
|
||||
|
||||
// Verify no key was registered
|
||||
let mut conn = db.get().await.unwrap();
|
||||
let count: i64 = schema::useragent_client::table
|
||||
.count()
|
||||
@@ -104,7 +103,6 @@ pub async fn test_challenge_auth() {
|
||||
let new_key = ed25519_dalek::SigningKey::generate(&mut rand::rng());
|
||||
let pubkey_bytes = new_key.verifying_key().to_bytes().to_vec();
|
||||
|
||||
// Pre-register key with key_type
|
||||
{
|
||||
let mut conn = db.get().await.unwrap();
|
||||
insert_into(schema::useragent_client::table)
|
||||
@@ -124,7 +122,6 @@ pub async fn test_challenge_auth() {
|
||||
auth::authenticate(&mut props, server_transport).await
|
||||
});
|
||||
|
||||
// Send challenge request
|
||||
test_transport
|
||||
.send(auth::Inbound::AuthChallengeRequest {
|
||||
pubkey: AuthPublicKey::Ed25519(new_key.verifying_key()),
|
||||
@@ -133,7 +130,6 @@ pub async fn test_challenge_auth() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Read the challenge response
|
||||
let response = test_transport
|
||||
.recv()
|
||||
.await
|
||||
@@ -228,7 +224,6 @@ pub async fn test_challenge_auth_rejects_invalid_signature() {
|
||||
let new_key = ed25519_dalek::SigningKey::generate(&mut rand::rng());
|
||||
let pubkey_bytes = new_key.verifying_key().to_bytes().to_vec();
|
||||
|
||||
// Pre-register key with key_type
|
||||
{
|
||||
let mut conn = db.get().await.unwrap();
|
||||
insert_into(schema::useragent_client::table)
|
||||
@@ -268,7 +263,6 @@ pub async fn test_challenge_auth_rejects_invalid_signature() {
|
||||
Err(err) => panic!("Expected Ok response, got Err({err:?})"),
|
||||
};
|
||||
|
||||
// Sign a different challenge value so signature format is valid but verification must fail.
|
||||
let wrong_challenge = arbiter_proto::format_challenge(challenge + 1, &pubkey_bytes);
|
||||
let signature = new_key.sign(&wrong_challenge);
|
||||
|
||||
@@ -280,9 +274,7 @@ pub async fn test_challenge_auth_rejects_invalid_signature() {
|
||||
.unwrap();
|
||||
|
||||
let expected_err = task.await.unwrap();
|
||||
|
||||
println!("Received expected error: {expected_err:#?}");
|
||||
|
||||
assert!(matches!(
|
||||
expected_err,
|
||||
Err(auth::Error::InvalidChallengeSolution)
|
||||
|
||||
Reference in New Issue
Block a user