From b60aa8be4a3c77daa2f2b30ae6b58fc50986ce25 Mon Sep 17 00:00:00 2001 From: Mustafa Buyukcelebi Date: Sun, 6 Jan 2019 23:48:56 +0300 Subject: [PATCH] Changed setUserDataWithPinCode function with new structure --- src/providers/steem/auth.js | 45 ++++++++++++++++++------------------- src/realm/realm.js | 4 +++- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/providers/steem/auth.js b/src/providers/steem/auth.js index 92979a3f1..ac8d4dbc2 100644 --- a/src/providers/steem/auth.js +++ b/src/providers/steem/auth.js @@ -9,6 +9,7 @@ import { getUserData, setSCAccount, getSCAccount, + setPinCode, } from '../../realm/realm'; import { encryptKey, decryptKey } from '../../utils/crypto'; import steemConnect from './steemConnectAPI'; @@ -128,30 +129,30 @@ export const loginWithSC2 = async (code) => { }; export const setUserDataWithPinCode = async (data) => { - const result = getUserDataWithUsername(data.username); - const userData = result[0]; + try { + const result = getUserDataWithUsername(data.username); + const userData = result[0]; - const privateKeys = getPrivateKeys(userData.username, data.password); + const privateKeys = getPrivateKeys(userData.username, data.password); - const updatedUserData = { - username: userData.username, - authType: userData.authType, - accessToken: - userData.authType === 'steemConnect' ? encryptKey(data.accessToken, data.pinCode) : '', - masterKey: userData.authType === 'masterKey' ? encryptKey(data.password, data.pinCode) : '', - postingKey: encryptKey(privateKeys.posting.toString(), data.pinCode), - activeKey: encryptKey(privateKeys.active.toString(), data.pinCode), - memoKey: encryptKey(privateKeys.memo.toString(), data.pinCode), - }; + const updatedUserData = { + username: userData.username, + authType: userData.authType, + accessToken: + userData.authType === 'steemConnect' ? encryptKey(data.accessToken, data.pinCode) : '', + masterKey: userData.authType === 'masterKey' ? encryptKey(data.password, data.pinCode) : '', + postingKey: encryptKey(privateKeys.posting.toString(), data.pinCode), + activeKey: encryptKey(privateKeys.active.toString(), data.pinCode), + memoKey: encryptKey(privateKeys.memo.toString(), data.pinCode), + }; - const response = await updateUserData(updatedUserData); - const authData = { - isLoggedIn: true, - currentUsername: userData.username, - }; + await setPinCode(data.pinCode); + await updateUserData(updatedUserData); - await setAuthStatus(authData); - return response; + return updatedUserData; + } catch (error) { + return Promise.reject(new Error('Unknown error, please contact to eSteem.')); + } }; export const updatePinCode = async (data) => { @@ -209,9 +210,7 @@ export const verifyPinCode = async (data) => { const newSCAccountData = await getSCAccessToken(scAccount.refreshToken); await setSCAccount(newSCAccountData); accessToken = newSCAccountData.access_token; - await updateUserData( - { ...userData, accessToken: encryptKey(accessToken, data.pinCode) }, - ); + await updateUserData({ ...userData, accessToken: encryptKey(accessToken, data.pinCode) }); } await steemConnect.setAccessToken(accessToken); account = await steemConnect.me(); diff --git a/src/realm/realm.js b/src/realm/realm.js index 89123ec14..45189b23f 100644 --- a/src/realm/realm.js +++ b/src/realm/realm.js @@ -1,4 +1,5 @@ import Realm from 'realm'; +import sha256 from 'crypto-js/sha256'; // CONSTANTS const USER_SCHEMA = 'user'; @@ -294,9 +295,10 @@ export const updateCurrentUsername = username => new Promise((resolve, reject) = export const setPinCode = pinCode => new Promise((resolve, reject) => { try { const auth = realm.objects(AUTH_SCHEMA); + const pinHash = sha256(pinCode); realm.write(() => { - auth[0].pinCode = pinCode; + auth[0].pinCode = pinHash; resolve(auth[0]); }); } catch (error) {