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

Feat/custom query #17

Merged
merged 8 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,3 @@ phf = { version = "0.11.2", features = ["macros"] }
rsa = { version = "0.9.2" }
getrandom = { version = "0.2.10", features = ["custom"] }
p256 = {version = "0.13.2", features = ["ecdsa-core", "arithmetic", "serde"]}
webauthn-rs = { git = "https://github.com/burnt-labs/webauthn-rs.git", features = ["danger-credential-internals"] }
webauthn-rs-proto = { git = "https://github.com/burnt-labs/webauthn-rs.git" }
webauthn-rs-core = { git = "https://github.com/burnt-labs/webauthn-rs.git" }
11 changes: 5 additions & 6 deletions account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ phf = { workspace = true }
rsa = { workspace = true }
getrandom = { workspace = true }
p256 = { workspace = true }
webauthn-rs = { workspace = true }
webauthn-rs-proto = { workspace = true }
webauthn-rs-core = { workspace = true }
passkey = { git="https://github.com/aptos-labs/passkey-rs.git", branch = "fix-passkey-rs"}
passkey-authenticator = { git="https://github.com/aptos-labs/passkey-rs.git", branch = "fix-passkey-rs", features = ["testable"] }
passkey = { git="https://github.com/aptos-labs/passkey-rs.git"}
passkey-authenticator = { git="https://github.com/aptos-labs/passkey-rs.git", features = ["testable"] }
url = "2.4.1"
coset = "0.3.5"
futures = "0.3.29"
async-trait = "0.1.74"
async-trait = "0.1.74"
prost = {version = "0.11.2", default-features = false, features = ["prost-derive"]}
osmosis-std-derive = "0.13.2"
4 changes: 2 additions & 2 deletions account/src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::auth::secp256r1::verify;
use crate::error::ContractError;
use crate::{auth::secp256r1::verify, proto::MyCustomQuery};
use cosmwasm_std::{Binary, Deps, Env};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -72,7 +72,7 @@ pub enum Authenticator {
impl Authenticator {
pub fn verify(
&self,
deps: Deps,
deps: Deps<MyCustomQuery>,
env: &Env,
tx_bytes: &Binary,
sig_bytes: &Binary,
Expand Down
20 changes: 11 additions & 9 deletions account/src/auth/passkey.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::ContractResult;
use crate::proto::{self, MyCustomQuery};
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use cosmwasm_schema::cw_serde;
Expand All @@ -18,19 +19,20 @@ struct QueryRegisterResponse {
credential: Binary,
}

pub fn register(deps: Deps, addr: Addr, rp: String, data: Binary) -> ContractResult<Binary> {
let query = QueryRegisterRequest {
pub fn register(
deps: Deps<MyCustomQuery>,
addr: Addr,
rp: String,
data: Binary,
) -> ContractResult<Binary> {
let query = proto::QueryWebAuthNVerifyRegisterRequest {
addr: addr.clone().into(),
challenge: addr.to_string(),
rp,
data,
data: data.to_vec(),
};
let query_bz = to_binary(&query)?;

let query_response: QueryRegisterResponse = deps.querier.query(&Stargate {
path: "xion.v1.Query/WebAuthNVerifyRegister".to_string(),
data: query_bz,
})?;
let query_response = deps.querier.query::<QueryRegisterResponse>(&query.into())?;

Ok(query_response.credential)
}
Expand All @@ -45,7 +47,7 @@ struct QueryVerifyRequest {
}

pub fn verify(
deps: Deps,
deps: Deps<MyCustomQuery>,
addr: Addr,
rp: String,
signature: &Binary,
Expand Down
14 changes: 11 additions & 3 deletions account/src/auth/sign_arb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ mod tests {
use crate::auth::AddAuthenticator::Secp256K1;
use crate::contract::instantiate;
use crate::msg::InstantiateMsg;
use crate::proto::MyCustomQuery;
use base64::{engine::general_purpose, Engine as _};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{Addr, Api, Binary};
use cosmwasm_std::testing::{
mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage,
};
use cosmwasm_std::{Addr, Api, Binary, OwnedDeps};

#[test]
fn test_derive_addr() {
Expand Down Expand Up @@ -92,7 +95,12 @@ mod tests {

#[test]
fn test_init_sign_arb() {
let mut deps = mock_dependencies();
let mut deps = OwnedDeps {
storage: MockStorage::default(),
api: MockApi::default(),
querier: MockQuerier::<MyCustomQuery>::new(&[]),
custom_query_type: core::marker::PhantomData::<MyCustomQuery>,
};
let mut env = mock_env();
let info = mock_info("sender", &[]);
// This is the local faucet address to simplify reuse
Expand Down
11 changes: 8 additions & 3 deletions account/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use absacc::AccountSudoMsg;

use crate::execute::{add_auth_method, assert_self, remove_auth_method};
use crate::msg::ExecuteMsg;
use crate::proto::MyCustomQuery;
use crate::{
error::ContractResult,
execute,
Expand All @@ -15,7 +16,7 @@ use crate::{

#[entry_point]
pub fn instantiate(
deps: DepsMut,
deps: DepsMut<MyCustomQuery>,
env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
Expand All @@ -25,7 +26,11 @@ pub fn instantiate(
}

#[entry_point]
pub fn sudo(deps: DepsMut, env: Env, msg: AccountSudoMsg) -> ContractResult<Response> {
pub fn sudo(
deps: DepsMut<MyCustomQuery>,
env: Env,
msg: AccountSudoMsg,
) -> ContractResult<Response> {
match msg {
AccountSudoMsg::BeforeTx {
tx_bytes,
Expand All @@ -45,7 +50,7 @@ pub fn sudo(deps: DepsMut, env: Env, msg: AccountSudoMsg) -> ContractResult<Resp

#[entry_point]
pub fn execute(
deps: DepsMut,
deps: DepsMut<MyCustomQuery>,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
Expand Down
3 changes: 0 additions & 3 deletions account/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ pub enum ContractError {
#[error(transparent)]
P256EcdsaCurve(#[from] p256::ecdsa::Error),

#[error(transparent)]
WebauthnError(#[from] webauthn_rs_core::error::WebauthnError),

#[error("error rebuilding key")]
RebuildingKey,

Expand Down
70 changes: 63 additions & 7 deletions account/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use cosmwasm_std::{Addr, Binary, Deps, DepsMut, Env, Event, Order, Response};

use crate::auth::{passkey, AddAuthenticator, Authenticator};
use crate::proto::MyCustomQuery;
use crate::{
error::{ContractError, ContractResult},
state::AUTHENTICATORS,
};

pub fn init(
deps: DepsMut,
deps: DepsMut<MyCustomQuery>,
env: Env,
add_authenticator: AddAuthenticator,
) -> ContractResult<Response> {
Expand All @@ -26,7 +27,7 @@ pub fn init(
}

pub fn before_tx(
deps: Deps,
deps: Deps<MyCustomQuery>,
env: &Env,
tx_bytes: &Binary,
cred_bytes: Option<&Binary>,
Expand Down Expand Up @@ -87,7 +88,7 @@ pub fn after_tx() -> ContractResult<Response> {
}

pub fn add_auth_method(
deps: DepsMut,
deps: DepsMut<MyCustomQuery>,
env: Env,
add_authenticator: AddAuthenticator,
) -> ContractResult<Response> {
Expand Down Expand Up @@ -219,7 +220,11 @@ pub fn add_auth_method(
)
}

pub fn remove_auth_method(deps: DepsMut, env: Env, id: u8) -> ContractResult<Response> {
pub fn remove_auth_method(
deps: DepsMut<MyCustomQuery>,
env: Env,
id: u8,
) -> ContractResult<Response> {
if AUTHENTICATORS
.keys(deps.storage, None, None, Order::Ascending)
.count()
Expand Down Expand Up @@ -248,17 +253,24 @@ pub fn assert_self(sender: &Addr, contract: &Addr) -> ContractResult<()> {
#[cfg(test)]
mod tests {
use base64::{engine::general_purpose, Engine as _};
use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cosmwasm_std::Binary;
use cosmwasm_std::testing::{mock_env, MockApi, MockQuerier, MockStorage};
use cosmwasm_std::{Binary, OwnedDeps};

use crate::auth::Authenticator;
use crate::execute::before_tx;
use crate::proto::{self, MyCustomQuery, QueryWebAuthNVerifyRegisterResponse};
use crate::state::AUTHENTICATORS;
use cosmwasm_std::QueryRequest::Custom;

#[test]
fn test_before_tx() {
let auth_id = 0;
let mut deps = mock_dependencies();
let mut deps = OwnedDeps {
storage: MockStorage::default(),
api: MockApi::default(),
querier: MockQuerier::<MyCustomQuery>::new(&[]),
custom_query_type: std::marker::PhantomData,
};
let env = mock_env();

let pubkey = "Ayrlj6q3WWs91p45LVKwI8JyfMYNmWMrcDinLNEdWYE4";
Expand Down Expand Up @@ -286,4 +298,48 @@ mod tests {

before_tx(deps.as_ref(), &env, &tx_bytes, Some(&sig_bytes), false).unwrap();
}

#[test]
pub fn test_custom_querier() {
let mut deps = OwnedDeps {
storage: MockStorage::default(),
api: MockApi::default(),
querier: MockQuerier::<MyCustomQuery>::new(&[]),
custom_query_type: core::marker::PhantomData::<MyCustomQuery>,
};

deps.querier = deps.querier.with_custom_handler(|query| match query {
MyCustomQuery::Verify(data) => {
assert_eq!(data.addr, "mock_address");
assert_eq!(data.challenge, "mock_challenge");
assert_eq!(data.rp, "mock_rp");
assert_eq!(data.data, vec![0u8]);

cosmwasm_std::SystemResult::Ok(cosmwasm_std::ContractResult::Ok(
serde_json::to_vec(&QueryWebAuthNVerifyRegisterResponse {
credential: Binary::from("true".as_bytes()).into(),
})
.unwrap()
.into(),
))
}
});

let query_msg = MyCustomQuery::Verify(proto::QueryWebAuthNVerifyRegisterRequest {
addr: "mock_address".to_string(),
challenge: "mock_challenge".to_string(),
rp: "mock_rp".to_string(),
data: vec![0u8],
});
let query_response = deps
.as_ref()
.querier
.query::<QueryWebAuthNVerifyRegisterResponse>(&Custom(query_msg));
assert!(query_response.is_ok());

assert_eq!(
query_response.unwrap().credential,
Binary::from("true".as_bytes())
);
}
}
1 change: 1 addition & 0 deletions account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod contract;
pub mod error;
pub mod execute;
pub mod msg;
pub mod proto;
pub mod query;
pub mod state;

Expand Down
41 changes: 41 additions & 0 deletions account/src/proto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use cosmwasm_std::CustomQuery;
use osmosis_std_derive::CosmwasmExt;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(
Clone,
PartialEq,
Eq,
::prost::Message,
serde::Serialize,
serde::Deserialize,
schemars::JsonSchema,
CosmwasmExt,
)]
#[proto_message(type_url = "/xion.v1.Query/WebAuthNVerifyRegister")]
#[proto_query(path = "/xion.v1.Query/WebAuthNVerifyRegister", response_type = QueryWebAuthNVerifyRegisterResponse)]
pub struct QueryWebAuthNVerifyRegisterRequest {
#[prost(string, tag = "1")]
pub addr: String,
#[prost(string, tag = "2")]
pub challenge: String,
#[prost(string, tag = "3")]
pub rp: String,
#[prost(bytes, tag = "4")]
pub data: Vec<u8>,
}

// We define the response as a prost message to be able to decode the protobuf data.
#[derive(Clone, PartialEq, Eq, ::prost::Message, serde::Serialize, serde::Deserialize)]
pub struct QueryWebAuthNVerifyRegisterResponse {
#[prost(bytes, tag = "1")]
pub credential: Vec<u8>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum MyCustomQuery {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks great, lets just change the name of this type to something more descriptive, like WebAuthNCustomQuery or XionCustomQuery

Verify(QueryWebAuthNVerifyRegisterRequest),
}
impl CustomQuery for MyCustomQuery {}
Loading