common: implement the post commitment

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
Vincenzo Palazzo 2024-03-11 17:52:01 +01:00
parent 369bd5ddb1
commit c18eceed17
3 changed files with 79 additions and 22 deletions

View File

@ -1,4 +1,5 @@
//! RGB Wallet mock
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::{Arc, Mutex};
@ -74,6 +75,10 @@ impl Wallet {
})
}
pub fn path(&self) -> PathBuf {
self.wallet.lock().unwrap().get_wallet_dir()
}
fn get_coin_type(bitcoin_network: BitcoinNetwork) -> u32 {
u32::from(bitcoin_network != BitcoinNetwork::Mainnet)
}

View File

@ -1,8 +1,11 @@
//! A module for operating an RGB HTTP JSON-RPC proxy
use core::str::FromStr;
use core::time::Duration;
use std::path::Path;
use amplify::s;
use reqwest::blocking::multipart::Form;
use reqwest::blocking::multipart::Part;
use reqwest::header::CONTENT_TYPE;
use serde::{Deserialize, Serialize};
@ -13,18 +16,23 @@ const JSON: &str = "application/json";
const PROXY_TIMEOUT: u8 = 90;
#[derive(Debug, Clone)]
pub struct Client {
pub struct ConsignmentClient {
inner: BlockingClient,
network: Network,
pub url: String,
}
impl Client {
impl ConsignmentClient {
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 })
Ok(Self {
inner,
network,
url: "".to_owned(),
})
}
pub fn get_consignment(&self, consignment_id: &str) -> anyhow::Result<JsonRpcResponse<String>> {
@ -38,7 +46,7 @@ impl Client {
};
// FIXME: add a URL for this
let url = "";
let url = format!("{}/TODO", self.url);
let resp = self
.inner
.post(format!("{url}"))
@ -48,6 +56,43 @@ impl Client {
.json::<JsonRpcResponse<String>>()?;
Ok(resp)
}
pub fn post_consignment(
&self,
consignment_path: &Path,
recipient_id: String,
txid: String,
vout: Option<u32>,
) -> anyhow::Result<()> {
let file_name = consignment_path
.file_name()
.map(|filename| filename.to_string_lossy().into_owned())
.unwrap();
let consignment_file = Part::file(consignment_path)?.file_name(file_name);
let params = serde_json::json!({
"recipient_id": recipient_id,
"txid": txid,
"vout": vout,
});
let form = Form::new()
.text("method", "consignment.post")
.text("jsonrpc", "2.0")
.text("id", "1")
.text("params", serde_json::to_string(&params)?)
.part("file", consignment_file);
// FIXME: add a URL for this
let url = format!("{}/TODO", self.url);
self.inner
.post(format!("{url}"))
.header(CONTENT_TYPE, JSON)
.multipart(form)
.send()?
.json::<JsonRpcResponse<String>>()?;
Ok(())
}
}
/// JSON-RPC Error

View File

@ -1,14 +1,9 @@
//! RGB Manager
use std::collections::HashMap;
use std::path::Path;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;
use bitcoin::bip32::ChildNumber;
use bitcoin::bip32::{ExtendedPrivKey, ExtendedPubKey};
use bitcoin::secp256k1::Secp256k1;
use bitcoin::Network;
use bitcoin::bip32::ExtendedPrivKey;
use rgb_lib::wallet::Recipient;
use rgb_lib::wallet::RecipientData;
use rgb_lib::ScriptBuf;
@ -16,7 +11,6 @@ use rgbwallet::bitcoin;
use rgbwallet::bitcoin::psbt::PartiallySignedTransaction;
use crate::internal_wallet::Wallet;
use crate::lib::wallet::{DatabaseType, WalletData};
use crate::lib::BitcoinNetwork;
use crate::proxy;
use crate::rgb_storage as store;
@ -28,11 +22,10 @@ use crate::types;
pub const STATIC_BLINDING: u64 = 777;
pub struct RGBManager {
esplora: Arc<proxy::Client>,
consignment_proxy: Arc<proxy::ConsignmentClient>,
storage: Box<dyn store::RGBStorage>,
wallet: Arc<Wallet>,
path: String,
proxy_endpoint: String,
}
impl std::fmt::Debug for RGBManager {
@ -47,16 +40,16 @@ impl RGBManager {
master_xprv: &ExtendedPrivKey,
network: &str,
) -> anyhow::Result<Self> {
let client = proxy::Client::new(network)?;
let storage = Box::new(store::InMemoryStorage::new()?);
let client = proxy::ConsignmentClient::new(network)?;
let bitcoin_network = BitcoinNetwork::from_str(network)?;
let wallet = Wallet::new(&bitcoin_network, *master_xprv, root_dir)?;
// FIXME: setting up the correct proxy client URL
Ok(Self {
esplora: Arc::new(client),
consignment_proxy: Arc::new(client),
wallet: Arc::new(wallet),
path: root_dir.to_owned(),
storage: Box::new(store::InMemoryStorage::new()?),
proxy_endpoint: String::from("TODO add the proxy endpoint"),
storage,
})
}
@ -64,8 +57,8 @@ impl RGBManager {
self.wallet.clone()
}
pub fn proxy_client(&self) -> Arc<proxy::Client> {
self.esplora.clone()
pub fn consignment_proxy(&self) -> Arc<proxy::ConsignmentClient> {
self.consignment_proxy.clone()
}
/// Modify the funding transaction before sign it with the node signer.
@ -89,7 +82,21 @@ impl RGBManager {
index: 0, /* FIXME: cln should tell this info to us */
};
self.prepare_rgb_tx(&info, funding_outpoint, &tx, psbt)?;
// TODO: Step 3: Make the cosignemtn and post it somewhere
// Step 3: Make the cosignemtn and post it somewhere
let consignment_path = self
.wallet()
.path()
.join("transfers")
.join(txid.to_string().clone())
.join(info.contract_id.to_string())
.join("consignment_out");
self.consignment_proxy().post_consignment(
&consignment_path,
txid.to_string(),
txid.to_string(),
Some(0),
)?;
return Ok(tx);
}
Ok(tx)
@ -116,7 +123,7 @@ impl RGBManager {
blinding: Some(STATIC_BLINDING),
},
amount: info.local_rgb_amount,
transport_endpoints: vec![self.proxy_endpoint.clone()]
transport_endpoints: vec![self.consignment_proxy.url.clone()]
}]
};
// FIXME: find the position of the vout;