Skip to content

Commit ff64350

Browse files
feat: add a convenience method to get the public key data for a private key depending on the key type
1 parent 240f6ce commit ff64350

File tree

1 file changed

+56
-1
lines changed
  • packages/rs-dpp/src/identity/identity_public_key

1 file changed

+56
-1
lines changed

packages/rs-dpp/src/identity/identity_public_key/key_type.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use lazy_static::lazy_static;
1717

1818
use crate::fee::Credits;
1919
use crate::version::PlatformVersion;
20-
use crate::ProtocolError;
20+
use crate::{InvalidVectorSizeError, ProtocolError};
2121
#[cfg(feature = "random-public-keys")]
2222
use rand::rngs::StdRng;
2323
#[cfg(feature = "random-public-keys")]
@@ -204,6 +204,61 @@ impl KeyType {
204204
}
205205
}
206206

207+
/// Gets the public key data for a private key depending on the key type
208+
pub fn public_key_data_from_private_key_data(
209+
&self,
210+
private_key_bytes: Vec<u8>,
211+
network: Network,
212+
) -> Result<Vec<u8>, ProtocolError> {
213+
match self {
214+
KeyType::ECDSA_SECP256K1 => {
215+
let secp = Secp256k1::new();
216+
let secret_key =
217+
dashcore::secp256k1::SecretKey::from_slice(private_key_bytes.as_slice())
218+
.map_err(|e| ProtocolError::Generic(e.to_string()))?;
219+
let private_key = dashcore::PrivateKey::new(secret_key, network);
220+
221+
Ok(private_key.public_key(&secp).to_bytes())
222+
}
223+
KeyType::BLS12_381 => {
224+
let private_key =
225+
bls_signatures::PrivateKey::from_bytes(private_key_bytes.as_slice(), false)
226+
.map_err(|e| ProtocolError::Generic(e.to_string()))?;
227+
let public_key_bytes = private_key
228+
.g1_element()
229+
.expect("expected to get a public key from a bls private key")
230+
.to_bytes()
231+
.to_vec();
232+
Ok(public_key_bytes)
233+
}
234+
KeyType::ECDSA_HASH160 => {
235+
let secp = Secp256k1::new();
236+
let secret_key =
237+
dashcore::secp256k1::SecretKey::from_slice(private_key_bytes.as_slice())
238+
.map_err(|e| ProtocolError::Generic(e.to_string()))?;
239+
let private_key = dashcore::PrivateKey::new(secret_key, network);
240+
241+
Ok(ripemd160_sha256(private_key.public_key(&secp).to_bytes().as_slice()).to_vec())
242+
}
243+
KeyType::EDDSA_25519_HASH160 => {
244+
let key_pair = ed25519_dalek::SigningKey::from_bytes(
245+
&private_key_bytes.as_slice().try_into().map_err(|_| {
246+
ProtocolError::InvalidVectorSizeError(InvalidVectorSizeError::new(
247+
32,
248+
private_key_bytes.len(),
249+
))
250+
})?,
251+
);
252+
Ok(ripemd160_sha256(key_pair.verifying_key().to_bytes().as_slice()).to_vec())
253+
}
254+
KeyType::BIP13_SCRIPT_HASH => {
255+
return Err(ProtocolError::NotSupported(
256+
"Converting a private key to a script hash is not supported".to_string(),
257+
));
258+
}
259+
}
260+
}
261+
207262
#[cfg(feature = "random-public-keys")]
208263
/// Gets the default size of the public key
209264
pub fn random_public_and_private_key_data_v0(&self, rng: &mut StdRng) -> (Vec<u8>, Vec<u8>) {

0 commit comments

Comments
 (0)