Skip to content

Commit

Permalink
hub message parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLaanpere committed Oct 31, 2019
1 parent 2c3345b commit b3a3c9c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
87 changes: 47 additions & 40 deletions src/sagas/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,23 @@ import { channel } from '@redux-saga/core';
import { sign } from 'obyte/lib/internal';
import { oClient, getDeviceMessageHashToSign } from './../lib/OCustom';
import { actionTypes } from './../constants';
import { setToastMessage } from './../actions/app';
import { setExchangeRates } from './../actions/exchangeRates';
import {
selectDeviceAddress,
selectPermanentDeviceKeyObj,
selectDeviceTempKeyData,
} from './../selectors/device';

let oChannel;

export const oChannel = channel();

export function* subscribeToHub() {
try {
oClient.subscribe((err, result) => {
if (err) {
throw new Error('Hub socket error');
} else {
const [messageType, message] = result;
oChannel.put({ type: messageType, payload: message });
}
});
yield call(setInterval, () => oClient.api.heartbeat(), 10 * 1000);
} catch (error) {
yield put(setToastMessage({
type: 'ERROR',
message: 'Hub connection error',
}));
console.log(error);
}
}

export function* watchHubMessages() {
while (true) {
const { type, payload } = yield take(oChannel)

if (type === 'justsaying') {
switch (payload.subject) {
case 'hub/challenge':
yield call(loginToHub, payload.body);
return;
default:
console.log(payload.body);
}
}
console.log(type, payload);
}
if (!oChannel) {
oChannel = channel();
}

export function* loginToHub(challenge) {
const deviceAddress = yield select(selectDeviceAddress());
const permanentDeviceKey = yield select(selectPermanentDeviceKeyObj());
const tempDeviceKeyData = yield select(selectDeviceTempKeyData());

const objLogin = { challenge, pubkey: permanentDeviceKey.pubB64 };
objLogin.signature = sign(
getDeviceMessageHashToSign(objLogin),
Expand All @@ -74,6 +39,48 @@ export function* loginToHub(challenge) {
oClient.justsaying('hub/refresh', null);
}

export function* subscribeToHub() {
try {
yield oClient.subscribe((err, result) => {
if (err) {
throw new Error('Hub socket error');
} else {
const [type, payload] = result;
oChannel.put({ type, payload });
}
});
yield call(setInterval, () => oClient.api.heartbeat(), 10 * 1000);
} catch (error) {
yield put(setToastMessage({
type: 'ERROR',
message: 'Hub connection error',
}));
console.log(error);
}
}

export function* watchHubMessages() {
try {
while (true) {
const { type, payload } = yield take(oChannel);

if (type === 'justsaying') {
switch (payload.subject) {
case 'hub/challenge':
yield call(loginToHub, payload.body);
case 'hub/message':
console.log('TODO - parse message');
case 'exchange_rates':
yield put(setExchangeRates(payload));
default:
}
}
}
} catch (error) {
console.log(error);
}
}

export default function* watch() {
yield watchHubMessages();
}
8 changes: 4 additions & 4 deletions src/sagas/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ import {
} from './../selectors/wallet';


export function* initWallet(action) {
export function* initWallet() {
try {
const walletData = yield select(selectWallet());
if (walletData.password === null || walletData.seedWords === null) {
yield put(createInitialWalletStart());
}

// Handle websocket traffic
yield call(subscribeToHub, action);
yield call(subscribeToHub);
// Fetch wallet data from hub
yield call(fetchBalances, action);
yield call(fetchWitnesses, action);
yield call(fetchBalances);
yield call(fetchWitnesses);
yield put(loadWalletHistory());
yield put(initWalletSuccess());
} catch (error) {
Expand Down

0 comments on commit b3a3c9c

Please sign in to comment.