-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathidentity.rs
97 lines (86 loc) · 2.98 KB
/
identity.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::fmt::Display;
use xmtp_cryptography::signature::IdentifierValidationError;
use xmtp_id::associations::{ident, Identifier};
use crate::GenericError;
#[derive(uniffi::Record, Hash, PartialEq, Eq, Clone, Debug)]
pub struct FfiIdentifier {
pub identifier: String,
pub identifier_kind: FfiIdentifierKind,
}
#[derive(uniffi::Enum, Hash, PartialEq, Eq, Clone, Debug)]
pub enum FfiIdentifierKind {
Ethereum,
Passkey,
}
impl FfiIdentifier {
pub fn inbox_id(&self, nonce: u64) -> Result<String, GenericError> {
let ident: Identifier = self.clone().try_into().map_err(GenericError::from_error)?;
Ok(ident.inbox_id(nonce)?)
}
}
impl Display for FfiIdentifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.identifier_kind {
FfiIdentifierKind::Ethereum => write!(f, "{}", self.identifier),
FfiIdentifierKind::Passkey => write!(f, "{}", hex::encode(&self.identifier)),
}
}
}
#[allow(unused)]
#[uniffi::export]
pub fn generate_inbox_id(
account_identifier: FfiIdentifier,
nonce: u64,
) -> Result<String, GenericError> {
account_identifier.inbox_id(nonce)
}
impl From<Identifier> for FfiIdentifier {
fn from(ident: Identifier) -> Self {
match ident {
Identifier::Ethereum(ident::Ethereum(addr)) => Self {
identifier: addr,
identifier_kind: FfiIdentifierKind::Ethereum,
},
Identifier::Passkey(ident::Passkey { key, .. }) => Self {
identifier: hex::encode(key),
identifier_kind: FfiIdentifierKind::Passkey,
},
}
}
}
impl TryFrom<FfiIdentifier> for Identifier {
type Error = IdentifierValidationError;
fn try_from(ident: FfiIdentifier) -> Result<Self, Self::Error> {
let ident = match ident.identifier_kind {
FfiIdentifierKind::Ethereum => Self::eth(ident.identifier)?,
FfiIdentifierKind::Passkey => Self::passkey_str(&ident.identifier, None)?,
};
Ok(ident)
}
}
pub trait IdentityExt<T, U> {
fn to_internal(self) -> Result<Vec<U>, IdentifierValidationError>;
}
impl IdentityExt<FfiIdentifier, Identifier> for Vec<FfiIdentifier> {
fn to_internal(self) -> Result<Vec<Identifier>, IdentifierValidationError> {
let ident: Result<Vec<_>, IdentifierValidationError> =
self.into_iter().map(|ident| ident.try_into()).collect();
ident
}
}
pub trait FfiCollectionExt<T> {
fn to_ffi(self) -> Vec<T>;
}
impl FfiCollectionExt<FfiIdentifier> for Vec<Identifier> {
fn to_ffi(self) -> Vec<FfiIdentifier> {
self.into_iter().map(Into::into).collect()
}
}
pub trait FfiCollectionTryExt<T> {
fn to_internal(self) -> Result<Vec<T>, IdentifierValidationError>;
}
impl FfiCollectionTryExt<Identifier> for Vec<FfiIdentifier> {
fn to_internal(self) -> Result<Vec<Identifier>, IdentifierValidationError> {
self.into_iter().map(|ident| ident.try_into()).collect()
}
}