Skip to content
Open
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 pkgs/primitives/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ pub use self::proupservtx::ProUpServTx;
pub use self::proupsharedregtx::ProUpSharedRegTx;
pub use self::proupsharetx::ProUpShareTx;
pub use self::share::{CollateralShare, PayoutShare};
pub use self::sig::{CollateralSig, DissolveSig, EcdsaRecSigArray, JoinSig, OperatorSig, OwnerSig, SharedRegistrarSig};
pub use self::sig::{
CollateralSig, DissolveSig, EcdsaRecSigArray, JoinSig, OperatorKey, OperatorSig, OwnerSig, SharedRegistrarSig,
};
pub use self::version::ProTxVersion;

pub use dash_pkc::__private::__EddsaPkHash as PlatformNodeId;
37 changes: 31 additions & 6 deletions pkgs/primitives/src/provider/proregtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! ProRegTx registration payload (type 1).

use super::share::{check_payout_version, check_payouts, check_shares, EVO_COLLATERAL, MAX_REWARD, REGULAR_COLLATERAL};
use super::{CollateralShare, CollateralSig, JoinSig, PayoutShare, ProTxVersion};
use super::{CollateralShare, CollateralSig, JoinSig, OperatorKey, PayoutShare, ProTxVersion};
use crate::codec::{decode_items, decode_list, encode_items, encode_list, impl_payload};
use crate::prelude::*;
use crate::provider::PlatformNodeId;
Expand All @@ -19,7 +19,6 @@ use crate::{hash_impl, TxHash};
use bitcoin_primitives::script::ScriptPubKeyBuf;
use dash_num::Hash256;
use dash_params::AddrParams;
use dash_pkc::bls::BlsPkBytes;
use dash_pkc::ecdsa::{EcdsaError, ECDSA_REC_SIG_LEN};
use dash_script::{PubKeyHash, Recipient};
use dash_types::codec::{BaseCodec, DecodeError, EncodeBuf};
Expand Down Expand Up @@ -58,7 +57,7 @@ pub struct ProRegTx {
key_id_owner: PubKeyHash,
/// Operator BLS public key (48 bytes).
#[getset(get_copy = "pub")]
pub_key_operator: BlsPkBytes,
pub_key_operator: OperatorKey,
/// Voting key id (20 bytes).
#[getset(get_copy = "pub")]
key_id_voting: PubKeyHash,
Expand Down Expand Up @@ -155,7 +154,7 @@ impl BaseCodec for ProRegTx {
NetInfo::Legacy(NetInfoV1::decode(data)?)
};
let key_id_owner = PubKeyHash::decode(data)?;
let pub_key_operator = BlsPkBytes::decode(data)?;
let pub_key_operator = OperatorKey::decode(data)?;
let key_id_voting = PubKeyHash::decode(data)?;
let operator_reward = u16::decode(data)?;
let (script_payout, payouts) = if version >= ProTxVersion::CoreV24 {
Expand Down Expand Up @@ -338,7 +337,7 @@ impl Checkable for ProRegTx {
return Some(ProTxInvalid::NullKey);
}
}
if self.pub_key_operator.is_null() || self.key_id_voting.is_null() {
if !self.pub_key_operator.is_valid(self.version) || self.key_id_voting.is_null() {
return Some(ProTxInvalid::NullKey);
}

Expand Down Expand Up @@ -401,7 +400,7 @@ impl ProRegTx {
collateral_index: u32,
net_info: NetInfo,
key_id_owner: PubKeyHash,
pub_key_operator: BlsPkBytes,
pub_key_operator: OperatorKey,
key_id_voting: PubKeyHash,
operator_reward: u16,
script_payout: ScriptPubKeyBuf,
Expand Down Expand Up @@ -703,6 +702,32 @@ mod tests {
|ptx: &mut ProRegTx| ptx.key_id_owner = PubKeyHash::default(),
ProTxInvalid::NullKey
)]
#[case::null_operator(
registration(),
|ptx: &mut ProRegTx| ptx.pub_key_operator = OperatorKey::default(),
ProTxInvalid::NullKey
)]
#[case::operator_key_version(
entry("a3d16d7b7f53adf5403b2082b1ddeae9ad52163d29d5c84cb5eea2e0bc1a486c"),
|ptx: &mut ProRegTx| ptx.version = ProTxVersion::LegacyBls,
ProTxInvalid::NullKey
)]
#[case::mode_before_null_operator(
registration(),
|ptx: &mut ProRegTx| {
ptx.pub_key_operator = OperatorKey::default();
ptx.mode = 1;
},
ProTxInvalid::BadMode { mode: 1 }
)]
#[case::null_operator_before_operator_reward(
registration(),
|ptx: &mut ProRegTx| {
ptx.pub_key_operator = OperatorKey::default();
ptx.operator_reward = MAX_OPERATOR_REWARD + 1;
},
ProTxInvalid::NullKey
)]
#[case::shared_external_collateral(
shared(),
|ptx: &mut ProRegTx| ptx.collateral_hash = TxHash::from_lendian([1; 32]),
Expand Down
37 changes: 31 additions & 6 deletions pkgs/primitives/src/provider/proupregtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
//! ProUpRegTx registrar-update payload (type 3).

use super::share::{check_payout_version, check_payouts, MAX_REWARD};
use super::{OwnerSig, PayoutShare, ProTxVersion};
use super::{OperatorKey, OwnerSig, PayoutShare, ProTxVersion};
use crate::codec::{decode_list, encode_list, impl_payload};
use crate::prelude::*;
use crate::sign::{unsigned_digest, verify_compact};
use crate::sptx::{InputsHash, ProTxInvalid};
use crate::{hash_impl, TxHash};

use bitcoin_primitives::script::ScriptPubKeyBuf;
use dash_pkc::bls::BlsPkBytes;
use dash_pkc::ecdsa::{EcdsaError, ECDSA_REC_SIG_LEN};
use dash_script::PubKeyHash;
use dash_types::codec::{BaseCodec, DecodeError, EncodeBuf};
Expand All @@ -41,7 +40,7 @@ pub struct ProUpRegTx {
mode: u16,
/// Operator BLS public key (48 bytes).
#[getset(get_copy = "pub")]
pub_key_operator: BlsPkBytes,
pub_key_operator: OperatorKey,
/// Voting key id (20 bytes).
#[getset(get_copy = "pub")]
key_id_voting: PubKeyHash,
Expand All @@ -67,7 +66,7 @@ impl BaseCodec for ProUpRegTx {
let version = ProTxVersion::decode(data)?;
let pro_tx_hash = TxHash::decode(data)?;
let mode = u16::decode(data)?;
let pub_key_operator = BlsPkBytes::decode(data)?;
let pub_key_operator = OperatorKey::decode(data)?;
let key_id_voting = PubKeyHash::decode(data)?;
let (script_payout, payouts) = if version >= ProTxVersion::CoreV24 {
(ScriptPubKeyBuf::new(), decode_list(data)?)
Expand Down Expand Up @@ -141,7 +140,7 @@ impl Checkable for ProUpRegTx {
return Some(e);
}

if self.pub_key_operator.is_null() {
if !self.pub_key_operator.is_valid(self.version) {
return Some(ProTxInvalid::NullKey);
}
if self.key_id_voting.is_null() {
Expand All @@ -165,7 +164,7 @@ impl ProUpRegTx {
version: ProTxVersion,
pro_tx_hash: TxHash,
mode: u16,
pub_key_operator: BlsPkBytes,
pub_key_operator: OperatorKey,
key_id_voting: PubKeyHash,
script_payout: ScriptPubKeyBuf,
payouts: Vec<PayoutShare>,
Expand Down Expand Up @@ -270,6 +269,32 @@ mod tests {
|ptx: &mut ProUpRegTx| ptx.script_payout = ptx.payouts[0].script_payout().clone(),
Some(ProTxInvalid::PayoutVersionMismatch { version: ProTxVersion::CoreV24 })
)]
#[case::null_operator(
update(),
|ptx: &mut ProUpRegTx| ptx.pub_key_operator = OperatorKey::default(),
Some(ProTxInvalid::NullKey)
)]
#[case::operator_key_version(
basic_bls(),
|ptx: &mut ProUpRegTx| ptx.version = ProTxVersion::LegacyBls,
Some(ProTxInvalid::NullKey)
)]
#[case::mode_before_null_operator(
update(),
|ptx: &mut ProUpRegTx| {
ptx.pub_key_operator = OperatorKey::default();
ptx.mode = 1;
},
Some(ProTxInvalid::BadMode { mode: 1 })
)]
#[case::null_operator_before_payouts(
update(),
|ptx: &mut ProUpRegTx| {
ptx.pub_key_operator = OperatorKey::default();
first_to(ptx, ptx.key_id_voting);
},
Some(ProTxInvalid::NullKey)
)]
fn check(#[case] mut ptx: ProUpRegTx, #[case] edit: fn(&mut ProUpRegTx), #[case] expected: Option<ProTxInvalid>) {
assert_eq!(ptx.check(), None);
edit(&mut ptx);
Expand Down
15 changes: 7 additions & 8 deletions pkgs/primitives/src/provider/prouprevtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@

//! ProUpRevTx revocation payload (type 4).

use super::sig::verify_operator;
use super::{OperatorSig, ProTxVersion};
use super::{OperatorKey, OperatorSig, ProTxVersion};
use crate::codec::codec_payload;
use crate::prelude::*;
use crate::sign::unsigned_digest;
use crate::sptx::{InputsHash, ProTxInvalid};
use crate::support::RevocationReason;
use crate::TxHash;

use dash_pkc::bls::{BlsError, BlsPkBytes, BlsSigBytes, BLS_SIG_LEN};
use dash_pkc::bls::{BlsError, BlsSigBytes, BLS_SIG_LEN};
use dash_types::type_id::TypeId;
use dash_types::{Checkable, Signable};
use getset::CopyGetters;
Expand Down Expand Up @@ -50,18 +49,18 @@ pub struct ProUpRevTx {
}

impl Signable for ProUpRevTx {
type Key = BlsPkBytes;
type Key = OperatorKey;
type Context = ();
type Error = BlsError;

fn payload(&self, _ctx: &()) -> Vec<u8> {
unsigned_digest(self, &self.sig).to_vec()
}

fn set_signature(&mut self, sig: &[u8], key: &BlsPkBytes, _ctx: &()) -> Result<(), BlsError> {
fn set_signature(&mut self, sig: &[u8], key: &OperatorKey, _ctx: &()) -> Result<(), BlsError> {
let sig = <[u8; BLS_SIG_LEN]>::try_from(sig).map_err(|_| BlsError::InvalidSignature)?;
let sig = OperatorSig::new(BlsSigBytes::from_bytes(sig));
verify_operator(self.version, &self.payload(&()), &sig, key)?;
key.verify(self.version, &self.payload(&()), &sig)?;
self.sig = sig;
Ok(())
}
Expand All @@ -70,8 +69,8 @@ impl Signable for ProUpRevTx {
self.sig = OperatorSig::default();
}

fn verify(&self, key: &BlsPkBytes, _ctx: &()) -> Option<BlsError> {
verify_operator(self.version, &self.payload(&()), &self.sig, key).err()
fn verify(&self, key: &OperatorKey, _ctx: &()) -> Option<BlsError> {
key.verify(self.version, &self.payload(&()), &self.sig).err()
}
}

Expand Down
15 changes: 7 additions & 8 deletions pkgs/primitives/src/provider/proupservtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
//! ProUpServTx service-update payload (type 2).

use super::proregtx::check_platform_fields;
use super::sig::verify_operator;
use super::{OperatorSig, ProTxVersion};
use super::{OperatorKey, OperatorSig, ProTxVersion};
use crate::codec::impl_payload;
use crate::prelude::*;
use crate::provider::PlatformNodeId;
Expand All @@ -18,7 +17,7 @@ use crate::types::{NITrait, NetInfo, NetInfoV1, NetInfoV2};
use crate::{hash_impl, TxHash};

use bitcoin_primitives::script::ScriptPubKeyBuf;
use dash_pkc::bls::{BlsError, BlsPkBytes, BlsSigBytes, BLS_SIG_LEN};
use dash_pkc::bls::{BlsError, BlsSigBytes, BLS_SIG_LEN};
use dash_types::codec::{BaseCodec, DecodeError, EncodeBuf};
use dash_types::{type_id::TypeId, Checkable, Numeric, Signable};
use getset::{CopyGetters, Getters};
Expand Down Expand Up @@ -138,18 +137,18 @@ impl BaseCodec for ProUpServTx {
}

impl Signable for ProUpServTx {
type Key = BlsPkBytes;
type Key = OperatorKey;
type Context = ();
type Error = BlsError;

fn payload(&self, _ctx: &()) -> Vec<u8> {
unsigned_digest(self, &self.sig).to_vec()
}

fn set_signature(&mut self, sig: &[u8], key: &BlsPkBytes, _ctx: &()) -> Result<(), BlsError> {
fn set_signature(&mut self, sig: &[u8], key: &OperatorKey, _ctx: &()) -> Result<(), BlsError> {
let sig = <[u8; BLS_SIG_LEN]>::try_from(sig).map_err(|_| BlsError::InvalidSignature)?;
let sig = OperatorSig::new(BlsSigBytes::from_bytes(sig));
verify_operator(self.version, &self.payload(&()), &sig, key)?;
key.verify(self.version, &self.payload(&()), &sig)?;
self.sig = sig;
Ok(())
}
Expand All @@ -158,8 +157,8 @@ impl Signable for ProUpServTx {
self.sig = OperatorSig::default();
}

fn verify(&self, key: &BlsPkBytes, _ctx: &()) -> Option<BlsError> {
verify_operator(self.version, &self.payload(&()), &self.sig, key).err()
fn verify(&self, key: &OperatorKey, _ctx: &()) -> Option<BlsError> {
key.verify(self.version, &self.payload(&()), &self.sig).err()
}
}

Expand Down
14 changes: 7 additions & 7 deletions pkgs/primitives/src/provider/proupsharedregtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//! ProUpSharedRegTx shared registrar update payload (type 12).

use super::share::MAX_SHARES;
use super::SharedRegistrarSig;
use super::{OperatorKey, SharedRegistrarSig};
use crate::codec::{decode_list, encode_list, impl_payload};
use crate::prelude::*;
use crate::sptx::{InputsHash, ProTxInvalid};
use crate::{hash_impl, TxHash};

use dash_pkc::bls::BlsPkBytes;
use dash_pkc::bls::BlsScIetf;
use dash_script::PubKeyHash;
use dash_types::codec::{BaseCodec, DecodeError, EncodeBuf};
use dash_types::type_id::TypeId;
Expand All @@ -36,7 +36,7 @@ pub struct ProUpSharedRegTx {
pro_tx_hash: TxHash,
/// Operator BLS public key (48 bytes, basic scheme).
#[getset(get_copy = "pub")]
pub_key_operator: BlsPkBytes,
pub_key_operator: OperatorKey,
/// Voting key id (20 bytes).
#[getset(get_copy = "pub")]
key_id_voting: PubKeyHash,
Expand All @@ -56,7 +56,7 @@ impl BaseCodec for ProUpSharedRegTx {
Ok(Self {
version: u16::decode(data)?,
pro_tx_hash: TxHash::decode(data)?,
pub_key_operator: BlsPkBytes::decode(data)?,
pub_key_operator: OperatorKey::decode(data)?,
key_id_voting: PubKeyHash::decode(data)?,
inputs_hash: InputsHash::decode(data)?,
sigs: decode_list(data)?,
Expand All @@ -80,7 +80,7 @@ impl Checkable for ProUpSharedRegTx {
if self.version != Self::CURRENT_VERSION {
return Some(ProTxInvalid::BadSharedRegistrarVersion { version: self.version });
}
if self.pub_key_operator.is_null() || self.key_id_voting.is_null() {
if !self.pub_key_operator.is_valid_under::<BlsScIetf>() || self.key_id_voting.is_null() {
return Some(ProTxInvalid::NullKey);
}
if self.sigs.is_empty() || self.sigs.len() > MAX_SHARES {
Expand Down Expand Up @@ -179,7 +179,7 @@ mod tests {
"votingAddress"
);
assert_eq!(
ptx.pub_key_operator.as_bytes().to_lower_hex_string(),
ptx.pub_key_operator.as_bytes().as_bytes().to_lower_hex_string(),
pub_key_operator,
"pubKeyOperator"
);
Expand All @@ -193,7 +193,7 @@ mod tests {
}

fn null_operator(ptx: &mut ProUpSharedRegTx) {
ptx.pub_key_operator = BlsPkBytes::default();
ptx.pub_key_operator = OperatorKey::default();
}

#[rstest]
Expand Down
Loading
Loading