Skip to content

Commit

Permalink
feat: publish verify attestation (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
heilhead authored Jul 30, 2024
1 parent edd6b6b commit d17f94b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async fn main() -> anyhow::Result<()> {
.publish(
topic.clone(),
message.clone(),
None,
1100,
Duration::from_secs(30),
false,
Expand Down
1 change: 1 addition & 0 deletions examples/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ async fn main() -> anyhow::Result<()> {
.publish(
topic.clone(),
message.clone(),
None,
1100,
Duration::from_secs(30),
false,
Expand Down
1 change: 1 addition & 0 deletions examples/websocket_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async fn main() -> anyhow::Result<()> {
.publish(
topic.clone(),
Arc::from("Hello WalletConnect!"),
None,
0,
Duration::from_secs(60),
false,
Expand Down
2 changes: 2 additions & 0 deletions relay_client/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl Client {
&self,
topic: Topic,
message: impl Into<Arc<str>>,
attestation: impl Into<Option<Arc<str>>>,
tag: u32,
ttl: Duration,
prompt: bool,
Expand All @@ -120,6 +121,7 @@ impl Client {
self.request(rpc::Publish {
topic,
message: message.into(),
attestation: attestation.into(),
ttl_secs,
tag,
prompt,
Expand Down
2 changes: 2 additions & 0 deletions relay_client/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,15 @@ impl Client {
&self,
topic: Topic,
message: impl Into<Arc<str>>,
attestation: impl Into<Option<Arc<str>>>,
tag: u32,
ttl: Duration,
prompt: bool,
) -> EmptyResponseFuture<Publish> {
let (request, response) = create_request(Publish {
topic,
message: message.into(),
attestation: attestation.into(),
ttl_secs: ttl.as_secs() as u32,
tag,
prompt,
Expand Down
7 changes: 7 additions & 0 deletions relay_rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,9 @@ pub struct Publish {
/// Message to publish.
pub message: Arc<str>,

#[serde(default, skip_serializing_if = "is_default")]
pub attestation: Option<Arc<str>>,

/// Duration for which the message should be kept in the mailbox if it can't
/// be delivered, in seconds.
#[serde(rename = "ttl")]
Expand Down Expand Up @@ -556,6 +559,7 @@ impl Publish {
data: SubscriptionData {
topic: self.topic.clone(),
message: self.message.clone(),
attestation: self.attestation.clone(),
published_at,
tag: self.tag,
},
Expand Down Expand Up @@ -728,6 +732,9 @@ pub struct SubscriptionData {
/// The message for the subscription.
pub message: Arc<str>,

#[serde(default, skip_serializing_if = "is_default")]
pub attestation: Option<Arc<str>>,

/// Message publish timestamp in UTC milliseconds.
pub published_at: i64,

Expand Down
14 changes: 11 additions & 3 deletions relay_rpc/src/rpc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fn request() {
Params::Publish(Publish {
topic: "topic".into(),
message: "payload".into(),
attestation: Some(Arc::from("attestation_payload")),
ttl_secs: 12,
tag: 0,
prompt: false,
Expand All @@ -17,7 +18,7 @@ fn request() {

assert_eq!(
&serialized,
r#"{"id":1,"jsonrpc":"2.0","method":"irn_publish","params":{"topic":"topic","message":"payload","ttl":12,"tag":0}}"#
r#"{"id":1,"jsonrpc":"2.0","method":"irn_publish","params":{"topic":"topic","message":"payload","attestation":"attestation_payload","ttl":12,"tag":0}}"#
);

let deserialized: Payload = serde_json::from_str(&serialized).unwrap();
Expand Down Expand Up @@ -91,6 +92,7 @@ fn subscription() {
let data = SubscriptionData {
topic: "test_topic".into(),
message: "test_message".into(),
attestation: Some(Arc::from("test_attestation")),
published_at: 123,
tag: 1000,
};
Expand All @@ -104,7 +106,7 @@ fn subscription() {

assert_eq!(
&serialized,
r#"{"id":1,"jsonrpc":"2.0","method":"irn_subscription","params":{"id":"test_id","data":{"topic":"test_topic","message":"test_message","publishedAt":123,"tag":1000}}}"#
r#"{"id":1,"jsonrpc":"2.0","method":"irn_subscription","params":{"id":"test_id","data":{"topic":"test_topic","message":"test_message","attestation":"test_attestation","publishedAt":123,"tag":1000}}}"#
);

let deserialized: Payload = serde_json::from_str(&serialized).unwrap();
Expand All @@ -127,7 +129,6 @@ fn batch_receive() {
));

let serialized = serde_json::to_string(&payload).unwrap();
eprintln!("{serialized}");

assert_eq!(
&serialized,
Expand Down Expand Up @@ -289,6 +290,7 @@ fn validation() {
params: Params::Publish(Publish {
topic: topic.clone(),
message: message.clone(),
attestation: None,
ttl_secs: 0,
tag: 0,
prompt: false,
Expand All @@ -303,6 +305,7 @@ fn validation() {
params: Params::Publish(Publish {
topic: topic.clone(),
message: message.clone(),
attestation: None,
ttl_secs: 0,
tag: 0,
prompt: false,
Expand All @@ -317,6 +320,7 @@ fn validation() {
params: Params::Publish(Publish {
topic: topic.clone(),
message: message.clone(),
attestation: None,
ttl_secs: 0,
tag: 0,
prompt: false,
Expand All @@ -331,6 +335,7 @@ fn validation() {
params: Params::Publish(Publish {
topic: Topic::from("invalid"),
message: message.clone(),
attestation: None,
ttl_secs: 0,
tag: 0,
prompt: false,
Expand Down Expand Up @@ -407,6 +412,7 @@ fn validation() {
data: SubscriptionData {
topic: topic.clone(),
message: message.clone(),
attestation: None,
published_at: 123,
tag: 1000,
},
Expand All @@ -423,6 +429,7 @@ fn validation() {
data: SubscriptionData {
topic: topic.clone(),
message: message.clone(),
attestation: None,
published_at: 123,
tag: 1000,
},
Expand All @@ -439,6 +446,7 @@ fn validation() {
data: SubscriptionData {
topic: Topic::from("invalid"),
message,
attestation: None,
published_at: 123,
tag: 1000,
},
Expand Down
5 changes: 4 additions & 1 deletion relay_rpc/src/rpc/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub struct WatchEventPayload {
pub topic: Topic,
/// The published message.
pub message: Arc<str>,
/// The Verify attestation JWT.
pub attestation: Option<Arc<str>>,
/// Message publishing timestamp.
pub published_at: i64,
/// Message tag.
Expand Down Expand Up @@ -224,6 +226,7 @@ mod test {
status: WatchStatus::Accepted,
topic,
message: Arc::from("test message"),
attestation: Some(Arc::from("test attestation")),
published_at: iat.timestamp(),
tag: 1100,
},
Expand All @@ -233,7 +236,7 @@ mod test {
// lowercase.
assert_eq!(
serde_json::to_string(&claims).unwrap(),
r#"{"iss":"did:key:z6Mku3wsRZTAHjr6xrYWVUfyGeNSNz1GJRVfazp3N76AL9gE","aud":"wss://relay.walletconnect.com","sub":"https://example.com","iat":946684800,"exp":32503680000,"act":"irn_watchEvent","typ":"subscriber","whu":"https://example.com","evt":{"messageId":12345678,"status":"accepted","topic":"474e88153f4db893de42c35e1891dc0e37a02e11961385de0475460fb48b8639","message":"test message","publishedAt":946684800,"tag":1100}}"#
r#"{"iss":"did:key:z6Mku3wsRZTAHjr6xrYWVUfyGeNSNz1GJRVfazp3N76AL9gE","aud":"wss://relay.walletconnect.com","sub":"https://example.com","iat":946684800,"exp":32503680000,"act":"irn_watchEvent","typ":"subscriber","whu":"https://example.com","evt":{"messageId":12345678,"status":"accepted","topic":"474e88153f4db893de42c35e1891dc0e37a02e11961385de0475460fb48b8639","message":"test message","attestation":"test attestation","publishedAt":946684800,"tag":1100}}"#
);

// Verify that the claims can be encoded and decoded correctly.
Expand Down

0 comments on commit d17f94b

Please sign in to comment.