Diversify ViewPair/Scanner into ViewPair/GuaranteedViewPair and Scanner/GuaranteedScanner

Also cleans the Scanner impl.
This commit is contained in:
Luke Parker
2024-07-03 13:35:19 -04:00
parent 64e74c52ec
commit daa0f8f7d5
32 changed files with 1458 additions and 1233 deletions

View File

@@ -1,5 +1,3 @@
use std::collections::HashSet;
use zeroize::Zeroizing;
use rand_core::{RngCore, OsRng};
@@ -104,7 +102,7 @@ pub enum Wallet {
handle: String,
spend_key: Zeroizing<curve25519_dalek::scalar::Scalar>,
view_pair: monero_wallet::ViewPair,
inputs: Vec<monero_wallet::scan::ReceivedOutput>,
last_tx: (usize, [u8; 32]),
},
}
@@ -190,18 +188,10 @@ impl Wallet {
NetworkId::Monero => {
use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, scalar::Scalar};
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::{
rpc::Rpc,
address::{Network, AddressSpec},
ViewPair,
scan::Scanner,
};
use monero_wallet::{rpc::Rpc, address::Network, ViewPair};
let mut bytes = [0; 64];
OsRng.fill_bytes(&mut bytes);
let spend_key = Scalar::from_bytes_mod_order_wide(&bytes);
OsRng.fill_bytes(&mut bytes);
let view_key = Scalar::from_bytes_mod_order_wide(&bytes);
let spend_key = Scalar::random(&mut OsRng);
let view_key = Scalar::random(&mut OsRng);
let view_pair =
ViewPair::new(ED25519_BASEPOINT_POINT * spend_key, Zeroizing::new(view_key));
@@ -214,10 +204,7 @@ impl Wallet {
.json_rpc_call(
"generateblocks",
Some(serde_json::json!({
"wallet_address": view_pair.address(
Network::Mainnet,
AddressSpec::Legacy
).to_string(),
"wallet_address": view_pair.legacy_address(Network::Mainnet).to_string(),
"amount_of_blocks": 200,
})),
)
@@ -225,19 +212,11 @@ impl Wallet {
.unwrap();
let block = rpc.get_block(rpc.get_block_hash(height).await.unwrap()).await.unwrap();
let output = Scanner::from_view(view_pair.clone(), Some(HashSet::new()))
.scan(&rpc, &block)
.await
.unwrap()
.remove(0)
.ignore_timelock()
.remove(0);
Wallet::Monero {
handle,
spend_key: Zeroizing::new(spend_key),
view_pair,
inputs: vec![output.output.clone()],
last_tx: (height, block.miner_transaction.hash()),
}
}
NetworkId::Serai => panic!("creating a wallet for for Serai"),
@@ -434,7 +413,7 @@ impl Wallet {
)
}
Wallet::Monero { handle, ref spend_key, ref view_pair, ref mut inputs } => {
Wallet::Monero { handle, ref spend_key, ref view_pair, ref mut last_tx } => {
use curve25519_dalek::constants::ED25519_BASEPOINT_POINT;
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::{
@@ -442,8 +421,7 @@ impl Wallet {
ringct::RctType,
rpc::{FeePriority, Rpc},
address::{Network, AddressType, Address},
DecoySelection, Decoys,
scan::{SpendableOutput, Scanner},
Scanner, DecoySelection, Decoys,
send::{Change, SignableTransaction},
};
use processor::{additional_key, networks::Monero};
@@ -452,21 +430,28 @@ impl Wallet {
let rpc = SimpleRequestRpc::new(rpc_url).await.expect("couldn't connect to the Monero RPC");
// Prepare inputs
let outputs = std::mem::take(inputs);
let mut these_inputs = vec![];
for output in outputs {
these_inputs.push(
SpendableOutput::from(&rpc, output)
let current_height = rpc.get_height().await.unwrap();
let mut inputs = vec![];
for block in last_tx.0 .. current_height {
let block = rpc.get_block_by_number(block).await.unwrap();
if (block.miner_transaction.hash() == last_tx.1) ||
block.transactions.contains(&last_tx.1)
{
inputs = Scanner::new(view_pair.clone())
.scan(&rpc, &block)
.await
.expect("prior transaction was never published"),
);
.unwrap()
.ignore_additional_timelock();
}
}
assert!(!inputs.is_empty());
let mut decoys = Decoys::fingerprintable_canonical_select(
&mut OsRng,
&rpc,
16,
rpc.get_height().await.unwrap(),
&these_inputs,
&inputs,
)
.await
.unwrap();
@@ -478,7 +463,8 @@ impl Wallet {
AddressType::Featured { subaddress: false, payment_id: None, guaranteed: true },
to_spend_key,
ED25519_BASEPOINT_POINT * to_view_key.0,
);
)
.unwrap();
// Create and sign the TX
const AMOUNT: u64 = 1_000_000_000_000;
@@ -491,9 +477,9 @@ impl Wallet {
let tx = SignableTransaction::new(
RctType::ClsagBulletproofPlus,
outgoing_view_key,
these_inputs.drain(..).zip(decoys.drain(..)).collect(),
inputs.drain(..).zip(decoys.drain(..)).collect(),
vec![(to_addr, AMOUNT)],
Change::new(view_pair, false),
Change::new(view_pair),
data,
rpc.get_fee_rate(FeePriority::Unimportant).await.unwrap(),
)
@@ -501,13 +487,9 @@ impl Wallet {
.sign(&mut OsRng, spend_key)
.unwrap();
// Push the change output
inputs.push(
Scanner::from_view(view_pair.clone(), Some(HashSet::new()))
.scan_transaction(&tx)
.ignore_timelock()
.remove(0),
);
// Update the last TX to track the change output
last_tx.0 = current_height;
last_tx.1 = tx.hash();
(tx.serialize(), Balance { coin: Coin::Monero, amount: Amount(AMOUNT) })
}
@@ -532,9 +514,9 @@ impl Wallet {
)
.unwrap(),
Wallet::Monero { view_pair, .. } => {
use monero_wallet::address::{Network, AddressSpec};
use monero_wallet::address::Network;
ExternalAddress::new(
networks::monero::Address::new(view_pair.address(Network::Mainnet, AddressSpec::Legacy))
networks::monero::Address::new(view_pair.legacy_address(Network::Mainnet))
.unwrap()
.into(),
)