From b58f55ee1a465e005f0d9f379ecf8e4f9b19d392 Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Fri, 15 Dec 2023 16:30:58 +0100 Subject: [PATCH 1/3] Change unstake to use stake-contract-types Co-authored-by: Milosz Muszynski --- Cargo.toml | 2 ++ src/imp.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index aef5de4..e8b06c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.1" + [dev-dependencies] rand = "^0.8" diff --git a/src/imp.rs b/src/imp.rs index 33bd45c..d7cbf9b 100644 --- a/src/imp.rs +++ b/src/imp.rs @@ -33,6 +33,7 @@ use rkyv::ser::serializers::{ use rkyv::validation::validators::CheckDeserializeError; use rkyv::Serialize; use rusk_abi::ContractId; +use stake_contract_types::Unstake; const MAX_INPUT_NOTES: usize = 4; @@ -581,6 +582,7 @@ where let signature = unstake_sign(&sk, &pk, stake.counter, unstake_note); + let unstake_note = unstake_note.to_bytes().to_vec(); let unstake = Unstake { public_key: pk, signature, From 121f2aaf2c5cbab905aa9e31406d3ef8663ae72c Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Fri, 15 Dec 2023 16:31:26 +0100 Subject: [PATCH 2/3] Bump to `0.24.0-plonk.0.16-rc.0` Co-authored-by: Milosz Muszynski --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e8b06c5..1d72005 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dusk-wallet-core" -version = "0.23.0-plonk.0.16" +version = "0.24.0-plonk.0.16-rc.0" edition = "2021" description = "The core functionality of the Dusk wallet" license = "MPL-2.0" From 8ee300fc41aeb651b2bad9d868bf44823e79d098 Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Mon, 18 Dec 2023 15:57:51 +0100 Subject: [PATCH 3/3] Using stake-contract-types 0.0.1-rc.2 --- Cargo.toml | 4 +- src/imp.rs | 124 +++++++++++++---------------------------------------- 2 files changed, 31 insertions(+), 97 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1d72005..8683593 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dusk-wallet-core" -version = "0.24.0-plonk.0.16-rc.0" +version = "0.24.0-plonk.0.16-rc.2" edition = "2021" description = "The core functionality of the Dusk wallet" license = "MPL-2.0" @@ -22,7 +22,7 @@ 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.1" +stake-contract-types = "0.0.1-rc.2" [dev-dependencies] rand = "^0.8" diff --git a/src/imp.rs b/src/imp.rs index d7cbf9b..b0ca7d9 100644 --- a/src/imp.rs +++ b/src/imp.rs @@ -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::{ @@ -33,7 +32,11 @@ use rkyv::ser::serializers::{ use rkyv::validation::validators::CheckDeserializeError; use rkyv::Serialize; use rusk_abi::ContractId; -use stake_contract_types::Unstake; +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; @@ -487,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, @@ -536,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, @@ -545,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); @@ -580,13 +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_note = unstake_note.to_bytes().to_vec(); let unstake = Unstake { - public_key: pk, + public_key, signature, - note: unstake_note, + note: unstake_note.to_vec(), proof: unstake_proof, }; @@ -654,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 @@ -731,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 @@ -893,85 +906,6 @@ fn pick_lexicographic 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: &mut Rng,