-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathinbox_owner.rs
59 lines (51 loc) · 1.74 KB
/
inbox_owner.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crate::identity::FfiPublicIdentifier;
use xmtp_cryptography::signature::{
IdentifierValidationError, RecoverableSignature, SignatureError,
};
use xmtp_id::associations::PublicIdentifier;
// TODO proper error handling
#[derive(uniffi::Error, Debug, thiserror::Error)]
pub enum SigningError {
#[error("This is a generic error")]
Generic,
}
#[derive(uniffi::Error, Debug, thiserror::Error)]
pub enum IdentityValidationError {
#[error("Error: {0:?}")]
Generic(String),
}
impl From<uniffi::UnexpectedUniFFICallbackError> for SigningError {
fn from(_: uniffi::UnexpectedUniFFICallbackError) -> Self {
Self::Generic
}
}
// A simplified InboxOwner passed to Rust across the FFI boundary
#[uniffi::export(with_foreign)]
pub trait FfiInboxOwner: Send + Sync {
fn get_identifier(&self) -> Result<FfiPublicIdentifier, IdentityValidationError>;
fn sign(&self, text: String) -> Result<Vec<u8>, SigningError>;
}
pub struct RustInboxOwner {
ffi_inbox_owner: Box<dyn FfiInboxOwner>,
}
impl RustInboxOwner {
pub fn new(ffi_inbox_owner: Box<dyn FfiInboxOwner>) -> Self {
Self { ffi_inbox_owner }
}
}
impl xmtp_mls::InboxOwner for RustInboxOwner {
fn get_identifier(&self) -> Result<PublicIdentifier, IdentifierValidationError> {
let ident = self
.ffi_inbox_owner
.get_identifier()
.map_err(|err| IdentifierValidationError::Generic(err.to_string()))?;
ident.try_into()
}
fn sign(&self, text: &str) -> Result<RecoverableSignature, SignatureError> {
let bytes = self
.ffi_inbox_owner
.sign(text.to_string())
.map_err(|_flat_err| SignatureError::Unknown)?;
Ok(RecoverableSignature::Eip191Signature(bytes))
}
}