Skip to content

Commit

Permalink
IdentityUpdate serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas committed Apr 10, 2024
1 parent 1a3105b commit 9a27de1
Show file tree
Hide file tree
Showing 14 changed files with 5,501 additions and 1,197 deletions.
2 changes: 1 addition & 1 deletion dev/gen_protos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if ! cargo install --list | grep "protoc-gen-prost-crate" > /dev/null; then
fi
fi

if ! buf generate https://github.com/xmtp/proto.git#branch=main,subdir=proto; then
if ! buf generate https://github.com/xmtp/proto.git#branch=nm/prototype-identity-apis,subdir=proto; then
echo "Failed to generate protobuf definitions"
exit 1
fi
Expand Down
1 change: 1 addition & 0 deletions xmtp_id/src/associations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod association_log;
pub mod builder;
mod hashes;
mod member;
mod serialization;
mod signature;
mod state;
#[cfg(test)]
Expand Down
100 changes: 100 additions & 0 deletions xmtp_id/src/associations/serialization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use super::{
unsigned_actions::{
SignatureTextCreator, UnsignedAction, UnsignedAddAssociation,
UnsignedChangeRecoveryAddress, UnsignedCreateInbox, UnsignedIdentityUpdate,
UnsignedRevokeAssociation,
},
IdentityUpdate, MemberIdentifier,
};
use thiserror::Error;
use xmtp_proto::xmtp::identity::associations::{
identity_action::Kind as IdentityActionKind,
member_identifier::Kind as MemberIdentifierKindProto, IdentityAction as IdentityActionProto,
IdentityUpdate as IdentityUpdateProto, MemberIdentifier as MemberIdentifierProto,
};

#[derive(Error, Debug)]
pub enum SerializationError {
#[error("Invalid action")]
InvalidAction,
#[error("Missing action")]
MissingAction,
#[error("Missing member identifier")]
MissingMemberIdentifier,
}

pub fn from_identity_update_proto(
proto: IdentityUpdateProto,
) -> Result<IdentityUpdate, SerializationError> {
let client_timestamp_ns = proto.client_timestamp_ns;
let all_actions: Vec<IdentityActionKind> = proto
.actions
.into_iter()
.map(|action| match action.kind {
Some(action) => Ok(action),
None => Err(SerializationError::MissingAction),
})
.collect()?;
}

fn get_signature_text(
actions: &Vec<IdentityActionKind>,
client_timestamp_ns: u64,
) -> Result<String, SerializationError> {
let unsigned_actions: Vec<UnsignedAction> = actions
.iter()
.map(|action| match action {
IdentityActionKind::Add(add_action) => {
Ok(UnsignedAction::AddAssociation(UnsignedAddAssociation {
inbox_id: add_action.inbox_id,
new_member_identifier: from_member_identifier_proto_option(
add_action.new_member_identifier,
)?,
}))
}
IdentityActionKind::CreateInbox(create_inbox_action) => {
Ok(UnsignedAction::CreateInbox(UnsignedCreateInbox {
nonce: create_inbox_action.nonce as u64,
account_address: create_inbox_action.initial_address,
}))
}
IdentityActionKind::ChangeRecoveryAddress(change_recovery_address_action) => Ok(
UnsignedAction::ChangeRecoveryAddress(UnsignedChangeRecoveryAddress {
inbox_id: change_recovery_address_action.inbox_id,
new_recovery_address: change_recovery_address_action.new_recovery_address,
}),
),
IdentityActionKind::Revoke(revoke_action) => Ok(UnsignedAction::RevokeAssociation(
UnsignedRevokeAssociation {
inbox_id: revoke_action.inbox_id,
revoked_member: from_member_identifier_proto_option(
revoke_action.member_to_revoke,
)?,
},
)),
})
.collect::<Result<Vec<UnsignedAction>, SerializationError>>()?;

let unsigned_update = UnsignedIdentityUpdate::new(client_timestamp_ns, unsigned_actions);

Ok(unsigned_update.signature_text())
}

fn from_member_identifier_proto_option(
proto: Option<MemberIdentifierProto>,
) -> Result<MemberIdentifier, SerializationError> {
match proto {
None => return Err(SerializationError::MissingMemberIdentifier),
Some(identifier_proto) => match identifier_proto.kind {
Some(identifier) => Ok(from_member_identifier_kind_proto(identifier)),
None => Err(SerializationError::MissingMemberIdentifier),
},
}
}

fn from_member_identifier_kind_proto(proto: MemberIdentifierKindProto) -> MemberIdentifier {
match proto {
MemberIdentifierKindProto::Address(address) => address.into(),
MemberIdentifierKindProto::InstallationPublicKey(public_key) => public_key.into(),
}
}
9 changes: 9 additions & 0 deletions xmtp_id/src/associations/unsigned_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ pub struct UnsignedIdentityUpdate {
pub actions: Vec<UnsignedAction>,
}

impl UnsignedIdentityUpdate {
pub fn new(client_timestamp_ns: u64, actions: Vec<UnsignedAction>) -> Self {
UnsignedIdentityUpdate {
client_timestamp_ns,
actions,
}
}
}

impl SignatureTextCreator for UnsignedIdentityUpdate {
fn signature_text(&self) -> String {
let all_signatures = self
Expand Down
4 changes: 3 additions & 1 deletion xmtp_proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ grpc = ["tonic"]
# @@protoc_deletion_point(features)
# This section is automatically generated by protoc-gen-prost-crate.
# Changes in this area may be lost on regeneration.
proto_full = ["xmtp-keystore_api-v1","xmtp-message_api-v1","xmtp-message_contents","xmtp-mls-api-v1","xmtp-mls-database","xmtp-mls-message_contents","xmtp-mls_validation-v1"]
proto_full = ["xmtp-identity-api-v1","xmtp-identity-associations","xmtp-keystore_api-v1","xmtp-message_api-v1","xmtp-message_contents","xmtp-mls-api-v1","xmtp-mls-database","xmtp-mls-message_contents","xmtp-mls_validation-v1"]
"xmtp-identity-api-v1" = ["xmtp-identity-associations"]
"xmtp-identity-associations" = ["xmtp-message_contents"]
"xmtp-keystore_api-v1" = ["xmtp-message_contents"]
"xmtp-message_api-v1" = ["xmtp-message_contents"]
"xmtp-message_contents" = []
Expand Down
16 changes: 16 additions & 0 deletions xmtp_proto/src/gen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// @generated
pub mod xmtp {
pub mod identity {
pub mod api {
#[cfg(feature = "xmtp-identity-api-v1")]
// @@protoc_insertion_point(attribute:xmtp.identity.api.v1)
pub mod v1 {
include!("xmtp.identity.api.v1.rs");
// @@protoc_insertion_point(xmtp.identity.api.v1)
}
}
#[cfg(feature = "xmtp-identity-associations")]
// @@protoc_insertion_point(attribute:xmtp.identity.associations)
pub mod associations {
include!("xmtp.identity.associations.rs");
// @@protoc_insertion_point(xmtp.identity.associations)
}
}
pub mod keystore_api {
#[cfg(feature = "xmtp-keystore_api-v1")]
// @@protoc_insertion_point(attribute:xmtp.keystore_api.v1)
Expand Down
Loading

0 comments on commit 9a27de1

Please sign in to comment.