2022-07-23 20:21:23 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::activitypub::vocabulary::{
|
|
|
|
IDENTITY_PROOF,
|
|
|
|
LINK,
|
|
|
|
PROPERTY_VALUE,
|
|
|
|
};
|
2022-07-23 22:02:47 +00:00
|
|
|
use crate::errors::ValidationError;
|
|
|
|
use crate::ethereum::identity::{
|
|
|
|
ETHEREUM_EIP191_PROOF,
|
2022-11-08 20:33:28 +00:00
|
|
|
verify_eip191_identity_proof,
|
2022-07-23 22:02:47 +00:00
|
|
|
};
|
2022-07-23 20:21:23 +00:00
|
|
|
use crate::frontend::get_subscription_page_url;
|
2022-11-08 21:59:45 +00:00
|
|
|
use crate::identity::did::Did;
|
2022-07-23 20:21:23 +00:00
|
|
|
use crate::models::profiles::types::{
|
|
|
|
ExtraField,
|
|
|
|
IdentityProof,
|
2022-08-25 00:09:37 +00:00
|
|
|
PaymentLink,
|
2022-07-23 20:21:23 +00:00
|
|
|
PaymentOption,
|
|
|
|
};
|
2022-07-23 22:02:47 +00:00
|
|
|
use super::types::ActorAttachment;
|
|
|
|
|
|
|
|
pub fn attach_identity_proof(
|
|
|
|
proof: IdentityProof,
|
|
|
|
) -> ActorAttachment {
|
|
|
|
ActorAttachment {
|
|
|
|
object_type: IDENTITY_PROOF.to_string(),
|
|
|
|
name: proof.issuer.to_string(),
|
|
|
|
value: None,
|
2022-07-23 20:21:23 +00:00
|
|
|
href: None,
|
2022-07-23 22:02:47 +00:00
|
|
|
signature_algorithm: Some(proof.proof_type),
|
|
|
|
signature_value: Some(proof.value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_identity_proof(
|
|
|
|
actor_id: &str,
|
|
|
|
attachment: &ActorAttachment,
|
|
|
|
) -> Result<IdentityProof, ValidationError> {
|
|
|
|
if attachment.object_type != IDENTITY_PROOF {
|
|
|
|
return Err(ValidationError("invalid attachment type"));
|
|
|
|
};
|
|
|
|
let proof_type = attachment.signature_algorithm.as_ref()
|
|
|
|
.ok_or(ValidationError("missing proof type"))?;
|
|
|
|
if proof_type != ETHEREUM_EIP191_PROOF {
|
|
|
|
return Err(ValidationError("unknown proof type"));
|
|
|
|
};
|
2022-11-08 21:59:45 +00:00
|
|
|
let did = attachment.name.parse::<Did>()
|
2022-07-23 22:02:47 +00:00
|
|
|
.map_err(|_| ValidationError("invalid did"))?;
|
2022-11-09 01:51:44 +00:00
|
|
|
let did_pkh = match did {
|
|
|
|
Did::Pkh(ref did_pkh) => did_pkh,
|
|
|
|
_ => return Err(ValidationError("invalid proof issuer")),
|
|
|
|
};
|
2022-07-23 22:02:47 +00:00
|
|
|
let signature = attachment.signature_value.as_ref()
|
|
|
|
.ok_or(ValidationError("missing signature"))?;
|
2022-11-08 20:33:28 +00:00
|
|
|
verify_eip191_identity_proof(
|
2022-07-23 22:02:47 +00:00
|
|
|
actor_id,
|
2022-11-08 21:59:45 +00:00
|
|
|
did_pkh,
|
2022-07-23 22:02:47 +00:00
|
|
|
signature,
|
|
|
|
).map_err(|_| ValidationError("invalid identity proof"))?;
|
|
|
|
let proof = IdentityProof {
|
|
|
|
issuer: did,
|
|
|
|
proof_type: proof_type.to_string(),
|
|
|
|
value: signature.to_string(),
|
|
|
|
};
|
|
|
|
Ok(proof)
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:21:23 +00:00
|
|
|
pub fn attach_payment_option(
|
|
|
|
instance_url: &str,
|
|
|
|
user_id: &Uuid,
|
|
|
|
payment_option: PaymentOption,
|
|
|
|
) -> ActorAttachment {
|
2022-08-25 21:51:18 +00:00
|
|
|
let (name, href) = match payment_option {
|
2022-08-29 19:17:16 +00:00
|
|
|
// Local actors can't have payment links
|
2022-08-25 00:09:37 +00:00
|
|
|
PaymentOption::Link(_) => unimplemented!(),
|
2022-08-29 19:17:16 +00:00
|
|
|
PaymentOption::EthereumSubscription(_) => {
|
2022-08-25 00:09:37 +00:00
|
|
|
let name = "EthereumSubscription".to_string();
|
2022-08-25 21:51:18 +00:00
|
|
|
let href = get_subscription_page_url(instance_url, user_id);
|
|
|
|
(name, href)
|
2022-07-23 20:21:23 +00:00
|
|
|
},
|
2022-08-25 21:51:18 +00:00
|
|
|
PaymentOption::MoneroSubscription(_) => {
|
|
|
|
let name = "MoneroSubscription".to_string();
|
|
|
|
let href = get_subscription_page_url(instance_url, user_id);
|
|
|
|
(name, href)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
ActorAttachment {
|
|
|
|
object_type: LINK.to_string(),
|
|
|
|
name: name,
|
|
|
|
value: None,
|
|
|
|
href: Some(href),
|
|
|
|
signature_algorithm: None,
|
|
|
|
signature_value: None,
|
2022-07-23 20:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_payment_option(
|
|
|
|
attachment: &ActorAttachment,
|
|
|
|
) -> Result<PaymentOption, ValidationError> {
|
|
|
|
if attachment.object_type != LINK {
|
|
|
|
return Err(ValidationError("invalid attachment type"));
|
|
|
|
};
|
2022-08-25 00:09:37 +00:00
|
|
|
let href = attachment.href.as_ref()
|
|
|
|
.ok_or(ValidationError("href attribute is required"))?
|
|
|
|
.to_string();
|
|
|
|
let payment_option = PaymentOption::Link(PaymentLink {
|
|
|
|
name: attachment.name.clone(),
|
|
|
|
href: href,
|
|
|
|
});
|
2022-07-23 20:21:23 +00:00
|
|
|
Ok(payment_option)
|
|
|
|
}
|
|
|
|
|
2022-07-23 22:02:47 +00:00
|
|
|
pub fn attach_extra_field(
|
|
|
|
field: ExtraField,
|
|
|
|
) -> ActorAttachment {
|
|
|
|
ActorAttachment {
|
|
|
|
object_type: PROPERTY_VALUE.to_string(),
|
|
|
|
name: field.name,
|
|
|
|
value: Some(field.value),
|
2022-07-23 20:21:23 +00:00
|
|
|
href: None,
|
2022-07-23 22:02:47 +00:00
|
|
|
signature_algorithm: None,
|
|
|
|
signature_value: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_extra_field(
|
|
|
|
attachment: &ActorAttachment,
|
|
|
|
) -> Result<ExtraField, ValidationError> {
|
|
|
|
if attachment.object_type != PROPERTY_VALUE {
|
|
|
|
return Err(ValidationError("invalid attachment type"));
|
|
|
|
};
|
|
|
|
let property_value = attachment.value.as_ref()
|
|
|
|
.ok_or(ValidationError("missing property value"))?;
|
|
|
|
let field = ExtraField {
|
|
|
|
name: attachment.name.clone(),
|
|
|
|
value: property_value.to_string(),
|
|
|
|
value_source: None,
|
|
|
|
};
|
|
|
|
Ok(field)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-08-29 19:17:16 +00:00
|
|
|
use crate::utils::caip2::ChainId;
|
2022-07-23 20:21:23 +00:00
|
|
|
use crate::utils::id::new_uuid;
|
2022-07-23 22:02:47 +00:00
|
|
|
use super::*;
|
|
|
|
|
2022-07-23 20:21:23 +00:00
|
|
|
const INSTANCE_URL: &str = "https://example.com";
|
|
|
|
|
2022-07-23 22:02:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_extra_field() {
|
|
|
|
let field = ExtraField {
|
|
|
|
name: "test".to_string(),
|
|
|
|
value: "value".to_string(),
|
|
|
|
value_source: None,
|
|
|
|
};
|
|
|
|
let attachment = attach_extra_field(field.clone());
|
|
|
|
assert_eq!(attachment.object_type, PROPERTY_VALUE);
|
2022-07-23 20:21:23 +00:00
|
|
|
|
2022-07-23 22:02:47 +00:00
|
|
|
let parsed_field = parse_extra_field(&attachment).unwrap();
|
|
|
|
assert_eq!(parsed_field.name, field.name);
|
|
|
|
assert_eq!(parsed_field.value, field.value);
|
|
|
|
}
|
2022-07-23 20:21:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_payment_option() {
|
|
|
|
let user_id = new_uuid();
|
2022-08-29 19:17:16 +00:00
|
|
|
let payment_option =
|
|
|
|
PaymentOption::ethereum_subscription(ChainId::ethereum_mainnet());
|
2022-07-23 20:21:23 +00:00
|
|
|
let subscription_page_url =
|
|
|
|
format!("https://example.com/profile/{}/subscription", user_id);
|
|
|
|
let attachment = attach_payment_option(
|
|
|
|
INSTANCE_URL,
|
|
|
|
&user_id,
|
|
|
|
payment_option,
|
|
|
|
);
|
|
|
|
assert_eq!(attachment.object_type, LINK);
|
|
|
|
assert_eq!(attachment.name, "EthereumSubscription");
|
|
|
|
assert_eq!(attachment.href.as_deref().unwrap(), subscription_page_url);
|
|
|
|
|
|
|
|
let parsed_option = parse_payment_option(&attachment).unwrap();
|
2022-08-25 00:09:37 +00:00
|
|
|
let link = match parsed_option {
|
|
|
|
PaymentOption::Link(link) => link,
|
|
|
|
_ => panic!("wrong option"),
|
|
|
|
};
|
|
|
|
assert_eq!(link.name, "EthereumSubscription");
|
|
|
|
assert_eq!(link.href, subscription_page_url);
|
2022-07-23 20:21:23 +00:00
|
|
|
}
|
2022-07-23 22:02:47 +00:00
|
|
|
}
|