common: fix the proxy design
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
parent
0b5f2f1213
commit
947c93907c
1
Makefile
1
Makefile
|
@ -11,6 +11,7 @@ fmt:
|
|||
$(CC) fmt --all
|
||||
|
||||
check:
|
||||
$(CC) build
|
||||
RUST_LOG=debug $(CC) test --all -- --show-output
|
||||
|
||||
example:
|
||||
|
|
|
@ -55,6 +55,8 @@
|
|||
export HOST_CC=gcc
|
||||
export PWD="$(pwd)"
|
||||
export RUST_LOG=debug
|
||||
|
||||
make
|
||||
'';
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,17 +1,55 @@
|
|||
//! A module for operating an RGB HTTP JSON-RPC proxy
|
||||
use core::str::FromStr;
|
||||
use core::time::Duration;
|
||||
|
||||
use amplify::s;
|
||||
use bitcoin::Network;
|
||||
use reqwest::header::CONTENT_TYPE;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::task;
|
||||
|
||||
use core::time::Duration;
|
||||
|
||||
use crate::BlockingClient;
|
||||
|
||||
const JSON: &str = "application/json";
|
||||
const PROXY_TIMEOUT: u8 = 90;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Client {
|
||||
inner: BlockingClient,
|
||||
network: Network,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(network: &str) -> anyhow::Result<Self> {
|
||||
let network = Network::from_str(network)?;
|
||||
let inner = BlockingClient::builder()
|
||||
.timeout(Duration::from_secs(PROXY_TIMEOUT as u64))
|
||||
.build()?;
|
||||
Ok(Self { inner, network })
|
||||
}
|
||||
|
||||
pub fn get_consignment(&self, consignment_id: &str) -> anyhow::Result<JsonRpcResponse<String>> {
|
||||
let body = JsonRpcRequest {
|
||||
method: s!("consignment.get"),
|
||||
jsonrpc: s!("2.0"),
|
||||
id: None,
|
||||
params: Some(BlindedUtxoParam {
|
||||
blinded_utxo: consignment_id.to_owned(),
|
||||
}),
|
||||
};
|
||||
|
||||
// FIXME: add a URL for this
|
||||
let url = "";
|
||||
let resp = self
|
||||
.inner
|
||||
.post(format!("{url}"))
|
||||
.header(CONTENT_TYPE, JSON)
|
||||
.json(&body)
|
||||
.send()?
|
||||
.json::<JsonRpcResponse<String>>()?;
|
||||
Ok(resp)
|
||||
}
|
||||
}
|
||||
|
||||
/// JSON-RPC Error
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct JsonRpcError {
|
||||
|
@ -41,32 +79,3 @@ pub struct JsonRpcResponse<R> {
|
|||
pub struct BlindedUtxoParam {
|
||||
blinded_utxo: String,
|
||||
}
|
||||
|
||||
pub(crate) fn get_blocking_client() -> BlockingClient {
|
||||
BlockingClient::builder()
|
||||
.timeout(Duration::from_secs(PROXY_TIMEOUT as u64))
|
||||
.build()
|
||||
.expect("valid proxy")
|
||||
}
|
||||
|
||||
pub(crate) fn get_consignment(
|
||||
url: &str,
|
||||
consignment_id: String,
|
||||
) -> Result<JsonRpcResponse<String>, reqwest::Error> {
|
||||
task::block_in_place(|| {
|
||||
let body = JsonRpcRequest {
|
||||
method: s!("consignment.get"),
|
||||
jsonrpc: s!("2.0"),
|
||||
id: None,
|
||||
params: Some(BlindedUtxoParam {
|
||||
blinded_utxo: consignment_id,
|
||||
}),
|
||||
};
|
||||
get_blocking_client()
|
||||
.post(url)
|
||||
.header(CONTENT_TYPE, JSON)
|
||||
.json(&body)
|
||||
.send()?
|
||||
.json::<JsonRpcResponse<String>>()
|
||||
})
|
||||
}
|
||||
|
|
|
@ -3,12 +3,14 @@ use std::str::FromStr;
|
|||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use bitcoin::Network;
|
||||
|
||||
use crate::lib::wallet::{DatabaseType, Wallet, WalletData};
|
||||
use crate::lib::BitcoinNetwork;
|
||||
use crate::proxy;
|
||||
|
||||
pub struct RGBManager {
|
||||
proxy_client: Arc<crate::BlockingClient>,
|
||||
proxy_client: Arc<proxy::Client>,
|
||||
wallet: Arc<Mutex<Wallet>>,
|
||||
}
|
||||
|
||||
|
@ -20,8 +22,8 @@ impl std::fmt::Debug for RGBManager {
|
|||
|
||||
impl RGBManager {
|
||||
pub fn init(root_dir: &str, pubkey: &str, network: &str) -> anyhow::Result<Self> {
|
||||
let client = proxy::get_blocking_client();
|
||||
let wallet = Wallet::new(WalletData {
|
||||
let client = proxy::Client::new(network)?;
|
||||
let mut wallet = Wallet::new(WalletData {
|
||||
data_dir: root_dir.to_owned(),
|
||||
bitcoin_network: BitcoinNetwork::from_str(network)?,
|
||||
database_type: DatabaseType::Sqlite,
|
||||
|
@ -30,7 +32,16 @@ impl RGBManager {
|
|||
mnemonic: None,
|
||||
vanilla_keychain: None,
|
||||
})?;
|
||||
// FIXME: go online
|
||||
let network = Network::from_str(network)?;
|
||||
let url = match network {
|
||||
Network::Bitcoin => "https://mempool.space/api",
|
||||
Network::Testnet => "https://mempool.space/testnet/api",
|
||||
Network::Signet => "https://mempool.space/signet/api",
|
||||
Network::Regtest => "",
|
||||
};
|
||||
if !url.is_empty() {
|
||||
let _ = wallet.go_online(false, url.to_owned())?;
|
||||
}
|
||||
// FIXME: setting up the correct proxy client URL
|
||||
Ok(Self {
|
||||
proxy_client: Arc::new(client),
|
||||
|
@ -42,7 +53,7 @@ impl RGBManager {
|
|||
self.wallet.clone()
|
||||
}
|
||||
|
||||
pub fn proxy_client(&self) -> Arc<crate::BlockingClient> {
|
||||
pub fn proxy_client(&self) -> Arc<proxy::Client> {
|
||||
self.proxy_client.clone()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,9 +33,11 @@ mod tests {
|
|||
"regtest",
|
||||
)
|
||||
.await?;
|
||||
let result = cln.rpc().call::<json::Value, json::Value>("getinfo", json::json!({}));
|
||||
let result = cln
|
||||
.rpc()
|
||||
.call::<json::Value, json::Value>("getinfo", json::json!({}));
|
||||
log::info!(target: "test_init_plugin", "{:?}", result);
|
||||
assert!(result.is_ok(), "{:?}", result);
|
||||
Ok(())
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
1.75
|
||||
stable
|
||||
|
|
Loading…
Reference in New Issue