Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 3 additions & 31 deletions relay_rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ pub const JSON_RPC_VERSION_STR: &str = "2.0";
pub static JSON_RPC_VERSION: once_cell::sync::Lazy<Arc<str>> =
once_cell::sync::Lazy::new(|| Arc::from(JSON_RPC_VERSION_STR));

/// The maximum message length in bytes.
///
/// See <https://github.com/WalletConnect/walletconnect-docs/blob/main/docs/specs/servers/relay/relay-server-rpc.md>
pub const MAX_MESSAGE_LENGTH: usize = 20000;

/// The maximum number of topics allowed for a batch subscribe request.
///
/// See <https://github.com/WalletConnect/walletconnect-docs/blob/main/docs/specs/servers/relay/relay-server-rpc.md>
Expand All @@ -40,12 +35,6 @@ pub enum ValidationError {
#[error("Invalid JSON RPC version")]
JsonRpcVersion,

#[error(
"Message is too long. Maximum message length is {} characters",
MAX_MESSAGE_LENGTH
)]
MessageLength,

#[error(
"The batch contains too many items. Maximum number of subscriptions is {}",
MAX_SUBSCRIPTION_BATCH_SIZE
Expand Down Expand Up @@ -258,16 +247,7 @@ impl ErrorResponse {
/// Validates the parameters.
pub fn validate(&self) -> Result<(), ValidationError> {
if self.jsonrpc.as_ref() != JSON_RPC_VERSION_STR {
return Err(ValidationError::JsonRpcVersion);
}

let data_len = self.error.data.as_deref().map(str::len).unwrap_or(0);
let total_len = data_len + self.error.message.len();

// Make sure the combined length of error message and the optional `data` param
// do not exceed the `MAX_MESSAGE_LENGTH` limit.
if total_len > MAX_MESSAGE_LENGTH {
Err(ValidationError::MessageLength)
Err(ValidationError::JsonRpcVersion)
} else {
Ok(())
}
Expand Down Expand Up @@ -487,11 +467,7 @@ impl RequestPayload for Publish {
.decode()
.map_err(ValidationError::TopicDecoding)?;

if self.message.len() > MAX_MESSAGE_LENGTH {
Err(ValidationError::MessageLength)
} else {
Ok(())
}
Ok(())
}

fn into_params(self) -> Params {
Expand Down Expand Up @@ -530,11 +506,7 @@ impl RequestPayload for Subscription {
.decode()
.map_err(ValidationError::TopicDecoding)?;

if self.data.message.len() > MAX_MESSAGE_LENGTH {
Err(ValidationError::MessageLength)
} else {
Ok(())
}
Ok(())
}

fn into_params(self) -> Params {
Expand Down
31 changes: 1 addition & 30 deletions relay_rpc/src/rpc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fn validation() {
// Valid data.
let id = MessageId::from(1);
let jsonrpc: Arc<str> = "2.0".into();
let message: Arc<str> = "0".repeat(MAX_MESSAGE_LENGTH).into();
let message: Arc<str> = "0".repeat(512).into();
let topic = Topic::from("c4163cf65859106b3f5435fc296e7765411178ed452d1c30337a6230138c9840");
let subscription_id =
SubscriptionId::from("c4163cf65859106b3f5435fc296e7765411178ed452d1c30337a6230138c9841");
Expand Down Expand Up @@ -245,20 +245,6 @@ fn validation() {
Err(ValidationError::TopicDecoding(DecodingError::Length))
);

// Publish: invalid message.
let request = Request {
id,
jsonrpc: jsonrpc.clone(),
params: Params::Publish(Publish {
topic: topic.clone(),
message: "0".repeat(MAX_MESSAGE_LENGTH + 1).into(),
ttl_secs: 0,
tag: 0,
prompt: false,
}),
};
assert_eq!(request.validate(), Err(ValidationError::MessageLength));

// Subscribe: valid.
let request = Request {
id,
Expand Down Expand Up @@ -360,21 +346,6 @@ fn validation() {
Err(ValidationError::TopicDecoding(DecodingError::Length))
);

// Subscription: invalid message.
let request = Request {
id,
jsonrpc: jsonrpc.clone(),
params: Params::Subscription(Subscription {
id: subscription_id.clone(),
data: SubscriptionData {
topic: topic.clone(),
message: "0".repeat(MAX_MESSAGE_LENGTH + 1).into(),
published_at: 123,
},
}),
};
assert_eq!(request.validate(), Err(ValidationError::MessageLength));

// Batch subscription: valid.
let request = Request {
id,
Expand Down