Skip to content

Commit

Permalink
feat: tvf data in publish payload
Browse files Browse the repository at this point in the history
  • Loading branch information
heilhead committed Jan 17, 2025
1 parent 51e984e commit 7ef7422
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions relay_client/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl Client {
ttl_secs,
tag,
prompt,
tvf_data: None,
})
.await
.map(|_| ())
Expand Down
23 changes: 23 additions & 0 deletions relay_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ pub struct ConnectionOptions {
/// Optional origin of the request. Subject to allow-list validation.
pub origin: Option<String>,

/// Optional package name. Used instead of `origin` for allow-list
/// validation.
pub package_name: Option<String>,

/// Optional bundle ID. Used instead of `origin` for allow-list validation.
pub bundle_id: Option<String>,

/// Optional user agent parameters.
pub user_agent: Option<UserAgent>,
}
Expand All @@ -60,6 +67,8 @@ impl ConnectionOptions {
auth: Authorization::Query(auth),
origin: None,
user_agent: None,
package_name: None,
bundle_id: None,
}
}

Expand All @@ -68,6 +77,16 @@ impl ConnectionOptions {
self
}

pub fn with_package_name(mut self, package_name: impl Into<String>) -> Self {
self.package_name = Some(package_name.into());
self
}

pub fn with_bundle_id(mut self, bundle_id: impl Into<String>) -> Self {
self.bundle_id = Some(bundle_id.into());
self
}

pub fn with_origin(mut self, origin: impl Into<Option<String>>) -> Self {
self.origin = origin.into();
self
Expand All @@ -85,6 +104,8 @@ impl ConnectionOptions {
project_id: &'a ProjectId,
auth: Option<&'a SerializedAuthToken>,
ua: Option<&'a UserAgent>,
package_name: Option<&'a str>,
bundle_id: Option<&'a str>,
}

let query = serde_qs::to_string(&QueryParams {
Expand All @@ -95,6 +116,8 @@ impl ConnectionOptions {
None
},
ua: self.user_agent.as_ref(),
package_name: self.package_name.as_deref(),
bundle_id: self.bundle_id.as_deref(),
})
.map_err(RequestBuildError::Query)?;

Expand Down
1 change: 1 addition & 0 deletions relay_client/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl Client {
ttl_secs: ttl.as_secs() as u32,
tag,
prompt,
tvf_data: None,
});

self.request(request);
Expand Down
2 changes: 1 addition & 1 deletion relay_rpc/src/auth/cacao/signature/eip191.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub const EIP191: &str = "eip191";
pub fn eip191_bytes(message: &str) -> Vec<u8> {
format!(
"\u{0019}Ethereum Signed Message:\n{}{}",
message.as_bytes().len(),
message.len(),
message
)
.into()
Expand Down
2 changes: 1 addition & 1 deletion relay_rpc/src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Default for JwtHeader<'_> {
}
}

impl<'a> JwtHeader<'a> {
impl JwtHeader<'_> {
pub fn is_valid(&self) -> bool {
self.typ == JWT_HEADER_TYP && self.alg == JWT_HEADER_ALG
}
Expand Down
25 changes: 25 additions & 0 deletions relay_rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ impl Payload {
Self::Response(response) => response.validate(),
}
}

pub fn strip_analytics(&mut self) {
if let Self::Request(req) = self {
req.strip_analytics();
}
}
}

impl<T> From<T> for Payload
Expand Down Expand Up @@ -520,6 +526,16 @@ impl ServiceRequest for BatchReceiveMessages {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TvfData {
pub correlation_id: Option<Arc<str>>,
pub chain_id: Option<Arc<str>>,
pub rpc_methods: Option<Vec<Arc<str>>>,
pub tx_hashes: Option<Vec<Arc<str>>>,
pub contract_addresses: Option<Vec<Arc<str>>>,
}

/// Data structure representing publish request params.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Publish {
Expand All @@ -545,6 +561,9 @@ pub struct Publish {
/// webhook to a client through a push server.
#[serde(default, skip_serializing_if = "is_default")]
pub prompt: bool,

#[serde(default, flatten, skip_serializing_if = "is_default")]
pub tvf_data: Option<TvfData>,
}

impl Publish {
Expand Down Expand Up @@ -860,4 +879,10 @@ impl Request {
Params::Subscription(params) => params.validate(),
}
}

pub fn strip_analytics(&mut self) {
if let Params::Publish(params) = &mut self.params {
params.tvf_data = None;
}
}
}
13 changes: 12 additions & 1 deletion relay_rpc/src/rpc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ fn request() {
ttl_secs: 12,
tag: 0,
prompt: false,
tvf_data: Some(TvfData {
correlation_id: Some("correlation_id".into()),
chain_id: Some("chain_id".into()),
rpc_methods: Some(vec!["rpc_method".into()]),
tx_hashes: Some(vec!["tx_hash".into()]),
contract_addresses: Some(vec!["contract_address".into()]),
}),
}),
));

let serialized = serde_json::to_string(&payload).unwrap();

assert_eq!(
&serialized,
r#"{"id":1,"jsonrpc":"2.0","method":"irn_publish","params":{"topic":"topic","message":"payload","attestation":"attestation_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,"correlationId":"correlation_id","chainId":"chain_id","rpcMethods":["rpc_method"],"txHashes":["tx_hash"],"contractAddresses":["contract_address"]}}"#
);

let deserialized: Payload = serde_json::from_str(&serialized).unwrap();
Expand Down Expand Up @@ -294,6 +301,7 @@ fn validation() {
ttl_secs: 0,
tag: 0,
prompt: false,
tvf_data: None,
}),
};
assert_eq!(request.validate(), Err(PayloadError::InvalidRequestId));
Expand All @@ -309,6 +317,7 @@ fn validation() {
ttl_secs: 0,
tag: 0,
prompt: false,
tvf_data: None,
}),
};
assert_eq!(request.validate(), Err(PayloadError::InvalidJsonRpcVersion));
Expand All @@ -324,6 +333,7 @@ fn validation() {
ttl_secs: 0,
tag: 0,
prompt: false,
tvf_data: None,
}),
};
assert_eq!(request.validate(), Ok(()));
Expand All @@ -339,6 +349,7 @@ fn validation() {
ttl_secs: 0,
tag: 0,
prompt: false,
tvf_data: None,
}),
};
assert_eq!(request.validate(), Err(PayloadError::InvalidTopic));
Expand Down

0 comments on commit 7ef7422

Please sign in to comment.