Skip to content
This repository has been archived by the owner on Jun 13, 2022. It is now read-only.

Wallet auth #5

Merged
merged 3 commits into from
Sep 1, 2021
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
7 changes: 6 additions & 1 deletion cattleia-backend/src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export const signIn = async (

const _user: User | null = await UserModel.findOne({
userName,
}).populate("role");
})
.populate("role")
.populate("account");

if (_user) {
const matchPass = await comparePassword(_user.password, password);
Expand All @@ -36,6 +38,7 @@ export const signIn = async (
role: _user.role.name,
email: _user.email,
token: createAcessToken(_user),
hasAccount: _user.account.payload != undefined,
},
});
}
Expand Down Expand Up @@ -72,6 +75,7 @@ export const signUp = async (

const _account = new AccountModel({
payload: null,
password: "",
});

_account.save();
Expand All @@ -89,6 +93,7 @@ export const signUp = async (
role: _user.role.name,
email: _user.email,
token: createAcessToken(_user),
hasAccount: false,
},
});
}
Expand Down
36 changes: 28 additions & 8 deletions cattleia-backend/src/controllers/web3.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Web3 from "web3";
import { Request, Response } from "express";
import { UserModel, AccountModel } from "../models";
import { Account, User } from "../libs";
import { Account, User, encryptPassword, comparePassword } from "../libs";

const web3 = new Web3(process.env.GANACHE);

Expand All @@ -15,16 +15,22 @@ const getNonce = (): string => {
return text;
};

export const getBalance = async (req: Request, res: Response) => {
export const access = async (req: Request, res: Response) => {
const id = req.id;

const { password } = req.body;
const _user: User | null = await UserModel.findById(id).populate("account");

if (!_user || !_user.account.payload) return res.json({ ok: false });
if (!(await comparePassword(_user.account.password, password)))
return res.json({ ok: false });

const balance = await web3.eth.getBalance(_user.account.payload.address);
const balanceWeis = await web3.eth.getBalance(_user.account.payload.address);
const balance = web3.utils.fromWei(balanceWeis, "ether");

res.json({ balance });
res.json({
ok: true,
address: _user.account.payload.address,
balance,
});
};

export const createAccount = async (req: Request, res: Response) => {
Expand All @@ -35,6 +41,8 @@ export const createAccount = async (req: Request, res: Response) => {
const _user = await UserModel.findById(_id).populate("account");

if (!_user || _user.account.payload) {
console.log("here");

return res.json({ ok: false });
}

Expand All @@ -48,6 +56,8 @@ export const createAccount = async (req: Request, res: Response) => {

if (!_account) return res.json({ ok: false });

_account.password = await encryptPassword(password);

_account.payload = encryptedKey;

await _account.save();
Expand All @@ -56,7 +66,7 @@ export const createAccount = async (req: Request, res: Response) => {

_user.save();

res.json({ encryptedKey });
res.json({ ok: true });
};

export const importAccount = async (req: Request, res: Response) => {
Expand All @@ -76,6 +86,8 @@ export const importAccount = async (req: Request, res: Response) => {

const _account: Account = new AccountModel({ payload: encryptedKey });

_account.password = await encryptPassword(password);

await _account.save();

_user.account = _account;
Expand All @@ -95,12 +107,16 @@ export const getPrivateKey = async (req: Request, res: Response) => {
if (!_user || !_user.account.payload) {
return res.json({ ok: false });
}

if (!(await comparePassword(_user.account.password, password)))
return res.json({ ok: false });

const payloadKey = web3.eth.accounts.decrypt(
_user.account.payload as any,
password
);

res.json({ payloadKey });
res.json({ privateKey: payloadKey.privateKey });
};

export const transferTo = async (req: Request, res: Response) => {
Expand All @@ -112,6 +128,9 @@ export const transferTo = async (req: Request, res: Response) => {

if (!_user || !_user.account.payload) return res.json({ ok: false });

if (!(await comparePassword(_user.account.password, password)))
return res.json({ ok: false });

const payloadKey = web3.eth.accounts.decrypt(
_user.account.payload as any,
password
Expand All @@ -134,6 +153,7 @@ export const transferTo = async (req: Request, res: Response) => {
const result = await web3.eth.sendSignedTransaction(raw);

res.json({
ok: true,
status: result.status,
hash: result.transactionHash,
to: result.to,
Expand Down
1 change: 1 addition & 0 deletions cattleia-backend/src/libs/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface AccountPayload {
export interface Account extends Document {
_id?: mongoose.ObjectId;
payload: AccountPayload;
password: string;
}

export interface User extends Document {
Expand Down
3 changes: 3 additions & 0 deletions cattleia-backend/src/models/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const accountSchema = new Schema(
payload: {
type: Object,
},
password: {
type: String,
},
},
{
versionKey: false,
Expand Down
4 changes: 2 additions & 2 deletions cattleia-backend/src/routes/web3.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from "express";
import {
createAccount,
getBalance,
access,
getPrivateKey,
importAccount,
transferTo,
Expand All @@ -11,7 +11,7 @@ import { validateToken } from "../middlewares";

const router = Router();

router.get("/balance", validateToken, getBalance);
router.post("/access", validateToken, access);

router.post("/create-account", validateToken, createAccount);

Expand Down
8 changes: 4 additions & 4 deletions cattleia-backend/src/test/test.http
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ POST http://localhost:4000/api/v1/auth/sign-in HTTP/1.1
Content-Type: application/json

{
"userName":"aku",
"password":"test"
"userName":"aku2",
"password":"test2"
}

###
Expand Down Expand Up @@ -48,10 +48,10 @@ Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYWt1IiwiX

POST http://localhost:4000/api/v1/web3/import-account HTTP/1.1
Content-Type: application/json
Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYWt1MiIsIl9pZCI6IjYxMmJiNDRjOTk2OGU5MWVhODIyYjI5YyIsInJvbGUiOiJ1c2VyIiwidG9rZW5WZXJzaW9uIjowLCJpYXQiOjE2MzAyNTQxNTksImV4cCI6MTYzMDM0MDU1OX0.PGNkNRtpblQ_JtrtEJVkNU21a1ViwKPHi_qNZb7bi5g
Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYWt1MiIsIl9pZCI6IjYxMmVjZjU2MjYzNGE5MDVmNGJmODcxYyIsInJvbGUiOiJ1c2VyIiwidG9rZW5WZXJzaW9uIjowLCJpYXQiOjE2MzA0NTc2OTcsImV4cCI6MTYzMDU0NDA5N30.8Ut_GEg75J5bJVa2PN4SyEeVXCOb1uk2MQlTiy7rgZU

{
"privateKey":"9023f8d9b423b72f67464276115f59dfb614d6a1e4c49710cc133fbf3cdd9fb1",
"privateKey":"2587479acdce25311aee18357b73a01187e070707251f2763a64daa7b8a155a5",
"password":"test2"
}

Expand Down
26 changes: 20 additions & 6 deletions cattleia-native-app/App/Components/Inputs/Elements/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import styled from 'styled-components/native';

export const InputGroup = styled.View`
export const InputGroup = styled.View<{lm?: boolean}>`
display: flex;
flex-direction: column;
justify-content: flex-start;
margin: 20px 0px;
margin: ${p => (p.lm ? '6px 0px' : '20px 0px')};
position: relative;
`;

Expand All @@ -24,13 +24,27 @@ export const Label = styled.Text<{colors: any; btn?: boolean}>`
margin-bottom: 4px;
`;

export const Btn = styled.TouchableOpacity<{colors: any}>`
export const Btn = styled.TouchableOpacity<{
colors: any;
width?: string;
sec?: boolean;
alignLabel: 'start' | 'center' | 'end';
}>`
display: flex;
justify-content: center;
flex-direction: row;
justify-content: ${p =>
p.alignLabel === 'start'
? 'flex-start'
: p.alignLabel === 'center'
? p.alignLabel
: p.alignLabel === 'end'
? 'flex-end'
: 'center'};
align-items: center;
background-color: ${p => p.colors.primary};
width: 330px;
background-color: ${p => (p.sec ? p.colors.inputBg : p.colors.primary)};
width: ${p => (p.width ? p.width : '330px')};
height: 40px;
padding: 0px 10px;
`;

export const Help = styled.TouchableOpacity`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export const PasswordInput: React.FC<IPassword> = props => {
const colors = darkTheme ? theme.dark : theme.light;
const [show, setShow] = useState(true);
const [icon, setIcon] = useState({
name: 'eye-off-outline',
name: 'eye-off',
active: true,
});
return (
<InputGroup>
<Label colors={colors}>Password</Label>
<Label colors={colors}>{props.label ? props.label : 'Password'}</Label>
<IconP
onPress={() => {
setShow(c => !c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface IPassword {
helpHandler?: () => void;
handler: (e: NativeSyntheticEvent<TextInputChangeEventData>) => void;
value: any;
label?: string;
}
14 changes: 6 additions & 8 deletions cattleia-native-app/App/Components/Inputs/SubmitBtn/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import React from 'react';
import {InputGroup, Btn, Label} from '../Elements';
import {ISubmit} from './interfaces';
import {theme} from '../../../utils';
import {useSelector} from 'react-redux';
import {RootState} from '../../../redux/store';

export const SubmitBtn: React.FC<ISubmit> = props => {
const darkTheme = useSelector((state: RootState) => state.themeReducer.dark);
const colors = darkTheme ? theme.dark : theme.light;
return (
<InputGroup>
<InputGroup lm={props.lm}>
<Btn
colors={colors}
alignLabel={props.alignLabel || 'center'}
sec={props.sec}
width={props.width}
colors={props.colors}
style={{
borderRadius: 4,
}}
onPress={props.handler}>
<Label btn colors={colors}>
<Label btn colors={props.colors}>
{props.label}
</Label>
</Btn>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export interface ISubmit {
label: string;
handler: () => void;
width?: string;
colors: any;
lm?: boolean;
sec?: boolean;
alignLabel?: 'start' | 'center' | 'end';
}
16 changes: 0 additions & 16 deletions cattleia-native-app/App/pages/HomePage/Elements/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,3 @@ export const Title = styled.Text<{mb: string; colors: any}>`
font-weight: bold;
margin-bottom: ${p => p.mb};
`;

export const Btn = styled.TouchableOpacity<{bg: string}>`
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
background-color: ${p => p.bg};
width: 200px;
height: 40px;
margin: 6px 0px;
`;

export const Txt = styled.Text<{colors: any; btn?: boolean}>`
color: ${p => (p.btn ? p.colors.inputTxt : p.colors.fontPrimary)};
margin-left: 20px;
`;
Loading