From 6eb10db78c594bd94792d732141c62af01b81e28 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 23:24:18 +0000 Subject: [PATCH 1/2] primitives%fix!: check operator keys under the payload's BLS scheme ProRegTx, ProUpRegTx and ProUpSharedRegTx hold the key as `OperatorKey` and refuse it as null when it is null or invalid under the payload's scheme, legacy for a version 1 ProRegTx or ProUpRegTx, else basic. --- pkgs/primitives/src/provider/mod.rs | 4 +- pkgs/primitives/src/provider/proregtx.rs | 21 ++++++--- pkgs/primitives/src/provider/proupregtx.rs | 16 ++++--- .../src/provider/proupsharedregtx.rs | 14 +++--- pkgs/primitives/src/provider/sig.rs | 44 ++++++++++++++++++- 5 files changed, 77 insertions(+), 22 deletions(-) diff --git a/pkgs/primitives/src/provider/mod.rs b/pkgs/primitives/src/provider/mod.rs index e20ffa4a1..c0377fb33 100644 --- a/pkgs/primitives/src/provider/mod.rs +++ b/pkgs/primitives/src/provider/mod.rs @@ -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; diff --git a/pkgs/primitives/src/provider/proregtx.rs b/pkgs/primitives/src/provider/proregtx.rs index 60d15d2a2..af146968f 100644 --- a/pkgs/primitives/src/provider/proregtx.rs +++ b/pkgs/primitives/src/provider/proregtx.rs @@ -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; @@ -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}; @@ -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, @@ -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 { @@ -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); } @@ -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, @@ -703,6 +702,16 @@ 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::shared_external_collateral( shared(), |ptx: &mut ProRegTx| ptx.collateral_hash = TxHash::from_lendian([1; 32]), diff --git a/pkgs/primitives/src/provider/proupregtx.rs b/pkgs/primitives/src/provider/proupregtx.rs index a945a543f..5c59d6b39 100644 --- a/pkgs/primitives/src/provider/proupregtx.rs +++ b/pkgs/primitives/src/provider/proupregtx.rs @@ -7,7 +7,7 @@ //! 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}; @@ -15,7 +15,6 @@ 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}; @@ -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, @@ -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)?) @@ -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() { @@ -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, @@ -270,6 +269,11 @@ 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) + )] fn check(#[case] mut ptx: ProUpRegTx, #[case] edit: fn(&mut ProUpRegTx), #[case] expected: Option) { assert_eq!(ptx.check(), None); edit(&mut ptx); diff --git a/pkgs/primitives/src/provider/proupsharedregtx.rs b/pkgs/primitives/src/provider/proupsharedregtx.rs index 7c249a65d..98493dbcc 100644 --- a/pkgs/primitives/src/provider/proupsharedregtx.rs +++ b/pkgs/primitives/src/provider/proupsharedregtx.rs @@ -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; @@ -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, @@ -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)?, @@ -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::() || self.key_id_voting.is_null() { return Some(ProTxInvalid::NullKey); } if self.sigs.is_empty() || self.sigs.len() > MAX_SHARES { @@ -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" ); @@ -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] diff --git a/pkgs/primitives/src/provider/sig.rs b/pkgs/primitives/src/provider/sig.rs index 78ae566ad..47e8e8b38 100644 --- a/pkgs/primitives/src/provider/sig.rs +++ b/pkgs/primitives/src/provider/sig.rs @@ -4,7 +4,7 @@ // See the accompanying file LICENSE or https://opensource.org/license/MIT // -//! Provider transaction signatures. +//! Provider transaction signatures and the operator key. use super::ProTxVersion; use crate::hash_impl; @@ -13,7 +13,9 @@ use crate::sign::verify_bls; use bitcoin_hashes::sha256d; use dash_num::Hash256; -use dash_pkc::bls::{BlsError, BlsPkBytes, BlsScChia, BlsScIetf, BlsSigBytes, BLS_SIG_LEN}; +use dash_pkc::bls::{ + BlsError, BlsPkBytes, BlsPkHash, BlsScChia, BlsScIetf, BlsScheme, BlsSigBytes, BLS_PK_LEN, BLS_SIG_LEN, +}; use dash_pkc::ecdsa::ECDSA_REC_SIG_LEN; use dash_types::codec::{BaseCodec, DecodeError, EncodeBuf}; #[cfg(feature = "serde")] @@ -207,6 +209,44 @@ impl Default for OperatorSig { } } +/// Operator BLS public key, all zero when null. +#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, TypeId)] +#[cfg_attr( + feature = "serde", + derive(::serde::Serialize, ::serde::Deserialize), + serde(transparent) +)] +pub struct OperatorKey(BlsPkBytes); + +dlgt_codec!(OperatorKey => BlsPkBytes, BlsPkHash, Infallible, BLS_PK_LEN); + +impl OperatorKey { + /// Borrows the key bytes. + pub fn as_bytes(&self) -> &BlsPkBytes { + &self.0 + } + + /// Whether the key is not null and passes [`BlsPkBytes::is_canonical`] + /// under the scheme `version` sets. + pub fn is_valid(&self, version: ProTxVersion) -> bool { + if version == ProTxVersion::LegacyBls { + self.is_valid_under::() + } else { + self.is_valid_under::() + } + } + + /// Whether the key is not null and passes [`BlsPkBytes::is_canonical`] + /// under `S`. + pub(crate) fn is_valid_under(&self) -> bool { + !self.0.is_null() && self.0.is_canonical::() + } +} + +type_cvrt!(From for OperatorKey, |key| Self(*key)); + +type_cvrt!(From for BlsPkBytes, |key| key.0); + /// Declares a compact signature that names what it signs and delegates its /// codec to [`EcdsaRecSigArray`]. macro_rules! rec_sig { From 1193b7e3aa9b2893d9bf21a796ebc0accba58767 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 23:24:56 +0000 Subject: [PATCH 2/2] primitives%refac!: fold `verify_operator` into `OperatorKey` ProUpServTx and ProUpRevTx take the operator key as `OperatorKey`, whose `verify` checks a signature under the scheme the payload version sets. --- pkgs/primitives/src/provider/prouprevtx.rs | 15 ++-- pkgs/primitives/src/provider/proupservtx.rs | 15 ++-- pkgs/primitives/src/provider/sig.rs | 76 +++++++++++++++------ 3 files changed, 68 insertions(+), 38 deletions(-) diff --git a/pkgs/primitives/src/provider/prouprevtx.rs b/pkgs/primitives/src/provider/prouprevtx.rs index 341fb0742..7d85c50f6 100644 --- a/pkgs/primitives/src/provider/prouprevtx.rs +++ b/pkgs/primitives/src/provider/prouprevtx.rs @@ -6,8 +6,7 @@ //! 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; @@ -15,7 +14,7 @@ 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; @@ -50,7 +49,7 @@ pub struct ProUpRevTx { } impl Signable for ProUpRevTx { - type Key = BlsPkBytes; + type Key = OperatorKey; type Context = (); type Error = BlsError; @@ -58,10 +57,10 @@ impl Signable for ProUpRevTx { 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(()) } @@ -70,8 +69,8 @@ impl Signable for ProUpRevTx { self.sig = OperatorSig::default(); } - fn verify(&self, key: &BlsPkBytes, _ctx: &()) -> Option { - verify_operator(self.version, &self.payload(&()), &self.sig, key).err() + fn verify(&self, key: &OperatorKey, _ctx: &()) -> Option { + key.verify(self.version, &self.payload(&()), &self.sig).err() } } diff --git a/pkgs/primitives/src/provider/proupservtx.rs b/pkgs/primitives/src/provider/proupservtx.rs index 80bcc7cd2..eefa5a0d1 100644 --- a/pkgs/primitives/src/provider/proupservtx.rs +++ b/pkgs/primitives/src/provider/proupservtx.rs @@ -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; @@ -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}; @@ -138,7 +137,7 @@ impl BaseCodec for ProUpServTx { } impl Signable for ProUpServTx { - type Key = BlsPkBytes; + type Key = OperatorKey; type Context = (); type Error = BlsError; @@ -146,10 +145,10 @@ impl Signable for ProUpServTx { 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(()) } @@ -158,8 +157,8 @@ impl Signable for ProUpServTx { self.sig = OperatorSig::default(); } - fn verify(&self, key: &BlsPkBytes, _ctx: &()) -> Option { - verify_operator(self.version, &self.payload(&()), &self.sig, key).err() + fn verify(&self, key: &OperatorKey, _ctx: &()) -> Option { + key.verify(self.version, &self.payload(&()), &self.sig).err() } } diff --git a/pkgs/primitives/src/provider/sig.rs b/pkgs/primitives/src/provider/sig.rs index 47e8e8b38..82edd27fd 100644 --- a/pkgs/primitives/src/provider/sig.rs +++ b/pkgs/primitives/src/provider/sig.rs @@ -75,20 +75,6 @@ pub(crate) mod compact_hex { } } -/// Checks an operator signature over `digest` under the scheme `version` sets. -pub(crate) fn verify_operator( - version: ProTxVersion, - digest: &[u8], - sig: &OperatorSig, - key: &BlsPkBytes, -) -> Result<(), BlsError> { - if version == ProTxVersion::LegacyBls { - verify_bls::(digest, sig.as_bytes(), key) - } else { - verify_bls::(digest, sig.as_bytes(), key) - } -} - /// Collateral owner signature, empty when the collateral is spent. #[derive(Clone, Debug, Default, Eq, Hash, PartialEq, TypeId)] #[cfg_attr( @@ -241,6 +227,20 @@ impl OperatorKey { pub(crate) fn is_valid_under(&self) -> bool { !self.0.is_null() && self.0.is_canonical::() } + + /// Checks an operator signature over `digest` under the scheme `version` + /// sets. + /// + /// # Errors + /// + /// Returns what [`verify_bls`] returns under that scheme. + pub(crate) fn verify(&self, version: ProTxVersion, digest: &[u8], sig: &OperatorSig) -> Result<(), BlsError> { + if version == ProTxVersion::LegacyBls { + verify_bls::(digest, sig.as_bytes(), &self.0) + } else { + verify_bls::(digest, sig.as_bytes(), &self.0) + } + } } type_cvrt!(From for OperatorKey, |key| Self(*key)); @@ -294,8 +294,12 @@ rec_sig! { #[expect(clippy::unwrap_used, reason = "test code")] mod tests { use super::*; + use crate::provider::{ProUpRevTx, ProUpServTx}; use crate::sign::tests::bls_sign; + use crate::{InputsHash, MnType, NetInfo, NetInfoV2, RevocationReason, TxHash}; + use bitcoin_primitives::script::ScriptPubKeyBuf; + use dash_types::Signable; use rstest::rstest; /// Any header byte survives a decode and encode round trip. @@ -319,16 +323,44 @@ mod tests { fn operator_scheme_follows_the_version(#[case] version: ProTxVersion, #[case] legacy: bool) { let (legacy_key, legacy_sig) = bls_sign::(1, &[7; 32]); let (basic_key, basic_sig) = bls_sign::(1, &[7; 32]); + let legacy_key = OperatorKey::from(legacy_key); + let basic_key = OperatorKey::from(basic_key); let legacy_sig = OperatorSig::new(BlsSigBytes::from_bytes(legacy_sig)); let basic_sig = OperatorSig::new(BlsSigBytes::from_bytes(basic_sig)); - assert_eq!( - verify_operator(version, &[7; 32], &legacy_sig, &legacy_key).is_ok(), - legacy - ); - assert_eq!( - verify_operator(version, &[7; 32], &basic_sig, &basic_key).is_ok(), - !legacy - ); + assert_eq!(legacy_key.verify(version, &[7; 32], &legacy_sig).is_ok(), legacy); + assert_eq!(basic_key.verify(version, &[7; 32], &basic_sig).is_ok(), !legacy); + } + + /// Stores an operator signature over the payload of `ptx` and verifies it. + fn operator_signs>(mut ptx: T) { + let digest = <[u8; 32]>::try_from(ptx.payload(&())).unwrap(); + let (key, sig) = bls_sign::(1, &digest); + let key = OperatorKey::from(key); + + assert_eq!(ptx.set_signature(&sig, &key, &()), Ok(())); + assert_eq!(ptx.verify(&key, &()), None); + } + + #[rstest] + fn operator_key_signs_updates() { + let pro_tx_hash = TxHash::from([0x11; 32]); + operator_signs(ProUpServTx::new( + ProTxVersion::CoreV24, + MnType::Regular, + pro_tx_hash, + NetInfo::Extended(NetInfoV2::new(1, Vec::new())), + ScriptPubKeyBuf::new(), + InputsHash::default(), + None, + None, + None, + )); + operator_signs(ProUpRevTx::new( + ProTxVersion::CoreV24, + pro_tx_hash, + RevocationReason::NotSpecified, + InputsHash::default(), + )); } }