UNPKG

edge-core-js

Version:

Edge account & wallet management library

165 lines (115 loc) 3.35 kB
import { buildReducer, mapReducer } from 'redux-keto' import { accountReducer, } from './account/account-reducer' import { currency, } from './currency/currency-reducer' import { login, } from './login/login-reducer' import { plugins, } from './plugins/plugins-reducer' import { storageWallets, } from './storage/storage-reducer' export const defaultLogSettings = { sources: {}, defaultLogLevel: 'warn' } const dummyClientInfo = { clientId: new Uint8Array(0), duressEnabled: false, loginWaitTimestamps: {} } export const reducer = buildReducer({ accountCount(state = 0, action) { return action.type === 'LOGIN' ? state + 1 : state }, accountIds(state = [], action, next) { switch (action.type) { case 'LOGIN': return [...state, next.lastAccountId] case 'LOGOUT': { const { accountId } = action.payload const out = state.filter(id => id !== accountId) if (out.length === state.length) { throw new Error(`Login ${accountId} does not exist`) } return out } case 'CLOSE': return [] } return state }, accounts: mapReducer(accountReducer, next => next.accountIds), changeServers(state = [], action) { return action.type === 'INIT' ? action.payload.changeServers : state }, clientInfo(state = dummyClientInfo, action) { switch (action.type) { case 'INIT': return action.payload.clientInfo case 'LOGIN_DURESS_MODE_DISABLED': { return { ...state, duressEnabled: false } } case 'LOGIN_DURESS_MODE_ENABLED': { return { ...state, duressEnabled: true } } case 'LOGIN_WAIT_TIMESTAMP_UPDATED': { return { ...state, loginWaitTimestamps: { ...state.loginWaitTimestamps, [action.payload.loginId]: action.payload.timestamp } } } } return state }, contextAppId: (state = '', action) => { return action.type === 'LOGIN' ? action.payload.appId : state }, hideKeys(state = true, action) { return action.type === 'INIT' ? action.payload.hideKeys : state }, infoCache(state = {}, action) { switch (action.type) { case 'INIT': return action.payload.infoCache case 'INFO_CACHE_FETCHED': return action.payload } return state }, infoServers(state = [], action) { return action.type === 'INIT' ? action.payload.infoServers : state }, lastAccountId(state, action, next) { return `login${next.accountCount}` }, logSettings(state = defaultLogSettings, action) { switch (action.type) { case 'INIT': return action.payload.logSettings case 'CHANGE_LOG_SETTINGS': return action.payload } return state }, paused(state = false, action) { return action.type === 'PAUSE' ? action.payload : state }, ready(state = false, action) { return action.type === 'INIT' ? true : state }, skipBlockHeight(state = false, action) { return action.type === 'INIT' ? action.payload.skipBlockHeight : state }, syncServers(state = [], action) { return action.type === 'INIT' ? action.payload.syncServers : state }, currency, login, plugins, storageWallets })