-
Notifications
You must be signed in to change notification settings - Fork 0
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
refactor: Is Eligible Changes #20
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e75388
refactor: separate query so one is just committee stake check, new on…
gluax 01749ba
fix: add back in removed test
gluax 48db6f5
refactor: move test function to test
gluax 3b5d523
feat: allow person creating message to get base64 string as well
gluax 632d07d
fix: return bytes so it works for cosmwasm
gluax 2bec955
refactor: `IsExecutorCommitteeEligible` to `IsStakerExecutor`
gluax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use core::str; | ||
|
||
use super::*; | ||
|
||
#[cfg_attr(feature = "cosmwasm", cw_serde)] | ||
#[cfg_attr(not(feature = "cosmwasm"), derive(Serialize, Debug, PartialEq))] | ||
#[cfg_attr(not(feature = "cosmwasm"), serde(rename_all = "snake_case"))] | ||
pub struct Query { | ||
pub data: Bytes, | ||
} | ||
|
||
impl Query { | ||
fn generate_hash(dr_id: &str, chain_id: &str, contract_addr: &str) -> Hash { | ||
hash([ | ||
"is_executor_eligible".as_bytes(), | ||
dr_id.as_bytes(), | ||
chain_id.as_bytes(), | ||
contract_addr.as_bytes(), | ||
]) | ||
} | ||
|
||
#[cfg(not(feature = "cosmwasm"))] | ||
fn decode(&self) -> Result<Vec<u8>> { | ||
use base64::{prelude::BASE64_STANDARD, Engine}; | ||
|
||
let decoded = BASE64_STANDARD.decode(&self.data)?; | ||
Ok(decoded) | ||
} | ||
|
||
#[cfg(feature = "cosmwasm")] | ||
fn decode(&self) -> Result<Vec<u8>> { | ||
Ok(self.data.to_vec()) | ||
} | ||
|
||
fn dr_id_hex(&self) -> Result<String> { | ||
let decoded = self.decode()?; | ||
Ok(str::from_utf8(&decoded[67..131]).unwrap().to_owned()) | ||
} | ||
|
||
pub fn parts(&self) -> Result<([u8; 33], Hash, Vec<u8>)> { | ||
let decoded = self.decode()?; | ||
let public_key = hex::decode(&decoded[..66])?.try_into().expect("Invalid public key"); | ||
let dr_id = hex::decode(&decoded[67..131])?.try_into().expect("Invalid dr_id"); | ||
let proof = hex::decode(&decoded[132..])?; | ||
|
||
Ok((public_key, dr_id, proof)) | ||
} | ||
} | ||
|
||
impl VerifySelf for Query { | ||
type Extra = (); | ||
|
||
fn proof(&self) -> Result<Vec<u8>> { | ||
let decoded = self.decode()?; | ||
Ok(hex::decode(&decoded[132..])?) | ||
} | ||
|
||
fn msg_hash(&self, chain_id: &str, contract_addr: &str, _: Self::Extra) -> Result<Hash> { | ||
Ok(Self::generate_hash(&self.dr_id_hex()?, chain_id, contract_addr)) | ||
} | ||
} | ||
|
||
pub struct QueryFactory { | ||
pub(crate) dr_id: String, | ||
pub(crate) public_key: String, | ||
pub(crate) hash: Hash, | ||
} | ||
|
||
impl QueryFactory { | ||
pub fn get_hash(&self) -> &[u8] { | ||
&self.hash | ||
} | ||
|
||
#[cfg(not(feature = "cosmwasm"))] | ||
pub(crate) fn encode_data(data: &str) -> Bytes { | ||
use base64::{prelude::BASE64_STANDARD, Engine}; | ||
|
||
BASE64_STANDARD.encode(data) | ||
} | ||
|
||
#[cfg(feature = "cosmwasm")] | ||
pub(crate) fn encode_data(data: &str) -> Bytes { | ||
use cosmwasm_std::Binary; | ||
Binary(data.as_bytes().to_vec()) | ||
} | ||
|
||
pub fn create_message(self, proof: Vec<u8>) -> (crate::msgs::QueryMsg, Bytes) { | ||
let data = format!("{}:{}:{}", self.public_key, self.dr_id, proof.to_hex()); | ||
let base64_data = Self::encode_data(&data); | ||
|
||
( | ||
Query { | ||
data: base64_data.clone(), | ||
} | ||
.into(), | ||
base64_data, | ||
) | ||
} | ||
} | ||
|
||
impl Query { | ||
pub fn factory(public_key: String, dr_id: String, chain_id: &str, contract_addr: &str) -> QueryFactory { | ||
let hash = Self::generate_hash(&dr_id, chain_id, contract_addr); | ||
QueryFactory { | ||
dr_id, | ||
public_key, | ||
hash, | ||
} | ||
} | ||
|
||
pub fn verify(&self, public_key: &[u8], chain_id: &str, contract_addr: &str) -> Result<()> { | ||
self.verify_inner(public_key, chain_id, contract_addr, ()) | ||
} | ||
} | ||
|
||
impl From<Query> for crate::msgs::QueryMsg { | ||
fn from(value: Query) -> Self { | ||
super::QueryMsg::IsExecutorEligible(value).into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To follow the rest of queries, why not like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be done that way, but our standard practice for messages is that if something is complicated to move to its type, we've been using tuples for it.
If we did this, it would change the
json
as well from:to