diff --git a/src/config/ecencyApi.js b/src/config/ecencyApi.js index 3c8abdb68..0b735883d 100644 --- a/src/config/ecencyApi.js +++ b/src/config/ecencyApi.js @@ -1,6 +1,8 @@ import axios from 'axios'; import Config from 'react-native-config'; import VersionNumber from 'react-native-version-number'; +import {store} from '../redux/store/store'; +import {get} from 'lodash'; const api = axios.create({ baseURL: Config.ECENCY_BACKEND_API, @@ -12,6 +14,19 @@ const api = axios.create({ api.interceptors.request.use((request) => { console.log('Starting ecency Request', request); + + const state = store.getState(); + const accessToken = get(state, 'account.currentAccount.accessToken'); + if(accessToken){ + if(!request.data){ + request.data = {} + } + request.data.code = accessToken; + console.log('Added access token:', accessToken) + }else{ + console.warn("No access token available") + } + return request; }); diff --git a/src/providers/hive/auth.js b/src/providers/hive/auth.js index 99e5305de..211d23628 100644 --- a/src/providers/hive/auth.js +++ b/src/providers/hive/auth.js @@ -22,6 +22,8 @@ import { getSCAccessToken } from '../ecency/ecency'; // Constants import AUTH_TYPE from '../../constants/authType'; +import { cryptoUtils } from '@hiveio/dhive'; +import { makeHsCode } from '../../utils/hive-signer-helper'; export const login = async (username, password, isPinCodeOpen) => { let loginFlag = false; @@ -62,6 +64,22 @@ export const login = async (username, password, isPinCodeOpen) => { } }); + + // Prepare hivesigner code + const signer = (message) => { + const hash = cryptoUtils.sha256(message) + return new Promise( + (resolve) => { + const key = privateKeys['activeKey'] + const signedKey = key.sign(hash) + const signedStr = signedKey.toString(); + resolve(signedStr) + } + ); + } + const code = await makeHsCode(account.name, signer); + const scTokens = await getSCAccessToken(code); + let jsonMetadata; try { jsonMetadata = JSON.parse(account.posting_json_metadata) || ''; @@ -101,6 +119,7 @@ export const login = async (username, password, isPinCodeOpen) => { currentUsername: username, }; await setAuthStatus(authData); + await setSCAccount(scTokens); // Save user data to Realm DB await setUserData(account.local); @@ -108,6 +127,7 @@ export const login = async (username, password, isPinCodeOpen) => { return { ...account, password, + accessToken:get(scTokens, 'access_token', '') }; } return Promise.reject(new Error('auth.invalid_credentials'));