Skip to content

Commit

Permalink
rusk-wallet: Add moonlight support
Browse files Browse the repository at this point in the history
Also included in the PR is the following is the addition of functions
that return a key for a given index
  • Loading branch information
moCello committed Sep 6, 2024
1 parent 7feb765 commit da74081
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 103 deletions.
17 changes: 10 additions & 7 deletions rusk-wallet/src/bin/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ impl Command {
};
let gas = Gas::new(gas_limit).with_price(gas_price);

let tx = wallet.transfer(sender, &rcvr, amt, gas).await?;
let tx =
wallet.phoenix_transfer(sender, &rcvr, amt, gas).await?;
Ok(RunResult::Tx(tx.hash()))
}
Command::Stake {
Expand All @@ -247,16 +248,18 @@ impl Command {
};
let gas = Gas::new(gas_limit).with_price(gas_price);

let tx = wallet.stake(addr, amt, gas).await?;
let tx = wallet.phoenix_stake(addr, amt, gas).await?;
Ok(RunResult::Tx(tx.hash()))
}
Command::StakeInfo { addr, reward } => {
let addr = match addr {
Some(addr) => wallet.claim_as_address(addr)?,
None => wallet.default_address(),
};
let si =
wallet.stake_info(addr).await?.ok_or(Error::NotStaked)?;
let si = wallet
.stake_info(addr.index)
.await?
.ok_or(Error::NotStaked)?;

Ok(RunResult::StakeInfo(si, reward))
}
Expand All @@ -273,7 +276,7 @@ impl Command {

let gas = Gas::new(gas_limit).with_price(gas_price);

let tx = wallet.unstake(addr, gas).await?;
let tx = wallet.phoenix_unstake(addr, gas).await?;
Ok(RunResult::Tx(tx.hash()))
}
Command::Withdraw {
Expand All @@ -289,7 +292,7 @@ impl Command {

let gas = Gas::new(gas_limit).with_price(gas_price);

let tx = wallet.withdraw_reward(addr, gas).await?;
let tx = wallet.phoenix_stake_withdraw(addr, gas).await?;
Ok(RunResult::Tx(tx.hash()))
}
Command::Export { addr, dir, name } => {
Expand All @@ -305,7 +308,7 @@ impl Command {
)?;

let (pub_key, key_pair) =
wallet.export_keys(addr, &dir, name, &pwd)?;
wallet.export_provisioner_keys(addr, &dir, name, &pwd)?;

Ok(RunResult::ExportedKeys(pub_key, key_pair))
}
Expand Down
6 changes: 6 additions & 0 deletions rusk-wallet/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ impl Ord for NoteData {
}
}

impl AsRef<Note> for NoteData {
fn as_ref(&self) -> &Note {
&self.note
}
}

impl Serializable<{ u64::SIZE + Note::SIZE }> for NoteData {
type Error = dusk_bytes::Error;
/// Converts a Note into a byte representation
Expand Down
38 changes: 30 additions & 8 deletions rusk-wallet/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ mod sync;

use dusk_bytes::Serializable;
use execution_core::{
transfer::{phoenix::Prove, Transaction},
signatures::bls::PublicKey as AccountPublicKey,
transfer::{moonlight::AccountData, phoenix::Prove, Transaction},
Error as ExecutionCoreError,
};
use flume::Receiver;
Expand Down Expand Up @@ -139,15 +140,15 @@ impl State {
/// Skips writing the proof for non phoenix transactions
pub fn prove_and_propagate(
&self,
utx: Transaction,
tx: Transaction,
) -> Result<Transaction, Error> {
let status = self.status;
let prover = &self.prover;
let mut utx = utx;
let mut tx = tx;

if let Transaction::Phoenix(tx) = &mut utx {
if let Transaction::Phoenix(utx) = &mut tx {
let status = self.status;
let proof = tx.proof();
let proof = utx.proof();

status("Attempt to prove tx...");

Expand All @@ -158,12 +159,12 @@ impl State {
ExecutionCoreError::PhoenixCircuit(e.to_string())
})?;

tx.set_proof(proof);
utx.set_proof(proof);

status("Proving sucesss!");
}

let tx_bytes = utx.to_var_bytes();
let tx_bytes = tx.to_var_bytes();

status("Attempt to preverify tx...");
let preverify_req = RuskRequest::new("preverify", tx_bytes.clone());
Expand All @@ -175,7 +176,7 @@ impl State {
let _ = self.client.call(2, "Chain", &propagate_req).wait()?;
status("Transaction propagated!");

Ok(utx)
Ok(tx)
}

/// Find notes for a view key, starting from the given block height.
Expand Down Expand Up @@ -215,6 +216,27 @@ impl State {
inputs
}

pub(crate) fn fetch_account(
&self,
pk: &AccountPublicKey,
) -> Result<AccountData, Error> {
let status = self.status;
status("Fetching account-data...");

let account = self
.client
.contract_query::<_, 1024>(TRANSFER_CONTRACT, "account", pk)
.wait()?;
let account = rkyv::from_bytes(&account).map_err(|_| Error::Rkyv)?;
status("account-data received!");

let account_address = pk.to_bytes().to_vec();
let account_address = bs58::encode(account_address).into_string();
println!("Account address: {}", account_address);

Ok(account)
}

pub(crate) fn fetch_notes(
&self,
pk: &PhoenixPublicKey,
Expand Down
26 changes: 13 additions & 13 deletions rusk-wallet/src/clients/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ pub(crate) async fn sync_db(
) -> Result<(), Error> {
let seed = store.get_seed();

let addresses: Vec<(PhoenixSecretKey, PhoenixViewKey, PhoenixPublicKey)> =
(0..MAX_ADDRESSES)
.map(|i| {
let i = i as u8;
(
derive_phoenix_sk(seed, i),
derive_phoenix_vk(seed, i),
derive_phoenix_pk(seed, i),
)
})
.collect();
let keys: Vec<(PhoenixSecretKey, PhoenixViewKey, PhoenixPublicKey)> = (0
..MAX_ADDRESSES)
.map(|i| {
let i = i as u8;
(
derive_phoenix_sk(seed, i),
derive_phoenix_vk(seed, i),
derive_phoenix_pk(seed, i),
)
})
.collect();

status("Getting cached note position...");

Expand Down Expand Up @@ -87,7 +87,7 @@ pub(crate) async fn sync_db(
buffer = leaf_chunk.remainder().to_vec();
}

for (sk, vk, pk) in addresses.iter() {
for (sk, vk, pk) in keys.iter() {
for (block_height, note) in note_data.iter() {
if vk.owns(note.stealth_address()) {
let nullifier = note.gen_nullifier(sk);
Expand All @@ -108,7 +108,7 @@ pub(crate) async fn sync_db(

// Remove spent nullifiers from live notes
// zerorize all the secret keys
for (mut sk, _, pk) in addresses {
for (mut sk, _, pk) in keys {
let nullifiers: Vec<BlsScalar> = cache.unspent_notes_id(&pk)?;

if !nullifiers.is_empty() {
Expand Down
Loading

0 comments on commit da74081

Please sign in to comment.