Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move PublicKey to a separate crate #911

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ wrappers/ios_legacy/vcx/vcx.framework/**
wrappers/ios_legacy/vcx/vcx.framework.dSYM/**
.vscode
**/tails.txt
.session.vim
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"did_resolver_registry",
"did_resolver_sov",
"did_resolver_web",
"public_key",
"indy_ledger_response_parser",
"wallet_migrator",
"tools/simple_message_relay"
Expand Down
1 change: 1 addition & 0 deletions did_peer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ did_parser = { path = "../did_parser" }
did_doc = { path = "../did_doc" }
did_doc_sov = { path = "../did_doc_sov" }
did_resolver = { path = "../did_resolver" }
public_key = { path = "../public_key" }
thiserror = "1.0.40"
regex = "1.8.4"
serde = { version = "1.0.164", features = ["derive"] }
Expand Down
29 changes: 2 additions & 27 deletions did_peer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,20 @@ pub enum DidPeerError {
InvalidNumalgoCharacter(char),
#[error("Unsupported purpose character: {0}")]
UnsupportedPurpose(char),
#[error("Unsupported multicodec descriptor: {0}")]
UnsupportedMulticodecDescriptor(u64),
#[error("Unsupported verification method type: {0}")]
UnsupportedVerificationMethodType(VerificationMethodType),
#[error("Base 64 decoding error")]
Base64DecodingError(#[from] base64::DecodeError),
#[error("Multibase decoding error")]
MultibaseDecodingError(#[from] multibase::Error),
#[error("Varint decoding error")]
VarintDecodingError(#[from] VarintDecodingError),
#[error("JSON error: {0}")]
JsonError(#[from] serde_json::Error),
#[error("Regex error: {0}")]
RegexError(#[from] regex::Error),
#[error("Public key error: {0}")]
PublicKeyError(#[from] public_key::PublicKeyError),
}

impl From<Infallible> for DidPeerError {
fn from(_: Infallible) -> Self {
panic!("Attempted to convert an Infallible error")
}
}

#[derive(Debug, Error)]
pub struct VarintDecodingError(unsigned_varint::decode::Error);

impl std::fmt::Display for VarintDecodingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Varint decoding error: {}", self.0)
}
}

impl From<unsigned_varint::decode::Error> for VarintDecodingError {
fn from(error: unsigned_varint::decode::Error) -> Self {
Self(error)
}
}

impl From<unsigned_varint::decode::Error> for DidPeerError {
fn from(error: unsigned_varint::decode::Error) -> Self {
Self::VarintDecodingError(error.into())
}
}
11 changes: 0 additions & 11 deletions did_peer/src/key/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion did_peer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod error;
pub mod key;
mod numalgos;
pub mod peer_did;
pub mod peer_did_resolver;
6 changes: 4 additions & 2 deletions did_peer/src/numalgos/numalgo2/generate/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use did_doc_sov::extra_fields::ExtraFieldsSov;

use crate::{
error::DidPeerError,
key::get_key_by_verification_method,
numalgos::numalgo2::{purpose::ElementPurpose, service_abbreviated::ServiceAbbreviated},
numalgos::numalgo2::{
purpose::ElementPurpose, service_abbreviated::ServiceAbbreviated,
verification_method::get_key_by_verification_method,
},
};

pub fn append_encoded_key_segments(
Expand Down
1 change: 1 addition & 0 deletions did_peer/src/numalgos/numalgo2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod generate;
mod purpose;
mod resolve;
mod service_abbreviated;
mod verification_method;

pub use generate::generate_numalgo2;
pub use resolve::resolve_numalgo2;
7 changes: 5 additions & 2 deletions did_peer/src/numalgos/numalgo2/resolve/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine};
use did_doc::schema::{did_doc::DidDocumentBuilder, service::Service, types::uri::Uri, utils::OneOrList};
use did_doc_sov::extra_fields::{aip1::ExtraFieldsAIP1, didcommv2::ExtraFieldsDidCommV2, ExtraFieldsSov};
use did_parser::Did;
use public_key::Key;

use crate::{
error::DidPeerError,
key::{get_verification_methods_by_key, Key},
numalgos::numalgo2::{purpose::ElementPurpose, service_abbreviated::ServiceAbbreviated},
numalgos::numalgo2::{
purpose::ElementPurpose, service_abbreviated::ServiceAbbreviated,
verification_method::get_verification_methods_by_key,
},
peer_did_resolver::options::PublicKeyEncoding,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use did_doc::schema::verification_method::{
IncompleteVerificationMethodBuilder, VerificationMethod, VerificationMethodType,
};
use did_parser::{Did, DidUrl};
use public_key::{Key, KeyType};

use crate::{error::DidPeerError, peer_did_resolver::options::PublicKeyEncoding};

use super::{Key, KeyType};

pub fn get_verification_methods_by_key(
key: &Key,
did: &Did,
Expand Down Expand Up @@ -51,7 +50,7 @@ pub fn get_key_by_verification_method(vm: &VerificationMethod) -> Result<Key, Di
}
t @ _ => return Err(DidPeerError::UnsupportedVerificationMethodType(t.to_owned())),
};
Key::new(vm.public_key().key_decoded()?, key_type)
Ok(Key::new(vm.public_key().key_decoded()?, key_type)?)
}

fn build_verification_methods_from_type_and_key(
Expand Down
30 changes: 6 additions & 24 deletions did_peer/tests/resolve_negative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ async fn test_resolve_numalgo_2_invalid_0() {
.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc\
.Vz6666YqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V\
.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0";
assert!(matches!(
resolve_error(peer_did).await,
DidPeerError::UnsupportedMulticodecDescriptor(3)
));
assert!(matches!(resolve_error(peer_did).await, DidPeerError::PublicKeyError(_)));
}

#[test]
Expand All @@ -40,10 +37,7 @@ async fn test_resolve_numalgo_2_invalid_1() {
.Ez7777sY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc\
.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V\
.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0";
assert!(matches!(
resolve_error(peer_did).await,
DidPeerError::UnsupportedMulticodecDescriptor(4)
));
assert!(matches!(resolve_error(peer_did).await, DidPeerError::PublicKeyError(_)));
}

#[test]
Expand Down Expand Up @@ -77,10 +71,7 @@ async fn test_resolve_numalgo_2_invalid_4() {
.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc\
.Vz6MkqRYqQiS\
.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0";
assert!(matches!(
resolve_error(peer_did).await,
DidPeerError::UnsupportedMulticodecDescriptor(12557)
));
assert!(matches!(resolve_error(peer_did).await, DidPeerError::PublicKeyError(_)));
}

#[test]
Expand All @@ -90,10 +81,7 @@ async fn test_resolve_numalgo_2_invalid_5() {
.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V\
.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg\
.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0";
assert!(matches!(
resolve_error(peer_did).await,
DidPeerError::UnsupportedMulticodecDescriptor(10)
));
assert!(matches!(resolve_error(peer_did).await, DidPeerError::PublicKeyError(_)));
}

#[test]
Expand All @@ -103,10 +91,7 @@ async fn test_resolve_numalgo_2_invalid_6() {
.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7Vcccccccccc\
.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg\
.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0";
assert!(matches!(
resolve_error(peer_did).await,
DidPeerError::UnsupportedMulticodecDescriptor(5)
));
assert!(matches!(resolve_error(peer_did).await, DidPeerError::PublicKeyError(_)));
}

#[test]
Expand All @@ -116,8 +101,5 @@ async fn test_resolve_numalgo_2_invalid_7() {
.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V\
.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg\
.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXX0";
assert!(matches!(
resolve_error(peer_did).await,
DidPeerError::UnsupportedMulticodecDescriptor(1)
));
assert!(matches!(resolve_error(peer_did).await, DidPeerError::PublicKeyError(_)));
}
13 changes: 13 additions & 0 deletions public_key/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "public_key"
version = "0.1.0"
edition = "2021"

[dependencies]
thiserror = "1.0.40"
serde = { version = "1.0.164", features = ["derive"] }
serde_json = "1.0.96"
base64 = "0.21.2"
bs58 = "0.5.0"
multibase = "0.9.1"
unsigned-varint = "0.7.1"
34 changes: 34 additions & 0 deletions public_key/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum PublicKeyError {
#[error("Base 64 decoding error")]
Base64DecodingError(#[from] base64::DecodeError),
#[error("Multibase decoding error")]
MultibaseDecodingError(#[from] multibase::Error),
#[error("Varint decoding error")]
VarintDecodingError(#[from] VarintDecodingError),
#[error("Unsupported multicodec descriptor: {0}")]
UnsupportedMulticodecDescriptor(u64),
}

#[derive(Debug, Error)]
pub struct VarintDecodingError(unsigned_varint::decode::Error);

impl std::fmt::Display for VarintDecodingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Varint decoding error: {}", self.0)
}
}

impl From<unsigned_varint::decode::Error> for VarintDecodingError {
fn from(error: unsigned_varint::decode::Error) -> Self {
Self(error)
}
}

impl From<unsigned_varint::decode::Error> for PublicKeyError {
fn from(error: unsigned_varint::decode::Error) -> Self {
Self::VarintDecodingError(error.into())
}
}
6 changes: 3 additions & 3 deletions did_peer/src/key/key.rs → public_key/src/key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::DidPeerError;
use crate::error::PublicKeyError;

use super::KeyType;

Expand All @@ -9,7 +9,7 @@ pub struct Key {
}

impl Key {
pub fn new(key: Vec<u8>, key_type: KeyType) -> Result<Self, DidPeerError> {
pub fn new(key: Vec<u8>, key_type: KeyType) -> Result<Self, PublicKeyError> {
// If the key is a multibase key coming from a verification method, for some reason it is also
// multicodec encoded, so we need to strip that. But should it be?
let key = Self::strip_multicodec_prefix_if_present(key, &key_type);
Expand Down Expand Up @@ -49,7 +49,7 @@ impl Key {
multibase::encode(multibase::Base::Base58Btc, &self.key)
}

pub fn from_fingerprint(fingerprint: &str) -> Result<Self, DidPeerError> {
pub fn from_fingerprint(fingerprint: &str) -> Result<Self, PublicKeyError> {
let (_base, decoded_bytes) = multibase::decode(fingerprint)?;
let (code, remaining_bytes) = unsigned_varint::decode::u64(&decoded_bytes)?;
Ok(Self {
Expand Down
6 changes: 3 additions & 3 deletions did_peer/src/key/key_type.rs → public_key/src/key_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::DidPeerError;
use crate::error::PublicKeyError;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum KeyType {
Expand Down Expand Up @@ -40,7 +40,7 @@ impl From<&KeyType> for u64 {
}

impl TryFrom<u64> for KeyType {
type Error = DidPeerError;
type Error = PublicKeyError;

fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
Expand All @@ -52,7 +52,7 @@ impl TryFrom<u64> for KeyType {
KeyType::C_P256 => Ok(KeyType::P256),
KeyType::C_P384 => Ok(KeyType::P384),
KeyType::C_P521 => Ok(KeyType::P521),
p @ _ => Err(DidPeerError::UnsupportedMulticodecDescriptor(p)),
p @ _ => Err(PublicKeyError::UnsupportedMulticodecDescriptor(p)),
}
}
}
7 changes: 7 additions & 0 deletions public_key/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod error;
mod key;
mod key_type;

pub use error::PublicKeyError;
pub use key::Key;
pub use key_type::KeyType;
Loading