diff --git a/rusk-wallet/src/wallet.rs b/rusk-wallet/src/wallet.rs index ae1cad2ff7..220e28f33a 100644 --- a/rusk-wallet/src/wallet.rs +++ b/rusk-wallet/src/wallet.rs @@ -22,13 +22,15 @@ use std::fs; use std::path::{Path, PathBuf}; use wallet_core::{ - moonlight::{moonlight, moonlight_stake, moonlight_unstake}, - phoenix::{phoenix, phoenix_stake, phoenix_stake_reward, phoenix_unstake}, phoenix_balance, prelude::keys::{ derive_bls_pk, derive_bls_sk, derive_phoenix_pk, derive_phoenix_sk, derive_phoenix_vk, }, + transaction::{ + moonlight, moonlight_stake, moonlight_unstake, phoenix, phoenix_stake, + phoenix_stake_reward, phoenix_unstake, + }, BalanceInfo, }; diff --git a/test-wallet/src/imp.rs b/test-wallet/src/imp.rs index 79e617bce6..34d917ec13 100644 --- a/test-wallet/src/imp.rs +++ b/test-wallet/src/imp.rs @@ -36,11 +36,12 @@ use execution_core::{ use rusk_prover::LocalProver; use wallet_core::{ keys::{derive_bls_sk, derive_phoenix_sk}, - phoenix::{ + phoenix_balance, + transaction::{ phoenix as phoenix_transaction, phoenix_stake, phoenix_stake_reward, phoenix_unstake, }, - phoenix_balance, BalanceInfo, + BalanceInfo, }; const MAX_INPUT_NOTES: usize = 4; diff --git a/wallet-core/src/lib.rs b/wallet-core/src/lib.rs index 12f70e629f..0b6e80bac7 100644 --- a/wallet-core/src/lib.rs +++ b/wallet-core/src/lib.rs @@ -24,9 +24,8 @@ mod ffi; pub mod input; pub mod keys; -pub mod moonlight; pub mod notes; -pub mod phoenix; +pub mod transaction; /// The seed used to generate the entropy for the keys pub type Seed = [u8; 64]; diff --git a/wallet-core/src/moonlight.rs b/wallet-core/src/moonlight.rs deleted file mode 100644 index f7e2e887b3..0000000000 --- a/wallet-core/src/moonlight.rs +++ /dev/null @@ -1,142 +0,0 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. -// -// Copyright (c) DUSK NETWORK. All rights reserved. - -//! Implementations of basic wallet functionalities to create moonlight -//! transactions. - -#![allow(clippy::module_name_repetitions)] - -use execution_core::{ - signatures::bls::{PublicKey as BlsPublicKey, SecretKey as BlsSecretKey}, - stake::{Stake, Withdraw as StakeWithdraw, STAKE_CONTRACT}, - transfer::{ - data::{ContractCall, TransactionData}, - moonlight::Transaction as MoonlightTransaction, - withdraw::{Withdraw, WithdrawReceiver, WithdrawReplayToken}, - Transaction, - }, - Error, -}; - -use rand::{CryptoRng, RngCore}; - -/// Generate a moonlight transaction -/// -/// # Errors -/// - the transaction-data is incorrect -#[allow(clippy::too_many_arguments)] -pub fn moonlight( - from_sk: &BlsSecretKey, - to_account: Option, - value: u64, - deposit: u64, - gas_limit: u64, - gas_price: u64, - nonce: u64, - chain_id: u8, - data: Option>, -) -> Result { - Ok(MoonlightTransaction::new( - from_sk, - to_account, - value, - deposit, - gas_limit, - gas_price, - nonce + 1, - chain_id, - data, - )? - .into()) -} - -/// Stake through moonlight, the `stake_nonce` is the nonce of the stake -/// which is obtained via stake info query on the chain -/// -/// The `nonce` is the nonce of the moonlight transaction -/// -/// # Errors -/// -/// This function most likey not fail but if the `nonce` is incorrect -/// or the `stake_nonce` the node will error and not accept the transcation -pub fn moonlight_stake( - from_sk: &BlsSecretKey, - stake_value: u64, - chain_id: u8, - stake_nonce: u64, - nonce: u64, - gas_limit: u64, - gas_price: u64, -) -> Result { - let receiver_pk = BlsPublicKey::from(from_sk); - - let transfer_value = 0; - let deposit = stake_value; - - let stake = Stake::new(from_sk, stake_value, stake_nonce + 1, chain_id); - - let contract_call = ContractCall::new(STAKE_CONTRACT, "stake", &stake)?; - - Ok(MoonlightTransaction::new( - from_sk, - Some(receiver_pk), - transfer_value, - deposit, - gas_limit, - gas_price, - nonce + 1, - chain_id, - Some(contract_call), - )? - .into()) -} - -/// Unstake through moonlight -/// -/// # Errors -/// -/// This function most likey not fail but if the `nonce` is incorrect -/// or the `stake_nonce` the node will error and not accept the transcation -pub fn moonlight_unstake( - rng: &mut R, - from_sk: &BlsSecretKey, - unstake_value: u64, - chain_id: u8, - nonce: u64, - gas_limit: u64, - gas_price: u64, -) -> Result { - let receiver_pk = BlsPublicKey::from(from_sk); - - let transfer_value = 0; - let deposit = unstake_value; - - let withdraw = Withdraw::new( - rng, - from_sk, - STAKE_CONTRACT, - unstake_value, - WithdrawReceiver::Moonlight(receiver_pk), - WithdrawReplayToken::Moonlight(nonce), - ); - - let unstake = StakeWithdraw::new(from_sk, withdraw); - - let contract_call = ContractCall::new(STAKE_CONTRACT, "unstake", &unstake)?; - - Ok(MoonlightTransaction::new( - from_sk, - Some(receiver_pk), - transfer_value, - deposit, - gas_limit, - gas_price, - nonce + 1, - chain_id, - Some(contract_call), - )? - .into()) -} diff --git a/wallet-core/src/phoenix.rs b/wallet-core/src/transaction.rs similarity index 76% rename from wallet-core/src/phoenix.rs rename to wallet-core/src/transaction.rs index f72aaa3777..3db1ba55e6 100644 --- a/wallet-core/src/phoenix.rs +++ b/wallet-core/src/transaction.rs @@ -7,8 +7,6 @@ //! Implementations of basic wallet functionalities to create phoenix //! transactions. -#![allow(clippy::module_name_repetitions)] - use alloc::vec::Vec; use rand::{CryptoRng, RngCore}; @@ -17,10 +15,11 @@ use ff::Field; use zeroize::Zeroize; use execution_core::{ - signatures::bls::SecretKey as BlsSecretKey, + signatures::bls::{PublicKey as BlsPublicKey, SecretKey as BlsSecretKey}, stake::{Stake, Withdraw as StakeWithdraw, STAKE_CONTRACT}, transfer::{ data::{ContractCall, TransactionData}, + moonlight::Transaction as MoonlightTransaction, phoenix::{ Note, NoteOpening, Prove, PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey, Transaction as PhoenixTransaction, @@ -328,3 +327,121 @@ fn withdraw_to_phoenix( withdraw } + +/// Generate a moonlight transaction +/// +/// # Errors +/// - the transaction-data is incorrect +#[allow(clippy::too_many_arguments)] +pub fn moonlight( + from_sk: &BlsSecretKey, + to_account: Option, + value: u64, + deposit: u64, + gas_limit: u64, + gas_price: u64, + nonce: u64, + chain_id: u8, + data: Option>, +) -> Result { + Ok(MoonlightTransaction::new( + from_sk, + to_account, + value, + deposit, + gas_limit, + gas_price, + nonce + 1, + chain_id, + data, + )? + .into()) +} + +/// Stake through moonlight, the `stake_nonce` is the nonce of the stake +/// which is obtained via stake info query on the chain +/// +/// The `nonce` is the nonce of the moonlight transaction +/// +/// # Errors +/// +/// This function most likey not fail but if the `nonce` is incorrect +/// or the `stake_nonce` the node will error and not accept the transcation +pub fn moonlight_stake( + from_sk: &BlsSecretKey, + stake_value: u64, + chain_id: u8, + stake_nonce: u64, + nonce: u64, + gas_limit: u64, + gas_price: u64, +) -> Result { + let receiver_pk = BlsPublicKey::from(from_sk); + + let transfer_value = 0; + let deposit = stake_value; + + let stake = Stake::new(from_sk, stake_value, stake_nonce + 1, chain_id); + + let contract_call = ContractCall::new(STAKE_CONTRACT, "stake", &stake)?; + + Ok(MoonlightTransaction::new( + from_sk, + Some(receiver_pk), + transfer_value, + deposit, + gas_limit, + gas_price, + nonce + 1, + chain_id, + Some(contract_call), + )? + .into()) +} + +/// Unstake through moonlight +/// +/// # Errors +/// +/// This function most likey not fail but if the `nonce` is incorrect +/// or the `stake_nonce` the node will error and not accept the transcation +pub fn moonlight_unstake( + rng: &mut R, + from_sk: &BlsSecretKey, + unstake_value: u64, + chain_id: u8, + nonce: u64, + gas_limit: u64, + gas_price: u64, +) -> Result { + let receiver_pk = BlsPublicKey::from(from_sk); + + let transfer_value = 0; + let deposit = unstake_value; + + let withdraw = Withdraw::new( + rng, + from_sk, + STAKE_CONTRACT, + unstake_value, + WithdrawReceiver::Moonlight(receiver_pk), + WithdrawReplayToken::Moonlight(nonce), + ); + + let unstake = StakeWithdraw::new(from_sk, withdraw); + + let contract_call = ContractCall::new(STAKE_CONTRACT, "unstake", &unstake)?; + + Ok(MoonlightTransaction::new( + from_sk, + Some(receiver_pk), + transfer_value, + deposit, + gas_limit, + gas_price, + nonce + 1, + chain_id, + Some(contract_call), + )? + .into()) +}