2021-05-04 16:59:44 +10:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
2021-05-14 18:14:39 +10:00
|
|
|
use monero::ViewPair;
|
2021-05-10 13:21:40 +10:00
|
|
|
use monero_harness::Monero;
|
2021-05-14 18:14:39 +10:00
|
|
|
use monero_rpc::monerod::MonerodRpc;
|
|
|
|
use monero_wallet::{ConfidentialTransactionBuilder, MonerodClientExt};
|
2021-05-11 17:47:59 +10:00
|
|
|
use rand::{Rng, SeedableRng};
|
2021-05-04 16:59:44 +10:00
|
|
|
use std::convert::TryInto;
|
2021-05-10 13:21:40 +10:00
|
|
|
use testcontainers::clients::Cli;
|
2021-05-04 13:37:07 +10:00
|
|
|
|
2021-05-10 17:25:58 +10:00
|
|
|
#[tokio::test]
|
|
|
|
async fn monerod_integration_test() {
|
|
|
|
let mut rng = rand::rngs::StdRng::from_seed([0u8; 32]);
|
2021-05-04 13:37:07 +10:00
|
|
|
|
2021-05-10 17:25:58 +10:00
|
|
|
let cli = Cli::default();
|
2021-05-10 13:21:40 +10:00
|
|
|
let (monero, _monerod_container, _monero_wallet_rpc_containers) =
|
|
|
|
Monero::new(&cli, vec![]).await.unwrap();
|
|
|
|
|
2021-05-11 17:47:59 +10:00
|
|
|
let signing_key = curve25519_dalek::scalar::Scalar::random(&mut rng);
|
2021-05-10 17:25:58 +10:00
|
|
|
let lock_kp = monero::KeyPair {
|
|
|
|
view: monero::PrivateKey::from_scalar(curve25519_dalek::scalar::Scalar::random(&mut rng)),
|
2021-05-11 17:47:59 +10:00
|
|
|
spend: monero::PrivateKey::from_scalar(signing_key),
|
2021-05-10 17:25:58 +10:00
|
|
|
};
|
|
|
|
|
2021-05-14 18:14:39 +10:00
|
|
|
let spend_amount = 999600000000;
|
2021-05-10 13:21:40 +10:00
|
|
|
|
2021-05-10 17:25:58 +10:00
|
|
|
let lock_address = monero::Address::from_keypair(monero::Network::Mainnet, &lock_kp);
|
|
|
|
|
|
|
|
monero.init_miner().await.unwrap();
|
2021-05-10 13:21:40 +10:00
|
|
|
let wallet = monero.wallet("miner").expect("wallet to exist");
|
|
|
|
|
|
|
|
let transfer = wallet
|
2021-05-14 18:14:39 +10:00
|
|
|
.transfer(&lock_address.to_string(), 1_000_000_000_000)
|
2021-05-10 13:21:40 +10:00
|
|
|
.await
|
|
|
|
.expect("lock to succeed");
|
|
|
|
|
2021-05-10 17:25:58 +10:00
|
|
|
let client = monero.monerod().client();
|
|
|
|
|
2021-05-10 13:21:40 +10:00
|
|
|
let miner_address = wallet
|
|
|
|
.address()
|
|
|
|
.await
|
|
|
|
.expect("miner address to exist")
|
|
|
|
.address;
|
2021-05-10 17:25:58 +10:00
|
|
|
client
|
2021-05-10 13:21:40 +10:00
|
|
|
.generateblocks(10, miner_address)
|
|
|
|
.await
|
|
|
|
.expect("can generate blocks");
|
|
|
|
|
2021-05-12 20:29:06 +10:00
|
|
|
let lock_tx_hash = transfer.tx_hash.parse().unwrap();
|
2021-05-04 13:37:07 +10:00
|
|
|
|
2021-05-12 20:29:06 +10:00
|
|
|
let lock_tx = client
|
|
|
|
.get_transactions(&[lock_tx_hash])
|
2021-05-10 13:21:40 +10:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.pop()
|
|
|
|
.unwrap();
|
2021-05-14 13:00:25 +10:00
|
|
|
let output_indices = client.get_o_indexes(lock_tx_hash).await.unwrap().o_indexes;
|
2021-05-07 12:12:24 +10:00
|
|
|
|
2021-05-14 18:14:39 +10:00
|
|
|
let lock_vp = ViewPair::from(&lock_kp);
|
|
|
|
|
|
|
|
let input_to_spend = lock_tx
|
|
|
|
.check_outputs(&lock_vp, 0..1, 0..1)
|
2021-05-14 13:00:25 +10:00
|
|
|
.unwrap()
|
2021-05-10 13:21:40 +10:00
|
|
|
.pop()
|
2021-05-14 13:00:25 +10:00
|
|
|
.unwrap();
|
2021-05-14 18:14:39 +10:00
|
|
|
let global_output_index = output_indices[input_to_spend.index];
|
2021-05-04 13:37:07 +10:00
|
|
|
|
2021-05-04 17:58:54 +10:00
|
|
|
let (lower, upper) = client.calculate_key_offset_boundaries().await.unwrap();
|
2021-05-04 13:37:07 +10:00
|
|
|
|
2021-05-14 18:14:39 +10:00
|
|
|
let mut decoy_indices = Vec::with_capacity(10);
|
2021-05-06 13:39:05 +10:00
|
|
|
for _ in 0..10 {
|
2021-05-04 13:37:07 +10:00
|
|
|
loop {
|
2021-05-14 18:14:39 +10:00
|
|
|
let decoy_index = rng.gen_range(lower.0, upper.0);
|
2021-05-04 13:37:07 +10:00
|
|
|
|
2021-05-14 18:14:39 +10:00
|
|
|
if decoy_indices.contains(&decoy_index) && decoy_index != global_output_index {
|
2021-05-04 13:37:07 +10:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:14:39 +10:00
|
|
|
decoy_indices.push(decoy_index);
|
2021-05-04 13:37:07 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:14:39 +10:00
|
|
|
let decoy_inputs = client
|
|
|
|
.fetch_decoy_inputs(decoy_indices.try_into().unwrap())
|
2021-05-04 16:59:44 +10:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-05-07 14:53:32 +10:00
|
|
|
|
2021-05-14 18:14:39 +10:00
|
|
|
let target_address = "498AVruCDWgP9Az9LjMm89VWjrBrSZ2W2K3HFBiyzzrRjUJWUcCVxvY1iitfuKoek2FdX6MKGAD9Qb1G1P8QgR5jPmmt3Vj".parse().unwrap();
|
2021-05-07 12:12:24 +10:00
|
|
|
|
2021-05-14 18:14:39 +10:00
|
|
|
let transaction =
|
|
|
|
ConfidentialTransactionBuilder::new(input_to_spend, global_output_index, decoy_inputs, lock_kp)
|
|
|
|
.with_output(target_address, spend_amount, &mut rng)
|
|
|
|
.with_output(target_address, 0, &mut rng) // TODO: Do this inside `build`
|
|
|
|
.build(&mut rng);
|
2021-05-04 17:58:54 +10:00
|
|
|
|
2021-05-06 17:25:53 +10:00
|
|
|
client.send_raw_transaction(transaction).await.unwrap();
|
2021-05-04 13:37:07 +10:00
|
|
|
}
|