Skip to content

Commit

Permalink
Merge pull request #2343 from dusk-network/rusk-wallet-sheild-unshield
Browse files Browse the repository at this point in the history
Rusk wallet sheild unshield
  • Loading branch information
Daksh14 authored Sep 11, 2024
2 parents 730a63a + be1de84 commit 888aaf0
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 40 deletions.
70 changes: 70 additions & 0 deletions rusk-wallet/src/bin/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,42 @@ pub(crate) enum Command {
gas_price: Lux,
},

/// Convert Phoenix balance to moonlight for the same owned address
PhoenixToMoonlight {
/// Bls or Phoenix Address from which to convert DUSK to
addr: Option<Address>,

/// Amount of DUSK to transfer to moonlight account
#[clap(short, long)]
amt: Dusk,

/// Max amt of gas for this transaction
#[clap(short = 'l', long, default_value_t= DEFAULT_STAKE_GAS_LIMIT)]
gas_limit: u64,

/// Price you're going to pay for each gas unit (in LUX)
#[clap(short = 'p', long, default_value_t= DEFAULT_PRICE)]
gas_price: Lux,
},

/// Convert moonlight balance to phoenix for the same owned address
MoonlightToPhoenix {
/// Bls or Phoenix Address from which to convert DUSK to
addr: Option<Address>,

/// Amount of DUSK to transfer to phoenix account
#[clap(short, long)]
amt: Dusk,

/// Max amt of gas for this transaction
#[clap(short = 'l', long, default_value_t= DEFAULT_STAKE_GAS_LIMIT)]
gas_limit: u64,

/// Price you're going to pay for each gas unit (in LUX)
#[clap(short = 'p', long, default_value_t= DEFAULT_PRICE)]
gas_price: Lux,
},

/// Export BLS provisioner key pair
Export {
/// Address for which you want the exported keys [default: first
Expand Down Expand Up @@ -385,6 +421,40 @@ impl Command {

Ok(RunResult::PhoenixHistory(transactions))
}
Command::PhoenixToMoonlight {
addr,
gas_limit,
gas_price,
amt,
} => {
wallet.sync().await?;
let addr = match addr {
Some(addr) => wallet.claim_as_address(addr)?,
None => wallet.default_address(),
};

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

let tx = wallet.phoenix_to_moonlight(addr, amt, gas).await?;
Ok(RunResult::Tx(tx.hash()))
}
Command::MoonlightToPhoenix {
addr,
amt,
gas_limit,
gas_price,
} => {
wallet.sync().await?;
let addr = match addr {
Some(addr) => wallet.claim_as_address(addr)?,
None => wallet.default_address(),
};

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

let tx = wallet.moonlight_to_phoenix(addr, amt, gas).await?;
Ok(RunResult::Tx(tx.hash()))
}
Command::Create { .. } => Ok(RunResult::Create()),
Command::Restore { .. } => Ok(RunResult::Restore()),
Command::Settings => Ok(RunResult::Settings()),
Expand Down
39 changes: 34 additions & 5 deletions rusk-wallet/src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ pub(crate) async fn run_loop(

// request operation to perform
let op = match wallet.is_online().await {
true => menu_op(addr.clone(), spendable, settings),
true => {
menu_op(addr.clone(), spendable, moonlight_bal, settings)
}
false => menu_op_offline(addr.clone(), settings),
};

Expand Down Expand Up @@ -210,6 +212,8 @@ enum CommandMenuItem {
PhoenixTransfer,
MoonlightTransfer,
PhoenixStake,
PhoenixToMoonlight,
MoonlightToPhoenix,
StakeInfo,
PhoenixUnstake,
PhoenixWithdraw,
Expand All @@ -221,7 +225,8 @@ enum CommandMenuItem {
/// selected address
fn menu_op(
addr: Address,
balance: Dusk,
phoenix_balance: Dusk,
moonlight_balance: Dusk,
settings: &Settings,
) -> anyhow::Result<AddrOp> {
use CommandMenuItem as CMI;
Expand All @@ -234,6 +239,14 @@ fn menu_op(
.add(CMI::StakeInfo, "Check existing stake")
.add(CMI::PhoenixUnstake, "Phoenix Unstake Dusk")
.add(CMI::PhoenixWithdraw, "Phoenix Withdraw staking reward")
.add(
CMI::PhoenixToMoonlight,
"Convert dusk from phoenix account to moonlight",
)
.add(
CMI::MoonlightToPhoenix,
"Convert dusk from moonlight account to phoenix",
)
.add(CMI::Export, "Export provisioner key-pair")
.separator()
.add(CMI::Back, "Back");
Expand All @@ -254,7 +267,7 @@ fn menu_op(
AddrOp::Run(Box::new(Command::PhoenixTransfer {
sndr: Some(addr),
rcvr: prompt::request_rcvr_addr("recipient")?,
amt: prompt::request_token_amt("transfer", balance)?,
amt: prompt::request_token_amt("transfer", phoenix_balance)?,
gas_limit: prompt::request_gas_limit(gas::DEFAULT_LIMIT)?,
gas_price: prompt::request_gas_price()?,
}))
Expand All @@ -263,14 +276,14 @@ fn menu_op(
AddrOp::Run(Box::new(Command::MoonlightTransfer {
sndr: Some(addr),
rcvr: prompt::request_rcvr_addr("recipient")?,
amt: prompt::request_token_amt("transfer", balance)?,
amt: prompt::request_token_amt("transfer", moonlight_balance)?,
gas_limit: prompt::request_gas_limit(gas::DEFAULT_LIMIT)?,
gas_price: prompt::request_gas_price()?,
}))
}
CMI::PhoenixStake => AddrOp::Run(Box::new(Command::PhoenixStake {
addr: Some(addr),
amt: prompt::request_token_amt("stake", balance)?,
amt: prompt::request_token_amt("stake", phoenix_balance)?,
gas_limit: prompt::request_gas_limit(DEFAULT_STAKE_GAS_LIMIT)?,
gas_price: prompt::request_gas_price()?,
})),
Expand All @@ -290,6 +303,22 @@ fn menu_op(
gas_price: prompt::request_gas_price()?,
}))
}
CMI::MoonlightToPhoenix => {
AddrOp::Run(Box::new(Command::MoonlightToPhoenix {
addr: Some(addr),
amt: prompt::request_token_amt("convert", moonlight_balance)?,
gas_limit: prompt::request_gas_limit(gas::DEFAULT_LIMIT)?,
gas_price: prompt::request_gas_price()?,
}))
}
CMI::PhoenixToMoonlight => {
AddrOp::Run(Box::new(Command::PhoenixToMoonlight {
addr: Some(addr),
amt: prompt::request_token_amt("convert", phoenix_balance)?,
gas_limit: prompt::request_gas_limit(gas::DEFAULT_LIMIT)?,
gas_price: prompt::request_gas_price()?,
}))
}
CMI::Export => AddrOp::Run(Box::new(Command::Export {
addr: Some(addr),
name: None,
Expand Down
88 changes: 77 additions & 11 deletions rusk-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ use wallet_core::{
derive_phoenix_vk,
},
transaction::{
moonlight, moonlight_stake, moonlight_unstake, phoenix, phoenix_stake,
phoenix_stake_reward, phoenix_unstake,
moonlight, moonlight_stake, moonlight_to_phoenix, moonlight_unstake,
phoenix, phoenix_stake, phoenix_stake_reward, phoenix_to_moonlight,
phoenix_unstake,
},
BalanceInfo,
};
Expand Down Expand Up @@ -631,12 +632,15 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
return Err(Error::NotEnoughGas);
}

let mut from_sk = self.bls_secret_key(sender.index()?);
let sender = sender.index()?;

let mut from_sk = self.bls_secret_key(sender);
let apk = rcvr.apk()?;
let from_pk = self.bls_public_key(sender);
let amt = *amt;

let state = self.state()?;
let account = state.fetch_account(apk)?;
let nonce = state.fetch_account(&from_pk)?.nonce + 1;
let chain_id = state.fetch_chain_id()?;

let tx = moonlight(
Expand All @@ -646,7 +650,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
0,
gas.limit,
gas.price,
account.nonce,
nonce,
chain_id,
None::<TransactionData>,
)?;
Expand Down Expand Up @@ -687,7 +691,8 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
let nonce = state
.fetch_stake(&AccountPublicKey::from(&stake_sk))?
.map(|s| s.nonce)
.unwrap_or(0);
.unwrap_or(0)
+ 1;

let inputs = state
.inputs(sender_index, amt + gas.limit * gas.price)?
Expand Down Expand Up @@ -734,18 +739,18 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
let sender_index = addr.index()?;
let mut stake_sk = self.bls_secret_key(sender_index);
let pk = AccountPublicKey::from(&stake_sk);
let account = state.fetch_account(&pk)?;
let chain_id = state.fetch_chain_id()?;
let moonlight_current_nonce = state.fetch_account(&pk)?.nonce + 1;

let nonce = state.fetch_stake(&pk)?.map(|s| s.nonce).unwrap_or(0);
let nonce = state.fetch_stake(&pk)?.map(|s| s.nonce + 1).unwrap_or(0);

let stake = moonlight_stake(
&stake_sk,
&stake_sk,
amt,
gas.limit,
gas.price,
account.nonce,
moonlight_current_nonce,
nonce,
chain_id,
)?;
Expand Down Expand Up @@ -831,7 +836,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
let pk = AccountPublicKey::from(&stake_sk);

let chain_id = state.fetch_chain_id()?;
let account = state.fetch_account(&pk)?;
let account_nonce = state.fetch_account(&pk)?.nonce + 1;

let unstake_value = state
.fetch_stake(&pk)?
Expand All @@ -846,7 +851,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
unstake_value,
gas.price,
gas.limit,
account.nonce + 1,
account_nonce,
chain_id,
)?;

Expand Down Expand Up @@ -902,6 +907,67 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
state.prove_and_propagate(withdraw)
}

/// Convert balance from phoenix to moonlight
pub async fn phoenix_to_moonlight(
&self,
sender_addr: &Address,
amt: Dusk,
gas: Gas,
) -> Result<Transaction, Error> {
let mut rng = StdRng::from_entropy();
let state = self.state()?;
let sender_index = sender_addr.index()?;
let amt = *amt;
let inputs = state.inputs(sender_index, amt + gas.limit * gas.price)?;

let root = state.fetch_root()?;
let chain_id = state.fetch_chain_id()?;

let mut sender_sk = self.phoenix_secret_key(sender_index);
let mut stake_sk = self.bls_secret_key(sender_index);

let convert = phoenix_to_moonlight(
&mut rng, &sender_sk, &stake_sk, inputs, root, amt, gas.limit,
gas.price, chain_id, &Prover,
)?;

sender_sk.zeroize();
stake_sk.zeroize();

state.prove_and_propagate(convert)
}

/// Convert balance from moonlight to phoenix
pub async fn moonlight_to_phoenix(
&self,
sender_addr: &Address,
amt: Dusk,
gas: Gas,
) -> Result<Transaction, Error> {
let mut rng = StdRng::from_entropy();
let state = self.state()?;
let sender_index = sender_addr.index()?;
let amt = *amt;

let pk = self.bls_public_key(sender_index);

let nonce = state.fetch_account(&pk)?.nonce + 1;
let chain_id = state.fetch_chain_id()?;

let mut sender_sk = self.phoenix_secret_key(sender_index);
let mut stake_sk = self.bls_secret_key(sender_index);

let convert = moonlight_to_phoenix(
&mut rng, &stake_sk, &sender_sk, amt, gas.limit, gas.price, nonce,
chain_id,
)?;

sender_sk.zeroize();
stake_sk.zeroize();

state.prove_and_propagate(convert)
}

/// Returns bls key pair for provisioner nodes
pub fn provisioner_keys(
&self,
Expand Down
13 changes: 7 additions & 6 deletions test-wallet/src/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ where
.state
.fetch_stake(&stake_pk)
.map_err(Error::from_state_err)?
.nonce;
.nonce
+ 1;

let chain_id =
self.state.fetch_chain_id().map_err(Error::from_state_err)?;
Expand Down Expand Up @@ -746,8 +747,8 @@ where
stake_value,
gas_limit,
gas_price,
sender_account.nonce,
staker_data.nonce,
sender_account.nonce + 1,
staker_data.nonce + 1,
chain_id,
)?;

Expand Down Expand Up @@ -798,7 +799,7 @@ where
unstake_value,
gas_limit,
gas_price,
sender_account.nonce,
sender_account.nonce + 1,
chain_id,
)?;

Expand Down Expand Up @@ -843,7 +844,7 @@ where
staker_data.reward,
gas_limit,
gas_price,
sender_account.nonce,
sender_account.nonce + 1,
chain_id,
)?;

Expand Down Expand Up @@ -875,7 +876,7 @@ where
.fetch_account(&moonlight_sender_pk)
.map_err(Error::from_state_err)?;

let nonce = moonlight_sender_account.nonce;
let nonce = moonlight_sender_account.nonce + 1;

let chain_id =
self.state.fetch_chain_id().map_err(Error::from_state_err)?;
Expand Down
Loading

0 comments on commit 888aaf0

Please sign in to comment.