Skip to content

Commit

Permalink
Bring back transaction module
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksh14 committed Sep 9, 2024
1 parent 0f4ceab commit c1b2ff1
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 151 deletions.
6 changes: 4 additions & 2 deletions rusk-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
5 changes: 3 additions & 2 deletions test-wallet/src/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions wallet-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
142 changes: 0 additions & 142 deletions wallet-core/src/moonlight.rs

This file was deleted.

123 changes: 120 additions & 3 deletions wallet-core/src/phoenix.rs → wallet-core/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -328,3 +327,121 @@ fn withdraw_to_phoenix<R: RngCore + CryptoRng>(

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<BlsPublicKey>,
value: u64,
deposit: u64,
gas_limit: u64,
gas_price: u64,
nonce: u64,
chain_id: u8,
data: Option<impl Into<TransactionData>>,
) -> Result<Transaction, Error> {
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<Transaction, Error> {
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<R: RngCore + CryptoRng>(
rng: &mut R,
from_sk: &BlsSecretKey,
unstake_value: u64,
chain_id: u8,
nonce: u64,
gas_limit: u64,
gas_price: u64,
) -> Result<Transaction, Error> {
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())
}

0 comments on commit c1b2ff1

Please sign in to comment.