|
| 1 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] |
| 2 | +pub enum DerivationPathReference { |
| 3 | + Unknown = 0, |
| 4 | + BIP32 = 1, |
| 5 | + BIP44 = 2, |
| 6 | + BlockchainIdentities = 3, |
| 7 | + ProviderFunds = 4, |
| 8 | + ProviderVotingKeys = 5, |
| 9 | + ProviderOperatorKeys = 6, |
| 10 | + ProviderOwnerKeys = 7, |
| 11 | + ContactBasedFunds = 8, |
| 12 | + ContactBasedFundsRoot = 9, |
| 13 | + ContactBasedFundsExternal = 10, |
| 14 | + BlockchainIdentityCreditRegistrationFunding = 11, |
| 15 | + BlockchainIdentityCreditTopupFunding = 12, |
| 16 | + BlockchainIdentityCreditInvitationFunding = 13, |
| 17 | + ProviderPlatformNodeKeys = 14, |
| 18 | + Root = 255, |
| 19 | +} |
| 20 | + |
| 21 | +use bitflags::bitflags; |
| 22 | +use secp256k1::Secp256k1; |
| 23 | + |
| 24 | +use crate::Network; |
| 25 | +use crate::bip32::{ChildNumber, DerivationPath, Error, ExtendedPrivKey, ExtendedPubKey}; |
| 26 | + |
| 27 | +bitflags! { |
| 28 | + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] |
| 29 | + pub struct DerivationPathType: u32 { |
| 30 | + const UNKNOWN = 0; |
| 31 | + const CLEAR_FUNDS = 1; |
| 32 | + const ANONYMOUS_FUNDS = 1 << 1; |
| 33 | + const VIEW_ONLY_FUNDS = 1 << 2; |
| 34 | + const SINGLE_USER_AUTHENTICATION = 1 << 3; |
| 35 | + const MULTIPLE_USER_AUTHENTICATION = 1 << 4; |
| 36 | + const PARTIAL_PATH = 1 << 5; |
| 37 | + const PROTECTED_FUNDS = 1 << 6; |
| 38 | + const CREDIT_FUNDING = 1 << 7; |
| 39 | + |
| 40 | + // Composite flags |
| 41 | + const IS_FOR_AUTHENTICATION = Self::SINGLE_USER_AUTHENTICATION.bits() | Self::MULTIPLE_USER_AUTHENTICATION.bits(); |
| 42 | + const IS_FOR_FUNDS = Self::CLEAR_FUNDS.bits() |
| 43 | + | Self::ANONYMOUS_FUNDS.bits() |
| 44 | + | Self::VIEW_ONLY_FUNDS.bits() |
| 45 | + | Self::PROTECTED_FUNDS.bits(); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)] |
| 50 | +pub struct IndexConstPath<const N: usize> { |
| 51 | + pub indexes: [ChildNumber; N], |
| 52 | + pub reference: DerivationPathReference, |
| 53 | + pub path_type: DerivationPathType, |
| 54 | +} |
| 55 | + |
| 56 | +impl<const N: usize> AsRef<[ChildNumber]> for IndexConstPath<N> { |
| 57 | + fn as_ref(&self) -> &[ChildNumber] { self.indexes.as_ref() } |
| 58 | +} |
| 59 | + |
| 60 | +impl<const N: usize> IndexConstPath<N> { |
| 61 | + pub fn append_path(&self, derivation_path: DerivationPath) -> DerivationPath { |
| 62 | + let mut root_derivation_path = DerivationPath::from(&self.indexes); |
| 63 | + root_derivation_path.extend(derivation_path); |
| 64 | + root_derivation_path |
| 65 | + } |
| 66 | + |
| 67 | + pub fn append(&self, child_number: ChildNumber) -> DerivationPath { |
| 68 | + let mut root_derivation_path = DerivationPath::from(&self.indexes); |
| 69 | + root_derivation_path.extend(&[child_number]); |
| 70 | + root_derivation_path |
| 71 | + } |
| 72 | + |
| 73 | + pub fn derive_priv_for_seed( |
| 74 | + &self, |
| 75 | + seed: &[u8], |
| 76 | + add_derivation_path: DerivationPath, |
| 77 | + network: Network, |
| 78 | + ) -> Result<ExtendedPrivKey, Error> { |
| 79 | + let secp = Secp256k1::new(); |
| 80 | + let sk = ExtendedPrivKey::new_master(network, seed)?; |
| 81 | + let path = self.append_path(add_derivation_path); |
| 82 | + sk.derive_priv(&secp, &path) |
| 83 | + } |
| 84 | + |
| 85 | + pub fn derive_pub_for_seed( |
| 86 | + &self, |
| 87 | + seed: &[u8], |
| 88 | + add_derivation_path: DerivationPath, |
| 89 | + network: Network, |
| 90 | + ) -> Result<ExtendedPubKey, Error> { |
| 91 | + let secp = Secp256k1::new(); |
| 92 | + let sk = self.derive_priv_for_seed(seed, add_derivation_path, network)?; |
| 93 | + Ok(ExtendedPubKey::from_priv(&secp, &sk)) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Constants for feature purposes and sub-features |
| 98 | +pub const FEATURE_PURPOSE: u32 = 9; |
| 99 | +pub const DASH_COIN_TYPE: u32 = 5; |
| 100 | +pub const DASH_TESTNET_COIN_TYPE: u32 = 1; |
| 101 | +pub const FEATURE_PURPOSE_IDENTITIES: u32 = 5; |
| 102 | +pub const FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_AUTHENTICATION: u32 = 0; |
| 103 | +pub const FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_REGISTRATION: u32 = 1; |
| 104 | +pub const FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_TOPUP: u32 = 2; |
| 105 | +pub const FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_INVITATIONS: u32 = 3; |
| 106 | +pub const FEATURE_PURPOSE_DASHPAY: u32 = 15; |
| 107 | +pub const IDENTITY_REGISTRATION_PATH: IndexConstPath<4> = IndexConstPath { |
| 108 | + indexes: [ |
| 109 | + ChildNumber::Hardened { index: FEATURE_PURPOSE }, |
| 110 | + ChildNumber::Hardened { index: DASH_COIN_TYPE }, |
| 111 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES }, |
| 112 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_REGISTRATION }, |
| 113 | + ], |
| 114 | + reference: DerivationPathReference::BlockchainIdentityCreditRegistrationFunding, |
| 115 | + path_type: DerivationPathType::CREDIT_FUNDING, |
| 116 | +}; |
| 117 | + |
| 118 | +pub const IDENTITY_REGISTRATION_PATH_TESTNET: IndexConstPath<4> = IndexConstPath { |
| 119 | + indexes: [ |
| 120 | + ChildNumber::Hardened { index: FEATURE_PURPOSE }, |
| 121 | + ChildNumber::Hardened { index: DASH_TESTNET_COIN_TYPE }, |
| 122 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES }, |
| 123 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_REGISTRATION }, |
| 124 | + ], |
| 125 | + reference: DerivationPathReference::BlockchainIdentityCreditRegistrationFunding, |
| 126 | + path_type: DerivationPathType::CREDIT_FUNDING, |
| 127 | +}; |
| 128 | + |
| 129 | +// Identity Top-Up Paths |
| 130 | +pub const IDENTITY_TOPUP_PATH: IndexConstPath<4> = IndexConstPath { |
| 131 | + indexes: [ |
| 132 | + ChildNumber::Hardened { index: FEATURE_PURPOSE }, |
| 133 | + ChildNumber::Hardened { index: DASH_COIN_TYPE }, |
| 134 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES }, |
| 135 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_TOPUP }, |
| 136 | + ], |
| 137 | + reference: DerivationPathReference::BlockchainIdentityCreditTopupFunding, |
| 138 | + path_type: DerivationPathType::CREDIT_FUNDING, |
| 139 | +}; |
| 140 | + |
| 141 | +pub const IDENTITY_TOPUP_PATH_TESTNET: IndexConstPath<4> = IndexConstPath { |
| 142 | + indexes: [ |
| 143 | + ChildNumber::Hardened { index: FEATURE_PURPOSE }, |
| 144 | + ChildNumber::Hardened { index: DASH_TESTNET_COIN_TYPE }, |
| 145 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES }, |
| 146 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_TOPUP }, |
| 147 | + ], |
| 148 | + reference: DerivationPathReference::BlockchainIdentityCreditTopupFunding, |
| 149 | + path_type: DerivationPathType::CREDIT_FUNDING, |
| 150 | +}; |
| 151 | + |
| 152 | +// Identity Invitation Paths |
| 153 | +pub const IDENTITY_INVITATION_PATH: IndexConstPath<4> = IndexConstPath { |
| 154 | + indexes: [ |
| 155 | + ChildNumber::Hardened { index: FEATURE_PURPOSE }, |
| 156 | + ChildNumber::Hardened { index: DASH_COIN_TYPE }, |
| 157 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES }, |
| 158 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_INVITATIONS }, |
| 159 | + ], |
| 160 | + reference: DerivationPathReference::BlockchainIdentityCreditInvitationFunding, |
| 161 | + path_type: DerivationPathType::CREDIT_FUNDING, |
| 162 | +}; |
| 163 | + |
| 164 | +pub const IDENTITY_INVITATION_PATH_TESTNET: IndexConstPath<4> = IndexConstPath { |
| 165 | + indexes: [ |
| 166 | + ChildNumber::Hardened { index: FEATURE_PURPOSE }, |
| 167 | + ChildNumber::Hardened { index: DASH_TESTNET_COIN_TYPE }, |
| 168 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES }, |
| 169 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_INVITATIONS }, |
| 170 | + ], |
| 171 | + reference: DerivationPathReference::BlockchainIdentityCreditInvitationFunding, |
| 172 | + path_type: DerivationPathType::CREDIT_FUNDING, |
| 173 | +}; |
| 174 | + |
| 175 | +// Authentication Keys Paths |
| 176 | +pub const IDENTITY_AUTHENTICATION_PATH: IndexConstPath<4> = IndexConstPath { |
| 177 | + indexes: [ |
| 178 | + ChildNumber::Hardened { index: FEATURE_PURPOSE }, |
| 179 | + ChildNumber::Hardened { index: DASH_COIN_TYPE }, |
| 180 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES }, |
| 181 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_AUTHENTICATION }, |
| 182 | + ], |
| 183 | + reference: DerivationPathReference::BlockchainIdentities, |
| 184 | + path_type: DerivationPathType::SINGLE_USER_AUTHENTICATION, |
| 185 | +}; |
| 186 | + |
| 187 | +pub const IDENTITY_AUTHENTICATION_PATH_TESTNET: IndexConstPath<4> = IndexConstPath { |
| 188 | + indexes: [ |
| 189 | + ChildNumber::Hardened { index: FEATURE_PURPOSE }, |
| 190 | + ChildNumber::Hardened { index: DASH_TESTNET_COIN_TYPE }, |
| 191 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES }, |
| 192 | + ChildNumber::Hardened { index: FEATURE_PURPOSE_IDENTITIES_SUBFEATURE_AUTHENTICATION }, |
| 193 | + ], |
| 194 | + reference: DerivationPathReference::BlockchainIdentities, |
| 195 | + path_type: DerivationPathType::SINGLE_USER_AUTHENTICATION, |
| 196 | +}; |
0 commit comments