diff --git a/Cargo.toml b/Cargo.toml index df01a1f..f2784f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] @@ -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" diff --git a/assets/dusk_wallet_core.wasm b/assets/dusk_wallet_core.wasm index a50e7ab..8f38b3c 100755 Binary files a/assets/dusk_wallet_core.wasm and b/assets/dusk_wallet_core.wasm differ diff --git a/dusk-wallet-core-0.21.0.wasm b/dusk-wallet-core-0.21.0.wasm index 59f0771..ce464af 100644 Binary files a/dusk-wallet-core-0.21.0.wasm and b/dusk-wallet-core-0.21.0.wasm differ diff --git a/src/compat/allow.rs b/src/compat/allow.rs index 31e67f1..e3dbf17 100644 --- a/src/compat/allow.rs +++ b/src/compat/allow.rs @@ -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] @@ -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); @@ -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) -} diff --git a/src/compat/mod.rs b/src/compat/mod.rs index fea6eb1..f45b5d9 100644 --- a/src/compat/mod.rs +++ b/src/compat/mod.rs @@ -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}; +} diff --git a/src/compat/stake.rs b/src/compat/stake.rs index 0b0dfe2..cdcfdc0 100644 --- a/src/compat/stake.rs +++ b/src/compat/stake.rs @@ -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 @@ -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, @@ -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) -} diff --git a/src/compat/unstake.rs b/src/compat/unstake.rs index a22da3b..eeb777d 100644 --- a/src/compat/unstake.rs +++ b/src/compat/unstake.rs @@ -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; @@ -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, }; @@ -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 = Vec::with_capacity(u64::SIZE + Note::SIZE); - - msg.extend(counter.to_bytes()); - msg.extend(note.to_bytes()); - - sk.sign(pk, &msg) -} diff --git a/src/compat/withdraw.rs b/src/compat/withdraw.rs index 9f93078..cd15d96 100644 --- a/src/compat/withdraw.rs +++ b/src/compat/withdraw.rs @@ -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] @@ -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 @@ -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) -}