2023-07-24 08:53:59 +00:00
|
|
|
use std::collections::HashSet;
|
2023-01-16 21:17:54 +00:00
|
|
|
|
2023-01-24 20:22:07 +00:00
|
|
|
use rand_core::{OsRng, RngCore};
|
|
|
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde_json::json;
|
2023-01-16 21:17:54 +00:00
|
|
|
|
|
|
|
use monero_serai::{
|
2023-01-30 09:27:39 +00:00
|
|
|
transaction::Transaction,
|
2023-07-24 08:53:59 +00:00
|
|
|
rpc::{EmptyResponse, HttpRpc, Rpc},
|
2023-01-30 09:25:46 +00:00
|
|
|
wallet::{
|
|
|
|
address::{Network, AddressSpec, SubaddressIndex, MoneroAddress},
|
2024-02-20 01:45:50 +00:00
|
|
|
extra::{MAX_TX_EXTRA_NONCE_SIZE, Extra, PaymentId},
|
2023-01-30 09:25:46 +00:00
|
|
|
Scanner,
|
|
|
|
},
|
2023-01-16 21:17:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mod runner;
|
|
|
|
|
2023-07-24 08:53:59 +00:00
|
|
|
async fn make_integrated_address(rpc: &Rpc<HttpRpc>, payment_id: [u8; 8]) -> String {
|
|
|
|
#[derive(Debug, Deserialize)]
|
2023-01-24 20:22:07 +00:00
|
|
|
struct IntegratedAddressResponse {
|
|
|
|
integrated_address: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = rpc
|
|
|
|
.json_rpc_call::<IntegratedAddressResponse>(
|
|
|
|
"make_integrated_address",
|
|
|
|
Some(json!({ "payment_id": hex::encode(payment_id) })),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
res.integrated_address
|
|
|
|
}
|
|
|
|
|
2023-07-24 08:53:59 +00:00
|
|
|
async fn initialize_rpcs() -> (Rpc<HttpRpc>, Rpc<HttpRpc>, String) {
|
2024-02-09 07:48:44 +00:00
|
|
|
let wallet_rpc = HttpRpc::new("http://127.0.0.1:18082".to_string()).await.unwrap();
|
2023-01-16 21:17:54 +00:00
|
|
|
let daemon_rpc = runner::rpc().await;
|
|
|
|
|
2023-07-24 08:53:59 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct AddressResponse {
|
|
|
|
address: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut wallet_id = [0; 8];
|
|
|
|
OsRng.fill_bytes(&mut wallet_id);
|
|
|
|
let _: EmptyResponse = wallet_rpc
|
|
|
|
.json_rpc_call(
|
|
|
|
"create_wallet",
|
|
|
|
Some(json!({ "filename": hex::encode(wallet_id), "language": "English" })),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let address: AddressResponse =
|
|
|
|
wallet_rpc.json_rpc_call("get_address", Some(json!({ "account_index": 0 }))).await.unwrap();
|
|
|
|
|
|
|
|
// Fund the new wallet
|
|
|
|
daemon_rpc.generate_blocks(&address.address, 70).await.unwrap();
|
|
|
|
|
|
|
|
(wallet_rpc, daemon_rpc, address.address)
|
2023-01-24 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 15:31:58 +00:00
|
|
|
async fn from_wallet_rpc_to_self(spec: AddressSpec) {
|
2023-01-24 20:22:07 +00:00
|
|
|
// initialize rpc
|
|
|
|
let (wallet_rpc, daemon_rpc, wallet_rpc_addr) = initialize_rpcs().await;
|
2023-01-16 21:17:54 +00:00
|
|
|
|
|
|
|
// make an addr
|
|
|
|
let (_, view_pair, _) = runner::random_address();
|
2023-07-24 08:53:59 +00:00
|
|
|
let addr = view_pair.address(Network::Mainnet, spec);
|
2023-01-16 21:17:54 +00:00
|
|
|
|
|
|
|
// refresh & make a tx
|
2023-07-24 08:53:59 +00:00
|
|
|
let _: EmptyResponse = wallet_rpc.json_rpc_call("refresh", None).await.unwrap();
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct TransferResponse {
|
|
|
|
tx_hash: String,
|
|
|
|
}
|
|
|
|
let tx: TransferResponse = wallet_rpc
|
|
|
|
.json_rpc_call(
|
|
|
|
"transfer",
|
|
|
|
Some(json!({
|
|
|
|
"destinations": [{"address": addr.to_string(), "amount": 1_000_000_000_000u64 }],
|
|
|
|
})),
|
2023-01-16 21:17:54 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-07-24 08:53:59 +00:00
|
|
|
let tx_hash = hex::decode(tx.tx_hash).unwrap().try_into().unwrap();
|
2023-01-16 21:17:54 +00:00
|
|
|
|
2024-03-22 21:26:42 +00:00
|
|
|
// TODO: Needs https://github.com/monero-project/monero/pull/9260
|
2023-07-19 19:06:05 +00:00
|
|
|
// let fee_rate = daemon_rpc
|
2024-02-20 02:03:27 +00:00
|
|
|
// .get_fee(daemon_rpc.get_protocol().await.unwrap(), FeePriority::Unimportant)
|
2023-07-19 19:06:05 +00:00
|
|
|
// .await
|
|
|
|
// .unwrap();
|
|
|
|
|
2023-01-16 21:17:54 +00:00
|
|
|
// unlock it
|
2023-07-24 08:53:59 +00:00
|
|
|
runner::mine_until_unlocked(&daemon_rpc, &wallet_rpc_addr, tx_hash).await;
|
2023-01-16 21:17:54 +00:00
|
|
|
|
2023-07-19 19:06:05 +00:00
|
|
|
// Create the scanner
|
2023-01-16 21:17:54 +00:00
|
|
|
let mut scanner = Scanner::from_view(view_pair, Some(HashSet::new()));
|
|
|
|
if let AddressSpec::Subaddress(index) = spec {
|
|
|
|
scanner.register_subaddress(index);
|
|
|
|
}
|
|
|
|
|
2023-07-19 19:06:05 +00:00
|
|
|
// Retrieve it and scan it
|
2023-01-16 21:17:54 +00:00
|
|
|
let tx = daemon_rpc.get_transaction(tx_hash).await.unwrap();
|
|
|
|
let output = scanner.scan_transaction(&tx).not_locked().swap_remove(0);
|
|
|
|
|
2024-03-22 21:26:42 +00:00
|
|
|
// TODO: Needs https://github.com/monero-project/monero/pull/9260
|
2023-07-19 19:06:05 +00:00
|
|
|
// runner::check_weight_and_fee(&tx, fee_rate);
|
|
|
|
|
2023-01-16 21:17:54 +00:00
|
|
|
match spec {
|
2024-02-20 01:45:50 +00:00
|
|
|
AddressSpec::Subaddress(index) => {
|
|
|
|
assert_eq!(output.metadata.subaddress, Some(index));
|
|
|
|
assert_eq!(output.metadata.payment_id, Some(PaymentId::Encrypted([0u8; 8])));
|
|
|
|
}
|
2023-01-16 21:17:54 +00:00
|
|
|
AddressSpec::Integrated(payment_id) => {
|
2024-02-20 01:45:50 +00:00
|
|
|
assert_eq!(output.metadata.payment_id, Some(PaymentId::Encrypted(payment_id)));
|
2023-01-16 21:17:54 +00:00
|
|
|
assert_eq!(output.metadata.subaddress, None);
|
|
|
|
}
|
2023-07-08 15:29:05 +00:00
|
|
|
AddressSpec::Standard | AddressSpec::Featured { .. } => {
|
2024-02-20 01:45:50 +00:00
|
|
|
assert_eq!(output.metadata.subaddress, None);
|
|
|
|
assert_eq!(output.metadata.payment_id, Some(PaymentId::Encrypted([0u8; 8])));
|
2023-07-08 15:29:05 +00:00
|
|
|
}
|
2023-01-16 21:17:54 +00:00
|
|
|
}
|
|
|
|
assert_eq!(output.commitment().amount, 1000000000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
async_sequential!(
|
2023-03-11 15:31:58 +00:00
|
|
|
async fn receipt_of_wallet_rpc_tx_standard() {
|
|
|
|
from_wallet_rpc_to_self(AddressSpec::Standard).await;
|
2023-01-16 21:17:54 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 15:31:58 +00:00
|
|
|
async fn receipt_of_wallet_rpc_tx_subaddress() {
|
|
|
|
from_wallet_rpc_to_self(AddressSpec::Subaddress(SubaddressIndex::new(0, 1).unwrap())).await;
|
2023-01-16 21:17:54 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 15:31:58 +00:00
|
|
|
async fn receipt_of_wallet_rpc_tx_integrated() {
|
2023-01-16 21:17:54 +00:00
|
|
|
let mut payment_id = [0u8; 8];
|
|
|
|
OsRng.fill_bytes(&mut payment_id);
|
2023-03-11 15:31:58 +00:00
|
|
|
from_wallet_rpc_to_self(AddressSpec::Integrated(payment_id)).await;
|
2023-01-16 21:17:54 +00:00
|
|
|
}
|
|
|
|
);
|
2023-01-24 20:22:07 +00:00
|
|
|
|
2023-07-24 08:53:59 +00:00
|
|
|
#[derive(PartialEq, Eq, Debug, Deserialize)]
|
|
|
|
struct Index {
|
|
|
|
major: u32,
|
|
|
|
minor: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct Transfer {
|
|
|
|
payment_id: String,
|
|
|
|
subaddr_index: Index,
|
|
|
|
amount: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct TransfersResponse {
|
|
|
|
transfer: Transfer,
|
2024-02-20 01:18:31 +00:00
|
|
|
transfers: Vec<Transfer>,
|
2023-07-24 08:53:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 20:22:07 +00:00
|
|
|
test!(
|
2023-03-11 15:31:58 +00:00
|
|
|
send_to_wallet_rpc_standard,
|
2023-01-24 20:22:07 +00:00
|
|
|
(
|
|
|
|
|_, mut builder: Builder, _| async move {
|
|
|
|
// initialize rpc
|
|
|
|
let (wallet_rpc, _, wallet_rpc_addr) = initialize_rpcs().await;
|
|
|
|
|
|
|
|
// add destination
|
2023-07-24 08:53:59 +00:00
|
|
|
builder
|
|
|
|
.add_payment(MoneroAddress::from_str(Network::Mainnet, &wallet_rpc_addr).unwrap(), 1000000);
|
|
|
|
(builder.build().unwrap(), wallet_rpc)
|
2023-01-24 20:22:07 +00:00
|
|
|
},
|
2023-07-24 08:53:59 +00:00
|
|
|
|_, tx: Transaction, _, data: Rpc<HttpRpc>| async move {
|
2023-01-24 20:22:07 +00:00
|
|
|
// confirm receipt
|
2023-07-24 08:53:59 +00:00
|
|
|
let _: EmptyResponse = data.json_rpc_call("refresh", None).await.unwrap();
|
|
|
|
let transfer: TransfersResponse = data
|
|
|
|
.json_rpc_call("get_transfer_by_txid", Some(json!({ "txid": hex::encode(tx.hash()) })))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(transfer.transfer.subaddr_index, Index { major: 0, minor: 0 });
|
|
|
|
assert_eq!(transfer.transfer.amount, 1000000);
|
2024-02-20 01:45:50 +00:00
|
|
|
assert_eq!(transfer.transfer.payment_id, hex::encode([0u8; 8]));
|
2023-01-24 20:22:07 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
test!(
|
2023-03-11 15:31:58 +00:00
|
|
|
send_to_wallet_rpc_subaddress,
|
2023-01-24 20:22:07 +00:00
|
|
|
(
|
|
|
|
|_, mut builder: Builder, _| async move {
|
|
|
|
// initialize rpc
|
|
|
|
let (wallet_rpc, _, _) = initialize_rpcs().await;
|
|
|
|
|
2023-07-24 08:53:59 +00:00
|
|
|
// make the subaddress
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct AccountResponse {
|
|
|
|
address: String,
|
|
|
|
account_index: u32,
|
|
|
|
}
|
|
|
|
let addr: AccountResponse = wallet_rpc.json_rpc_call("create_account", None).await.unwrap();
|
|
|
|
assert!(addr.account_index != 0);
|
2023-01-24 20:22:07 +00:00
|
|
|
|
2023-07-24 08:53:59 +00:00
|
|
|
builder
|
|
|
|
.add_payment(MoneroAddress::from_str(Network::Mainnet, &addr.address).unwrap(), 1000000);
|
|
|
|
(builder.build().unwrap(), (wallet_rpc, addr.account_index))
|
2023-01-24 20:22:07 +00:00
|
|
|
},
|
2023-07-24 08:53:59 +00:00
|
|
|
|_, tx: Transaction, _, data: (Rpc<HttpRpc>, u32)| async move {
|
2023-01-24 20:22:07 +00:00
|
|
|
// confirm receipt
|
2023-07-24 08:53:59 +00:00
|
|
|
let _: EmptyResponse = data.0.json_rpc_call("refresh", None).await.unwrap();
|
|
|
|
let transfer: TransfersResponse = data
|
|
|
|
.0
|
|
|
|
.json_rpc_call(
|
|
|
|
"get_transfer_by_txid",
|
|
|
|
Some(json!({ "txid": hex::encode(tx.hash()), "account_index": data.1 })),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(transfer.transfer.subaddr_index, Index { major: data.1, minor: 0 });
|
|
|
|
assert_eq!(transfer.transfer.amount, 1000000);
|
2024-02-20 01:45:50 +00:00
|
|
|
assert_eq!(transfer.transfer.payment_id, hex::encode([0u8; 8]));
|
2023-03-11 15:31:58 +00:00
|
|
|
|
|
|
|
// Make sure only one R was included in TX extra
|
|
|
|
assert!(Extra::read::<&[u8]>(&mut tx.prefix.extra.as_ref())
|
|
|
|
.unwrap()
|
|
|
|
.keys()
|
|
|
|
.unwrap()
|
|
|
|
.1
|
|
|
|
.is_none());
|
2023-01-24 20:22:07 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2024-02-20 01:18:31 +00:00
|
|
|
test!(
|
|
|
|
send_to_wallet_rpc_subaddresses,
|
|
|
|
(
|
|
|
|
|_, mut builder: Builder, _| async move {
|
|
|
|
// initialize rpc
|
|
|
|
let (wallet_rpc, daemon_rpc, _) = initialize_rpcs().await;
|
|
|
|
|
|
|
|
// make the subaddress
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct AddressesResponse {
|
|
|
|
addresses: Vec<String>,
|
|
|
|
address_index: u32,
|
|
|
|
}
|
|
|
|
let addrs: AddressesResponse = wallet_rpc
|
|
|
|
.json_rpc_call("create_address", Some(json!({ "account_index": 0, "count": 2 })))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(addrs.address_index != 0);
|
|
|
|
assert!(addrs.addresses.len() == 2);
|
|
|
|
|
|
|
|
builder.add_payments(&[
|
|
|
|
(MoneroAddress::from_str(Network::Mainnet, &addrs.addresses[0]).unwrap(), 1000000),
|
|
|
|
(MoneroAddress::from_str(Network::Mainnet, &addrs.addresses[1]).unwrap(), 2000000),
|
|
|
|
]);
|
|
|
|
(builder.build().unwrap(), (wallet_rpc, daemon_rpc, addrs.address_index))
|
|
|
|
},
|
|
|
|
|_, tx: Transaction, _, data: (Rpc<HttpRpc>, Rpc<HttpRpc>, u32)| async move {
|
|
|
|
// confirm receipt
|
|
|
|
let _: EmptyResponse = data.0.json_rpc_call("refresh", None).await.unwrap();
|
|
|
|
let transfer: TransfersResponse = data
|
|
|
|
.0
|
|
|
|
.json_rpc_call(
|
|
|
|
"get_transfer_by_txid",
|
|
|
|
Some(json!({ "txid": hex::encode(tx.hash()), "account_index": 0 })),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(transfer.transfers.len(), 2);
|
|
|
|
for t in transfer.transfers {
|
|
|
|
match t.amount {
|
|
|
|
1000000 => assert_eq!(t.subaddr_index, Index { major: 0, minor: data.2 }),
|
|
|
|
2000000 => assert_eq!(t.subaddr_index, Index { major: 0, minor: data.2 + 1 }),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure 3 additional pub keys are included in TX extra
|
|
|
|
let keys =
|
|
|
|
Extra::read::<&[u8]>(&mut tx.prefix.extra.as_ref()).unwrap().keys().unwrap().1.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(keys.len(), 3);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2023-01-24 20:22:07 +00:00
|
|
|
test!(
|
2023-03-11 15:31:58 +00:00
|
|
|
send_to_wallet_rpc_integrated,
|
2023-01-24 20:22:07 +00:00
|
|
|
(
|
|
|
|
|_, mut builder: Builder, _| async move {
|
|
|
|
// initialize rpc
|
|
|
|
let (wallet_rpc, _, _) = initialize_rpcs().await;
|
|
|
|
|
|
|
|
// make the addr
|
|
|
|
let mut payment_id = [0u8; 8];
|
|
|
|
OsRng.fill_bytes(&mut payment_id);
|
2023-07-24 08:53:59 +00:00
|
|
|
let addr = make_integrated_address(&wallet_rpc, payment_id).await;
|
2023-01-24 20:22:07 +00:00
|
|
|
|
|
|
|
builder.add_payment(MoneroAddress::from_str(Network::Mainnet, &addr).unwrap(), 1000000);
|
|
|
|
(builder.build().unwrap(), (wallet_rpc, payment_id))
|
|
|
|
},
|
2023-07-24 08:53:59 +00:00
|
|
|
|_, tx: Transaction, _, data: (Rpc<HttpRpc>, [u8; 8])| async move {
|
2023-01-24 20:22:07 +00:00
|
|
|
// confirm receipt
|
2023-07-24 08:53:59 +00:00
|
|
|
let _: EmptyResponse = data.0.json_rpc_call("refresh", None).await.unwrap();
|
|
|
|
let transfer: TransfersResponse = data
|
|
|
|
.0
|
|
|
|
.json_rpc_call("get_transfer_by_txid", Some(json!({ "txid": hex::encode(tx.hash()) })))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(transfer.transfer.subaddr_index, Index { major: 0, minor: 0 });
|
|
|
|
assert_eq!(transfer.transfer.payment_id, hex::encode(data.1));
|
|
|
|
assert_eq!(transfer.transfer.amount, 1000000);
|
2023-01-24 20:22:07 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2023-01-30 09:25:46 +00:00
|
|
|
|
|
|
|
test!(
|
2023-03-11 15:31:58 +00:00
|
|
|
send_to_wallet_rpc_with_arb_data,
|
2023-01-30 09:25:46 +00:00
|
|
|
(
|
|
|
|
|_, mut builder: Builder, _| async move {
|
|
|
|
// initialize rpc
|
|
|
|
let (wallet_rpc, _, wallet_rpc_addr) = initialize_rpcs().await;
|
|
|
|
|
|
|
|
// add destination
|
2023-07-24 08:53:59 +00:00
|
|
|
builder
|
|
|
|
.add_payment(MoneroAddress::from_str(Network::Mainnet, &wallet_rpc_addr).unwrap(), 1000000);
|
2023-01-30 09:25:46 +00:00
|
|
|
|
2023-03-11 10:47:25 +00:00
|
|
|
// Make 2 data that is the full 255 bytes
|
2023-01-30 09:25:46 +00:00
|
|
|
for _ in 0 .. 2 {
|
2023-03-11 10:47:25 +00:00
|
|
|
// Subtract 1 since we prefix data with 127
|
|
|
|
let data = vec![b'a'; MAX_TX_EXTRA_NONCE_SIZE - 1];
|
2023-07-08 15:29:05 +00:00
|
|
|
builder.add_data(data).unwrap();
|
2023-01-30 09:25:46 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 08:53:59 +00:00
|
|
|
(builder.build().unwrap(), wallet_rpc)
|
2023-01-30 09:25:46 +00:00
|
|
|
},
|
2023-07-24 08:53:59 +00:00
|
|
|
|_, tx: Transaction, _, data: Rpc<HttpRpc>| async move {
|
2023-01-30 09:25:46 +00:00
|
|
|
// confirm receipt
|
2023-07-24 08:53:59 +00:00
|
|
|
let _: EmptyResponse = data.json_rpc_call("refresh", None).await.unwrap();
|
|
|
|
let transfer: TransfersResponse = data
|
|
|
|
.json_rpc_call("get_transfer_by_txid", Some(json!({ "txid": hex::encode(tx.hash()) })))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(transfer.transfer.subaddr_index, Index { major: 0, minor: 0 });
|
|
|
|
assert_eq!(transfer.transfer.amount, 1000000);
|
2023-01-30 09:25:46 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|