Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #171 from ZbayApp/fix/prevent-unregistered-from-se…
Browse files Browse the repository at this point in the history
…nding-msgs

Fix/prevent unregistered from sending msgs
  • Loading branch information
vinkabuki authored Feb 8, 2021
2 parents edbba0b + 3e8b374 commit 93c9a3a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 130 deletions.
4 changes: 2 additions & 2 deletions src/main/websockets/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ new Promise((resolve, reject) => {
try {
const options = url.parse(proxy)
const agent = new HttpsProxyAgent(options)
const socket = new WebSocketClient(address, { agent: agent }, { handshakeTimeout: 30000 })
const socket = new WebSocketClient(address, { agent: agent }, { handshakeTimeout: 80_000 })
const id = setTimeout(() => {
// eslint-disable-next-line
reject('timeout')
}, 35000)
}, 80_000)
socket.on('unexpected-response', err => {
console.log(err)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const inputStateToMessage = {
[INPUT_STATE.LOCKED]:
'All of your funds are locked - please wait for network confirmation or deposit more ZEC to your account',
[INPUT_STATE.UNREGISTERED]:
'You can not reply to this message because you are not registered. Please register your nickname ( button next to your balance )'
'You can not reply to this message because you are not registered yet, please wait.'
}

export const ChannelInput = ({
Expand Down
13 changes: 0 additions & 13 deletions src/renderer/store/handlers/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,20 +410,7 @@ export const setIdentityEpic = identityToSet => async (dispatch, getState) => {
await dispatch(messagesHandlers.epics.fetchMessages())
if (!useTor) {
ipcRenderer.send('killTor')
// await dispatch(appHandlers.epics.initializeUseTor())
}
const usernameStatus = electronStore.get('registrationStatus.status')
const nickname = electronStore.get('registrationStatus.nickname')
console.log(usernameStatus)
if (nickname && usernameStatus !== 'SUCCESS') {
if (usernameStatus === 'IN_PROGRESS') {
dispatch(usersHandlers.epics.checkRegistrationConfirmations({ firstRun: true }))
} else {
await dispatch(usersHandlers.epics.createOrUpdateUser({ nickname, debounce: true }))
console.log('after registration')
}
}
console.log('before coordinator')
setTimeout(() => dispatch(coordinatorHandlers.epics.coordinator()), 5000)
dispatch(setLoadingMessage('Loading users and messages'))
} catch (err) {}
Expand Down
178 changes: 66 additions & 112 deletions src/renderer/store/handlers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import identitySelector from '../selectors/identity'
import { actions as identityActions } from '../handlers/identity'
import appSelectors from '../selectors/app'
import { getPublicKeysFromSignature } from '../../zbay/messages'
import { messageType, actionTypes, unknownUserId, REQUEST_USER_REGISTRATION_ENDPOINT, FETCH_USERNAMES_ENDPOINT } from '../../../shared/static'
import {
messageType,
actionTypes,
unknownUserId,
REQUEST_USER_REGISTRATION_ENDPOINT,
FETCH_USERNAMES_ENDPOINT
} from '../../../shared/static'
import { messages as zbayMessages } from '../../zbay'
import staticChannels from '../../zcash/channels'
import notificationsHandlers from './notifications'
Expand All @@ -23,7 +29,7 @@ import electronStore from '../../../shared/electronStore'

import { DisplayableMessage } from '../../zbay/messages.types'

import {packMemo} from '../../zbay/transit'
import { packMemo } from '../../zbay/transit'

import { ActionsType, PayloadType } from './types'

Expand Down Expand Up @@ -133,44 +139,6 @@ export const registerAnonUsername = () => async (dispatch, getState) => {
await dispatch(createOrUpdateUser({ nickname: `anon${publicKey.substring(0, 10)}` }))
}

export const checkRegistrationConfirmations = ({ firstRun }) => async (dispatch, getState) => {
if (firstRun) {
const publicKey = identitySelector.signerPubKey(getState())
const address = identitySelector.address(getState())
const nickname = electronStore.get('registrationStatus.nickname')
dispatch(
identityActions.setRegistraionStatus({
nickname,
status: 'IN_PROGRESS'
})
)
dispatch(
mockOwnUser({
sigPubKey: publicKey,
address,
nickname
})
)
}
setTimeout(async () => {
const publicKey = identitySelector.signerPubKey(getState())
const users = usersSelector.users(getState())
const usersArray = Array.from(Object.keys(users))

if(usersArray.includes(publicKey)){
electronStore.set('registrationStatus.status', 'SUCCESS')
electronStore.set('registrationStatus.confirmation', 10)
dispatch(
notificationsHandlers.actions.enqueueSnackbar(
successNotification({
message: `Username registered.`
})
)
)
}
}, 75000)
}

export const createOrUpdateUser = payload => async (dispatch, getState) => {
const { nickname, firstName = '', lastName = '', debounce = false, retry = 0 } = payload
const publicKey = identitySelector.signerPubKey(getState())
Expand All @@ -182,6 +150,7 @@ export const createOrUpdateUser = payload => async (dispatch, getState) => {
nickname,
address
}

const registrationMessage = zbayMessages.createMessage({
messageData: {
type: zbayMessages.messageType.USER,
Expand Down Expand Up @@ -223,39 +192,30 @@ export const createOrUpdateUser = payload => async (dispatch, getState) => {
})
)
)
dispatch(
identityActions.setRegistraionStatus({
nickname,
status: 'ERROR'
})
)
dispatch(
identityActions.setRegistraionStatus({
nickname,
status: 'ERROR'
})
)
dispatch(actionCreators.openModal('failedUsernameRegister')())
return
}

try {

dispatch(
identityActions.setRegistraionStatus({
nickname,
status: 'IN_PROGRESS'
})
)
dispatch(
mockOwnUser({
sigPubKey: publicKey,
address,
nickname
})
)
electronStore.set('registrationStatus', {
dispatch(
identityActions.setRegistraionStatus({
nickname,
status: 'IN_PROGRESS'
})

// ipcRenderer.send('spawnTor')
// electronStore.set('useTor', true)
// dispatch(appHandlers.actions.setUseTor(true))
dispatch(checkRegistrationConfirmations({ firstRun: true }))
)
dispatch(
mockOwnUser({
sigPubKey: publicKey,
address,
nickname
})
)
dispatch(
notificationsHandlers.actions.enqueueSnackbar(
successNotification({
Expand All @@ -273,12 +233,12 @@ export const createOrUpdateUser = payload => async (dispatch, getState) => {
dispatch(notificationsHandlers.actions.removeSnackbar('username'))
} catch (err) {
console.log(err)
dispatch(
identityActions.setRegistraionStatus({
nickname,
status: 'ERROR'
})
)
dispatch(
identityActions.setRegistraionStatus({
nickname,
status: 'ERROR'
})
)
dispatch(actionCreators.openModal('failedUsernameRegister')())
}
}
Expand Down Expand Up @@ -319,7 +279,6 @@ export const fetchUsers = (address, messages: DisplayableMessage[]) => async (
let minfee = 0
let users = {}
const network = nodeSelectors.network(getState())
const { status: registrationStatus } = identitySelector.registrationStatus(getState())
const signerPubKey = identitySelector.signerPubKey(getState())
for (const msg of registrationMessages) {
if (
Expand All @@ -345,29 +304,22 @@ export const fetchUsers = (address, messages: DisplayableMessage[]) => async (
}
dispatch(feesHandlers.actions.setUserFee(minfee))
const isRegistrationComplete = users[signerPubKey]
if (!isRegistrationComplete && registrationStatus === 'IN_PROGRESS') {
const publicKey = identitySelector.signerPubKey(getState())
const address = identitySelector.address(getState())
const { nickname } = identitySelector.registrationStatus(getState())
const mockedUser = {
[publicKey]: {
..._UserData,
address,
nickname,
publicKey
}
}
users = {
...users,
...mockedUser
if (isRegistrationComplete) {
if (users[signerPubKey].createdAt !== 0) {
dispatch(
identityActions.setRegistraionStatus({
nickname: '',
status: 'SUCCESS'
})
)
dispatch(
notificationsHandlers.actions.enqueueSnackbar(
successNotification({
message: `Username registered.`
})
)
)
}
} else {
dispatch(
identityActions.setRegistraionStatus({
nickname: '',
status: 'SUCCESS'
})
)
}
dispatch(setUsers({ users }))
} catch (err) {
Expand Down Expand Up @@ -407,26 +359,28 @@ export const fetchOnionAddresses = (address, messages: DisplayableMessage[]) =>
}
}

let usernames = [];
let usernames = []

if (true) {
(function (){
try {
axios.get(FETCH_USERNAMES_ENDPOINT).then((res) => {
usernames = res.data.message
}
).catch((err) => {
console.log('cant fetch usernames')
console.log(err)
})
} catch (err) {
console.log(err)
}
})();
(function () {
try {
axios
.get(FETCH_USERNAMES_ENDPOINT)
.then(res => {
usernames = res.data.message
})
.catch(err => {
console.log('cant fetch usernames')
console.log(err)
})
} catch (err) {
console.log(err)
}
})()
}

export const isNicknameTaken = username => (dispatch, getState) => {
return R.includes(username, usernames)
return R.includes(username, usernames)
}

export const epics = {
Expand All @@ -435,8 +389,8 @@ export const epics = {
createOrUpdateUser,
registerAnonUsername,
fetchOnionAddresses,
registerOnionAddress,
checkRegistrationConfirmations
registerOnionAddress
// checkRegistrationConfirmations
}

export const reducer = handleActions<UsersStore, PayloadType<UserActions>>(
Expand Down
13 changes: 11 additions & 2 deletions src/renderer/store/selectors/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSelector } from "reselect";
import identitySelectors from "./identity";
import messagesQueueSelectors from "./messagesQueue";
import contacts from "./contacts";
import users from './users'
import { networkFee, messageType } from "../../../shared/static";

import { Store } from '../reducers'
Expand Down Expand Up @@ -182,9 +183,17 @@ export const shareableUri = createSelector(channel, (c) => c.shareableUri);
export const inputLocked = createSelector(
identitySelectors.balance("zec"),
identitySelectors.lockedBalance("zec"),
(available, locked) => {
users.users,
identitySelectors.signerPubKey,
(available, locked, users, signerPubKey) => {
if (available.gt(networkFee)) {
return INPUT_STATE.AVAILABLE;
if (users[signerPubKey]) {
if (users[signerPubKey].createdAt) {
return INPUT_STATE.AVAILABLE;
} else {
return INPUT_STATE.UNREGISTERED
}
}
} else {
if (locked.gt(0)) {
return INPUT_STATE.LOCKED;
Expand Down

0 comments on commit 93c9a3a

Please sign in to comment.