Skip to content

Commit

Permalink
payment bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLaanpere committed Jul 13, 2020
1 parent 9668c0e commit 971b03c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
28 changes: 19 additions & 9 deletions src/sagas/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
let oChannel;

if (!oChannel) {
console.log('oChannel init');
oChannel = channel();
}

Expand All @@ -76,10 +77,12 @@ export function* loginToHub(challenge) {
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))
Expand Down Expand Up @@ -113,7 +116,7 @@ export function* watchHubMessages() {
while (true) {
const { type, payload } = yield take(oChannel);
if (type === 'justsaying') {
if (payload.subject === 'hub/challenge') {
if (payload.subject === 'hub/challenge' && !!payload.body) {
yield call(loginToHub, payload.body);
} else if (payload.subject === 'hub/message') {
yield call(receiveMessage, payload);
Expand All @@ -130,13 +133,13 @@ export function* watchHubMessages() {
);
}
} else if (type === 'request') {
// console.log('UNHANDLED REQUEST FROM HUB: ', payload);
console.log('UNHANDLED REQUEST FROM HUB: ', payload);
} else {
// console.log('UNHANDLED PACKAGE FROM HUB: ', type, payload);
console.log('UNHANDLED PACKAGE FROM HUB: ', type, payload);
}
}
} catch (error) {
// (error);
console.log('UNHANDLED HUB MESSAGE ERROR', error);
}
}

Expand All @@ -157,6 +160,7 @@ export function* receiveMessage(message) {
yield put(removeCorrespondent({ address: decryptedMessage.from }));
oClient.justsaying('hub/delete', body.message_hash);
} else if (decryptedMessage.subject === 'pairing') {
console.log('Pairing');
const existingCorrespondent = yield select(
selectCorrespondentByPairingSecret(
decryptedMessage.body.pairing_secret,
Expand All @@ -166,6 +170,7 @@ export function* receiveMessage(message) {
decryptedMessage.body?.reverse_pairing_secret;
console.log('checks', existingCorrespondent, reversePairingSecret);
if (!reversePairingSecret && !existingCorrespondent) {
console.log('Correspondent started adding you');
// Correspondent started adding you
const correspondent = {
address: decryptedMessage.from,
Expand All @@ -182,7 +187,9 @@ export function* receiveMessage(message) {
pairingSecret: correspondent.pairingSecret,
recipientPubKey: body.message.pubkey,
});
oClient.justsaying('hub/delete', body.message_hash);
} else if (!existingCorrespondent && reversePairingSecret) {
console.log('I send pairing confirmation');
// I send pairing confirmation
const correspondent = {
address: decryptedMessage.from,
Expand All @@ -200,11 +207,13 @@ export function* receiveMessage(message) {
pairingSecret: reversePairingSecret,
recipientPubKey: body.message.pubkey,
});
oClient.justsaying('hub/delete', body.message_hash);
} else if (
!reversePairingSecret &&
existingCorrespondent?.reversePairingSecret ===
decryptedMessage.body.pairing_secret
) {
console.log('Correspondent sends pairing confirmation');
// Correspondent sends pairing confirmation
yield put(
setCorrespondentName({
Expand All @@ -218,7 +227,7 @@ export function* receiveMessage(message) {
selectCorrespondent(decryptedMessage.from),
);
if (!correspondent) {
console.error("Can't finis pairing, correspondent not stored");
console.error("Can't finish pairing, correspondent not stored");
}
} else if (decryptedMessage.subject === 'text') {
// Check if signed message with wallet address info
Expand All @@ -235,17 +244,17 @@ export function* receiveMessage(message) {
timestamp: Date.now(),
}),
);
oClient.justsaying('hub/delete', body.message_hash);
} else if (decryptedMessage.subject === 'payment_notification') {
console.log('Payment notification', decryptedMessage);
oClient.justsaying('hub/delete', body.message_hash);
}
} catch (error) {
console.log('MESSAGE PARSING ERROR:', {
error,
message: message.body.message,
});
if (error === 'INVALID_DECRYPTION_KEY') {
oClient.justsaying('hub/delete', message.body.message_hash);
}
oClient.justsaying('hub/delete', message.body.message_hash);
}
}

Expand Down Expand Up @@ -436,10 +445,12 @@ export function* sendPairingMessage({
}
}

// Deprecated, as reconnect option was introduced to obyte.js
export function* startHubHeartbeat() {
while (true) {
yield delay(10000);
yield call(oClient.api.heartbeat);
yield console.log('HB');
}
}

Expand All @@ -449,6 +460,5 @@ export default function* watch() {
takeEvery(actionTypes.MESSAGE_ADD_START, sendMessage),
takeEvery(actionTypes.MESSAGE_RECEIVE_START, handleReceivedMessage),
takeEvery(actionTypes.CORRESPONDENT_INVITATION_ACCEPT, acceptInvitation),
takeEvery(actionTypes.WALLET_INIT_SUCCESS, startHubHeartbeat),
]);
}
7 changes: 3 additions & 4 deletions src/sagas/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
loadWalletBalancesFail,
} from './../actions/balances';
import {
selectWallet,
selectWalletAddress,
selectWitnesses,
selectAddressWif,
Expand Down Expand Up @@ -71,7 +70,7 @@ export function* createInitialWallet(action) {
const xPrivKey = mnemonic.toHDPrivateKey();

// Wallet wif
const walletPath = testnet ? "m/44'/1'/0'/0" : "m/44'/0'/0'/0";
const walletPath = testnet ? "m/44'/1'/0'/0'" : "m/44'/0'/0'/0";
const { privateKey: walletPirvateKey } = yield xPrivKey.derive(
walletPath,
);
Expand Down Expand Up @@ -195,12 +194,12 @@ export function* fetchWalletHistory(action) {

export function* sendPayment(action) {
try {
const walletWif = yield select(selectAddressWif());
const addressWif = yield select(selectAddressWif());
const params = {
...action.payload,
};

yield call(oClient.post.payment, params, walletWif);
yield call(oClient.post.payment, params, addressWif);
yield call(fetchBalances, action);
yield call(fetchWalletHistory, action);
yield call(NavigationService.navigate, 'Wallet');
Expand Down
20 changes: 16 additions & 4 deletions src/screens/PaymentScreen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ class PaymentScreen extends React.Component {
}

this.setState({
primaryValue: type === 'primary' ? value : !isNaN(primaryValue) ? primaryValue : 0,
secondaryValue: type === 'secondary' ? value : !isNaN(secondaryValue) ? secondaryValue : 0,
primaryValue:
type === 'primary' ? value : !isNaN(primaryValue) ? primaryValue : 0,
secondaryValue:
type === 'secondary'
? value
: !isNaN(secondaryValue)
? secondaryValue
: 0,
});
}

Expand Down Expand Up @@ -292,7 +298,10 @@ class PaymentScreen extends React.Component {
<ActionSheet
currentValue={primaryUnit}
onChange={value => this.changePrimaryUnit(value)}
items={PRIMARY_UNITS.map(({label, altValue}) => ({ label, value: altValue }) )}
items={PRIMARY_UNITS.map(({ label, altValue }) => ({
label,
value: altValue,
}))}
/>
</View>
<View style={styles.field}>
Expand All @@ -305,7 +314,10 @@ class PaymentScreen extends React.Component {
<ActionSheet
currentValue={secondaryUnit}
onChange={value => this.changeSecondaryUnit(value)}
items={SECONDARY_UNITS.map(({label, altValue}) => ({ label, value: altValue }) )}
items={SECONDARY_UNITS.map(({ label, altValue }) => ({
label,
value: altValue,
}))}
/>
</View>
<Button
Expand Down
2 changes: 1 addition & 1 deletion src/selectors/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const selectWalletWif = () =>
createSelector(getWalletState, state => state.walletWif);

export const selectAddressWif = () =>
createSelector(getWalletState, state => state.selectAddressWif);
createSelector(getWalletState, state => state.addressWif);

export const selectWitnesses = () =>
createSelector(getWalletState, state => state.witnesses);

0 comments on commit 971b03c

Please sign in to comment.