Skip to content

Commit

Permalink
Added support for get_tx_proof and check_tx_proof (monero-rs#122)
Browse files Browse the repository at this point in the history
* Added support for the create_wallet rpc method

* Fixed error in returned value of create_account method

* Formatted lib.rs

* Formatted models.rs

* Formatted wallet.rs

* Fixed deprecated UTC time and offset functions

* Fixed naming conventions and added support for get_tx_proof and check_tx_proof RPC calls

* Fixed formatting and import issues
  • Loading branch information
mchtilianov authored Oct 27, 2023
1 parent 9fc02ff commit 0e764c1
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 10 deletions.
45 changes: 43 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,10 +1358,51 @@ impl WalletClient {
}

/// Create a new account
pub async fn create_account(&self, label: Option<String>) -> anyhow::Result<CreateWallet> {
pub async fn create_account(&self, label: Option<String>) -> anyhow::Result<AccountCreation> {
let params = empty().chain(once(("label", label.into())));
self.inner
.request::<CreateWallet>("create_account", RpcParams::map(params))
.request::<AccountCreation>("create_account", RpcParams::map(params))
.await
}

/// Create a transaction proof
pub async fn get_tx_proof(
&self,
txid: HashString<Vec<u8>>,
address: Address,
message: Option<String>,
) -> anyhow::Result<String> {
let params = empty()
.chain(once(("txid", txid.to_string().into())))
.chain(once(("address", address.to_string().into())))
.chain(once(("message", message.into())));
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Rsp {
signature: String,
}

Ok(self
.inner
.request::<Rsp>("get_tx_proof", RpcParams::map(params))
.await?
.signature)
}

/// Check a transaction proof
pub async fn check_tx_proof(
&self,
txid: HashString<Vec<u8>>,
address: Address,
message: Option<String>,
signature: String,
) -> anyhow::Result<TxProofOutput> {
let params = empty()
.chain(once(("txid", txid.to_string().into())))
.chain(once(("address", address.to_string().into())))
.chain(once(("message", message.into())))
.chain(once(("signature", signature.into())));
self.inner
.request::<TxProofOutput>("check_tx_proof", RpcParams::map(params))
.await
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,15 +559,29 @@ pub struct KeyImageImportResponse {
pub unspent: Amount,
}

/// Return type of wallet `create_wallet`.
/// Return type of `create_wallet`.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CreateWallet {
pub struct AccountCreation {
/// Index of the new account.
pub account_index: u32,
/// Generated wallet address.
pub address: Address,
}

/// Return type of `check_tx_proof`.
#[derive(Clone, Debug, Deserialize)]
pub struct TxProofOutput {
/// Number of block mined after the one with the transaction.
pub confirmations: u32,
/// States if the inputs proves the transaction.
pub good: bool,
/// States if the transaction is still in pool or has been added to a block.
pub in_pool: bool,
/// Amount of the transaction.
#[serde(with = "amount::serde::as_pico")]
pub received: Amount,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
41 changes: 40 additions & 1 deletion tests/clients_tests/all_clients_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,10 @@ pub async fn run() {
subaddr_index: Index { major: 0, minor: 0 },
suggested_confirmations_threshold: 1,
// this is any date, since it will not be tested against anything
timestamp: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc),
timestamp: DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(0, 0).unwrap(),
Utc,
),
txid: HashString(transfer_1_data.tx_hash.0.as_ref().to_vec()),
transfer_type: GetTransfersCategory::Pending,
unlock_time: 0,
Expand Down Expand Up @@ -874,4 +877,40 @@ pub async fn run() {
},
)
.await;

// Create tx proof and check tx proof test
//---------------------------------------------------------------------------------------//
let selector_data: HashMap<GetTransfersCategory, bool> = HashMap::from([
(GetTransfersCategory::In, true),
(GetTransfersCategory::Out, true),
(GetTransfersCategory::Pending, false),
(GetTransfersCategory::Failed, false),
(GetTransfersCategory::Pool, false),
(GetTransfersCategory::Block, false),
]);
let selector = GetTransfersSelector {
category_selector: selector_data,
account_index: None,
subaddr_indices: None,
block_height_filter: None,
};
let res = wallet.get_transfers(selector).await;
assert!(res.is_ok());
let res = res.unwrap();
let transfers = res.get(&GetTransfersCategory::Out);
if transfers.is_some() {
let transfers = transfers.unwrap();
let transfer = transfers[0].clone();

helpers::wallet::create_check_tx_proof_assert_ok(
&wallet,
transfer.txid,
transfer.address,
Some(String::from("Test")),
)
.await;
} else {
panic!("No Transfers to Test for");
}
//---------------------------------------------------------------------------------------//
}
5 changes: 4 additions & 1 deletion tests/clients_tests/empty_blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ pub async fn run() {
// this **is** used inside the test functions, since this block header corresponds
// to the genesis block;
// note that in the `non_empty_blockchain`, this field is **not** tested.
timestamp: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc),
timestamp: DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(0, 0).unwrap(),
Utc,
),
};

helpers::regtest::get_last_block_header_assert_block_header(
Expand Down
2 changes: 1 addition & 1 deletion tests/clients_tests/helpers/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fn test_get_block_header_assert_block_header(
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap();
let start_2022_date = DateTime::<Utc>::from_utc(start_2022_date, Utc);
let start_2022_date = DateTime::<Utc>::from_naive_utc_and_offset(start_2022_date, Utc);
assert!(block_header.timestamp >= start_2022_date);
}

Expand Down
24 changes: 22 additions & 2 deletions tests/clients_tests/helpers/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use monero::{
};
use monero_rpc::{
AddressData, BalanceData, GenerateFromKeysArgs, GetAccountsData, GetTransfersCategory,
GetTransfersSelector, GotTransfer, IncomingTransfers, KeyImageImportResponse, Payment,
PrivateKeyType, SignedKeyImage, SignedTransferOutput, SweepAllArgs, TransferData,
GetTransfersSelector, GotTransfer, HashString, IncomingTransfers, KeyImageImportResponse,
Payment, PrivateKeyType, SignedKeyImage, SignedTransferOutput, SweepAllArgs, TransferData,
TransferOptions, TransferPriority, TransferType, WalletClient, WalletCreation,
};

Expand Down Expand Up @@ -761,3 +761,23 @@ pub async fn create_account_assert_ok(wallet: &WalletClient, label: Option<Strin
assert!(res.is_ok());
assert!(res.unwrap().account_index >= 1);
}

pub async fn create_check_tx_proof_assert_ok(
wallet: &WalletClient,
txid: HashString<Vec<u8>>,
address: Address,
message: Option<String>,
) {
let proof_res = wallet
.get_tx_proof(txid.clone(), address, message.clone())
.await;

assert!(proof_res.is_ok());

let check_res = wallet
.check_tx_proof(txid, address, message, proof_res.unwrap())
.await;

assert!(check_res.is_ok());
assert!(check_res.unwrap().good);
}
5 changes: 4 additions & 1 deletion tests/clients_tests/non_empty_blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ pub async fn run() {
reward: Amount::from_pico(35180379334199),
// this is not used inside the test functions below, since its value depend on when the
// test was run, so use any date in this field since it is insignificant for testing.
timestamp: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc),
timestamp: DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(0, 0).unwrap(),
Utc,
),
};
helpers::regtest::get_last_block_header_assert_block_header(
&regtest,
Expand Down

0 comments on commit 0e764c1

Please sign in to comment.