Skip to content
Open
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
21 changes: 21 additions & 0 deletions contracts/reclaim_user_map/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "reclaim-user-map"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
# enable feature if you want to disable entry points
library = []

[dependencies]
cosmwasm-schema = "1.3.1"
cosmwasm-std = "1.3.1"
cw-storage-plus = "1.1.0"
thiserror = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
getrandom = { workspace = true }
reclaim_xion = { package = "reclaim-xion", git = "https://github.com/burnt-labs/xion-sdk-onchain-integration.git", features = ["library"], rev = "5c67c33d0575c2ede6d2512c5d724b52019974bb"}
11 changes: 11 additions & 0 deletions contracts/reclaim_user_map/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use cosmwasm_schema::write_api;
use reclaim_user_map::msg::*;

fn main() {
write_api! {
instantiate: InstantiateMsg,
query: QueryMsg,
execute: ExecuteMsg,
migrate: MigrateMsg,
};
}
84 changes: 84 additions & 0 deletions contracts/reclaim_user_map/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::collections::HashMap;
use crate::error::ContractError;
use crate::error::ContractResult;
use crate::msg::InstantiateMsg;
use crate::msg::{ExecuteMsg, QueryMsg};
use crate::state::{CLAIM_VALUE_KEY, USER_MAP, VERIFICATION_ADDR};
use cosmwasm_std::{entry_point, to_json_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Order, Response, StdResult, WasmMsg};
use serde_json::Value;
use crate::error::ContractError::ClaimKeyInvalid;

#[entry_point]
pub fn instantiate(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
deps.api.addr_validate(msg.verification_addr.as_str())?;
VERIFICATION_ADDR.save(deps.storage, &msg.verification_addr)?;
if msg.claim_key.is_empty() {
return Err(ClaimKeyInvalid)
}
CLAIM_VALUE_KEY.save(deps.storage, &msg.claim_key)?;

Ok(Response::new()
.add_attribute("method", "instantiate")
.add_attribute("owner", info.sender))
}
#[entry_point]
pub fn execute(
deps: DepsMut,
_: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> ContractResult<Response> {
match msg {
ExecuteMsg::Update { value } => {
// validate JSON
let context: HashMap<&str, Value> = serde_json::from_str(&value.proof.claimInfo.context)?;

let extracted_parameters = match context.get("extractedParameters") {
None => return Err(ContractError::ExtractedParametersMissing {}),
Some(v) => v
};

let verified_value = match extracted_parameters.get(CLAIM_VALUE_KEY.load(deps.storage)?.as_str()) {
Some(v) => v.to_string(),
None => return Err(ContractError::JSONKeyMissing {}),
};

USER_MAP.save(deps.storage, info.sender, &verified_value)?;
Ok(Response::default().add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: VERIFICATION_ADDR.load(deps.storage)?.into_string(),
msg: to_json_binary(&reclaim_xion::msg::ExecuteMsg::VerifyProof(value))?,
funds: vec![],
})))
}
}
}

#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetValueByUser { address } => {
let value = USER_MAP.load(deps.storage, address)?;
to_json_binary(&value)
}
QueryMsg::GetUsers {} => {
let mut addrs: Vec<Addr> = Vec::new();
for addr in USER_MAP.keys(deps.storage, None, None, Order::Ascending) {
addrs.push(addr?)
}
to_json_binary(&addrs)
}
QueryMsg::GetMap {} => {
let mut response: Vec<(Addr, String)> = Vec::new();
for item in USER_MAP.range(deps.storage, None, None, Order::Ascending) {
let (key, value) = item?;
response.push((key, value))
}
to_json_binary(&response)
}
}
}
19 changes: 19 additions & 0 deletions contracts/reclaim_user_map/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[derive(Debug, thiserror::Error)]
pub enum ContractError {
#[error(transparent)]
Std(#[from] cosmwasm_std::StdError),

#[error(transparent)]
JsonError(#[from] serde_json::Error),

#[error("json key missing")]
JSONKeyMissing,

#[error("extracted paramters missing")]
ExtractedParametersMissing,

#[error("claim key invalid")]
ClaimKeyInvalid,
}

pub type ContractResult<T> = Result<T, ContractError>;
18 changes: 18 additions & 0 deletions contracts/reclaim_user_map/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extern crate core;

#[cfg(not(feature = "library"))]
pub mod contract;
mod error;
pub mod msg;
mod state;

// the random function must be disabled in cosmwasm
use core::num::NonZeroU32;
use getrandom::Error;

pub fn always_fail(_buf: &mut [u8]) -> Result<(), Error> {
let code = NonZeroU32::new(Error::CUSTOM_START).unwrap();
Err(Error::from(code))
}
use getrandom::register_custom_getrandom;
register_custom_getrandom!(always_fail);
28 changes: 28 additions & 0 deletions contracts/reclaim_user_map/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Addr;
use reclaim_xion::msg::ProofMsg;

#[cw_serde]
pub struct InstantiateMsg {
pub verification_addr: Addr,
pub claim_key: String,
}

#[cw_serde]
pub enum ExecuteMsg {
Update { value: ProofMsg },
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
#[returns(Vec<Addr>)]
GetUsers {},
#[returns(String)]
GetValueByUser { address: Addr },
#[returns(Vec<(Addr, String)>)]
GetMap {},
}

#[cw_serde]
pub struct MigrateMsg {}
7 changes: 7 additions & 0 deletions contracts/reclaim_user_map/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use cosmwasm_std::Addr;
use cw_storage_plus::{Item, Map};

pub const USER_MAP: Map<Addr, String> = Map::new("user_map");

pub const VERIFICATION_ADDR: Item<Addr> = Item::new("verification_addr");
pub const CLAIM_VALUE_KEY: Item<String> = Item::new("claim_value_key");
Loading