From ec0f1b56ed43b1d663c437b4b3e1897cce8eaf33 Mon Sep 17 00:00:00 2001 From: Aku-mi Date: Mon, 30 Aug 2021 23:12:26 -0500 Subject: [PATCH 1/3] Wallet Auth Wallet Account Verification and some refactories. --- .../App/Components/Inputs/Elements/index.tsx | 26 +++- .../Components/Inputs/PasswordInput/index.tsx | 4 +- .../Inputs/PasswordInput/interfaces.ts | 1 + .../App/Components/Inputs/SubmitBtn/index.tsx | 14 +- .../Components/Inputs/SubmitBtn/interfaces.ts | 5 + .../App/pages/HomePage/Elements/index.tsx | 16 --- .../App/pages/HomePage/index.tsx | 59 ++++----- cattleia-native-app/App/pages/Login/index.tsx | 7 +- .../App/pages/Register/index.tsx | 14 +- .../App/pages/Wallet/Access/index.tsx | 41 ++++++ .../App/pages/Wallet/CreateAccount/index.tsx | 46 +++++++ .../pages/Wallet/Elements/Wallet/index.tsx | 78 +++++++++++ .../App/pages/Wallet/Elements/index.tsx | 77 ++--------- .../App/pages/Wallet/ImportAccount/index.tsx | 56 ++++++++ .../App/pages/Wallet/MainWallet/index.tsx | 112 ++++++++++++++++ .../App/pages/Wallet/NoWallet/index.tsx | 46 +++++++ .../App/pages/Wallet/index.tsx | 122 ++++-------------- cattleia-native-app/App/pages/index.tsx | 2 +- 18 files changed, 492 insertions(+), 234 deletions(-) create mode 100644 cattleia-native-app/App/pages/Wallet/Access/index.tsx create mode 100644 cattleia-native-app/App/pages/Wallet/CreateAccount/index.tsx create mode 100644 cattleia-native-app/App/pages/Wallet/Elements/Wallet/index.tsx create mode 100644 cattleia-native-app/App/pages/Wallet/ImportAccount/index.tsx create mode 100644 cattleia-native-app/App/pages/Wallet/MainWallet/index.tsx create mode 100644 cattleia-native-app/App/pages/Wallet/NoWallet/index.tsx diff --git a/cattleia-native-app/App/Components/Inputs/Elements/index.tsx b/cattleia-native-app/App/Components/Inputs/Elements/index.tsx index 03412cc..49091c3 100644 --- a/cattleia-native-app/App/Components/Inputs/Elements/index.tsx +++ b/cattleia-native-app/App/Components/Inputs/Elements/index.tsx @@ -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; `; @@ -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` diff --git a/cattleia-native-app/App/Components/Inputs/PasswordInput/index.tsx b/cattleia-native-app/App/Components/Inputs/PasswordInput/index.tsx index 0b89e64..904c57e 100644 --- a/cattleia-native-app/App/Components/Inputs/PasswordInput/index.tsx +++ b/cattleia-native-app/App/Components/Inputs/PasswordInput/index.tsx @@ -22,12 +22,12 @@ export const PasswordInput: React.FC = 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 ( - + { setShow(c => !c); diff --git a/cattleia-native-app/App/Components/Inputs/PasswordInput/interfaces.ts b/cattleia-native-app/App/Components/Inputs/PasswordInput/interfaces.ts index a7847b6..1c8a01c 100644 --- a/cattleia-native-app/App/Components/Inputs/PasswordInput/interfaces.ts +++ b/cattleia-native-app/App/Components/Inputs/PasswordInput/interfaces.ts @@ -6,4 +6,5 @@ export interface IPassword { helpHandler?: () => void; handler: (e: NativeSyntheticEvent) => void; value: any; + label?: string; } diff --git a/cattleia-native-app/App/Components/Inputs/SubmitBtn/index.tsx b/cattleia-native-app/App/Components/Inputs/SubmitBtn/index.tsx index 009e4f2..e35a3c9 100644 --- a/cattleia-native-app/App/Components/Inputs/SubmitBtn/index.tsx +++ b/cattleia-native-app/App/Components/Inputs/SubmitBtn/index.tsx @@ -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 = props => { - const darkTheme = useSelector((state: RootState) => state.themeReducer.dark); - const colors = darkTheme ? theme.dark : theme.light; return ( - + - diff --git a/cattleia-native-app/App/Components/Inputs/SubmitBtn/interfaces.ts b/cattleia-native-app/App/Components/Inputs/SubmitBtn/interfaces.ts index af74738..742c763 100644 --- a/cattleia-native-app/App/Components/Inputs/SubmitBtn/interfaces.ts +++ b/cattleia-native-app/App/Components/Inputs/SubmitBtn/interfaces.ts @@ -1,4 +1,9 @@ export interface ISubmit { label: string; handler: () => void; + width?: string; + colors: any; + lm?: boolean; + sec?: boolean; + alignLabel?: 'start' | 'center' | 'end'; } diff --git a/cattleia-native-app/App/pages/HomePage/Elements/index.tsx b/cattleia-native-app/App/pages/HomePage/Elements/index.tsx index 46309e0..467745d 100644 --- a/cattleia-native-app/App/pages/HomePage/Elements/index.tsx +++ b/cattleia-native-app/App/pages/HomePage/Elements/index.tsx @@ -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; -`; diff --git a/cattleia-native-app/App/pages/HomePage/index.tsx b/cattleia-native-app/App/pages/HomePage/index.tsx index 7bee62b..254040b 100644 --- a/cattleia-native-app/App/pages/HomePage/index.tsx +++ b/cattleia-native-app/App/pages/HomePage/index.tsx @@ -1,7 +1,8 @@ import React from 'react'; -import {Btn, Container, Title, Txt} from './Elements'; +import {Container, Title} from './Elements'; import {NativeStackScreenProps} from '@react-navigation/native-stack'; import {theme} from '../../utils'; +import {SubmitBtn} from '../../Components/Inputs'; import {useSelector} from 'react-redux'; import {RootState} from '../../redux/store'; @@ -26,42 +27,38 @@ export const HomePage: React.FC = ({navigation}) => { to Cattleia - { + { navigation.navigate('Register'); }} - style={{ - borderRadius: 4, - }}> - - Sign Up - - - { + colors={colors} + label="Sign Up" + alignLabel="start" + /> + { navigation.navigate('Login'); }} - style={{ - borderRadius: 4, - }}> - - Sign In - - - { + colors={colors} + label="Sign In" + alignLabel="start" + /> + { navigation.navigate('Main'); }} - style={{ - borderRadius: 4, - }}> - - Profile - - + colors={colors} + label="Profile" + alignLabel="start" + /> ); }; diff --git a/cattleia-native-app/App/pages/Login/index.tsx b/cattleia-native-app/App/pages/Login/index.tsx index 71413e5..194bbdd 100644 --- a/cattleia-native-app/App/pages/Login/index.tsx +++ b/cattleia-native-app/App/pages/Login/index.tsx @@ -7,13 +7,17 @@ import {Post} from '../../services'; import {IAuth} from '../../types'; import {useDispatch} from 'react-redux'; import {saveUser} from '../../redux/user'; +import {RootState} from '../../redux/store'; +import {theme} from '../../utils'; +import {useSelector} from 'react-redux'; type Props = NativeStackScreenProps<{Main: undefined}, 'Main'>; export const Login: React.FC = ({navigation}) => { const {values, handler} = useInputHandler({userName: '', password: ''}); const dispatch = useDispatch(); - + const darkTheme = useSelector((state: RootState) => state.themeReducer.dark); + const colors = darkTheme ? theme.dark : theme.light; return ( = ({navigation}) => { value={values.password} /> { const res = await Post('/auth/sign-in', values); diff --git a/cattleia-native-app/App/pages/Register/index.tsx b/cattleia-native-app/App/pages/Register/index.tsx index c3b3dda..731dffe 100644 --- a/cattleia-native-app/App/pages/Register/index.tsx +++ b/cattleia-native-app/App/pages/Register/index.tsx @@ -10,15 +10,20 @@ import {NativeStackScreenProps} from '@react-navigation/native-stack'; import {useInputHandler} from '../../hooks'; import {Post} from '../../services'; import {IAuth} from '../../types'; +import {RootState} from '../../redux/store'; +import {theme} from '../../utils'; +import {useSelector} from 'react-redux'; -type RootStackParamList = { - Main: undefined; - Register: {}; +type ParamList = { + Main: {}; + Register: undefined; }; -type Props = NativeStackScreenProps; +type Props = NativeStackScreenProps; export const Register: React.FC = ({navigation}) => { + const darkTheme = useSelector((state: RootState) => state.themeReducer.dark); + const colors = darkTheme ? theme.dark : theme.light; const {values, handler} = useInputHandler({ email: '', name: '', @@ -49,6 +54,7 @@ export const Register: React.FC = ({navigation}) => { handler={handler('password')} /> { const res = await Post('/auth/sign-up', values); diff --git a/cattleia-native-app/App/pages/Wallet/Access/index.tsx b/cattleia-native-app/App/pages/Wallet/Access/index.tsx new file mode 100644 index 0000000..17fb99a --- /dev/null +++ b/cattleia-native-app/App/pages/Wallet/Access/index.tsx @@ -0,0 +1,41 @@ +import React from 'react'; +import {Header, Container, Wrapper} from '../Elements'; +import {useSelector} from 'react-redux'; +import {theme} from '../../../utils'; +import {RootState} from '../../../redux/store'; +import {PasswordInput, SubmitBtn} from '../../../Components/Inputs'; +import {useInputHandler} from '../../../hooks'; + +import {NativeStackScreenProps} from '@react-navigation/native-stack'; + +type ParamList = { + MainWallet: undefined; + Access: undefined; +}; + +type Props = NativeStackScreenProps; + +export const Access: React.FC = ({navigation}) => { + const darkTheme = useSelector((state: RootState) => state.themeReducer.dark); + const colors = darkTheme ? theme.dark : theme.light; + const {values, handler} = useInputHandler({password: ''}); + return ( + +
Go to your
+
Wallet
+ + + + + navigation.navigate('MainWallet')} + /> + +
+ ); +}; diff --git a/cattleia-native-app/App/pages/Wallet/CreateAccount/index.tsx b/cattleia-native-app/App/pages/Wallet/CreateAccount/index.tsx new file mode 100644 index 0000000..b5eb234 --- /dev/null +++ b/cattleia-native-app/App/pages/Wallet/CreateAccount/index.tsx @@ -0,0 +1,46 @@ +import React from 'react'; +import {Header, Container, Wrapper} from '../Elements'; +import {useSelector} from 'react-redux'; +import {theme} from '../../../utils'; +import {RootState} from '../../../redux/store'; +import {PasswordInput, SubmitBtn} from '../../../Components/Inputs'; +import {useInputHandler} from '../../../hooks'; + +import {NativeStackScreenProps} from '@react-navigation/native-stack'; + +type ParamList = { + MainWallet: undefined; + CreateAccount: undefined; +}; + +type Props = NativeStackScreenProps; + +export const CreateAccount: React.FC = ({navigation}) => { + const darkTheme = useSelector((state: RootState) => state.themeReducer.dark); + const colors = darkTheme ? theme.dark : theme.light; + const {values, handler} = useInputHandler({password: '', password2: ''}); + return ( + +
Create
+
Account
+ + + + + + navigation.navigate('MainWallet')} + /> + +
+ ); +}; diff --git a/cattleia-native-app/App/pages/Wallet/Elements/Wallet/index.tsx b/cattleia-native-app/App/pages/Wallet/Elements/Wallet/index.tsx new file mode 100644 index 0000000..97ae793 --- /dev/null +++ b/cattleia-native-app/App/pages/Wallet/Elements/Wallet/index.tsx @@ -0,0 +1,78 @@ +import styled from 'styled-components/native'; + +export const Container = styled.View<{color: string}>` + display: flex; + flex-direction: column; + width: 100%; + background-color: ${p => p.color}; +`; + +export const Section = styled.View<{heigth: string; border?: boolean}>` + display: flex; + flex-direction: column; + justify-content: flex-start; + align-items: center; + width: 100%; + padding: 20px 0px; + height: ${p => p.heigth}; + border-top-width: ${p => (p.border ? '1px' : '0px')}; +`; + +export const Logo = styled.Image` + width: 117px; + height: 111px; + margin: 5px 0px; +`; + +export const Balance = styled.View` + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + width: 100%; + margin: 20px 0px; +`; + +export const MainTxt = styled.Text<{color: string}>` + font-size: 20px; + font-weight: bold; + margin: 10px 0px; + color: ${p => p.color}; +`; + +export const Txt = styled.Text<{color: string}>` + font-size: 16px; + margin: 10px 0px; + color: ${p => p.color}; +`; + +export const MainBtns = styled.View` + display: flex; + flex-direction: row; + justify-content: space-around; + align-items: center; + width: 100%; + padding: 5px 40px; +`; + +export const Grapper = styled.View` + display: flex; + flex-direction: row; + justify-content: flex-start; + align-items: center; + width: 90%; +`; + +export const Option = styled.TouchableOpacity<{ + direction: string; + justify: string; + width: string; + padding: string; +}>` + display: flex; + flex-direction: ${p => p.direction}; + align-items: center; + justify-content: ${p => p.justify}; + width: ${p => p.width}; + padding: ${p => p.padding}; +`; diff --git a/cattleia-native-app/App/pages/Wallet/Elements/index.tsx b/cattleia-native-app/App/pages/Wallet/Elements/index.tsx index 97ae793..d5152f4 100644 --- a/cattleia-native-app/App/pages/Wallet/Elements/index.tsx +++ b/cattleia-native-app/App/pages/Wallet/Elements/index.tsx @@ -1,78 +1,25 @@ import styled from 'styled-components/native'; -export const Container = styled.View<{color: string}>` - display: flex; - flex-direction: column; - width: 100%; - background-color: ${p => p.color}; -`; - -export const Section = styled.View<{heigth: string; border?: boolean}>` - display: flex; - flex-direction: column; - justify-content: flex-start; - align-items: center; - width: 100%; - padding: 20px 0px; - height: ${p => p.heigth}; - border-top-width: ${p => (p.border ? '1px' : '0px')}; -`; - -export const Logo = styled.Image` - width: 117px; - height: 111px; - margin: 5px 0px; -`; - -export const Balance = styled.View` +export const Container = styled.View` display: flex; flex-direction: column; justify-content: center; - align-items: center; + align-items: flex-start; width: 100%; - margin: 20px 0px; + height: 100%; + padding: 30px; `; -export const MainTxt = styled.Text<{color: string}>` - font-size: 20px; +export const Header = styled.Text<{colors: any}>` + color: ${p => p.colors.primary}; + font-size: 30px; font-weight: bold; - margin: 10px 0px; - color: ${p => p.color}; `; -export const Txt = styled.Text<{color: string}>` - font-size: 16px; - margin: 10px 0px; - color: ${p => p.color}; -`; - -export const MainBtns = styled.View` - display: flex; - flex-direction: row; - justify-content: space-around; - align-items: center; - width: 100%; - padding: 5px 40px; -`; - -export const Grapper = styled.View` +export const Wrapper = styled.View<{mt: string}>` display: flex; - flex-direction: row; - justify-content: flex-start; - align-items: center; - width: 90%; -`; - -export const Option = styled.TouchableOpacity<{ - direction: string; - justify: string; - width: string; - padding: string; -}>` - display: flex; - flex-direction: ${p => p.direction}; - align-items: center; - justify-content: ${p => p.justify}; - width: ${p => p.width}; - padding: ${p => p.padding}; + flex-direction: column; + align-items: flex-start; + justify-content: center; + margin: ${p => p.mt}; `; diff --git a/cattleia-native-app/App/pages/Wallet/ImportAccount/index.tsx b/cattleia-native-app/App/pages/Wallet/ImportAccount/index.tsx new file mode 100644 index 0000000..33c97cd --- /dev/null +++ b/cattleia-native-app/App/pages/Wallet/ImportAccount/index.tsx @@ -0,0 +1,56 @@ +import React from 'react'; +import {Header, Container, Wrapper} from '../Elements'; +import {useSelector} from 'react-redux'; +import {theme} from '../../../utils'; +import {RootState} from '../../../redux/store'; +import {SubmitBtn, PasswordInput} from '../../../Components/Inputs'; +import {useInputHandler} from '../../../hooks'; + +import {NativeStackScreenProps} from '@react-navigation/native-stack'; + +type ParamList = { + MainWallet: undefined; + ImportAccount: undefined; +}; + +type Props = NativeStackScreenProps; + +export const ImportAccount: React.FC = ({navigation}) => { + const {values, handler} = useInputHandler({ + password: '', + password2: '', + privateKey: '', + }); + const darkTheme = useSelector((state: RootState) => state.themeReducer.dark); + const colors = darkTheme ? theme.dark : theme.light; + return ( + +
Import
+
Account
+ + + + + + + + navigation.navigate('MainWallet')} + /> + +
+ ); +}; diff --git a/cattleia-native-app/App/pages/Wallet/MainWallet/index.tsx b/cattleia-native-app/App/pages/Wallet/MainWallet/index.tsx new file mode 100644 index 0000000..d2a08a0 --- /dev/null +++ b/cattleia-native-app/App/pages/Wallet/MainWallet/index.tsx @@ -0,0 +1,112 @@ +import React from 'react'; +import {useSelector} from 'react-redux'; +import {RootState} from '../../../redux/store'; +import {theme} from '../../../utils'; +import { + MainTxt, + Option, + MainBtns, + Txt, + Grapper, + Section, + Container, + Balance, + Logo, +} from '../Elements/Wallet'; + +import IconM from 'react-native-vector-icons/MaterialCommunityIcons'; +import IconI from 'react-native-vector-icons/Ionicons'; + +export const MainWallet: React.FC = () => { + const darkTheme = useSelector((state: RootState) => state.themeReducer.dark); + + const user = useSelector((state: RootState) => state.userReducer.user); + + const colors = darkTheme ? theme.dark : theme.light; + return ( + +
+ + Account: {user.userName} + Address: 0xd3f8...708f +
+
+ + + 10.221001 ETH + + + + + + +
+
+ + +
+
+ ); +}; diff --git a/cattleia-native-app/App/pages/Wallet/NoWallet/index.tsx b/cattleia-native-app/App/pages/Wallet/NoWallet/index.tsx new file mode 100644 index 0000000..829f695 --- /dev/null +++ b/cattleia-native-app/App/pages/Wallet/NoWallet/index.tsx @@ -0,0 +1,46 @@ +import React from 'react'; +import {Header, Container, Wrapper} from '../Elements'; +import {useSelector} from 'react-redux'; +import {theme} from '../../../utils'; +import {RootState} from '../../../redux/store'; +import {SubmitBtn} from '../../../Components/Inputs'; + +import {NativeStackScreenProps} from '@react-navigation/native-stack'; + +type ParamList = { + ImportAccount: undefined; + CreateAccount: undefined; + NoWallet: undefined; +}; + +type Props = NativeStackScreenProps; + +export const NoWallet: React.FC = ({navigation}) => { + const darkTheme = useSelector((state: RootState) => state.themeReducer.dark); + const colors = darkTheme ? theme.dark : theme.light; + return ( + +
Account's
+
Wallet
+ + navigation.navigate('ImportAccount')} + /> + navigation.navigate('CreateAccount')} + /> + +
+ ); +}; diff --git a/cattleia-native-app/App/pages/Wallet/index.tsx b/cattleia-native-app/App/pages/Wallet/index.tsx index 79ce63f..b72987d 100644 --- a/cattleia-native-app/App/pages/Wallet/index.tsx +++ b/cattleia-native-app/App/pages/Wallet/index.tsx @@ -1,112 +1,34 @@ import React from 'react'; +import {createNativeStackNavigator} from '@react-navigation/native-stack'; import {useSelector} from 'react-redux'; import {RootState} from '../../redux/store'; import {theme} from '../../utils'; -import { - MainTxt, - Option, - MainBtns, - Txt, - Grapper, - Section, - Container, - Balance, - Logo, -} from './Elements'; +import {MainWallet} from './MainWallet'; +import {NoWallet} from './NoWallet'; +import {ImportAccount} from './ImportAccount'; +import {CreateAccount} from './CreateAccount'; +import {Access} from './Access'; -import IconM from 'react-native-vector-icons/MaterialCommunityIcons'; -import IconI from 'react-native-vector-icons/Ionicons'; +const Stack = createNativeStackNavigator(); export const Wallet: React.FC = () => { const darkTheme = useSelector((state: RootState) => state.themeReducer.dark); - - const user = useSelector((state: RootState) => state.userReducer.user); - const colors = darkTheme ? theme.dark : theme.light; return ( - -
- - Account: {user.userName} - Address: 0xd3f8...708f -
-
- - - 10.221001 ETH - - - - - - -
-
- - -
-
+ + + + + + + ); }; diff --git a/cattleia-native-app/App/pages/index.tsx b/cattleia-native-app/App/pages/index.tsx index b5d512e..f052c73 100644 --- a/cattleia-native-app/App/pages/index.tsx +++ b/cattleia-native-app/App/pages/index.tsx @@ -10,10 +10,10 @@ import {Register} from './Register'; import {setMode} from '../redux/theme'; import {Settins} from './Settings'; import {Product} from './Main/Shop/Product'; +import {Wallet} from './Wallet'; import {Login} from './Login'; import {About} from './About'; import {Main} from './Main'; -import {Wallet} from './Wallet'; const Stack = createNativeStackNavigator(); From fdaff8c46ae820db58995367ed9979f365784959 Mon Sep 17 00:00:00 2001 From: Aku-mi Date: Tue, 31 Aug 2021 22:02:13 -0500 Subject: [PATCH 2/3] Some simple changes Password Encryption --- .../src/controllers/auth.controller.ts | 7 +++- .../src/controllers/web3.controller.ts | 36 ++++++++++++++----- cattleia-backend/src/libs/interfaces.ts | 1 + cattleia-backend/src/models/account.ts | 3 ++ cattleia-backend/src/routes/web3.routes.ts | 4 +-- cattleia-backend/src/test/test.http | 8 ++--- 6 files changed, 44 insertions(+), 15 deletions(-) diff --git a/cattleia-backend/src/controllers/auth.controller.ts b/cattleia-backend/src/controllers/auth.controller.ts index 064dbc3..abf3984 100644 --- a/cattleia-backend/src/controllers/auth.controller.ts +++ b/cattleia-backend/src/controllers/auth.controller.ts @@ -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); @@ -36,6 +38,7 @@ export const signIn = async ( role: _user.role.name, email: _user.email, token: createAcessToken(_user), + hasAccount: _user.account.payload != undefined, }, }); } @@ -72,6 +75,7 @@ export const signUp = async ( const _account = new AccountModel({ payload: null, + password: "", }); _account.save(); @@ -89,6 +93,7 @@ export const signUp = async ( role: _user.role.name, email: _user.email, token: createAcessToken(_user), + hasAccount: false, }, }); } diff --git a/cattleia-backend/src/controllers/web3.controller.ts b/cattleia-backend/src/controllers/web3.controller.ts index 172d993..1fd44cb 100644 --- a/cattleia-backend/src/controllers/web3.controller.ts +++ b/cattleia-backend/src/controllers/web3.controller.ts @@ -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); @@ -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) => { @@ -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 }); } @@ -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(); @@ -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) => { @@ -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; @@ -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) => { @@ -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 @@ -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, diff --git a/cattleia-backend/src/libs/interfaces.ts b/cattleia-backend/src/libs/interfaces.ts index 287526f..9dd8628 100644 --- a/cattleia-backend/src/libs/interfaces.ts +++ b/cattleia-backend/src/libs/interfaces.ts @@ -15,6 +15,7 @@ export interface AccountPayload { export interface Account extends Document { _id?: mongoose.ObjectId; payload: AccountPayload; + password: string; } export interface User extends Document { diff --git a/cattleia-backend/src/models/account.ts b/cattleia-backend/src/models/account.ts index 98aa2fc..dfaad6e 100644 --- a/cattleia-backend/src/models/account.ts +++ b/cattleia-backend/src/models/account.ts @@ -6,6 +6,9 @@ const accountSchema = new Schema( payload: { type: Object, }, + password: { + type: String, + }, }, { versionKey: false, diff --git a/cattleia-backend/src/routes/web3.routes.ts b/cattleia-backend/src/routes/web3.routes.ts index 7da2393..07f4846 100644 --- a/cattleia-backend/src/routes/web3.routes.ts +++ b/cattleia-backend/src/routes/web3.routes.ts @@ -1,7 +1,7 @@ import { Router } from "express"; import { createAccount, - getBalance, + access, getPrivateKey, importAccount, transferTo, @@ -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); diff --git a/cattleia-backend/src/test/test.http b/cattleia-backend/src/test/test.http index 8216f3f..90051dd 100644 --- a/cattleia-backend/src/test/test.http +++ b/cattleia-backend/src/test/test.http @@ -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" } ### @@ -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" } From 8eff61330f7082801c225bc0559899414b796c39 Mon Sep 17 00:00:00 2001 From: Aku-mi Date: Tue, 31 Aug 2021 22:03:13 -0500 Subject: [PATCH 3/3] API implementation --- .../App/pages/Main/Profile/index.tsx | 3 + .../App/pages/Wallet/Access/index.tsx | 19 ++++- .../App/pages/Wallet/CreateAccount/index.tsx | 21 ++++- .../App/pages/Wallet/ImportAccount/index.tsx | 21 ++++- .../App/pages/Wallet/MainWallet/index.tsx | 77 +++++++++++++++++-- .../App/pages/Wallet/Send/index.tsx | 57 ++++++++++++++ .../App/pages/Wallet/index.tsx | 3 +- cattleia-native-app/App/redux/user.ts | 2 + cattleia-native-app/App/types/index.ts | 3 + cattleia-native-app/package.json | 1 + cattleia-native-app/yarn.lock | 5 ++ 11 files changed, 197 insertions(+), 15 deletions(-) create mode 100644 cattleia-native-app/App/pages/Wallet/Send/index.tsx diff --git a/cattleia-native-app/App/pages/Main/Profile/index.tsx b/cattleia-native-app/App/pages/Main/Profile/index.tsx index 6399dac..e6ddadd 100644 --- a/cattleia-native-app/App/pages/Main/Profile/index.tsx +++ b/cattleia-native-app/App/pages/Main/Profile/index.tsx @@ -30,6 +30,9 @@ export const Profile: React.FC = ({navigation}) => { Role: {user.role} + + Has Account: {user.hasAccount ? 'yes' : 'no'} +