Skip to content

Commit

Permalink
device address selector fix, hub login with keys
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLaanpere committed Oct 31, 2019
1 parent 1048f96 commit 2c3345b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
23 changes: 16 additions & 7 deletions src/sagas/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down Expand Up @@ -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();
}
26 changes: 20 additions & 6 deletions src/selectors/device.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 => {
Expand All @@ -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')
})
);
11 changes: 0 additions & 11 deletions src/selectors/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 2c3345b

Please sign in to comment.