2022-05-21 15:33:35 -04:00
|
|
|
use std::fmt::Debug;
|
2022-04-28 03:31:09 -04:00
|
|
|
|
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
|
|
use curve25519_dalek::edwards::{EdwardsPoint, CompressedEdwardsY};
|
|
|
|
|
|
|
|
|
|
use serde::{Serialize, Deserialize, de::DeserializeOwned};
|
2022-11-14 21:49:49 -05:00
|
|
|
use serde_json::{Value, json};
|
2022-04-28 03:31:09 -04:00
|
|
|
|
2022-11-14 23:24:35 -05:00
|
|
|
use digest_auth::AuthContext;
|
|
|
|
|
use reqwest::{Client, RequestBuilder};
|
2022-04-28 03:31:09 -04:00
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
use crate::{
|
2022-07-27 04:05:43 -05:00
|
|
|
Protocol,
|
2022-07-15 01:26:07 -04:00
|
|
|
transaction::{Input, Timelock, Transaction},
|
|
|
|
|
block::Block,
|
|
|
|
|
wallet::Fee,
|
|
|
|
|
};
|
2022-05-21 15:33:35 -04:00
|
|
|
|
2022-04-28 03:31:09 -04:00
|
|
|
#[derive(Deserialize, Debug)]
|
2022-08-21 11:29:01 -04:00
|
|
|
pub struct EmptyResponse {}
|
2022-04-28 03:31:09 -04:00
|
|
|
#[derive(Deserialize, Debug)]
|
2022-08-21 11:29:01 -04:00
|
|
|
pub struct JsonRpcResponse<T> {
|
2022-07-15 01:26:07 -04:00
|
|
|
result: T,
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-15 19:51:59 -04:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
struct TransactionResponse {
|
|
|
|
|
tx_hash: String,
|
|
|
|
|
block_height: usize,
|
|
|
|
|
as_hex: String,
|
|
|
|
|
pruned_as_hex: String,
|
|
|
|
|
}
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
struct TransactionsResponse {
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
missed_tx: Vec<String>,
|
|
|
|
|
txs: Vec<TransactionResponse>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 05:08:37 -04:00
|
|
|
#[derive(Clone, Error, Debug)]
|
2022-04-28 03:31:09 -04:00
|
|
|
pub enum RpcError {
|
|
|
|
|
#[error("internal error ({0})")]
|
|
|
|
|
InternalError(String),
|
|
|
|
|
#[error("connection error")]
|
|
|
|
|
ConnectionError,
|
2022-10-15 22:32:56 -04:00
|
|
|
#[error("invalid node")]
|
|
|
|
|
InvalidNode,
|
2022-05-28 05:08:37 -04:00
|
|
|
#[error("transactions not found")]
|
|
|
|
|
TransactionsNotFound(Vec<[u8; 32]>),
|
2022-04-28 03:31:09 -04:00
|
|
|
#[error("invalid point ({0})")]
|
|
|
|
|
InvalidPoint(String),
|
2022-05-21 23:16:06 -04:00
|
|
|
#[error("pruned transaction")]
|
|
|
|
|
PrunedTransaction,
|
|
|
|
|
#[error("invalid transaction ({0:?})")]
|
2022-07-15 01:26:07 -04:00
|
|
|
InvalidTransaction([u8; 32]),
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn rpc_hex(value: &str) -> Result<Vec<u8>, RpcError> {
|
2022-10-15 22:32:56 -04:00
|
|
|
hex::decode(value).map_err(|_| RpcError::InvalidNode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hash_hex(hash: &str) -> Result<[u8; 32], RpcError> {
|
|
|
|
|
rpc_hex(hash)?.try_into().map_err(|_| RpcError::InvalidNode)
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn rpc_point(point: &str) -> Result<EdwardsPoint, RpcError> {
|
|
|
|
|
CompressedEdwardsY(
|
2022-07-15 01:26:07 -04:00
|
|
|
rpc_hex(point)?.try_into().map_err(|_| RpcError::InvalidPoint(point.to_string()))?,
|
|
|
|
|
)
|
|
|
|
|
.decompress()
|
2022-07-22 02:34:36 -04:00
|
|
|
.ok_or_else(|| RpcError::InvalidPoint(point.to_string()))
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-19 12:03:01 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2022-11-14 23:24:35 -05:00
|
|
|
pub struct Rpc {
|
|
|
|
|
client: Client,
|
|
|
|
|
userpass: Option<(String, String)>,
|
|
|
|
|
url: String,
|
|
|
|
|
}
|
2022-06-19 12:03:01 -04:00
|
|
|
|
2022-04-28 03:31:09 -04:00
|
|
|
impl Rpc {
|
2022-11-14 23:24:35 -05:00
|
|
|
/// Create a new RPC connection.
|
|
|
|
|
/// A daemon requiring authentication can be used via including the username and password in the
|
|
|
|
|
/// URL.
|
|
|
|
|
pub fn new(mut url: String) -> Result<Rpc, RpcError> {
|
|
|
|
|
// Parse out the username and password
|
|
|
|
|
let userpass = if url.contains('@') {
|
|
|
|
|
let url_clone = url.clone();
|
|
|
|
|
let split_url = url_clone.split('@').collect::<Vec<_>>();
|
|
|
|
|
if split_url.len() != 2 {
|
|
|
|
|
Err(RpcError::InvalidNode)?;
|
|
|
|
|
}
|
|
|
|
|
let mut userpass = split_url[0];
|
|
|
|
|
url = split_url[1].to_string();
|
|
|
|
|
|
|
|
|
|
// If there was additionally a protocol string, restore that to the daemon URL
|
|
|
|
|
if userpass.contains("://") {
|
|
|
|
|
let split_userpass = userpass.split("://").collect::<Vec<_>>();
|
|
|
|
|
if split_userpass.len() != 2 {
|
|
|
|
|
Err(RpcError::InvalidNode)?;
|
|
|
|
|
}
|
|
|
|
|
url = split_userpass[0].to_string() + "://" + &url;
|
|
|
|
|
userpass = split_userpass[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let split_userpass = userpass.split(':').collect::<Vec<_>>();
|
|
|
|
|
if split_userpass.len() != 2 {
|
|
|
|
|
Err(RpcError::InvalidNode)?;
|
|
|
|
|
}
|
|
|
|
|
Some((split_userpass[0].to_string(), split_userpass[1].to_string()))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(Rpc { client: Client::new(), userpass, url })
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-14 21:49:49 -05:00
|
|
|
/// Perform a RPC call to the specified method with the provided parameters.
|
|
|
|
|
/// This is NOT a JSON-RPC call, which use a method of "json_rpc" and are available via
|
|
|
|
|
/// `json_rpc_call`.
|
2022-07-15 01:26:07 -04:00
|
|
|
pub async fn rpc_call<Params: Serialize + Debug, Response: DeserializeOwned + Debug>(
|
|
|
|
|
&self,
|
|
|
|
|
method: &str,
|
|
|
|
|
params: Option<Params>,
|
|
|
|
|
) -> Result<Response, RpcError> {
|
2022-11-14 23:24:35 -05:00
|
|
|
let mut builder = self.client.post(self.url.clone() + "/" + method);
|
2022-04-28 03:31:09 -04:00
|
|
|
if let Some(params) = params.as_ref() {
|
|
|
|
|
builder = builder.json(params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.call_tail(method, builder).await
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 21:49:49 -05:00
|
|
|
/// Perform a JSON-RPC call to the specified method with the provided parameters
|
|
|
|
|
pub async fn json_rpc_call<Response: DeserializeOwned + Debug>(
|
|
|
|
|
&self,
|
|
|
|
|
method: &str,
|
|
|
|
|
params: Option<Value>,
|
|
|
|
|
) -> Result<Response, RpcError> {
|
|
|
|
|
let mut req = json!({ "method": method });
|
|
|
|
|
if let Some(params) = params {
|
|
|
|
|
req.as_object_mut().unwrap().insert("params".into(), params);
|
|
|
|
|
}
|
|
|
|
|
Ok(self.rpc_call::<_, JsonRpcResponse<Response>>("json_rpc", Some(req)).await?.result)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 07:44:49 -05:00
|
|
|
/// Perform a binary call to the specified method with the provided parameters.
|
2022-07-15 01:26:07 -04:00
|
|
|
pub async fn bin_call<Response: DeserializeOwned + Debug>(
|
|
|
|
|
&self,
|
|
|
|
|
method: &str,
|
|
|
|
|
params: Vec<u8>,
|
|
|
|
|
) -> Result<Response, RpcError> {
|
2022-11-14 23:24:35 -05:00
|
|
|
let builder = self.client.post(self.url.clone() + "/" + method).body(params.clone());
|
2022-04-28 03:31:09 -04:00
|
|
|
self.call_tail(method, builder.header("Content-Type", "application/octet-stream")).await
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
async fn call_tail<Response: DeserializeOwned + Debug>(
|
|
|
|
|
&self,
|
|
|
|
|
method: &str,
|
2022-11-14 23:24:35 -05:00
|
|
|
mut builder: RequestBuilder,
|
2022-07-15 01:26:07 -04:00
|
|
|
) -> Result<Response, RpcError> {
|
2022-11-14 23:24:35 -05:00
|
|
|
if let Some((user, pass)) = &self.userpass {
|
|
|
|
|
let req = self.client.post(&self.url).send().await.map_err(|_| RpcError::InvalidNode)?;
|
|
|
|
|
// Only provide authentication if this daemon actually expects it
|
|
|
|
|
if let Some(header) = req.headers().get("www-authenticate") {
|
|
|
|
|
builder = builder.header(
|
|
|
|
|
"Authorization",
|
|
|
|
|
digest_auth::parse(header.to_str().map_err(|_| RpcError::InvalidNode)?)
|
|
|
|
|
.map_err(|_| RpcError::InvalidNode)?
|
|
|
|
|
.respond(&AuthContext::new_post::<_, _, _, &[u8]>(
|
|
|
|
|
user,
|
|
|
|
|
pass,
|
|
|
|
|
"/".to_string() + method,
|
|
|
|
|
None,
|
|
|
|
|
))
|
|
|
|
|
.map_err(|_| RpcError::InvalidNode)?
|
|
|
|
|
.to_header_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
let res = builder.send().await.map_err(|_| RpcError::ConnectionError)?;
|
|
|
|
|
|
|
|
|
|
Ok(if !method.ends_with(".bin") {
|
|
|
|
|
serde_json::from_str(&res.text().await.map_err(|_| RpcError::ConnectionError)?)
|
|
|
|
|
.map_err(|_| RpcError::InternalError("Failed to parse JSON response".to_string()))?
|
|
|
|
|
} else {
|
|
|
|
|
monero_epee_bin_serde::from_bytes(&res.bytes().await.map_err(|_| RpcError::ConnectionError)?)
|
|
|
|
|
.map_err(|_| RpcError::InternalError("Failed to parse binary response".to_string()))?
|
|
|
|
|
})
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-28 07:44:49 -05:00
|
|
|
/// Get the active blockchain protocol version.
|
2022-07-27 04:05:43 -05:00
|
|
|
pub async fn get_protocol(&self) -> Result<Protocol, RpcError> {
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
struct ProtocolResponse {
|
|
|
|
|
major_version: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
struct LastHeaderResponse {
|
|
|
|
|
block_header: ProtocolResponse,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(
|
|
|
|
|
match self
|
2022-11-14 21:49:49 -05:00
|
|
|
.json_rpc_call::<LastHeaderResponse>("get_last_block_header", None)
|
2022-07-27 04:05:43 -05:00
|
|
|
.await?
|
|
|
|
|
.block_header
|
|
|
|
|
.major_version
|
|
|
|
|
{
|
|
|
|
|
13 | 14 => Protocol::v14,
|
|
|
|
|
15 | 16 => Protocol::v16,
|
|
|
|
|
_ => Protocol::Unsupported,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 03:31:09 -04:00
|
|
|
pub async fn get_height(&self) -> Result<usize, RpcError> {
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
struct HeightResponse {
|
2022-07-15 01:26:07 -04:00
|
|
|
height: usize,
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
Ok(self.rpc_call::<Option<()>, HeightResponse>("get_height", None).await?.height)
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-22 12:15:14 -04:00
|
|
|
pub async fn get_transactions(&self, hashes: &[[u8; 32]]) -> Result<Vec<Transaction>, RpcError> {
|
2022-07-22 02:34:36 -04:00
|
|
|
if hashes.is_empty() {
|
2022-08-22 12:15:14 -04:00
|
|
|
return Ok(vec![]);
|
2022-05-21 21:35:25 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
let txs: TransactionsResponse = self
|
|
|
|
|
.rpc_call(
|
|
|
|
|
"get_transactions",
|
|
|
|
|
Some(json!({
|
2022-09-04 21:23:38 -04:00
|
|
|
"txs_hashes": hashes.iter().map(hex::encode).collect::<Vec<_>>()
|
2022-07-15 01:26:07 -04:00
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2022-04-28 03:31:09 -04:00
|
|
|
|
2022-08-22 13:35:49 -04:00
|
|
|
if !txs.missed_tx.is_empty() {
|
2022-08-22 12:15:14 -04:00
|
|
|
Err(RpcError::TransactionsNotFound(
|
2022-10-15 22:32:56 -04:00
|
|
|
txs.missed_tx.iter().map(|hash| hash_hex(hash)).collect::<Result<_, _>>()?,
|
2022-08-22 12:15:14 -04:00
|
|
|
))?;
|
2022-05-28 05:08:37 -04:00
|
|
|
}
|
|
|
|
|
|
2022-08-22 12:15:14 -04:00
|
|
|
txs
|
|
|
|
|
.txs
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|res| {
|
2022-10-15 22:32:56 -04:00
|
|
|
let tx = Transaction::deserialize(&mut std::io::Cursor::new(rpc_hex(
|
|
|
|
|
if !res.as_hex.is_empty() { &res.as_hex } else { &res.pruned_as_hex },
|
|
|
|
|
)?))
|
|
|
|
|
.map_err(|_| match hash_hex(&res.tx_hash) {
|
|
|
|
|
Ok(hash) => RpcError::InvalidTransaction(hash),
|
|
|
|
|
Err(err) => err,
|
2022-08-22 12:15:14 -04:00
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
// https://github.com/monero-project/monero/issues/8311
|
|
|
|
|
if res.as_hex.is_empty() {
|
|
|
|
|
match tx.prefix.inputs.get(0) {
|
|
|
|
|
Some(Input::Gen { .. }) => (),
|
|
|
|
|
_ => Err(RpcError::PrunedTransaction)?,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(tx)
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-28 05:28:42 -04:00
|
|
|
pub async fn get_transaction(&self, tx: [u8; 32]) -> Result<Transaction, RpcError> {
|
|
|
|
|
self.get_transactions(&[tx]).await.map(|mut txs| txs.swap_remove(0))
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-15 21:39:06 -04:00
|
|
|
pub async fn get_transaction_block_number(&self, tx: &[u8]) -> Result<usize, RpcError> {
|
2022-10-15 19:51:59 -04:00
|
|
|
let txs: TransactionsResponse =
|
|
|
|
|
self.rpc_call("get_transactions", Some(json!({ "txs_hashes": [hex::encode(tx)] }))).await?;
|
|
|
|
|
|
|
|
|
|
if !txs.missed_tx.is_empty() {
|
|
|
|
|
Err(RpcError::TransactionsNotFound(
|
2022-10-15 22:32:56 -04:00
|
|
|
txs.missed_tx.iter().map(|hash| hash_hex(hash)).collect::<Result<_, _>>()?,
|
2022-10-15 19:51:59 -04:00
|
|
|
))?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(txs.txs[0].block_height)
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 06:24:52 -04:00
|
|
|
pub async fn get_block(&self, height: usize) -> Result<Block, RpcError> {
|
2022-04-28 03:31:09 -04:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
struct BlockResponse {
|
2022-07-15 01:26:07 -04:00
|
|
|
blob: String,
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-14 21:49:49 -05:00
|
|
|
let block: BlockResponse =
|
|
|
|
|
self.json_rpc_call("get_block", Some(json!({ "height": height }))).await?;
|
2022-05-04 06:24:52 -04:00
|
|
|
Ok(
|
2022-11-14 21:49:49 -05:00
|
|
|
Block::deserialize(&mut std::io::Cursor::new(rpc_hex(&block.blob)?))
|
2022-07-15 01:26:07 -04:00
|
|
|
.expect("Monero returned a block we couldn't deserialize"),
|
2022-05-04 06:24:52 -04:00
|
|
|
)
|
|
|
|
|
}
|
2022-04-28 03:31:09 -04:00
|
|
|
|
2022-08-22 12:15:14 -04:00
|
|
|
pub async fn get_block_transactions(&self, height: usize) -> Result<Vec<Transaction>, RpcError> {
|
2022-05-04 06:24:52 -04:00
|
|
|
let block = self.get_block(height).await?;
|
2022-04-28 03:31:09 -04:00
|
|
|
let mut res = vec![block.miner_tx];
|
2022-08-22 12:15:14 -04:00
|
|
|
res.extend(self.get_transactions(&block.txs).await?);
|
2022-04-28 03:31:09 -04:00
|
|
|
Ok(res)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 07:44:49 -05:00
|
|
|
/// Get the output indexes of the specified transaction.
|
2022-05-21 15:33:35 -04:00
|
|
|
pub async fn get_o_indexes(&self, hash: [u8; 32]) -> Result<Vec<u64>, RpcError> {
|
2022-04-28 03:31:09 -04:00
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
|
struct Request {
|
2022-07-15 01:26:07 -04:00
|
|
|
txid: [u8; 32],
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
struct OIndexes {
|
|
|
|
|
o_indexes: Vec<u64>,
|
|
|
|
|
status: String,
|
|
|
|
|
untrusted: bool,
|
|
|
|
|
credits: usize,
|
2022-07-15 01:26:07 -04:00
|
|
|
top_hash: String,
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
let indexes: OIndexes = self
|
|
|
|
|
.bin_call(
|
|
|
|
|
"get_o_indexes.bin",
|
|
|
|
|
monero_epee_bin_serde::to_bytes(&Request { txid: hash }).unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2022-04-28 03:31:09 -04:00
|
|
|
|
|
|
|
|
Ok(indexes.o_indexes)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 07:44:49 -05:00
|
|
|
/// Get the output distribution, from the specified height to the specified height (both
|
|
|
|
|
/// inclusive).
|
2022-07-15 01:26:07 -04:00
|
|
|
pub async fn get_output_distribution(
|
|
|
|
|
&self,
|
|
|
|
|
from: usize,
|
|
|
|
|
to: usize,
|
|
|
|
|
) -> Result<Vec<u64>, RpcError> {
|
2022-06-19 12:03:01 -04:00
|
|
|
#[allow(dead_code)]
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
2022-08-21 11:06:17 -04:00
|
|
|
struct Distribution {
|
2022-07-15 01:26:07 -04:00
|
|
|
distribution: Vec<u64>,
|
2022-06-19 12:03:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
struct Distributions {
|
2022-07-15 01:26:07 -04:00
|
|
|
distributions: Vec<Distribution>,
|
2022-06-19 12:03:01 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-14 21:49:49 -05:00
|
|
|
let mut distributions: Distributions = self
|
|
|
|
|
.json_rpc_call(
|
|
|
|
|
"get_output_distribution",
|
2022-07-15 01:26:07 -04:00
|
|
|
Some(json!({
|
2022-11-14 21:49:49 -05:00
|
|
|
"binary": false,
|
|
|
|
|
"amounts": [0],
|
|
|
|
|
"cumulative": true,
|
|
|
|
|
"from_height": from,
|
|
|
|
|
"to_height": to,
|
2022-07-15 01:26:07 -04:00
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2022-06-19 12:03:01 -04:00
|
|
|
|
2022-11-14 21:49:49 -05:00
|
|
|
Ok(distributions.distributions.swap_remove(0).distribution)
|
2022-06-19 12:03:01 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-28 07:44:49 -05:00
|
|
|
/// Get the specified outputs from the RingCT (zero-amount) pool, but only return them if they're
|
|
|
|
|
/// unlocked.
|
2022-08-21 05:13:07 -04:00
|
|
|
pub async fn get_unlocked_outputs(
|
2022-05-04 06:24:52 -04:00
|
|
|
&self,
|
|
|
|
|
indexes: &[u64],
|
2022-07-15 01:26:07 -04:00
|
|
|
height: usize,
|
2022-05-04 06:24:52 -04:00
|
|
|
) -> Result<Vec<Option<[EdwardsPoint; 2]>>, RpcError> {
|
2022-04-28 03:31:09 -04:00
|
|
|
#[derive(Deserialize, Debug)]
|
2022-08-21 11:06:17 -04:00
|
|
|
struct Out {
|
2022-04-28 03:31:09 -04:00
|
|
|
key: String,
|
2022-05-04 06:24:52 -04:00
|
|
|
mask: String,
|
2022-07-15 01:26:07 -04:00
|
|
|
txid: String,
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
struct Outs {
|
2022-07-15 01:26:07 -04:00
|
|
|
outs: Vec<Out>,
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
let outs: Outs = self
|
|
|
|
|
.rpc_call(
|
|
|
|
|
"get_outs",
|
|
|
|
|
Some(json!({
|
|
|
|
|
"get_txid": true,
|
|
|
|
|
"outputs": indexes.iter().map(|o| json!({
|
|
|
|
|
"amount": 0,
|
|
|
|
|
"index": o
|
|
|
|
|
})).collect::<Vec<_>>()
|
|
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
let txs = self
|
|
|
|
|
.get_transactions(
|
|
|
|
|
&outs
|
|
|
|
|
.outs
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|out| {
|
|
|
|
|
rpc_hex(&out.txid)
|
|
|
|
|
.expect("Monero returned an invalidly encoded hash")
|
|
|
|
|
.try_into()
|
|
|
|
|
.expect("Monero returned an invalid sized hash")
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2022-08-21 05:13:07 -04:00
|
|
|
|
2022-08-21 10:35:10 -04:00
|
|
|
// TODO: https://github.com/serai-dex/serai/issues/104
|
2022-07-15 01:26:07 -04:00
|
|
|
outs
|
|
|
|
|
.outs
|
|
|
|
|
.iter()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.map(|(i, out)| {
|
|
|
|
|
Ok(Some([rpc_point(&out.key)?, rpc_point(&out.mask)?]).filter(|_| {
|
2022-06-02 00:00:26 -04:00
|
|
|
match txs[i].prefix.timelock {
|
2022-08-12 15:53:48 -04:00
|
|
|
Timelock::Block(t_height) => t_height <= height,
|
2022-07-15 01:26:07 -04:00
|
|
|
_ => false,
|
2022-06-02 00:00:26 -04:00
|
|
|
}
|
2022-07-15 01:26:07 -04:00
|
|
|
}))
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
2022-05-04 06:24:52 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-28 07:44:49 -05:00
|
|
|
/// Get the currently estimated fee from the node. This may be manipulated to unsafe levels and
|
|
|
|
|
/// MUST be sanity checked.
|
|
|
|
|
// TODO: Take a sanity check argument
|
2022-06-19 12:03:01 -04:00
|
|
|
pub async fn get_fee(&self) -> Result<Fee, RpcError> {
|
2022-05-04 08:18:43 -04:00
|
|
|
#[allow(dead_code)]
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
2022-06-19 12:03:01 -04:00
|
|
|
struct FeeResponse {
|
|
|
|
|
fee: u64,
|
2022-07-15 01:26:07 -04:00
|
|
|
quantization_mask: u64,
|
2022-05-04 08:18:43 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-14 21:49:49 -05:00
|
|
|
let res: FeeResponse = self.json_rpc_call("get_fee_estimate", None).await?;
|
|
|
|
|
Ok(Fee { per_weight: res.fee, mask: res.quantization_mask })
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn publish_transaction(&self, tx: &Transaction) -> Result<(), RpcError> {
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
|
struct SendRawResponse {
|
|
|
|
|
status: String,
|
|
|
|
|
double_spend: bool,
|
|
|
|
|
fee_too_low: bool,
|
|
|
|
|
invalid_input: bool,
|
|
|
|
|
invalid_output: bool,
|
|
|
|
|
low_mixin: bool,
|
|
|
|
|
not_relayed: bool,
|
|
|
|
|
overspend: bool,
|
|
|
|
|
too_big: bool,
|
|
|
|
|
too_few_outputs: bool,
|
2022-07-15 01:26:07 -04:00
|
|
|
reason: String,
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
let mut buf = Vec::with_capacity(2048);
|
|
|
|
|
tx.serialize(&mut buf).unwrap();
|
2022-07-15 01:26:07 -04:00
|
|
|
let res: SendRawResponse = self
|
|
|
|
|
.rpc_call("send_raw_transaction", Some(json!({ "tx_as_hex": hex::encode(&buf) })))
|
|
|
|
|
.await?;
|
2022-04-28 03:31:09 -04:00
|
|
|
|
|
|
|
|
if res.status != "OK" {
|
2022-05-21 23:16:06 -04:00
|
|
|
Err(RpcError::InvalidTransaction(tx.hash()))?;
|
2022-04-28 03:31:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|