Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

New unstake note #99

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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dusk-bls12_381 = { version = "0.12.3", default-features = false, features = [
] }
hex = { version = "0.4", default_features = false, features = ["alloc"] }
hashbrown = "0.14.3"
stake-contract-types = "0.0.1-rc.2"


[features]
Expand All @@ -63,10 +64,10 @@ default = ["compat"]
compat = ["dep:bip39"]

[target.'cfg(target_family = "wasm")'.dependencies]
rusk-abi = "0.11"
rusk-abi = "0.12.0-rc.0"

[target.'cfg(not(target_family = "wasm"))'.dependencies]
rusk-abi = { version = "0.11", default-features = false }
rusk-abi = { version = "0.12.0-rc.0", default-features = false }

[dev-dependencies]
rand = "^0.8"
Expand Down
Binary file modified assets/dusk_wallet_core.wasm
Binary file not shown.
Binary file modified dusk-wallet-core-0.21.0.wasm
Binary file not shown.
30 changes: 6 additions & 24 deletions src/compat/allow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use crate::{key::*, types, utils, MAX_LEN};

use alloc::string::String;
use alloc::vec::Vec;

use dusk_bls12_381_sign::{PublicKey, SecretKey, Signature as BlsSignature};
use dusk_bytes::Serializable;
use dusk_bls12_381_sign::PublicKey;
use dusk_jubjub::JubJubScalar;
use phoenix_core::{transaction::*, Note, *};
use phoenix_core::{Note, *};

use super::stake_contract_types::*;

/// Get unstake call data
#[no_mangle]
Expand Down Expand Up @@ -54,7 +54,8 @@ pub fn get_allow_call_data(args: i32, len: i32) -> i64 {

let rng = &mut utils::rng(rng_seed);

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

let blinder = JubJubScalar::random(rng);
let note = Note::obfuscated(rng, &refund, 0, blinder);
Expand Down Expand Up @@ -103,22 +104,3 @@ pub fn get_allow_call_data(args: i32, len: i32) -> i64 {
fee,
})
}

/// 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,
) -> BlsSignature {
let mut msg = Vec::with_capacity(u64::SIZE + PublicKey::SIZE);

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

sk.sign(pk, &msg)
}
8 changes: 8 additions & 0 deletions src/compat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ pub mod tx;
pub mod unstake;
/// Includes functions to interact with the stake contract withdraw tx
pub mod withdraw;

mod stake_contract_types {
pub use stake_contract_types::{
allow_signature_message, stake_signature_message,
unstake_signature_message, withdraw_signature_message,
};
pub use stake_contract_types::{Allow, Stake, Unstake, Withdraw};
}
32 changes: 9 additions & 23 deletions src/compat/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ use alloc::string::String;
use alloc::vec::Vec;

use dusk_bls12_381::BlsScalar;
use dusk_bls12_381_sign::{PublicKey, SecretKey, Signature as BlsSignature};
use dusk_bls12_381_sign::PublicKey;
use dusk_bytes::Serializable;
use dusk_bytes::Write;
use dusk_jubjub::JubJubScalar;
use dusk_pki::{Ownable, SecretKey as SchnorrKey};
use dusk_plonk::proof_system::Proof;
use dusk_schnorr::Signature;
use phoenix_core::{transaction::*, Note, *};
use phoenix_core::{
transaction::{stct_signature_message, StakeData},
*,
};

use super::stake_contract_types::*;

const STCT_INPUT_SIZE: usize = Fee::SIZE
+ Crossover::SIZE
Expand Down Expand Up @@ -175,7 +180,8 @@ pub fn get_stake_call_data(args: i32, len: i32) -> i64 {
let sk = derive_sk(&seed, staker_index);
let pk = PublicKey::from(&sk);

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

let stake = Stake {
public_key: pk,
Expand Down Expand Up @@ -240,23 +246,3 @@ fn get_stake_info(args: i32, len: i32) -> i64 {
}),
}
}

/// 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,
) -> BlsSignature {
let size = u64::SIZE + u64::SIZE;
let mut msg = Vec::with_capacity(size);

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

sk.sign(pk, &msg)
}
36 changes: 11 additions & 25 deletions src/compat/unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ use crate::{
use alloc::string::String;
use alloc::vec::Vec;

use dusk_bls12_381_sign::{PublicKey, SecretKey, Signature as BlsSignature};
use dusk_bls12_381_sign::PublicKey;
use dusk_bytes::Serializable;
use dusk_bytes::Write;
use dusk_jubjub::{JubJubAffine, JubJubScalar};
use dusk_plonk::proof_system::Proof;
use phoenix_core::{transaction::*, Note, *};
use phoenix_core::*;

use super::stake_contract_types::*;

const WFCT_INPUT_SIZE: usize =
JubJubAffine::SIZE + u64::SIZE + JubJubScalar::SIZE;
Expand Down Expand Up @@ -163,14 +165,17 @@ pub fn get_unstake_call_data(args: i32, len: i32) -> i64 {
};

let sk = derive_sk(&seed, sender_index);
let pk = PublicKey::from(&sk);
let public_key = PublicKey::from(&sk);

let unstake_note = unstake_note.to_bytes();
let signature_message = unstake_signature_message(counter, unstake_note);

let signature = unstake_sign(&sk, &pk, 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(),
proof,
};

Expand All @@ -188,22 +193,3 @@ pub fn get_unstake_call_data(args: i32, len: i32) -> i64 {
payload,
})
}

/// 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,
) -> BlsSignature {
let mut msg: Vec<u8> = Vec::with_capacity(u64::SIZE + Note::SIZE);

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

sk.sign(pk, &msg)
}
33 changes: 6 additions & 27 deletions src/compat/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use crate::{key::*, types, utils, MAX_LEN};

use alloc::string::String;
use alloc::vec::Vec;
use ff::Field;

use dusk_bls12_381_sign::{PublicKey, SecretKey, Signature as BlsSignature};
use dusk_bytes::Serializable;
use dusk_bls12_381_sign::PublicKey;
use dusk_jubjub::{BlsScalar, JubJubScalar};
use dusk_pki::StealthAddress;
use phoenix_core::{transaction::*, Note, *};
use phoenix_core::*;

use super::stake_contract_types::*;

/// Get unstake call data
#[no_mangle]
Expand Down Expand Up @@ -59,7 +59,8 @@ pub fn get_withdraw_call_data(args: i32, len: i32) -> i64 {
let address: StealthAddress = sender_psk.gen_stealth_address(&withdraw_r);
let nonce = BlsScalar::random(&mut *rng);

let signature = withdraw_sign(&sk, &pk, counter, address, nonce);
let msg = withdraw_signature_message(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 @@ -112,25 +113,3 @@ pub fn get_withdraw_call_data(args: i32, len: i32) -> i64 {
fee,
})
}

/// 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,
) -> BlsSignature {
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)
}
Loading