mirror of
https://github.com/jwinterm/mwixnet.git
synced 2026-11-20 05:03:53 +01:00
converted to async/.await to support integration testing framework
(cherry picked from commit 26c129fa787ffec44f229ad8047d5c375fb4d257)
This commit is contained in:
+77
-29
@@ -1,10 +1,10 @@
|
||||
use crate::crypto::secp;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use grin_api::client;
|
||||
use grin_api::json_rpc::{build_request, Request, Response};
|
||||
use grin_api::json_rpc::{build_request, Request, Response, RpcError};
|
||||
use grin_core::core::Output;
|
||||
use grin_core::libtx::secp_ser;
|
||||
use grin_keychain::BlindingFactor;
|
||||
use grin_onion::crypto::secp;
|
||||
use grin_util::{ToHex, ZeroingString};
|
||||
use grin_wallet_api::{EncryptedRequest, EncryptedResponse, JsonId, Token};
|
||||
use secp256k1zkp::{PublicKey, Secp256k1, SecretKey};
|
||||
@@ -13,9 +13,13 @@ use serde_json::json;
|
||||
use std::net::SocketAddr;
|
||||
use thiserror::Error;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Wallet: Send + Sync {
|
||||
/// Builds an output for the wallet with the provided amount.
|
||||
fn build_output(&self, amount: u64) -> Result<(BlindingFactor, Output), WalletError>;
|
||||
async fn async_build_output(
|
||||
&self,
|
||||
amount: u64,
|
||||
) -> Result<(BlindingFactor, Output), WalletError>;
|
||||
}
|
||||
|
||||
/// Error types for interacting with wallets
|
||||
@@ -31,6 +35,8 @@ pub enum WalletError {
|
||||
ApiCommError(grin_api::Error),
|
||||
#[error("Error decoding JSON-RPC response: {0:?}")]
|
||||
ResponseParseError(grin_api::json_rpc::Error),
|
||||
#[error("Unsucessful response returned: {0:?}")]
|
||||
ResponseRpcError(Option<RpcError>),
|
||||
}
|
||||
|
||||
/// HTTP (JSONRPC) implementation of the 'Wallet' trait.
|
||||
@@ -55,26 +61,27 @@ pub struct ECDHPubkey {
|
||||
|
||||
impl HttpWallet {
|
||||
/// Calls the 'open_wallet' using the RPC API.
|
||||
pub fn open_wallet(
|
||||
pub async fn async_open_wallet(
|
||||
wallet_owner_url: &SocketAddr,
|
||||
wallet_owner_secret: &Option<String>,
|
||||
wallet_pass: &ZeroingString,
|
||||
) -> Result<HttpWallet, WalletError> {
|
||||
println!("Opening wallet at {}", wallet_owner_url);
|
||||
let shared_key = HttpWallet::init_secure_api(&wallet_owner_url, &wallet_owner_secret)?;
|
||||
|
||||
info!("Opening wallet at {}", wallet_owner_url);
|
||||
let shared_key =
|
||||
HttpWallet::async_init_secure_api(&wallet_owner_url, &wallet_owner_secret).await?;
|
||||
let open_wallet_params = json!({
|
||||
"name": null,
|
||||
"password": wallet_pass.to_string()
|
||||
});
|
||||
let token: Token = HttpWallet::send_enc_request(
|
||||
let token: Token = HttpWallet::async_send_enc_request(
|
||||
&wallet_owner_url,
|
||||
&wallet_owner_secret,
|
||||
"open_wallet",
|
||||
&open_wallet_params,
|
||||
&shared_key,
|
||||
)?;
|
||||
println!("Connected to wallet");
|
||||
)
|
||||
.await?;
|
||||
info!("Connected to wallet");
|
||||
|
||||
Ok(HttpWallet {
|
||||
wallet_owner_url: wallet_owner_url.clone(),
|
||||
@@ -84,7 +91,7 @@ impl HttpWallet {
|
||||
})
|
||||
}
|
||||
|
||||
fn init_secure_api(
|
||||
async fn async_init_secure_api(
|
||||
wallet_owner_url: &SocketAddr,
|
||||
wallet_owner_secret: &Option<String>,
|
||||
) -> Result<SecretKey, WalletError> {
|
||||
@@ -95,12 +102,13 @@ impl HttpWallet {
|
||||
"ecdh_pubkey": ephemeral_pk.serialize_vec(&secp, true).to_hex()
|
||||
});
|
||||
|
||||
let response_pk: ECDHPubkey = HttpWallet::send_json_request(
|
||||
let response_pk: ECDHPubkey = HttpWallet::async_send_json_request(
|
||||
&wallet_owner_url,
|
||||
&wallet_owner_secret,
|
||||
"init_secure_api",
|
||||
&init_params,
|
||||
)?;
|
||||
)
|
||||
.await?;
|
||||
|
||||
let shared_key = {
|
||||
let mut shared_pubkey = response_pk.ecdh_pubkey.clone();
|
||||
@@ -113,7 +121,22 @@ impl HttpWallet {
|
||||
Ok(shared_key)
|
||||
}
|
||||
|
||||
fn send_enc_request<D: serde::de::DeserializeOwned>(
|
||||
pub async fn async_perform_request<D: serde::de::DeserializeOwned>(
|
||||
&self,
|
||||
method: &str,
|
||||
params: &serde_json::Value,
|
||||
) -> Result<D, WalletError> {
|
||||
HttpWallet::async_send_enc_request(
|
||||
&self.wallet_owner_url,
|
||||
&self.wallet_owner_secret,
|
||||
method,
|
||||
params,
|
||||
&self.shared_key,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn async_send_enc_request<D: serde::de::DeserializeOwned>(
|
||||
wallet_owner_url: &SocketAddr,
|
||||
wallet_owner_secret: &Option<String>,
|
||||
method: &str,
|
||||
@@ -129,23 +152,30 @@ impl HttpWallet {
|
||||
});
|
||||
let enc_req = EncryptedRequest::from_json(&JsonId::IntId(1), &req, &shared_key)
|
||||
.map_err(WalletError::EncryptRequestError)?;
|
||||
let res = client::post::<EncryptedRequest, EncryptedResponse>(
|
||||
let res = client::post_async::<EncryptedRequest, EncryptedResponse>(
|
||||
url.as_str(),
|
||||
wallet_owner_secret.clone(),
|
||||
&enc_req,
|
||||
wallet_owner_secret.clone(),
|
||||
)
|
||||
.await
|
||||
.map_err(WalletError::ApiCommError)?;
|
||||
let decrypted = res
|
||||
.decrypt(&shared_key)
|
||||
.map_err(WalletError::DecryptResponseError)?;
|
||||
let response: Response =
|
||||
serde_json::from_value(decrypted).map_err(WalletError::DecodeResponseError)?;
|
||||
let ok = response.result.unwrap().get("Ok").unwrap().clone();
|
||||
let parsed = serde_json::from_value(ok).map_err(WalletError::DecodeResponseError)?;
|
||||
let result = response
|
||||
.result
|
||||
.ok_or(WalletError::ResponseRpcError(response.error.clone()))?;
|
||||
let ok = result
|
||||
.get("Ok")
|
||||
.ok_or(WalletError::ResponseRpcError(response.error.clone()))?;
|
||||
let parsed =
|
||||
serde_json::from_value(ok.clone()).map_err(WalletError::DecodeResponseError)?;
|
||||
Ok(parsed)
|
||||
}
|
||||
|
||||
fn send_json_request<D: serde::de::DeserializeOwned>(
|
||||
async fn async_send_json_request<D: serde::de::DeserializeOwned>(
|
||||
wallet_owner_url: &SocketAddr,
|
||||
wallet_owner_secret: &Option<String>,
|
||||
method: &str,
|
||||
@@ -153,15 +183,23 @@ impl HttpWallet {
|
||||
) -> Result<D, WalletError> {
|
||||
let url = format!("http://{}{}", wallet_owner_url, ENDPOINT);
|
||||
let req = build_request(method, params);
|
||||
let res =
|
||||
client::post::<Request, Response>(url.as_str(), wallet_owner_secret.clone(), &req)
|
||||
.map_err(WalletError::ApiCommError)?;
|
||||
let res = client::post_async::<Request, Response>(
|
||||
url.as_str(),
|
||||
&req,
|
||||
wallet_owner_secret.clone(),
|
||||
)
|
||||
.await
|
||||
.map_err(WalletError::ApiCommError)?;
|
||||
let parsed = res
|
||||
.clone()
|
||||
.into_result()
|
||||
.map_err(WalletError::ResponseParseError)?;
|
||||
Ok(parsed)
|
||||
}
|
||||
|
||||
pub fn get_token(&self) -> Token {
|
||||
self.token.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
@@ -174,21 +212,26 @@ pub struct OutputWithBlind {
|
||||
output: Output,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Wallet for HttpWallet {
|
||||
/// Builds an 'Output' for the wallet using the 'build_output' RPC API.
|
||||
fn build_output(&self, amount: u64) -> Result<(BlindingFactor, Output), WalletError> {
|
||||
async fn async_build_output(
|
||||
&self,
|
||||
amount: u64,
|
||||
) -> Result<(BlindingFactor, Output), WalletError> {
|
||||
let req_json = json!({
|
||||
"token": self.token.keychain_mask.clone().unwrap().0,
|
||||
"token": self.token,
|
||||
"features": "Plain",
|
||||
"amount": amount
|
||||
});
|
||||
let output: OutputWithBlind = HttpWallet::send_enc_request(
|
||||
let output: OutputWithBlind = HttpWallet::async_send_enc_request(
|
||||
&self.wallet_owner_url,
|
||||
&self.wallet_owner_secret,
|
||||
"build_output",
|
||||
&req_json,
|
||||
&self.shared_key,
|
||||
)?;
|
||||
)
|
||||
.await?;
|
||||
Ok((output.blind, output.output))
|
||||
}
|
||||
}
|
||||
@@ -196,11 +239,12 @@ impl Wallet for HttpWallet {
|
||||
#[cfg(test)]
|
||||
pub mod mock {
|
||||
use super::{Wallet, WalletError};
|
||||
use crate::crypto::secp;
|
||||
use std::borrow::BorrowMut;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use grin_core::core::{Output, OutputFeatures};
|
||||
use grin_keychain::BlindingFactor;
|
||||
use grin_onion::crypto::secp;
|
||||
use secp256k1zkp::pedersen::Commitment;
|
||||
use secp256k1zkp::Secp256k1;
|
||||
use std::sync::{Arc, Mutex};
|
||||
@@ -225,9 +269,13 @@ pub mod mock {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Wallet for MockWallet {
|
||||
/// Builds an 'Output' for the wallet using the 'build_output' RPC API.
|
||||
fn build_output(&self, amount: u64) -> Result<(BlindingFactor, Output), WalletError> {
|
||||
async fn async_build_output(
|
||||
&self,
|
||||
amount: u64,
|
||||
) -> Result<(BlindingFactor, Output), WalletError> {
|
||||
let secp = Secp256k1::new();
|
||||
let blind = secp::random_secret();
|
||||
let commit = secp::commit(amount, &blind).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user