From 2c3345b944d969652a815c42b0450147df39e133 Mon Sep 17 00:00:00 2001 From: Daniel Laanpere Date: Thu, 31 Oct 2019 11:29:06 +0200 Subject: [PATCH] device address selector fix, hub login with keys --- src/sagas/device.js | 23 ++++++++++++++++------- src/selectors/device.js | 26 ++++++++++++++++++++------ src/selectors/wallet.js | 11 ----------- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/sagas/device.js b/src/sagas/device.js index 2c1f215a..f3f80016 100644 --- a/src/sagas/device.js +++ b/src/sagas/device.js @@ -5,8 +5,9 @@ import { oClient, getDeviceMessageHashToSign } from './../lib/OCustom'; import { actionTypes } from './../constants'; import { setExchangeRates } from './../actions/exchangeRates'; import { - selectPermanentDeviceKeyObj, selectDeviceAddress, + selectPermanentDeviceKeyObj, + selectDeviceTempKeyData, } from './../selectors/device'; @@ -45,26 +46,34 @@ export function* watchHubMessages() { console.log(payload.body); } } + console.log(type, payload); } } export function* loginToHub(challenge) { + const deviceAddress = yield select(selectDeviceAddress()); const permanentDeviceKey = yield select(selectPermanentDeviceKeyObj()); - - const objLogin = { challenge, pubkey: permanentDeviceKey.pub_b64 }; + const tempDeviceKeyData = yield select(selectDeviceTempKeyData()); + + const objLogin = { challenge, pubkey: permanentDeviceKey.pubB64 }; objLogin.signature = sign( getDeviceMessageHashToSign(objLogin), permanentDeviceKey.priv, ); oClient.justsaying('hub/login', objLogin); -} + const objTempPubkey = { + temp_pubkey: tempDeviceKeyData.pubB64, + pubkey: permanentDeviceKey.pubB64, + }; + objTempPubkey.signature = sign(getDeviceMessageHashToSign(objTempPubkey), permanentDeviceKey.priv); + oClient.api.tempPubkey(objTempPubkey) + .then(result => console.log('Temp pubkey result', result)) + .catch(e => console.log('Temp pubkey error', e)); -export function* rotateDeviceTempKey() { - const ROTATION_PERIOD = 3600 * 1000; + oClient.justsaying('hub/refresh', null); } export default function* watch() { - yield takeEvery(actionTypes.DEVICE_TEMP_KEY_ROTATE, rotateDeviceTempKey); yield watchHubMessages(); } \ No newline at end of file diff --git a/src/selectors/device.js b/src/selectors/device.js index 94e2d7ee..a6d783a0 100644 --- a/src/selectors/device.js +++ b/src/selectors/device.js @@ -1,12 +1,12 @@ import Mnemonic from 'bitcore-mnemonic'; import { createSelector } from 'reselect'; import { publicKeyCreate } from 'secp256k1'; -import { toWif, fromWif } from 'obyte/lib/utils'; +import { toWif, fromWif, getChash160 } from 'obyte/lib/utils'; import { testnet } from './../lib/OCustom'; import { getWalletState } from './wallet'; -export const getAppState = (state) => state.main.app; +export const getDeviceState = (state) => state.main.device; export const selectDeviceWif = () => createSelector( getWalletState, @@ -21,6 +21,16 @@ export const selectDeviceWif = () => createSelector( } ); +export const selectDeviceAddress = () => createSelector( + getWalletState, + selectDeviceWif(), + (state, deviceWif) => { + const devicePrivKey = fromWif(deviceWif, testnet).privateKey; + const devicePubKey = publicKeyCreate(devicePrivKey, true).toString('base64'); + const myDeviceAddress = `0${getChash160(devicePubKey)}`; + return myDeviceAddress; + }, +); export const selectDevicePrivKey = () => createSelector( selectDeviceWif(), wif => { @@ -38,13 +48,17 @@ export const selectDevicePubKey = () => createSelector( export const selectPermanentDeviceKeyObj = () => createSelector( selectDevicePrivKey(), selectDevicePubKey(), - (priv, pub_b64) => { - return { priv, pub_b64 }; + (priv, pubB64) => { + return { priv, pubB64 }; } ); export const selectDeviceTempKeyData = () => createSelector( - getAppState, - state => state.deviceTempKeys, + getDeviceState, + state => ({ + ...state.deviceTempKeys, + pubB64: publicKeyCreate(state.deviceTempKeys.privKey, true).toString('base64'), + prevPubB64: publicKeyCreate(state.deviceTempKeys.prevPrivKey, true).toString('base64') + }) ); \ No newline at end of file diff --git a/src/selectors/wallet.js b/src/selectors/wallet.js index b64415e4..e1b8c01a 100644 --- a/src/selectors/wallet.js +++ b/src/selectors/wallet.js @@ -31,17 +31,6 @@ export const selectWalletAddress = () => createSelector( }, ); -export const selectDeviceAddress = () => createSelector( - getWalletState, - selectDeviceWif(), - (state, deviceWif) => { - const devicePrivKey = fromWif(deviceWif, testnet).privateKey; - const devicePubKey = publicKeyCreate(devicePrivKey, true).toString('base64'); - const myDeviceAddress = `0${devicePubKey}`; - return myDeviceAddress; - }, -); - export const selectWalletWif = () => createSelector( getWalletState, state => {