Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change unstake to use stake-contract-types #98

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dusk-wallet-core"
version = "0.23.0-plonk.0.16"
version = "0.24.0-plonk.0.16-rc.2"
edition = "2021"
description = "The core functionality of the Dusk wallet"
license = "MPL-2.0"
Expand All @@ -22,6 +22,8 @@ dusk-bls12_381-sign = { version = "0.5", default-features = false }
rkyv = { version = "0.7", default-features = false }
ff = { version = "0.13", default-features = false }

stake-contract-types = "0.0.1-rc.2"

[dev-dependencies]
rand = "^0.8"

Expand Down
122 changes: 29 additions & 93 deletions src/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ use core::convert::Infallible;
use alloc::string::{FromUtf8Error, String};
use alloc::vec::Vec;

use dusk_bls12_381_sign::{PublicKey, SecretKey, Signature};
use dusk_bls12_381_sign::PublicKey;
use dusk_bytes::{Error as BytesError, Serializable};
use dusk_jubjub::{BlsScalar, JubJubScalar};
use dusk_pki::{
Ownable, PublicSpendKey, SecretKey as SchnorrKey, SecretSpendKey,
StealthAddress,
};
use dusk_schnorr::Signature as SchnorrSignature;
use ff::Field;
use phoenix_core::transaction::*;
use phoenix_core::transaction::{stct_signature_message, Transaction};
use phoenix_core::{Error as PhoenixError, Fee, Note, NoteType};
use rand_core::{CryptoRng, Error as RngError, RngCore};
use rkyv::ser::serializers::{
Expand All @@ -33,6 +32,11 @@ use rkyv::ser::serializers::{
use rkyv::validation::validators::CheckDeserializeError;
use rkyv::Serialize;
use rusk_abi::ContractId;
use stake_contract_types::{
allow_signature_message, stake_signature_message,
unstake_signature_message, withdraw_signature_message,
};
use stake_contract_types::{Allow, Stake, Unstake, Withdraw};

const MAX_INPUT_NOTES: usize = 4;

Expand Down Expand Up @@ -486,7 +490,8 @@ where
.to_bytes()
.to_vec();

let signature = stake_sign(&sk, &pk, stake.counter, value);
let msg = stake_signature_message(stake.counter, value);
let signature = sk.sign(&pk, &msg);

let stake = Stake {
public_key: pk,
Expand Down Expand Up @@ -535,7 +540,7 @@ where
.store
.retrieve_sk(staker_index)
.map_err(Error::from_store_err)?;
let pk = PublicKey::from(&sk);
let public_key = PublicKey::from(&sk);

let (inputs, outputs) = self.inputs_and_change_output(
rng,
Expand All @@ -544,10 +549,14 @@ where
gas_limit * gas_price,
)?;

let stake =
self.state.fetch_stake(&pk).map_err(Error::from_state_err)?;
let (value, _) =
stake.amount.ok_or(Error::NotStaked { key: pk, stake })?;
let stake = self
.state
.fetch_stake(&public_key)
.map_err(Error::from_state_err)?;
let (value, _) = stake.amount.ok_or(Error::NotStaked {
key: public_key,
stake,
})?;

let blinder = JubJubScalar::random(rng);

Expand Down Expand Up @@ -579,12 +588,16 @@ where
.to_bytes()
.to_vec();

let signature = unstake_sign(&sk, &pk, stake.counter, unstake_note);
let unstake_note = unstake_note.to_bytes();
let signature_message =
unstake_signature_message(stake.counter, unstake_note);

let signature = sk.sign(&public_key, &signature_message);

let unstake = Unstake {
public_key: pk,
public_key,
signature,
note: unstake_note,
note: unstake_note.to_vec(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Daksh14 here

proof: unstake_proof,
};

Expand Down Expand Up @@ -652,7 +665,8 @@ where
let address = sender_psk.gen_stealth_address(&withdraw_r);
let nonce = BlsScalar::random(&mut *rng);

let signature = withdraw_sign(&sk, &pk, stake.counter, address, nonce);
let msg = withdraw_signature_message(stake.counter, address, nonce);
let signature = sk.sign(&pk, &msg);

// Since we're not transferring value *to* the contract the crossover
// shouldn't contain a value. As such the note used to created it should
Expand Down Expand Up @@ -729,7 +743,8 @@ where
.fetch_stake(&owner_pk)
.map_err(Error::from_state_err)?;

let signature = allow_sign(&owner_sk, &owner_pk, stake.counter, staker);
let msg = allow_signature_message(stake.counter, staker);
let signature = owner_sk.sign(&owner_pk, &msg);

// Since we're not transferring value *to* the contract the crossover
// shouldn't contain a value. As such the note used to created it should
Expand Down Expand Up @@ -891,85 +906,6 @@ fn pick_lexicographic<F: Fn(&[usize; MAX_INPUT_NOTES]) -> bool>(
None
}

/// Creates a signature compatible with what the stake contract expects for a
/// stake transaction.
///
/// The counter is the number of transactions that have been sent to the
/// transfer contract by a given key, and is reported in `StakeInfo`.
fn stake_sign(
sk: &SecretKey,
pk: &PublicKey,
counter: u64,
value: u64,
) -> Signature {
let mut msg = Vec::with_capacity(u64::SIZE + u64::SIZE);

msg.extend(counter.to_bytes());
msg.extend(value.to_bytes());

sk.sign(pk, &msg)
}

/// Creates a signature compatible with what the stake contract expects for a
/// unstake transaction.
///
/// The counter is the number of transactions that have been sent to the
/// transfer contract by a given key, and is reported in `StakeInfo`.
fn unstake_sign(
sk: &SecretKey,
pk: &PublicKey,
counter: u64,
note: Note,
) -> Signature {
let mut msg = Vec::with_capacity(u64::SIZE + Note::SIZE);

msg.extend(counter.to_bytes());
msg.extend(note.to_bytes());

sk.sign(pk, &msg)
}

/// Creates a signature compatible with what the stake contract expects for a
/// withdraw transaction.
///
/// The counter is the number of transactions that have been sent to the
/// transfer contract by a given key, and is reported in `StakeInfo`.
fn withdraw_sign(
sk: &SecretKey,
pk: &PublicKey,
counter: u64,
address: StealthAddress,
nonce: BlsScalar,
) -> Signature {
let mut msg =
Vec::with_capacity(u64::SIZE + StealthAddress::SIZE + BlsScalar::SIZE);

msg.extend(counter.to_bytes());
msg.extend(address.to_bytes());
msg.extend(nonce.to_bytes());

sk.sign(pk, &msg)
}

/// Creates a signature compatible with what the stake contract expects for a
/// ADD_ALLOWLIST transaction.
///
/// The counter is the number of transactions that have been sent to the
/// transfer contract by a given key, and is reported in `StakeInfo`.
fn allow_sign(
sk: &SecretKey,
pk: &PublicKey,
counter: u64,
staker: &PublicKey,
) -> Signature {
let mut msg = Vec::with_capacity(u64::SIZE + PublicKey::SIZE);

msg.extend(counter.to_bytes());
msg.extend(staker.to_bytes());

sk.sign(pk, &msg)
}

/// Generates an obfuscated note for the given public spend key.
fn generate_obfuscated_note<Rng: RngCore + CryptoRng>(
rng: &mut Rng,
Expand Down
Loading