Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: identity derivation path changes #48

Merged
merged 1 commit into from
Nov 26, 2024
Merged
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
28 changes: 21 additions & 7 deletions dash/src/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion dash/src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ mod tests {

#[test]
fn block_test_2() {

let some_block = hex!(
"000000200ec684405b58b3a0f0144c9a92c7d4296587ba6fc71041fff2130a038a00000078e259b490cfc8a8e50e1933afde3f777a47bdac8b61d504a51b68dede2ac181cd0822679008011e2e0506000103000500010000000000000000000000000000000000000000000000000000000000000000ffffffff06031e42110101ffffffff037a681d04000000001976a914c69a0bda7daaae481be8def95e5f347a1d00a4b488ac8815a10400000000016ae523b707000000001976a91464f2b2b84f62d68a2cd7f7f5fb2b5aa75ef716d788ac00000000af03001e421100032459005e51c0c2180b5a06001eacee43e1ab9a169dc0c9fb8ca40506258fed68c5aa969c10a3015bde2cfbe1700c795f97a4fa3946e9de4decc14d3a2117be00b695cd60fa9339a8318b65dab7a8a45655ffc6daf0ff1b0b19958903460613f92ac911f3b80bf71bfaeff3968aff87dd0750233a73145f913a3a2a9b1ddaca1ef8f14738870767a80d539ad639837dff1140f78b8f4d65eb4216b7128b736a2453afd99ce4040000"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 };
Expand All @@ -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};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)?;
Expand All @@ -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)?;
Expand Down Expand Up @@ -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() {
Expand Down
35 changes: 11 additions & 24 deletions dash/src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -935,10 +936,7 @@ pub fn read_compact_size<R: Read + ?Sized>(r: &mut R) -> io::Result<u32> {
}
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),
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1004,7 +1000,11 @@ pub fn read_fixed_bitset<R: Read + ?Sized>(r: &mut R, size: usize) -> std::io::R
Ok(bits)
}

pub fn write_fixed_bitset<W: Write + ?Sized>(w: &mut W, bits: &[bool], size: usize) -> io::Result<usize> {
pub fn write_fixed_bitset<W: Write + ?Sized>(
w: &mut W,
bits: &[bool],
size: usize,
) -> io::Result<usize> {
if bits.len() < size {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
Expand Down Expand Up @@ -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();
Expand Down
Loading