Skip to content

Commit

Permalink
[fix] Multibase public key decoding incorrectly (#1279)
Browse files Browse the repository at this point in the history
* impl fix and remove misleading api

Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>

* fix up numalgo 2 tests

Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>

---------

Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>
Co-authored-by: George Mulhearn <gmulhearn@anonyome.com>
  • Loading branch information
gmulhearn and gmulhearn-anonyome committed Aug 9, 2024
1 parent 1837081 commit 7d82f51
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
7 changes: 0 additions & 7 deletions did_core/did_doc/src/schema/types/jsonwebkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ impl JsonWebKey {
source: Box::new(err),
})
}

pub fn to_vec(&self) -> Result<Vec<u8>, JsonWebKeyError> {
serde_json::to_vec(self).map_err(|err| JsonWebKeyError {
reason: "Serializing JWK to vector failed",
source: Box::new(err),
})
}
}

impl FromStr for JsonWebKey {
Expand Down
9 changes: 9 additions & 0 deletions did_core/did_doc/src/schema/verification_method/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,12 @@ impl From<JsonWebKeyError> for KeyDecodingError {
}
}
}

impl From<public_key::PublicKeyError> for KeyDecodingError {
fn from(error: public_key::PublicKeyError) -> Self {
KeyDecodingError {
reason: "Failed to decode multibase public key",
source: Some(Box::new(error)),
}
}
}
28 changes: 13 additions & 15 deletions did_core/did_doc/src/schema/verification_method/public_key.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::str::FromStr;

use base64::{engine::general_purpose, Engine};
use public_key::Key;
use serde::{Deserialize, Serialize};

use crate::schema::{
types::{jsonwebkey::JsonWebKey, multibase::Multibase},
verification_method::error::KeyDecodingError,
};
use crate::schema::{types::jsonwebkey::JsonWebKey, verification_method::error::KeyDecodingError};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
Expand Down Expand Up @@ -34,10 +30,9 @@ impl PublicKeyField {
PublicKeyField::Multibase {
public_key_multibase,
} => {
let multibase = Multibase::from_str(public_key_multibase)?;
Ok(multibase.as_ref().to_vec())
let key = Key::from_fingerprint(public_key_multibase)?;
Ok(key.key().to_vec())
}
PublicKeyField::Jwk { public_key_jwk } => Ok(public_key_jwk.to_vec()?),
PublicKeyField::Base58 { public_key_base58 } => {
Ok(bs58::decode(public_key_base58).into_vec()?)
}
Expand All @@ -48,6 +43,9 @@ impl PublicKeyField {
PublicKeyField::Pem { public_key_pem } => {
Ok(pem::parse(public_key_pem.as_bytes())?.contents().to_vec())
}
PublicKeyField::Jwk { public_key_jwk: _ } => Err(KeyDecodingError::new(
"JWK public key decoding not supported",
)),
PublicKeyField::Pgp { public_key_pgp: _ } => Err(KeyDecodingError::new(
"PGP public key decoding not supported",
)),
Expand All @@ -67,13 +65,13 @@ mod tests {
use super::*;

static PUBLIC_KEY_MULTIBASE: &str = "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc";
static PUBLIC_KEY_BASE58: &str = "6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc";
static PUBLIC_KEY_BASE64: &str = "7AEEiIVxASfd1+8HamOWE5BCi6vqNfL13mzYUoQk1M4mKQ";
static PUBLIC_KEY_BASE58: &str = "JhNWeSVLMYccCk7iopQW4guaSJTojqpMEELgSLhKwRr";
static PUBLIC_KEY_BASE64: &str = "BIiFcQEn3dfvB2pjlhOQQour6jXy9d5s2FKEJNTOJik";
static PUBLIC_KEY_HEX: &str =
"ec01048885710127ddd7ef076a63961390428babea35f2f5de6cd8528424d4ce2629";
static PUBLIC_KEY_BYTES: [u8; 34] = [
236, 1, 4, 136, 133, 113, 1, 39, 221, 215, 239, 7, 106, 99, 150, 19, 144, 66, 139, 171,
234, 53, 242, 245, 222, 108, 216, 82, 132, 36, 212, 206, 38, 41,
"048885710127ddd7ef076a63961390428babea35f2f5de6cd8528424d4ce2629";
static PUBLIC_KEY_BYTES: [u8; 32] = [
4, 136, 133, 113, 1, 39, 221, 215, 239, 7, 106, 99, 150, 19, 144, 66, 139, 171, 234, 53,
242, 245, 222, 108, 216, 82, 132, 36, 212, 206, 38, 41,
];

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,12 @@ mod tests {
)
.unwrap();
assert_eq!(vms.len(), 1);
assert_eq!(
vms[0].public_key_field().key_decoded().unwrap(),
key.multicodec_prefixed_key()
);
assert_ne!(vms[0].public_key_field().key_decoded().unwrap(), key.key());
let vm = &vms[0];
assert!(matches!(
vm.public_key_field(),
PublicKeyField::Multibase { .. }
));
assert_eq!(vm.public_key_field().key_decoded().unwrap(), key.key());
}

// ... and base58 encoded keys are not
Expand All @@ -211,11 +212,12 @@ mod tests {
)
.unwrap();
assert_eq!(vms.len(), 1);
assert_ne!(
vms[0].public_key_field().key_decoded().unwrap(),
key.multicodec_prefixed_key()
);
assert_eq!(vms[0].public_key_field().key_decoded().unwrap(), key.key());
let vm = &vms[0];
assert!(matches!(
vm.public_key_field(),
PublicKeyField::Base58 { .. }
));
assert_eq!(vm.public_key_field().key_decoded().unwrap(), key.key());
}

#[test]
Expand Down

0 comments on commit 7d82f51

Please sign in to comment.