Skip to content

Commit

Permalink
wallet-core: Split transaction models in their respective modules
Browse files Browse the repository at this point in the history
Add moonlight staking/unstaking support
  • Loading branch information
Daksh14 committed Sep 9, 2024
1 parent da74081 commit 1316893
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 25 deletions.
3 changes: 2 additions & 1 deletion wallet-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ mod ffi;

pub mod input;
pub mod keys;
pub mod transaction;
pub mod moonlight;
pub mod phoenix;

pub mod prelude {
//! Re-export of the most commonly used types and traits.
Expand Down
130 changes: 130 additions & 0 deletions wallet-core/src/moonlight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// 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.
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<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
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
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())
}
26 changes: 2 additions & 24 deletions wallet-core/src/transaction.rs → wallet-core/src/phoenix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

//! Implementations of basic wallet functionalities to create transactions.
//! Implementations of basic wallet functionalities to create phoenix
//! transactions.
use alloc::vec::Vec;

Expand Down Expand Up @@ -73,29 +74,6 @@ pub fn phoenix<R: RngCore + CryptoRng, P: Prove>(
.into())
}

/// 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,
chain_id, data,
)?
.into())
}

/// Create a [`Transaction`] to stake from phoenix-notes.
///
/// # Errors
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 1316893

Please sign in to comment.