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 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ base64 = "0.21.4"
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"]}
p256 = {version = "0.13.2", features = ["ecdsa-core", "arithmetic", "serde"]}
4 changes: 3 additions & 1 deletion account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ p256 = { workspace = true }
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::XionCustomQuery};
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<XionCustomQuery>,
env: &Env,
tx_bytes: &Binary,
sig_bytes: &Binary,
Expand Down
46 changes: 24 additions & 22 deletions account/src/auth/passkey.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::error::ContractResult;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use crate::proto::{self, XionCustomQuery};
use base64::engine::general_purpose::{self};
use base64::Engine;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::QueryRequest::Stargate;
use cosmwasm_std::{to_binary, Addr, Binary, Deps};
use cosmwasm_std::{Addr, Binary, Deps};

#[cw_serde]
struct QueryRegisterRequest {
Expand All @@ -18,19 +18,23 @@ struct QueryRegisterResponse {
credential: Binary,
}

pub fn register(deps: Deps, addr: Addr, rp: String, data: Binary) -> ContractResult<Binary> {
let query = QueryRegisterRequest {
#[cw_serde]
struct QueryAuthenticateResponse {}

pub fn register(
deps: Deps<XionCustomQuery>,
addr: Addr,
rp: String,
data: Binary,
) -> ContractResult<Binary> {
let query = proto::QueryWebAuthNVerifyRegisterRequest {
addr: addr.clone().into(),
challenge: addr.to_string(),
challenge: Binary::from(addr.as_bytes()).to_base64(),
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,28 +49,26 @@ struct QueryVerifyRequest {
}

pub fn verify(
deps: Deps,
deps: Deps<XionCustomQuery>,
addr: Addr,
rp: String,
signature: &Binary,
tx_hash: Vec<u8>,
credential: &Binary,
) -> ContractResult<bool> {
let challenge = URL_SAFE_NO_PAD.encode(tx_hash);
let challenge =
general_purpose::URL_SAFE_NO_PAD.encode(general_purpose::STANDARD.encode(tx_hash));

let query = QueryVerifyRequest {
let query = proto::QueryWebAuthNVerifyAuthenticateRequest {
addr: addr.into(),
challenge,
rp,
credential: credential.clone(),
data: signature.clone(),
credential: credential.clone().into(),
data: signature.clone().into(),
};
let query_bz = to_binary(&query)?;

deps.querier.query(&Stargate {
path: "xion.v1.Query/WebAuthNVerifyAuthenticate".to_string(),
data: query_bz,
})?;
deps.querier
.query::<QueryAuthenticateResponse>(&query.into())?;

Ok(true)
}
17 changes: 12 additions & 5 deletions account/src/auth/sign_arb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ fn wrap_message(msg_bytes: &[u8], signer: Addr) -> Vec<u8> {
mod tests {
use crate::auth::sign_arb::wrap_message;
use crate::auth::util;
use crate::auth::AddAuthenticator::Secp256K1;
use crate::contract::instantiate;
use crate::msg::InstantiateMsg;
use crate::proto::XionCustomQuery;
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 +94,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::<XionCustomQuery>::new(&[]),
custom_query_type: core::marker::PhantomData::<XionCustomQuery>,
};
let mut env = mock_env();
let info = mock_info("sender", &[]);
// This is the local faucet address to simplify reuse
Expand All @@ -115,7 +122,7 @@ mod tests {
let signature_bytes = general_purpose::STANDARD.decode(signature).unwrap();

let instantiate_msg = InstantiateMsg {
authenticator: Secp256K1 {
authenticator: crate::auth::AddAuthenticator::Secp256K1 {
id: 0,
pubkey: Binary::from(pubkey_bytes),
signature: Binary::from(signature_bytes),
Expand Down
18 changes: 12 additions & 6 deletions account/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use absacc::AccountSudoMsg;
use crate::error::ContractError;
use crate::execute::{add_auth_method, assert_self, remove_auth_method};
use crate::msg::{ExecuteMsg, MigrateMsg};
use crate::proto::XionCustomQuery;
use crate::{
error::ContractResult,
execute,
Expand All @@ -16,17 +17,21 @@ use crate::{

#[entry_point]
pub fn instantiate(
deps: DepsMut,
deps: DepsMut<XionCustomQuery>,
env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> ContractResult<Response> {
cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
execute::init(deps, env, msg.authenticator)
execute::init(deps, env, &mut msg.authenticator.clone())
}

#[entry_point]
pub fn sudo(deps: DepsMut, env: Env, msg: AccountSudoMsg) -> ContractResult<Response> {
pub fn sudo(
deps: DepsMut<XionCustomQuery>,
env: Env,
msg: AccountSudoMsg,
) -> ContractResult<Response> {
match msg {
AccountSudoMsg::BeforeTx {
tx_bytes,
Expand All @@ -46,17 +51,18 @@ pub fn sudo(deps: DepsMut, env: Env, msg: AccountSudoMsg) -> ContractResult<Resp

#[entry_point]
pub fn execute(
deps: DepsMut,
deps: DepsMut<XionCustomQuery>,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> ContractResult<Response> {
assert_self(&info.sender, &env.contract.address)?;
match msg {
let mut owned_msg = msg.clone();
match &mut owned_msg {
ExecuteMsg::AddAuthMethod { add_authenticator } => {
add_auth_method(deps, env, add_authenticator)
}
ExecuteMsg::RemoveAuthMethod { id } => remove_auth_method(deps, env, id),
ExecuteMsg::RemoveAuthMethod { id } => remove_auth_method(deps, env, *id),
}
}

Expand Down
3 changes: 3 additions & 0 deletions account/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub enum ContractError {
#[error("invalid token")]
InvalidToken,

#[error("url parse error: {url}")]
URLParse { url: String },

#[error("cannot override existing authenticator at index {index}")]
OverridingIndex { index: u8 },
}
Expand Down
Loading
Loading