Test that a watch only wallet can be loaded from address + view key

This commit is contained in:
Lucas Soriano del Pino 2020-11-04 16:05:08 +11:00 committed by rishflab
parent e7682a42a4
commit 7371dfb055
5 changed files with 61 additions and 5 deletions

View File

@ -18,3 +18,7 @@ testcontainers = "0.11"
tokio = { version = "0.2", default-features = false, features = ["blocking", "macros", "rt-core", "time"] }
tracing = "0.1"
url = "2"
[dev-dependencies]
curve25519-dalek = "2"
monero = "0.9"

View File

@ -236,14 +236,14 @@ impl Client {
pub async fn generate_from_keys(
&self,
address: &str,
spend_key: &str,
spend_key: Option<&str>,
view_key: &str,
) -> Result<GenerateFromKeys> {
let params = GenerateFromKeysParams {
restore_height: 0,
filename: view_key.into(),
address: address.into(),
spendkey: spend_key.into(),
spendkey: spend_key.map(|sk| sk.into()),
viewkey: view_key.into(),
password: "".into(),
autosave_current: true,
@ -400,7 +400,8 @@ pub struct GenerateFromKeysParams {
pub restore_height: u32,
pub filename: String,
pub address: String,
pub spendkey: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub spendkey: Option<String>,
pub viewkey: String,
pub password: String,
pub autosave_current: bool,

View File

@ -1,4 +1,7 @@
use curve25519_dalek::scalar::Scalar;
use monero::{Address, Network, PrivateKey, PublicKey};
use monero_harness::Monero;
use rand::rngs::OsRng;
use spectral::prelude::*;
use testcontainers::clients::Cli;
@ -61,3 +64,51 @@ async fn fund_transfer_and_check_tx_key() {
assert_that!(res.received).is_equal_to(send_to_bob);
}
#[tokio::test]
async fn load_watch_only_wallet() {
let tc = Cli::default();
let (monero, _containers) = Monero::new(&tc, None, vec!["watch-only".into()])
.await
.unwrap();
let miner_wallet = monero.wallet("miner").unwrap();
let (_spend_sk, spend_pk) = {
let scalar = Scalar::random(&mut OsRng);
let sk = PrivateKey::from_scalar(scalar);
let pk = PublicKey::from_private_key(&sk);
(sk, pk)
};
let (view_sk, view_pk) = {
let scalar = Scalar::random(&mut OsRng);
let sk = PrivateKey::from_scalar(scalar);
let pk = PublicKey::from_private_key(&sk);
(sk, pk)
};
let address = Address::standard(Network::Mainnet, spend_pk, view_pk);
let one_xmr = 1_000_000_000_000;
monero.init(Vec::new()).await.unwrap();
miner_wallet
.client()
.transfer(0, one_xmr, &address.to_string())
.await
.unwrap();
let watch_only_wallet = monero.wallet("watch-only").unwrap();
watch_only_wallet
.client()
.generate_from_keys(&address.to_string(), None, &view_sk.to_string())
.await
.unwrap();
watch_only_wallet.client().refresh().await.unwrap();
let balance = watch_only_wallet.client().get_balance(0).await.unwrap();
assert_eq!(balance, one_xmr);
}

View File

@ -63,7 +63,7 @@ impl CreateWalletForOutput for Wallet {
.0
.generate_from_keys(
&address.to_string(),
&private_spend_key.to_string(),
Some(&private_spend_key.to_string()),
&PrivateKey::from(private_view_key).to_string(),
)
.await?;

View File

@ -60,7 +60,7 @@ impl CreateWalletForOutput for Wallet {
.0
.generate_from_keys(
&address.to_string(),
&private_spend_key.to_string(),
Some(&private_spend_key.to_string()),
&PrivateKey::from(private_view_key).to_string(),
)
.await?;