Skip to content

Commit

Permalink
verification trait
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Mar 28, 2024
1 parent 98c50df commit fcb717d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 76 deletions.
91 changes: 23 additions & 68 deletions xmtp_id/src/credential.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use prost::Message;
use xmtp_mls::{
credential::{GrantMessagingAccessAssociation, LegacyCreateIdentityAssociation},
credential::{Credential, GrantMessagingAccessAssociation, LegacyCreateIdentityAssociation},

Check warning on line 3 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / Test

unused imports: `GrantMessagingAccessAssociation`, `LegacyCreateIdentityAssociation`

Check warning on line 3 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / workspace

unused imports: `GrantMessagingAccessAssociation`, `LegacyCreateIdentityAssociation`

warning: unused imports: `GrantMessagingAccessAssociation`, `LegacyCreateIdentityAssociation` --> xmtp_id/src/credential.rs:3:30 | 3 | credential::{Credential, GrantMessagingAccessAssociation, LegacyCreateIdentityAssociation}, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
types::Address,
};
use xmtp_proto::xmtp::mls::message_contents::MlsCredential as CredentialProto;

#[derive(Debug, Clone)]
pub enum AssociationType {
ExternallyOwned,
SmartContract,
Expand Down Expand Up @@ -40,19 +40,21 @@ impl VerifiedCredential {
}

pub struct VerificationRequest {
expected_account_address: String,
installation_public_key: Vec<u8>,
credential: Vec<u8>,
}

type VerificationResult = Result<VerifiedCredential, VerificationError>;

pub trait Credential {
fn address(&self) -> String;
fn installation_public_key(&self) -> Vec<u8>;
fn created_ns(&self) -> u64;
impl VerificationRequest {
pub fn new(installation_public_key: Vec<u8>, credential: Vec<u8>) -> Self {
Self {
installation_public_key,
credential,
}
}
}

type VerificationResult = Result<VerifiedCredential, VerificationError>;

#[async_trait::async_trait]
pub trait CredentialVerifier {
async fn verify_credential(request: VerificationRequest) -> VerificationResult;
Expand All @@ -68,68 +70,21 @@ pub trait CredentialVerifier {
}
}

impl<'a> Credential for &'a GrantMessagingAccessAssociation {
fn address(&self) -> String {
self.account_address().clone()
}

fn installation_public_key(&self) -> Vec<u8> {
self.installation_public_key().clone()
}

fn created_ns(&self) -> u64 {
GrantMessagingAccessAssociation::created_ns(self)
}
}

fn validate_credential(
credential: impl Credential,
request: VerificationRequest,
) -> Result<(), VerificationError> {
if credential.address() != request.expected_account_address {
return Err(VerificationError::AddressMismatch {
provided_addr: request.expected_account_address.to_string(),
signing_addr: credential.address(),
});
}

if credential.installation_public_key() != request.installation_public_key {
return Err(VerificationError::InstallationPublicKeyMismatch);
}

Ok(())
}

#[async_trait::async_trait]
impl CredentialVerifier for GrantMessagingAccessAssociation {
async fn verify_credential(request: VerificationRequest) -> VerificationResult {
let proto = CredentialProto::decode(request.credential);
let credential = GrantMessagingAccessAssociation::from_proto_validated(
proto,
Some(request.installation_public_key),
);
validate_credential(&credential, request)?;

Ok(VerifiedCredential {
account_address: credential.account_address(),
account_type: AssociationType::EOA,
})
}
}

#[async_trait::async_trait]
impl CredentialVerifier for LegacyCreateIdentityAssociation {
impl CredentialVerifier for Credential {
async fn verify_credential(request: VerificationRequest) -> VerificationResult {
let proto = CredentialProto::decode(request.credential);

Check failure on line 76 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / Test

the trait bound `Vec<u8>: Buf` is not satisfied

Check failure on line 76 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / workspace

the trait bound `std::vec::Vec<u8>: prost::bytes::Buf` is not satisfied

error[E0277]: the trait bound `std::vec::Vec<u8>: prost::bytes::Buf` is not satisfied --> xmtp_id/src/credential.rs:76:45 | 76 | let proto = CredentialProto::decode(request.credential); | ----------------------- ^^^^^^^^^^^^^^^^^^ the trait `prost::bytes::Buf` is not implemented for `std::vec::Vec<u8>` | | | required by a bound introduced by this call | = help: the following other types implement trait `prost::bytes::Buf`: std::boxed::Box<T> prost::bytes::Bytes prost::bytes::BytesMut tungstenite::buffer::ReadBuffer<CHUNK_SIZE> prost::bytes::buf::Chain<T, U> prost::bytes::buf::Take<T> tonic::codec::buffer::DecodeBuf<'_> std::collections::VecDeque<u8> and 3 others note: required by a bound in `prost::Message::decode` --> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/prost-0.12.3/src/message.rs:112:12 | 110 | fn decode<B>(mut buf: B) -> Result<Self, DecodeError> | ------ required by a bound in this associated function 111 | where 112 | B: Buf, | ^^^ required by this bound in `Message::decode`
let credential = LegacyCreateIdentityAssociation::from_proto_validated(
proto,
Some(request.installation_public_key),
);
validate_credential(&credential, request)?;

Ok(VerifiedCredential {
account_address: credential.account_address(),
account_type: AssociationType::Legacy,
let credential =
Credential::from_proto_validated(proto, None, Some(request.installation_public_key));

Check failure on line 78 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / Test

mismatched types

Check failure on line 78 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / Test

mismatched types

Check failure on line 78 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / workspace

mismatched types

error[E0308]: mismatched types --> xmtp_id/src/credential.rs:78:46 | 78 | Credential::from_proto_validated(proto, None, Some(request.installation_public_key)); | -------------------------------- ^^^^^ expected `MlsCredential`, found `Result<MlsCredential, DecodeError>` | | | arguments to this function are incorrect | = note: expected struct `xmtp_proto::xmtp::mls::message_contents::MlsCredential` found enum `std::result::Result<xmtp_proto::xmtp::mls::message_contents::MlsCredential, prost::DecodeError>` note: associated function defined here --> /home/runner/work/libxmtp/libxmtp/xmtp_mls/src/credential/mod.rs:92:12 | 92 | pub fn from_proto_validated( | ^^^^^^^^^^^^^^^^^^^^ help: consider using `Result::expect` to unwrap the `std::result::Result<xmtp_proto::xmtp::mls::message_contents::MlsCredential, prost::DecodeError>` value, panicking if the value is a `Result::Err` | 78 | Credential::from_proto_validated(proto.expect("REASON"), None, Some(request.installation_public_key)); | +++++++++++++++++

Check failure on line 78 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / workspace

mismatched types

error[E0308]: mismatched types --> xmtp_id/src/credential.rs:78:64 | 78 | Credential::from_proto_validated(proto, None, Some(request.installation_public_key)); | ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&[u8]`, found `Vec<u8>` | | | arguments to this enum variant are incorrect | = note: expected reference `&[u8]` found struct `std::vec::Vec<u8>` help: the type constructed contains `std::vec::Vec<u8>` due to the type of the argument passed --> xmtp_id/src/credential.rs:78:59 | 78 | Credential::from_proto_validated(proto, None, Some(request.installation_public_key)); | ^^^^^-------------------------------^ | | | this argument influences the type of `Some` note: tuple variant defined here --> /rustc/7cf61ebde7b22796c69757901dd346d0fe70bd97/library/core/src/option.rs:578:5 help: consider borrowing here | 78 | Credential::from_proto_validated(proto, None, Some(&request.installation_public_key)); | +
Ok(match credential {
Credential::GrantMessagingAccess(cred) => VerifiedCredential {

Check failure on line 80 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / Test

mismatched types

Check failure on line 80 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / workspace

mismatched types

error[E0308]: mismatched types --> xmtp_id/src/credential.rs:80:13 | 79 | Ok(match credential { | ---------- this expression has type `std::result::Result<xmtp_mls::credential::Credential, xmtp_mls::credential::AssociationError>` 80 | Credential::GrantMessagingAccess(cred) => VerifiedCredential { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<Credential, ...>`, found `Credential` | = note: expected enum `std::result::Result<xmtp_mls::credential::Credential, xmtp_mls::credential::AssociationError>` found enum `xmtp_mls::credential::Credential` help: try wrapping the pattern in `Ok` | 80 | Ok(Credential::GrantMessagingAccess(cred)) => VerifiedCredential { | +++ +
account_address: cred.account_address(),
account_type: AssociationType::ExternallyOwned,
},
Credential::LegacyCreateIdentity(cred) => VerifiedCredential {

Check failure on line 84 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / Test

mismatched types

Check failure on line 84 in xmtp_id/src/credential.rs

View workflow job for this annotation

GitHub Actions / workspace

mismatched types

error[E0308]: mismatched types --> xmtp_id/src/credential.rs:84:13 | 79 | Ok(match credential { | ---------- this expression has type `std::result::Result<xmtp_mls::credential::Credential, xmtp_mls::credential::AssociationError>` ... 84 | Credential::LegacyCreateIdentity(cred) => VerifiedCredential { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<Credential, ...>`, found `Credential` | = note: expected enum `std::result::Result<xmtp_mls::credential::Credential, xmtp_mls::credential::AssociationError>` found enum `xmtp_mls::credential::Credential` help: try wrapping the pattern in `Ok` | 84 | Ok(Credential::LegacyCreateIdentity(cred)) => VerifiedCredential { | +++ +
account_address: cred.account_address(),
account_type: AssociationType::Legacy,
},
})
}
}
14 changes: 6 additions & 8 deletions xmtp_id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use xmtp_mls::{
};
use xmtp_proto::xmtp::mls::message_contents::MlsCredential as CredentialProto;

Check warning on line 14 in xmtp_id/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `xmtp_proto::xmtp::mls::message_contents::MlsCredential as CredentialProto`

Check warning on line 14 in xmtp_id/src/lib.rs

View workflow job for this annotation

GitHub Actions / workspace

unused import: `xmtp_proto::xmtp::mls::message_contents::MlsCredential as CredentialProto`

warning: unused import: `xmtp_proto::xmtp::mls::message_contents::MlsCredential as CredentialProto` --> xmtp_id/src/lib.rs:14:5 | 14 | use xmtp_proto::xmtp::mls::message_contents::MlsCredential as CredentialProto; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

use crate::error::IdentityError;
use crate::{
credential::{CredentialVerifier, VerificationRequest, VerifiedCredential},

Check warning on line 17 in xmtp_id/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `VerifiedCredential`

Check warning on line 17 in xmtp_id/src/lib.rs

View workflow job for this annotation

GitHub Actions / workspace

unused import: `VerifiedCredential`

warning: unused import: `VerifiedCredential` --> xmtp_id/src/lib.rs:17:59 | 17 | credential::{CredentialVerifier, VerificationRequest, VerifiedCredential}, | ^^^^^^^^^^^^^^^^^^
error::IdentityError,
};

pub struct Identity {
#[allow(dead_code)]
Expand Down Expand Up @@ -66,13 +69,8 @@ impl Identity {
credential: &[u8],
installation_public_key: &[u8],
) -> Result<String, IdentityError> {
let proto = CredentialProto::decode(credential)?;
let credential = Credential::from_proto_validated(
proto,
None, // expected_account_address
Some(installation_public_key),
)?;

let request = VerificationRequest::new(credential, installation_public_key);

Check failure on line 72 in xmtp_id/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test

arguments to this function are incorrect

Check failure on line 72 in xmtp_id/src/lib.rs

View workflow job for this annotation

GitHub Actions / workspace

arguments to this function are incorrect

error[E0308]: arguments to this function are incorrect --> xmtp_id/src/lib.rs:72:23 | 72 | let request = VerificationRequest::new(credential, installation_public_key); | ^^^^^^^^^^^^^^^^^^^^^^^^ | note: expected `Vec<u8>`, found `&[u8]` --> xmtp_id/src/lib.rs:72:48 | 72 | let request = VerificationRequest::new(credential, installation_public_key); | ^^^^^^^^^^ = note: expected struct `std::vec::Vec<u8>` found reference `&[u8]` note: expected `Vec<u8>`, found `&[u8]` --> xmtp_id/src/lib.rs:72:60 | 72 | let request = VerificationRequest::new(credential, installation_public_key); | ^^^^^^^^^^^^^^^^^^^^^^^ = note: expected struct `std::vec::Vec<u8>` found reference `&[u8]` note: associated function defined here --> xmtp_id/src/credential.rs:48:12 | 48 | pub fn new(installation_public_key: Vec<u8>, credential: Vec<u8>) -> Self { | ^^^ -------------------------------- ------------------- help: try using a conversion method | 72 | let request = VerificationRequest::new(credential.to_vec(), installation_public_key); | +++++++++ help: try using a conversion method | 72 | let request = VerificationRequest::new(credential, installation_public_key.to_vec()); | +++++++++
let credential = CredentialVerifier::verify_credential(request)?;

Check failure on line 73 in xmtp_id/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test

the `?` operator can only be applied to values that implement `Try`

Check failure on line 73 in xmtp_id/src/lib.rs

View workflow job for this annotation

GitHub Actions / workspace

the `?` operator can only be applied to values that implement `std::ops::Try`

error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try` --> xmtp_id/src/lib.rs:73:26 | 73 | let credential = CredentialVerifier::verify_credential(request)?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `std::pin::Pin<std::boxed::Box<dyn futures::Future<Output = std::result::Result<credential::VerifiedCredential, credential::VerificationError>> + std::marker::Send>>` | = help: the trait `std::ops::Try` is not implemented for `Pin<Box<dyn Future<Output = Result<VerifiedCredential, VerificationError>> + Send>>`
Ok(credential.address())
}
}
Expand Down

0 comments on commit fcb717d

Please sign in to comment.