From d741be563c4df71c5b13fa04cd2d9a8106a6cb38 Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Tue, 26 Nov 2024 12:38:30 +0300 Subject: [PATCH] feat: identity derivation path changes (#48) --- dash/src/bip32.rs | 28 +++++++++++---- dash/src/blockdata/block.rs | 1 - .../special_transaction/coinbase.rs | 9 +++-- .../special_transaction/quorum_commitment.rs | 16 ++++++--- dash/src/consensus/encode.rs | 35 ++++++------------- 5 files changed, 48 insertions(+), 41 deletions(-) diff --git a/dash/src/bip32.rs b/dash/src/bip32.rs index 1ed8b66c95..15c6fb143f 100644 --- a/dash/src/bip32.rs +++ b/dash/src/bip32.rs @@ -398,7 +398,8 @@ impl DerivationPath { root_derivation_path } - pub fn identity_registration_path(network: Network, index: u32) -> Self { + /// This might have been used in the past + pub fn identity_registration_path_child_non_hardened(network: Network, index: u32) -> Self { let mut root_derivation_path: DerivationPath = match network { Network::Dash => IDENTITY_REGISTRATION_PATH_MAINNET, _ => IDENTITY_REGISTRATION_PATH_TESTNET, @@ -408,13 +409,26 @@ impl DerivationPath { root_derivation_path } - pub fn identity_top_up_path(network: Network, index: u32) -> Self { + pub fn identity_registration_path(network: Network, index: u32) -> Self { + let mut root_derivation_path: DerivationPath = match network { + Network::Dash => IDENTITY_REGISTRATION_PATH_MAINNET, + _ => IDENTITY_REGISTRATION_PATH_TESTNET, + } + .into(); + root_derivation_path.0.extend(&[ChildNumber::Hardened { index }]); + root_derivation_path + } + + pub fn identity_top_up_path(network: Network, identity_index: u32, top_up_index: u32) -> Self { let mut root_derivation_path: DerivationPath = match network { Network::Dash => IDENTITY_TOPUP_PATH_MAINNET, _ => IDENTITY_TOPUP_PATH_TESTNET, } .into(); - root_derivation_path.0.extend(&[ChildNumber::Normal { index }]); + root_derivation_path.0.extend(&[ + ChildNumber::Hardened { index: identity_index }, + ChildNumber::Normal { index: top_up_index }, + ]); root_derivation_path } @@ -424,7 +438,7 @@ impl DerivationPath { _ => IDENTITY_INVITATION_PATH_TESTNET, } .into(); - root_derivation_path.0.extend(&[ChildNumber::Normal { index }]); + root_derivation_path.0.extend(&[ChildNumber::Hardened { index }]); root_derivation_path } @@ -1820,13 +1834,13 @@ mod tests { #[test] fn test_identity_registration_path() { let path = DerivationPath::identity_registration_path(Network::Dash, 10); - assert_eq!(path.to_string(), "m/9'/5'/5'/1'/10"); + assert_eq!(path.to_string(), "m/9'/5'/5'/1'/10'"); } #[test] fn test_identity_top_up_path() { - let path = DerivationPath::identity_top_up_path(Network::Testnet, 2); - assert_eq!(path.to_string(), "m/9'/1'/5'/2'/2"); + let path = DerivationPath::identity_top_up_path(Network::Testnet, 2, 3); + assert_eq!(path.to_string(), "m/9'/1'/5'/2'/2'/3"); } #[test] diff --git a/dash/src/blockdata/block.rs b/dash/src/blockdata/block.rs index 52ad6dc576..8114c4740f 100644 --- a/dash/src/blockdata/block.rs +++ b/dash/src/blockdata/block.rs @@ -468,7 +468,6 @@ mod tests { #[test] fn block_test_2() { - let some_block = hex!( "000000200ec684405b58b3a0f0144c9a92c7d4296587ba6fc71041fff2130a038a00000078e259b490cfc8a8e50e1933afde3f777a47bdac8b61d504a51b68dede2ac181cd0822679008011e2e0506000103000500010000000000000000000000000000000000000000000000000000000000000000ffffffff06031e42110101ffffffff037a681d04000000001976a914c69a0bda7daaae481be8def95e5f347a1d00a4b488ac8815a10400000000016ae523b707000000001976a91464f2b2b84f62d68a2cd7f7f5fb2b5aa75ef716d788ac00000000af03001e421100032459005e51c0c2180b5a06001eacee43e1ab9a169dc0c9fb8ca40506258fed68c5aa969c10a3015bde2cfbe1700c795f97a4fa3946e9de4decc14d3a2117be00b695cd60fa9339a8318b65dab7a8a45655ffc6daf0ff1b0b19958903460613f92ac911f3b80bf71bfaeff3968aff87dd0750233a73145f913a3a2a9b1ddaca1ef8f14738870767a80d539ad639837dff1140f78b8f4d65eb4216b7128b736a2453afd99ce4040000" ); diff --git a/dash/src/blockdata/transaction/special_transaction/coinbase.rs b/dash/src/blockdata/transaction/special_transaction/coinbase.rs index 921d6d4924..7a55990029 100644 --- a/dash/src/blockdata/transaction/special_transaction/coinbase.rs +++ b/dash/src/blockdata/transaction/special_transaction/coinbase.rs @@ -18,12 +18,12 @@ //! It is defined in DIP4 [dip-0004](https://github.com/dashpay/dips/blob/master/dip-0004.md). //! +use crate::bls_sig_utils::BLSSignature; +use crate::consensus::encode::{compact_size_len, read_compact_size, write_compact_size}; use crate::consensus::{Decodable, Encodable, encode}; use crate::hash_types::{MerkleRootMasternodeList, MerkleRootQuorums}; use crate::io::{Error, ErrorKind}; use crate::{VarInt, io}; -use crate::bls_sig_utils::BLSSignature; -use crate::consensus::encode::{compact_size_len, read_compact_size, write_compact_size}; /// A Coinbase payload. This is contained as the payload of a coinbase special transaction. /// The Coinbase payload is described in DIP4. @@ -95,9 +95,7 @@ impl Decodable for CoinbasePayload { let height = u32::consensus_decode(r)?; let merkle_root_masternode_list = MerkleRootMasternodeList::consensus_decode(r)?; let merkle_root_quorums = MerkleRootQuorums::consensus_decode(r)?; - let best_cl_height = if version >= 3 { - Some(read_compact_size(r)?) - } else { None }; + let best_cl_height = if version >= 3 { Some(read_compact_size(r)?) } else { None }; let best_cl_signature = if version >= 3 { Some(BLSSignature::consensus_decode(r)?) } else { None }; let asset_locked_amount = if version >= 3 { Some(u64::consensus_decode(r)?) } else { None }; @@ -116,6 +114,7 @@ impl Decodable for CoinbasePayload { #[cfg(test)] mod tests { use hashes::Hash; + use crate::bls_sig_utils::BLSSignature; use crate::consensus::Encodable; use crate::hash_types::{MerkleRootMasternodeList, MerkleRootQuorums}; diff --git a/dash/src/blockdata/transaction/special_transaction/quorum_commitment.rs b/dash/src/blockdata/transaction/special_transaction/quorum_commitment.rs index 23dbdc8256..87e648c10c 100644 --- a/dash/src/blockdata/transaction/special_transaction/quorum_commitment.rs +++ b/dash/src/blockdata/transaction/special_transaction/quorum_commitment.rs @@ -18,12 +18,16 @@ //! use std::io::{Read, Write}; + use crate::bls_sig_utils::{BLSPublicKey, BLSSignature}; +use crate::consensus::encode::{ + compact_size_len, fixed_bitset_len, read_compact_size, read_fixed_bitset, write_compact_size, + write_fixed_bitset, +}; use crate::consensus::{Decodable, Encodable, encode}; use crate::hash_types::{QuorumHash, QuorumVVecHash}; use crate::prelude::*; use crate::{VarInt, io}; -use crate::consensus::encode::{compact_size_len, fixed_bitset_len, read_compact_size, read_fixed_bitset, write_compact_size, write_fixed_bitset}; /// A Quorum Finalization Commitment. It is described in the finalization section of DIP6: /// [dip-0006.md#6-finalization-phase](https://github.com/dashpay/dips/blob/master/dip-0006.md#6-finalization-phase) @@ -73,7 +77,8 @@ impl Encodable for QuorumFinalizationCommitment { len += write_compact_size(w, self.signers.len() as u32)?; len += write_fixed_bitset(w, self.signers.as_slice(), self.signers.iter().len())?; len += write_compact_size(w, self.valid_members.len() as u32)?; - len += write_fixed_bitset(w, self.valid_members.as_slice(), self.valid_members.iter().len())?; + len += + write_fixed_bitset(w, self.valid_members.as_slice(), self.valid_members.iter().len())?; len += self.quorum_public_key.consensus_encode(w)?; len += self.quorum_vvec_hash.consensus_encode(w)?; len += self.quorum_sig.consensus_encode(w)?; @@ -87,7 +92,8 @@ impl Decodable for QuorumFinalizationCommitment { let version = u16::consensus_decode(r)?; let llmq_type = u8::consensus_decode(r)?; let quorum_hash = QuorumHash::consensus_decode(r)?; - let quorum_index = if version == 2 || version == 4 { Some(i16::consensus_decode(r)?) } else { None }; + let quorum_index = + if version == 2 || version == 4 { Some(i16::consensus_decode(r)?) } else { None }; let signers_count = read_compact_size(r)?; let signers = read_fixed_bitset(r, signers_count as usize)?; let valid_members_count = read_compact_size(r)?; @@ -156,7 +162,9 @@ mod tests { use crate::bls_sig_utils::{BLSPublicKey, BLSSignature}; use crate::consensus::Encodable; use crate::hash_types::{QuorumHash, QuorumVVecHash}; - use crate::transaction::special_transaction::quorum_commitment::{QuorumCommitmentPayload, QuorumFinalizationCommitment}; + use crate::transaction::special_transaction::quorum_commitment::{ + QuorumCommitmentPayload, QuorumFinalizationCommitment, + }; #[test] fn size() { diff --git a/dash/src/consensus/encode.rs b/dash/src/consensus/encode.rs index 8b3cf7f9bc..966b033ba1 100644 --- a/dash/src/consensus/encode.rs +++ b/dash/src/consensus/encode.rs @@ -33,6 +33,7 @@ use core::convert::From; use core::{fmt, mem, u32}; use std::io::Write; + #[cfg(feature = "core-block-hash-use-x11")] use hashes::hash_x11; use hashes::{Hash, hash160, sha256, sha256d}; @@ -935,10 +936,7 @@ pub fn read_compact_size(r: &mut R) -> io::Result { } 0xFF => { // Value is too large to fit in u32 - Err(io::Error::new( - io::ErrorKind::InvalidData, - "CompactSize value exceeds u32::MAX", - )) + Err(io::Error::new(io::ErrorKind::InvalidData, "CompactSize value exceeds u32::MAX")) } value => Ok(value as u32), } @@ -967,11 +965,9 @@ pub fn compact_size_len(value: u32) -> usize { let mut size: usize = 0; if value < 253 { size += 1; - } - else if value < 65536 { + } else if value < 65536 { size += 3; - } - else { + } else { size += 5; } size @@ -1004,7 +1000,11 @@ pub fn read_fixed_bitset(r: &mut R, size: usize) -> std::io::R Ok(bits) } -pub fn write_fixed_bitset(w: &mut W, bits: &[bool], size: usize) -> io::Result { +pub fn write_fixed_bitset( + w: &mut W, + bits: &[bool], + size: usize, +) -> io::Result { if bits.len() < size { return Err(io::Error::new( io::ErrorKind::InvalidInput, @@ -1452,21 +1452,8 @@ mod tests { #[test] fn test_compact_size_round_trip() { - let test_values = vec![ - 0u32, - 1, - 252, - 253, - 254, - 255, - 300, - 5000, - 65535, - 65536, - 70000, - 1_000_000, - u32::MAX, - ]; + let test_values = + vec![0u32, 1, 252, 253, 254, 255, 300, 5000, 65535, 65536, 70000, 1_000_000, u32::MAX]; for &value in &test_values { let mut buffer = Vec::new();