Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Msg filter #872

Draft
wants to merge 7 commits into
base: development
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ cw4-group = "1.1"
cw721 = "0.18"
cw721-base = "0.18"
env_logger = "0.10"
itertools = "0.13.0"
once_cell = "1.18"
omniflix-std = "0.1.8"
osmosis-std = "0.20.1"
Expand Down Expand Up @@ -89,6 +90,7 @@ cw-admin-factory = { path = "./contracts/external/cw-admin-factory", version = "
cw-denom = { path = "./packages/cw-denom", version = "2.5.0" }
cw-fund-distributor = { path = "./contracts/distribution/cw-fund-distributor", version = "2.5.0" }
cw-hooks = { path = "./packages/cw-hooks", version = "2.5.0" }
cw-msg-filter = { path = "./packages/cw-msg-filter", version = "2.5.0" }
cw-paginate-storage = { path = "./packages/cw-paginate-storage", version = "2.5.0" }
cw-payroll-factory = { path = "./contracts/external/cw-payroll-factory", version = "2.5.0" }
cw-stake-tracker = { path = "./packages/cw-stake-tracker", version = "2.5.0" }
Expand Down
20 changes: 20 additions & 0 deletions packages/cw-msg-filter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "cw-msg-filter"
authors = ["Jake Hartnell <[email protected]>"]
description = "A package to filter CosmWasm messages."
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
version = { workspace = true }

[dependencies]
cosmwasm-std = { workspace = true, features = ["staking"] }
cosmwasm-schema = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
cw20 = { workspace = true }
itertools = { workspace = true }

[dev-dependencies]
cw20-base = { workspace = true }
cw-multi-test = { workspace = true }
116 changes: 116 additions & 0 deletions packages/cw-msg-filter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# CosmWasm Message Filter

## Overview

The CosmWasm Message Filter is a Rust library designed to filter and validate CosmWasm messages based on configurable criteria. It provides a flexible and robust way to ensure that only allowed messages are processed, enforcing spending limits and message count restrictions.

## Features

- Filter messages based on exact match, generic Wasm execute criteria, or message type
- Enforce maximum message count
- Apply spending limits across multiple denominations
- Detailed error reporting for invalid messages or exceeded limits

## Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
cw-msg-filter = "2.5.0"

```

## Usage

Here's a basic example of how to use the `MsgFilter`:

```rust
use cw_msg_filter::{MsgFilter, AllowedMsg, MsgType};
use cosmwasm_std::{CosmosMsg, coins};

let filter = MsgFilter {
allowed_msgs: Some(vec![
AllowedMsg::Type(MsgType::BankSend),
AllowedMsg::GenericWasmExecuteMsg {
contract: Some("allowed_contract".to_string()),
key: Some("action".to_string()),
funds: Some(coins(100, "utoken")),
},
]),
max_msg_count: Some(5),
spending_limits: Some(coins(1000, "utoken")),
};

// Validate the configuration
match filter.validate_config() {
Ok(()) => println!("Configuration is valid"),
Err(e) => println!("Invalid configuration: {}", e),
}

let messages: Vec<CosmosMsg> = vec![
// Your messages here
];

// Check if messages are allowed
match filter.check_messages(&messages) {
Ok(()) => println!("All messages are valid!"),
Err(e) => println!("Error: {}", e),
}

// Filter messages
match filter.filter_messages(&messages) {
Ok(filtered) => println!("Filtered messages: {:?}", filtered),
Err(e) => println!("Error while filtering: {}", e),
}
```

## API

### `MsgFilter`

The main struct for configuring message filtering rules.

#### Fields:

- `allowed_msgs`: Optional `Vec<AllowedMsg>` specifying which messages are permitted.
- `max_msg_count`: Optional `u8` setting the maximum number of messages allowed.
- `spending_limits`: Optional `Vec<Coin>` specifying spending limits per denomination.

#### Methods:

- `validate_config(&self) -> Result<(), ConfigValidationError>`: Validates the configuration of the `MsgFilter`.
- `check_messages(&self, messages: &[CosmosMsg]) -> Result<(), MsgFilterError>`: Checks if all messages are allowed according to the filter rules.
- `filter_messages<'a>(&self, messages: &'a [CosmosMsg]) -> Result<Vec<&'a CosmosMsg>, MsgFilterError>`: Returns a list of messages that meet the filter criteria.

### `AllowedMsg`

An enum specifying the types of allowed messages:

- `Exact(CosmosMsg)`: Allows an exact message.
- `GenericWasmExecuteMsg { contract: Option<String>, key: Option<String>, funds: Option<Vec<Coin>> }`: Allows Wasm execute messages matching specified criteria.
- `Type(MsgType)`: Allows messages of a specific type.

### `MsgType`

An enum representing different types of CosmWasm messages (e.g., `BankSend`, `WasmExecute`, etc.).

### `MsgFilterError`

An enum representing different types of errors that can occur during message filtering:

- `Std(StdError)`: Standard error from cosmwasm_std.
- `InvalidConfiguration`: Invalid filter configuration.
- `InvalidMsg`: Message not allowed by the filter.
- `SpendingLimitExceeded { denom: String, limit: Uint128, actual: Uint128 }`: Spending limit exceeded for a specific denomination.
- `TooManyMsgs { count: u8, max: u8 }`: Maximum message count exceeded.

### `ConfigValidationError`

An enum representing different types of configuration validation errors:

- `NoFilteringCriteria`: No filtering criteria specified.
- `InvalidMaxMsgCount(u8)`: Invalid maximum message count.
- `InvalidSpendingLimit { denom: String }`: Invalid spending limit for a specific denomination.
- `EmptyGenericWasmExecuteMsg`: Generic Wasm Execute Message has no criteria specified.
- `InvalidCoinAmount { denom: String }`: Invalid coin amount in funds for a specific denomination.
Loading
Loading