Skip to content

Commit

Permalink
feat(dpp): function for getting enabled matching public keys in ident…
Browse files Browse the repository at this point in the history
…ities (dashpay#2052)
  • Loading branch information
pauldelucia authored Aug 20, 2024
1 parent 1ba00e0 commit 00f0b6a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/rs-dpp/src/identity/accessors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,20 @@ impl IdentityGettersV0 for Identity {
}
}

/// Get first public key matching a purpose, security levels or key types
/// Get first public key matching a purpose, security levels, or key types, optionally allowing disabled keys
fn get_first_public_key_matching(
&self,
purpose: Purpose,
security_levels: HashSet<SecurityLevel>,
key_types: HashSet<KeyType>,
allow_disabled: bool,
) -> Option<&IdentityPublicKey> {
match self {
Identity::V0(identity) => identity.public_keys.values().find(|key| {
key.purpose() == purpose
&& security_levels.contains(&key.security_level())
&& key_types.contains(&key.key_type())
&& (allow_disabled || !key.is_disabled())
}),
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/rs-dpp/src/identity/accessors/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub trait IdentityGettersV0 {
purpose: Purpose,
security_levels: HashSet<SecurityLevel>,
key_types: HashSet<KeyType>,
allow_disabled: bool,
) -> Option<&IdentityPublicKey>;
/// Add an identity public key
fn add_public_key(&mut self, key: IdentityPublicKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl IdentityCreditWithdrawalTransitionMethodsV0 for IdentityCreditWithdrawalTra
Purpose::TRANSFER,
SecurityLevel::full_range().into(),
KeyType::all_key_types().into(),
true,
)
.ok_or_else(|| {
ProtocolError::DesiredKeyWithTypePurposeSecurityLevelMissing(
Expand Down
3 changes: 3 additions & 0 deletions packages/rs-drive-abci/tests/strategy_tests/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ impl NetworkStrategy {
KeyType::ECDSA_SECP256K1,
KeyType::BLS12_381,
]),
false,
)
.expect("expected to get a signing key");

Expand Down Expand Up @@ -786,6 +787,7 @@ impl NetworkStrategy {
KeyType::ECDSA_SECP256K1,
KeyType::BLS12_381,
]),
false,
)
.expect("expected to get a signing key");

Expand Down Expand Up @@ -1293,6 +1295,7 @@ impl NetworkStrategy {
Purpose::AUTHENTICATION,
HashSet::from([SecurityLevel::CRITICAL]),
HashSet::from([KeyType::ECDSA_SECP256K1]),
false,
)
.expect("Expected to get identity public key in ContractCreate");
let mut state_transition =
Expand Down
6 changes: 6 additions & 0 deletions packages/strategy-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ impl Strategy {
KeyType::ECDSA_SECP256K1,
KeyType::BLS12_381,
]),
false,
)
.expect("expected to get a signing key");

Expand Down Expand Up @@ -847,6 +848,7 @@ impl Strategy {
KeyType::ECDSA_SECP256K1,
KeyType::BLS12_381,
]),
false,
)
.expect("expected to get a signing key");

Expand Down Expand Up @@ -931,6 +933,7 @@ impl Strategy {
Purpose::AUTHENTICATION,
HashSet::from([SecurityLevel::CRITICAL]),
HashSet::from([KeyType::ECDSA_SECP256K1, KeyType::BLS12_381]),
false,
)
.expect("expected to get a signing key");

Expand Down Expand Up @@ -1385,6 +1388,7 @@ impl Strategy {
Purpose::AUTHENTICATION,
HashSet::from([SecurityLevel::HIGH, SecurityLevel::CRITICAL]),
HashSet::from([KeyType::ECDSA_SECP256K1]),
false,
)
.expect("Expected to get identity public key in ContractCreate");
let mut state_transition =
Expand Down Expand Up @@ -1450,6 +1454,7 @@ impl Strategy {
Purpose::AUTHENTICATION,
HashSet::from([SecurityLevel::CRITICAL]),
HashSet::from([KeyType::ECDSA_SECP256K1, KeyType::BLS12_381]),
false
)
.expect("expected to get a signing key with CRITICAL security level");

Expand Down Expand Up @@ -1638,6 +1643,7 @@ impl Strategy {
Purpose::AUTHENTICATION,
HashSet::from([SecurityLevel::HIGH, SecurityLevel::CRITICAL]),
HashSet::from([KeyType::ECDSA_SECP256K1]),
false
)
.expect("Expected to get identity public key in initial_contract_state_transitions");
let key_id = identity_public_key.id();
Expand Down
2 changes: 2 additions & 0 deletions packages/strategy-tests/src/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ pub fn create_identity_withdrawal_transition(
KeyType::BIP13_SCRIPT_HASH,
KeyType::EDDSA_25519_HASH160,
]),
false,
)
.expect("expected to get a signing key");

Expand Down Expand Up @@ -580,6 +581,7 @@ pub fn create_identity_credit_transfer_transition(
Purpose::TRANSFER,
HashSet::from([SecurityLevel::CRITICAL]),
HashSet::from([KeyType::ECDSA_SECP256K1, KeyType::BLS12_381]),
false,
)
.expect("expected to get a signing key");

Expand Down

0 comments on commit 00f0b6a

Please sign in to comment.