Skip to content

Commit

Permalink
add google auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Saeed committed Mar 27, 2024
1 parent 45de642 commit 19e93f2
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 264 deletions.
5 changes: 0 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml",
"./Cargo.toml",
"./Cargo.toml",
"./Cargo.toml",
"./Cargo.toml"
],
"debug.allowBreakpointsEverywhere": true,
"cosmywasmy.chainConfigName": "Juno uni-6"
}
28 changes: 19 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "haypay"
version = "0.1.1"
authors = ["Saeed <github.com/omni001s>", "Sam <github.com/meisamtaher>"]
name = "heypay"
version = "0.1.2"
authors = ["Max <github.com/0xmaxyz>", "Sam <github.com/meisamtaher>"]
edition = "2021"

[lib]
Expand Down Expand Up @@ -50,6 +50,7 @@ thiserror = { version = "1.0.49" }
getrandom = { version = "0.2.10", features = ["custom"] }
email_address = "0.2.4"
semver = "1.0.21"
serde-json-wasm = "1.0.1"

[dev-dependencies]
cw-multi-test = "0.17.0"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![CosmWasm](https://img.shields.io/badge/CosmWasm-green)
![Xion](https://img.shields.io/badge/Xion-black)
# HayPay Contract
# HeyPay Contract
## Compiling and testing
### Requirements
This project is bootstrapped from CosmWasm Starter Pack ([minimal code repo](https://github.com/CosmWasm/cw-template?tab=readme-ov-file#creating-a-new-repo-from-template)), you need rust to build and test the contract, instructions for installing rust, could be found [here](https://www.rust-lang.org/tools/install). Additionally, you need the Wasm rust compiler to build Wasm binaries:
Expand Down Expand Up @@ -60,4 +60,4 @@ Upon successful deployment, the script provides the `code_id`, `contract_address
The `./Scripts/test.sh` script helps test the contract on the testnet, but it requires a JWT containing your email address and Xion address. Manual setup of this custom JWT can be complex, you have to inject email address and xion address in your request to receive a valid JWT with custom claims.
A video of our demo using our front app is [here](https://youtu.be/NIFiNzDLiOY).
## Deployed contract in Xion Testnet
The contract is deployed to Xion testnet at `xion1zt64jdruawm4dl0svxe973gkd4kgvllwzgw6wexlgkpjmcssu4gqnqwdyp`.
The contract is deployed to Xion testnet at `xion1xg3elrmwhu0u0e2yq2vexxs383r0clcyyc2nvezygn3mmgdxx9kq3x5l9e`.
4 changes: 2 additions & 2 deletions Scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ instantiate_contract() {

wait_some() {
local Heights="$1"
echo -n "Waiting for some block heights ($Heights)"
echo -n "Waiting for some blocks ($Heights)"
INIT_HEIGHT=$(xiond query block --node $RPC | jq -r '.block.header.height')
TARGET_HEIGHT=$((INIT_HEIGHT + Heights))
while true; do
Expand Down Expand Up @@ -74,7 +74,7 @@ if [[ "$OPTIMIZER" == *"status: 0" ]]; then
read -s -p "Enter keyring passphrase (if any): " password
echo -en "033[1A\033[2K\r"

store_wasm "artifacts/haypay.wasm"
store_wasm "artifacts/heypay.wasm"
wait_some 7
get_code_id
instantiate_contract "___"
Expand Down
75 changes: 75 additions & 0 deletions Scripts/migrate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash
source .env
MIGRATE_MSG='{"default_gas_limit":1000000}'
optimize_wasm() {
echo -n "Optimizing wasm binaries ..."
OPTIMIZER=$(cargo run-script optimize 2>&1)
echo -en "033[1A\033[2K\r"
}
store_code() {
local c_addr="$1"
echo "Save Binary"
echo -n "Saving ..."
RES=$(xiond tx wasm store $c_addr --from $DEPLOYER_ADDRESS --node $RPC --chain-id $CHAIN_ID --gas auto --gas-adjustment 1.4 --gas-prices $GAS_PRICE --output json --yes <<<"$password")
echo -en "033[1A\033[2K\r"
STORE_TX_HASH=$(echo $RES | jq -r '.txhash')
echo "Save binary tx hash: https://explorer.burnt.com/xion-testnet-1/tx/$STORE_TX_HASH"
}

get_code_id() {
echo -n "Getting the code id ..."
CODE_ID=$(xiond q tx $STORE_TX_HASH --node $RPC --output json | jq -r '.logs[0].events[-1].attributes[1].value')
echo -en "033[1A\033[2K\r"
echo "Code id: $CODE_ID"
}

migrate() {
echo "Migrating contract"
echo -n "Migrating ..."
RES=$(xiond tx wasm migrate $HEYPAY_ADDRESS $CODE_ID $MIGRATE_MSG --from $DEPLOYER_ADDRESS --node $RPC --chain-id $CHAIN_ID --gas auto --gas-adjustment 1.4 --gas-prices $GAS_PRICE --output json --yes <<<"$password")
echo -en "033[1A\033[2K\r"
STORE_TX_HASH=$(echo $RES | jq -r '.txhash')
echo "Migration tx hash: https://explorer.burnt.com/xion-testnet-1/tx/$STORE_TX_HASH"
}

wait_some() {
local Heights="$1"
echo -n "Waiting for some blocks ($Heights)"
INIT_HEIGHT=$(xiond query block --node $RPC | jq -r '.block.header.height')
TARGET_HEIGHT=$((INIT_HEIGHT + Heights))
while true; do
CURRENT_HEIGHT=$(xiond query block --node "$RPC" | jq -r '.block.header.height')

if [ "$CURRENT_HEIGHT" -ge "$TARGET_HEIGHT" ]; then
break
fi

sleep 1
done
echo -en "033[1A\033[2K\r"
}

# Check for env vars
if [ -z "$DEPLOYER_ADDRESS" ] || [ -z "$RPC" ] || [ -z "$CHAIN_ID" ] || [ -z "$GAS_PRICE" ] || [ -z "$HEYPAY_ADDRESS" ]; then
echo "Error: Environment variables not set"
echo "The environment variable are in .env.ex, the required ones for this script are: { DEPLOYER_ADDRESS, RPC, CHAIN_ID, GAS_PRICE, HEYPAY_ADDRESS }"
exit 1
fi

clear
echo "Migrating contract"

optimize_wasm

if [[ "$OPTIMIZER" == *"status: 0" ]]; then
read -s -p "Enter keyring passphrase (if any): " password
echo -en "033[1A\033[2K\r"

store_code "artifacts/heypay.wasm"
wait_some 7
get_code_id
migrate
else
echo "There was an error optimizing the wasm, run cargo wasm to check for errors"
exit 1
fi
12 changes: 6 additions & 6 deletions Scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ transfer_token() {
TOKEN_MSG=$(echo -e "{ \"email\": \"$TEST_EMAIL\",\"memo\": \"\" }" | base64 -w 0)
SEND_MSG="{
\"send\": {
\"contract\": \"$HAYPAY_ADDRESS\",
\"contract\": \"$HEYPAY_ADDRESS\",
\"amount\": \"1000\",
\"msg\": \"$TOKEN_MSG\"
}
Expand All @@ -32,14 +32,14 @@ transfer_token() {
#echo -e "\033[2K"
TX_HASH=$(echo "$RESP" | jq -r '.txhash')
echo -en "033[1A\033[2K\r"
echo "Tx Hash for transfering token to HayPay contract: https://explorer.burnt.com/xion-testnet-1/tx/${TX_HASH}"
echo "Tx Hash for transfering token to HeyPay contract: https://explorer.burnt.com/xion-testnet-1/tx/${TX_HASH}"
}

claimable() {
echo -n "Querying the claimable tokens for the test email..."
QUERY_MSG="{\"claims\":{ \"email\": \"$TEST_EMAIL\" }}"
BASE64_Q_MSG=$(echo "$QUERY_MSG" | base64)
RESP=$(xiond query wasm contract-state smart $HAYPAY_ADDRESS $BASE64_Q_MSG --b64 --node $RPC --output json)
RESP=$(xiond query wasm contract-state smart $HEYPAY_ADDRESS $BASE64_Q_MSG --b64 --node $RPC --output json)
BALANCE=$(echo "$RESP" | jq -r '.data.claims')
echo -en "033[1A\033[2K\r"
echo "Total amount of claimable tokens for test email is: {$BALANCE}"
Expand All @@ -58,7 +58,7 @@ claim() {
}
}"

RESP=$(xiond tx wasm execute $HAYPAY_ADDRESS "$CLAIM_MSG" --from $DEPLOYER_ADDRESS --chain-id $CHAIN_ID --gas 2000000 --gas-prices $GAS_PRICE --node $RPC --output json --yes <<<"$password")
RESP=$(xiond tx wasm execute $HEYPAY_ADDRESS "$CLAIM_MSG" --from $DEPLOYER_ADDRESS --chain-id $CHAIN_ID --gas 2000000 --gas-prices $GAS_PRICE --node $RPC --output json --yes <<<"$password")
TX_HASH=$(echo "$RESP" | jq -r '.txhash')
echo -en "033[1A\033[2K\r"
echo "Tx Hash for claiming tokens: https://explorer.burnt.com/xion-testnet-1/tx/${TX_HASH}"
Expand Down Expand Up @@ -91,7 +91,7 @@ echo -en "\033[2K\r"

query "$DEPLOYER_ADDRESS" "Deployer" "$TOKEN_ADDRESS"

query "$HAYPAY_ADDRESS" "Haypay contract" "$TOKEN_ADDRESS"
query "$HEYPAY_ADDRESS" "Heypay contract" "$TOKEN_ADDRESS"

claimable

Expand All @@ -105,6 +105,6 @@ claimable

query "$DEPLOYER_ADDRESS" "Deployer" "$TOKEN_ADDRESS"

query "$HAYPAY_ADDRESS" "Haypay contract" "$TOKEN_ADDRESS"
query "$HEYPAY_ADDRESS" "Heypay contract" "$TOKEN_ADDRESS"

#rm .t
2 changes: 1 addition & 1 deletion src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_schema::write_api;

use haypay::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use heypay::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
write_api! {
Expand Down
6 changes: 3 additions & 3 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ mod execute {
_msg: TokenClaimMsg,
) -> Result<Response, ContractError> {
// verify token and get email
match crate::jwt::Token::verify(&_msg.aud, &_msg.jwt) {
match crate::jwt::Token::verify(&_msg.jwt, _env.block.time.seconds(), _msg.testing) {
Ok(payload) => {
// check if sender is equal to address in token
let addr_in_jwt = _deps.api.addr_validate(&payload.xion_address)?;
let addr_in_jwt = _deps.api.addr_validate(&payload.nonce)?;
if addr_in_jwt != _info.sender {
return Err(ContractError::Unauthorized {});
}
// Then the msg.sender is the one who owns the token
let email = payload.email_address;
let email = payload.email;
// Check for Claims
if CLAIMS.has(_deps.as_ref().storage, &email) {
let txs =
Expand Down
7 changes: 5 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ pub enum ContractError {
#[error("Unauthorized")]
Unauthorized {},

#[error("invalid jwt aud")]
InvalidJWTAud,
#[error("invalid jwt kid")]
InvalidJWTKid,

#[error("invalid token")]
InvalidToken,

#[error("expired token")]
ExpiredToken,

#[error("nothing to claim")]
NotClaimable,

Expand Down
26 changes: 13 additions & 13 deletions src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
msg::{QueryClaimResponse, QueryMsg, TokenReceiveMsg},
unit_tests::EMAIL_2,
unit_tests::EMAIL,
};
use cosmwasm_std::{coins, to_json_binary, Addr, Empty, Uint128};
use cw20::{Cw20Coin, Cw20Contract, Cw20ExecuteMsg};
Expand Down Expand Up @@ -40,21 +40,21 @@ fn test_receive() {
.instantiate_contract(cw20_id, owner.clone(), &msg, &[], "token", None)
.unwrap();

// setup haypay
let haypay_id = router.store_code(haypay_contract());
let haypay_addr = router
// setup heypay
let heypay_id = router.store_code(heypay_contract());
let heypay_addr = router
.instantiate_contract(
haypay_id,
heypay_id,
owner.clone(),
&crate::msg::InstantiateMsg {},
&[],
"HayPay",
"HeyPay",
None,
)
.unwrap();

// two different contracts
assert_ne!(haypay_addr, cw20_addr);
assert_ne!(heypay_addr, cw20_addr);

let cash = Cw20Contract(cw20_addr.clone());

Expand All @@ -63,18 +63,18 @@ fn test_receive() {

// Create ReceiveMsg (for calling Send)
let token_msg = TokenReceiveMsg {
email: EMAIL_2.to_owned(),
email: EMAIL.to_owned(),
memo: Option::Some("This is a test memo".to_string()),
};

// create send token message
let send_token_msg = Cw20ExecuteMsg::Send {
contract: haypay_addr.to_string(),
contract: heypay_addr.to_string(),
amount: Uint128::new(1),
msg: to_json_binary(&token_msg).unwrap(),
};

// send some cw20 tokens to haypay contract
// send some cw20 tokens to heypay contract
_ = router
.execute_contract::<Cw20ExecuteMsg>(owner.clone(), cw20_addr.clone(), &send_token_msg, &[])
.unwrap();
Expand All @@ -86,17 +86,17 @@ fn test_receive() {

// create query request to get the claims for email_1
let _qmsg = QueryMsg::Claims {
email: EMAIL_2.to_owned(),
email: EMAIL.to_owned(),
};

let query_resp: QueryClaimResponse =
router.wrap().query_wasm_smart(haypay_addr, &_qmsg).unwrap();
router.wrap().query_wasm_smart(heypay_addr, &_qmsg).unwrap();
_ = &query_resp.claims;
}

//

fn haypay_contract() -> Box<dyn Contract<Empty>> {
fn heypay_contract() -> Box<dyn Contract<Empty>> {
let contract = ContractWrapper::new(
crate::contract::execute,
crate::contract::instantiate,
Expand Down
Loading

0 comments on commit 19e93f2

Please sign in to comment.