diff --git a/examples/bitseed_generator/src/lib.rs b/examples/bitseed_generator/src/lib.rs index 3a99087b9..5c48eef83 100644 --- a/examples/bitseed_generator/src/lib.rs +++ b/examples/bitseed_generator/src/lib.rs @@ -1,6 +1,5 @@ use cosmwasm_std::{ - to_json_binary, Binary, Deps, Env, StdResult, Response, MessageInfo, DepsMut, - entry_point, + entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, }; use serde::{Deserialize, Serialize}; @@ -10,137 +9,142 @@ pub struct InstantiateMsg {} #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] #[serde(rename_all = "snake_case")] pub enum QueryMsg { - InscribeGenerate(InputData), - InscribeVerify { input: InputData, output: OutputData }, - IndexerGenerate(InputData), + InscribeGenerate(InputData), + InscribeVerify { + input: InputData, + output: OutputData, + }, + IndexerGenerate(InputData), } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct InputData { - seed: String, - user_input: String, - attributes: Vec, + seed: String, + user_input: String, + attributes: Vec, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct AttributeGenerateRule { - key: String, - rule_type: AttributeRuleType, - parameters: Vec, + key: String, + rule_type: AttributeRuleType, + parameters: Vec, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub enum AttributeRuleType { - Range, - List, + Range, + List, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct OutputData { - amount: u32, - attributes: Vec, - content: Binary, + amount: u32, + attributes: Vec, + content: Binary, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct Attribute { - key: String, - value: String, + key: String, + value: String, } - #[entry_point] pub fn instantiate( - _deps: DepsMut, - _env: Env, - _info: MessageInfo, - _msg: InstantiateMsg, + _deps: DepsMut, + _env: Env, + _info: MessageInfo, + _msg: InstantiateMsg, ) -> StdResult { - Ok(Response::default()) + Ok(Response::default()) } #[entry_point] pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { - match msg { - QueryMsg::InscribeGenerate(input) => to_json_binary(&inscribe_generate(input)?), - QueryMsg::InscribeVerify { input, output } => to_json_binary(&inscribe_verify(input, output)?), - QueryMsg::IndexerGenerate(input) => to_json_binary(&indexer_generate(input)?), - } + match msg { + QueryMsg::InscribeGenerate(input) => to_json_binary(&inscribe_generate(input)?), + QueryMsg::InscribeVerify { input, output } => { + to_json_binary(&inscribe_verify(input, output)?) + } + QueryMsg::IndexerGenerate(input) => to_json_binary(&indexer_generate(input)?), + } } fn inscribe_generate(input: InputData) -> StdResult { - let hash_value = hash_str_uint32(&format!("{}{}", input.seed, input.user_input)); - - let mut attributes = vec![ - Attribute { - key: "id".to_string(), - value: input.user_input.clone(), - } - ]; - - for attr in input.attributes { - let value = match attr.rule_type { - AttributeRuleType::Range => { - if attr.parameters.len() == 2 { - if let (Ok(min), Ok(max)) = (attr.parameters[0].parse::(), attr.parameters[1].parse::()) { - let random_value = min + (hash_value % (max - min + 1)); - random_value.to_string() - } else { - continue; // Skip this attribute if parsing fails - } - } else { - continue; // Skip this attribute if parameters are incorrect - } - }, - AttributeRuleType::List => { - if !attr.parameters.is_empty() { - let index = (hash_value as usize) % attr.parameters.len(); - attr.parameters[index].clone() - } else { - continue; // Skip this attribute if the list is empty - } - }, - }; - - attributes.push(Attribute { - key: attr.key, - value, - }); - } - - Ok(OutputData { - amount: 1000, - attributes, - content: Binary::default(), - }) + let hash_value = hash_str_uint32(&format!("{}{}", input.seed, input.user_input)); + + let mut attributes = vec![Attribute { + key: "id".to_string(), + value: input.user_input.clone(), + }]; + + for attr in input.attributes { + let value = match attr.rule_type { + AttributeRuleType::Range => { + if attr.parameters.len() == 2 { + if let (Ok(min), Ok(max)) = ( + attr.parameters[0].parse::(), + attr.parameters[1].parse::(), + ) { + let random_value = min + (hash_value % (max - min + 1)); + random_value.to_string() + } else { + continue; // Skip this attribute if parsing fails + } + } else { + continue; // Skip this attribute if parameters are incorrect + } + } + AttributeRuleType::List => { + if !attr.parameters.is_empty() { + let index = (hash_value as usize) % attr.parameters.len(); + attr.parameters[index].clone() + } else { + continue; // Skip this attribute if the list is empty + } + } + }; + + attributes.push(Attribute { + key: attr.key, + value, + }); + } + + Ok(OutputData { + amount: 1000, + attributes, + content: Binary::default(), + }) } fn inscribe_verify(input: InputData, output: OutputData) -> StdResult { - let generated_output = inscribe_generate(input)?; - Ok(generated_output == output) + let generated_output = inscribe_generate(input)?; + Ok(generated_output == output) } fn indexer_generate(input: InputData) -> StdResult { - inscribe_generate(input) + inscribe_generate(input) } fn hash_str_uint32(str: &str) -> u32 { - let mut hash: u32 = 0x811c9dc5; - let prime: u32 = 0x1000193; + let mut hash: u32 = 0x811c9dc5; + let prime: u32 = 0x1000193; - for &byte in str.as_bytes() { - hash ^= u32::from(byte); - hash = hash.wrapping_mul(prime); - } + for &byte in str.as_bytes() { + hash ^= u32::from(byte); + hash = hash.wrapping_mul(prime); + } - hash + hash } #[cfg(test)] mod tests { use super::*; - use cosmwasm_std::testing::{mock_dependencies, mock_env}; use cosmwasm_std::from_json; + use cosmwasm_std::testing::{mock_dependencies, mock_env}; #[test] fn test_inscribe_generate() { @@ -204,17 +208,15 @@ mod tests { let input = InputData { seed: "test_seed".to_string(), user_input: "test_user_input".to_string(), - attributes: vec![ - AttributeGenerateRule { - key: "test_range".to_string(), - rule_type: AttributeRuleType::Range, - parameters: vec!["1".to_string(), "10".to_string()], - }, - ], + attributes: vec![AttributeGenerateRule { + key: "test_range".to_string(), + rule_type: AttributeRuleType::Range, + parameters: vec!["1".to_string(), "10".to_string()], + }], }; // Generate expected output - let expected_output = inscribe_generate( input.clone()).unwrap(); + let expected_output = inscribe_generate(input.clone()).unwrap(); // Test verification with correct output let query_msg = QueryMsg::InscribeVerify { @@ -238,5 +240,4 @@ mod tests { let result: bool = from_json(&binary_response).unwrap(); assert!(!result); } - }