Skip to content

Commit

Permalink
feat: added identity public key private key validation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Oct 10, 2024
1 parent 3ca878d commit a32b867
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod v0;

use crate::identity::IdentityPublicKey;
use crate::ProtocolError;
use dashcore::Network;
pub use v0::*;

impl IdentityPublicKeyHashMethodsV0 for IdentityPublicKey {
Expand All @@ -10,4 +11,14 @@ impl IdentityPublicKeyHashMethodsV0 for IdentityPublicKey {
IdentityPublicKey::V0(v0) => v0.public_key_hash(),
}
}

fn validate_private_key_bytes(
&self,
private_key_bytes: &[u8],
network: Network,
) -> Result<bool, ProtocolError> {
match self {
IdentityPublicKey::V0(v0) => v0.validate_private_key_bytes(private_key_bytes, network),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use crate::ProtocolError;
use dashcore::Network;

pub trait IdentityPublicKeyHashMethodsV0 {
/// Get the original public key hash
fn public_key_hash(&self) -> Result<[u8; 20], ProtocolError>;

/// Verifies that the private key bytes match this identity public key
fn validate_private_key_bytes(
&self,
private_key_bytes: &[u8],
network: Network,
) -> Result<bool, ProtocolError>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::util::hash::ripemd160_sha256;
use crate::ProtocolError;
use anyhow::anyhow;
use dashcore::hashes::Hash;
use dashcore::PublicKey as ECDSAPublicKey;
use dashcore::key::Secp256k1;
use dashcore::secp256k1::SecretKey;
use dashcore::{Network, PublicKey as ECDSAPublicKey};
use platform_value::Bytes20;

impl IdentityPublicKeyHashMethodsV0 for IdentityPublicKeyV0 {
Expand Down Expand Up @@ -45,4 +47,80 @@ impl IdentityPublicKeyHashMethodsV0 for IdentityPublicKeyV0 {
}
}
}

fn validate_private_key_bytes(
&self,
private_key_bytes: &[u8],
network: Network,
) -> Result<bool, ProtocolError> {
match self.key_type {
KeyType::ECDSA_SECP256K1 => {
let secp = Secp256k1::new();
let secret_key = match SecretKey::from_slice(private_key_bytes) {
Ok(secret_key) => secret_key,
Err(_) => return Ok(false),
};
let private_key = dashcore::PrivateKey::new(secret_key, network);

Ok(private_key.public_key(&secp).to_bytes() == self.data.as_slice())
}
KeyType::BLS12_381 => {
#[cfg(feature = "bls-signatures")]
{
let private_key =
match bls_signatures::PrivateKey::from_bytes(private_key_bytes, false) {
Ok(secret_key) => secret_key,
Err(_) => return Ok(false),
};
let public_key_bytes = private_key
.g1_element()
.expect("expected to get a public key from a bls private key")
.to_bytes()
.to_vec();
Ok(public_key_bytes == self.data.as_slice())
}
#[cfg(not(feature = "bls-signatures"))]
return Err(ProtocolError::NotSupported(
"Converting a private key to a bls public key is not supported without the bls-signatures feature".to_string(),
));
}
KeyType::ECDSA_HASH160 => {
let secp = Secp256k1::new();
let secret_key = match SecretKey::from_slice(private_key_bytes) {
Ok(secret_key) => secret_key,
Err(_) => return Ok(false),
};
let private_key = dashcore::PrivateKey::new(secret_key, network);

Ok(
ripemd160_sha256(private_key.public_key(&secp).to_bytes().as_slice())
.as_slice()
== self.data.as_slice(),
)
}
KeyType::EDDSA_25519_HASH160 => {
#[cfg(feature = "ed25519-dalek")]
{
let secret_key = match private_key_bytes.try_into() {
Ok(secret_key) => secret_key,
Err(_) => return Ok(false),
};
let key_pair = ed25519_dalek::SigningKey::from_bytes(&secret_key);
Ok(
ripemd160_sha256(key_pair.verifying_key().to_bytes().as_slice()).as_slice()
== self.data.as_slice(),
)
}
#[cfg(not(feature = "ed25519-dalek"))]
return Err(ProtocolError::NotSupported(
"Converting a private key to a eddsa hash 160 is not supported without the ed25519-dalek feature".to_string(),
));
}
KeyType::BIP13_SCRIPT_HASH => {
return Err(ProtocolError::NotSupported(
"Converting a private key to a script hash is not supported".to_string(),
));
}
}
}
}
1 change: 0 additions & 1 deletion packages/rs-platform-value/src/types/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "json")]
use serde_json::Value as JsonValue;

use crate::string_encoding::Encoding::Base58;
use crate::string_encoding::{Encoding, ALL_ENCODINGS};
use crate::types::encoding_string_to_encoding;
use crate::{string_encoding, Error, Value};
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-sdk/src/platform/types/evonode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use futures::{FutureExt, TryFutureExt};
use rs_dapi_client::transport::{
AppliedRequestSettings, PlatformGrpcClient, TransportClient, TransportRequest,
};
use rs_dapi_client::{Address, ConnectionPool, DapiClientError, RequestSettings};
use rs_dapi_client::{Address, ConnectionPool, RequestSettings};
#[cfg(feature = "mocks")]
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
Expand Down

0 comments on commit a32b867

Please sign in to comment.