Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Dispatch } from 'redux'
import {
checkLedgerConfiguration,
LedgerSubProvider,
setLedgerProvider
} from '../utils/ledger/setLedgerProvider'
import { clearError, resetLedgerState } from './state'
import { WalletProviderAction } from './types'

const connectWithLedger = async (dispatch, LedgerProvider) => {
const connectWithLedger = async (
dispatch: Dispatch<WalletProviderAction>,
LedgerProvider: (_: any) => LedgerSubProvider
) => {
dispatch(clearError())
dispatch(resetLedgerState())
const provider = await setLedgerProvider(dispatch, LedgerProvider)
Expand Down
28 changes: 18 additions & 10 deletions WalletProvider/state.test.js → WalletProvider/state.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import reducer, {
initialState,
SET_WALLET_TYPE,
CREATE_WALLET_PROVIDER,
WALLET_ERROR,
CLEAR_ERROR,
RESET_STATE,
walletActionTypes,
setWalletType,
createWalletProvider,
setError,
Expand All @@ -13,6 +9,12 @@ import reducer, {
resetState
} from './state'
import {
ledgerActionTypes,
initialLedgerState
} from '../utils/ledger/ledgerStateManagement'
import { IMPORT_MNEMONIC } from '../constants'

const {
LEDGER_RESET_STATE,
LEDGER_USER_INITIATED_IMPORT,
LEDGER_NOT_FOUND,
Expand All @@ -25,10 +27,16 @@ import {
LEDGER_BUSY,
LEDGER_REPLUG,
LEDGER_USED_BY_ANOTHER_APP,
LEDGER_BAD_VERSION,
initialLedgerState
} from '../utils/ledger/ledgerStateManagement'
import { IMPORT_MNEMONIC } from '../constants'
LEDGER_BAD_VERSION
} = ledgerActionTypes

const {
SET_WALLET_TYPE,
CREATE_WALLET_PROVIDER,
WALLET_ERROR,
CLEAR_ERROR,
RESET_STATE
} = walletActionTypes

describe('WalletProvider', () => {
describe('actions', () => {
Expand Down Expand Up @@ -99,7 +107,7 @@ describe('WalletProvider', () => {
test('it clears walletErrors', () => {
const state = Object.freeze({ ...initialState, error: 'fake' })
const nextState = reducer(state, clearError())
expect(nextState.err).toBeFalsy()
expect(nextState.error).toBeFalsy()
})

test('it resets state', () => {
Expand Down
105 changes: 50 additions & 55 deletions WalletProvider/state.js → WalletProvider/state.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {
initialLedgerState,
LEDGER_USER_INITIATED_IMPORT,
LEDGER_NOT_FOUND,
LEDGER_RESET_STATE,
LEDGER_CONNECTED,
LEDGER_ESTABLISHING_CONNECTION_W_FILECOIN_APP,
LEDGER_FILECOIN_APP_NOT_OPEN,
LEDGER_FILECOIN_APP_OPEN,
LEDGER_LOCKED,
LEDGER_UNLOCKED,
LEDGER_REPLUG,
LEDGER_BUSY,
LEDGER_USED_BY_ANOTHER_APP,
LEDGER_BAD_VERSION,
WEBUSB_UNSUPPORTED
ledgerActionTypes
} from '../utils/ledger/ledgerStateManagement'
import {
WalletProviderAction,
WalletProviderActionType,
WalletProviderState
} from './types'

export const walletActionTypes = {
SET_WALLET_TYPE: 'SET_WALLET_TYPE',
CREATE_WALLET_PROVIDER: 'CREATE_WALLET_PROVIDER',
WALLET_ERROR: 'WALLET_ERROR',
CLEAR_ERROR: 'CLEAR_ERROR',
RESET_STATE: 'RESET_STATE'
} as Record<string, WalletProviderActionType>

export const initialState = {
walletType: null,
Expand All @@ -23,63 +23,59 @@ export const initialState = {
ledger: initialLedgerState
}

/* ACTION TYPES */
export const SET_WALLET_TYPE = 'SET_WALLET_TYPE'
export const CREATE_WALLET_PROVIDER = 'CREATE_WALLET_PROVIDER'
export const WALLET_ERROR = 'WALLET_ERROR'
export const CLEAR_ERROR = 'CLEAR_ERROR'
export const RESET_STATE = 'RESET_STATE'

/* ACTIONS */
export const setWalletType = walletType => ({
type: SET_WALLET_TYPE,
export const setWalletType = (walletType): WalletProviderAction => ({
type: walletActionTypes.SET_WALLET_TYPE,
payload: {
walletType
}
})

export const createWalletProvider = provider => ({
type: CREATE_WALLET_PROVIDER,
export const createWalletProvider = (provider): WalletProviderAction => ({
type: walletActionTypes.CREATE_WALLET_PROVIDER,
payload: {
provider
}
})

export const setError = errMessage => ({
type: WALLET_ERROR,
export const setError = (errMessage): WalletProviderAction => ({
type: walletActionTypes.WALLET_ERROR,
error: errMessage
})

export const clearError = () => ({
type: CLEAR_ERROR
export const clearError = (): WalletProviderAction => ({
type: walletActionTypes.CLEAR_ERROR
})

export const resetLedgerState = () => ({
type: LEDGER_RESET_STATE
export const resetLedgerState = (): WalletProviderAction => ({
type: ledgerActionTypes.LEDGER_RESET_STATE
})

export const resetState = () => ({
type: RESET_STATE
export const resetState = (): WalletProviderAction => ({
type: walletActionTypes.RESET_STATE
})

/* REDUCER */
export default (state, action) => {
export default (
state: WalletProviderState,
action: WalletProviderAction
): WalletProviderState => {
switch (action.type) {
case SET_WALLET_TYPE:
case walletActionTypes.SET_WALLET_TYPE:
return { ...Object.freeze(state), walletType: action.payload.walletType }
case CREATE_WALLET_PROVIDER:
case walletActionTypes.CREATE_WALLET_PROVIDER:
return {
...Object.freeze(state),
walletProvider: action.payload.provider
}
case WALLET_ERROR:
case walletActionTypes.WALLET_ERROR:
return { ...Object.freeze(state), error: action.error }
case CLEAR_ERROR:
case walletActionTypes.CLEAR_ERROR:
return { ...Object.freeze(state), error: '' }
case RESET_STATE:
case walletActionTypes.RESET_STATE:
return Object.freeze(initialState)
// ledger cases
case LEDGER_USER_INITIATED_IMPORT:
case ledgerActionTypes.LEDGER_USER_INITIATED_IMPORT:
return {
...Object.freeze(state),
ledger: {
Expand All @@ -88,7 +84,7 @@ export default (state, action) => {
connectedFailure: false
}
}
case LEDGER_NOT_FOUND:
case ledgerActionTypes.LEDGER_NOT_FOUND:
return {
...Object.freeze(state),
ledger: {
Expand All @@ -98,7 +94,7 @@ export default (state, action) => {
userImportFailure: true
}
}
case LEDGER_CONNECTED:
case ledgerActionTypes.LEDGER_CONNECTED:
return {
...Object.freeze(state),
ledger: {
Expand All @@ -108,7 +104,7 @@ export default (state, action) => {
inUseByAnotherApp: false
}
}
case LEDGER_ESTABLISHING_CONNECTION_W_FILECOIN_APP:
case ledgerActionTypes.LEDGER_ESTABLISHING_CONNECTION_W_FILECOIN_APP:
return {
...Object.freeze(state),
ledger: {
Expand All @@ -120,7 +116,7 @@ export default (state, action) => {
replug: false
}
}
case LEDGER_LOCKED:
case ledgerActionTypes.LEDGER_LOCKED:
return {
...Object.freeze(state),
ledger: {
Expand All @@ -130,7 +126,7 @@ export default (state, action) => {
userImportFailure: true
}
}
case LEDGER_UNLOCKED:
case ledgerActionTypes.LEDGER_UNLOCKED:
return {
...Object.freeze(state),
ledger: {
Expand All @@ -139,7 +135,7 @@ export default (state, action) => {
unlocked: true
}
}
case LEDGER_FILECOIN_APP_NOT_OPEN:
case ledgerActionTypes.LEDGER_FILECOIN_APP_NOT_OPEN:
return {
...Object.freeze(state),
ledger: {
Expand All @@ -151,7 +147,7 @@ export default (state, action) => {
unlocked: true
}
}
case LEDGER_FILECOIN_APP_OPEN:
case ledgerActionTypes.LEDGER_FILECOIN_APP_OPEN:
return {
...Object.freeze(state),
ledger: {
Expand All @@ -160,51 +156,50 @@ export default (state, action) => {
locked: false,
unlocked: true,
replug: false,
busy: false,
provider: action.provider
busy: false
}
}
case LEDGER_BUSY:
case ledgerActionTypes.LEDGER_BUSY:
return {
...Object.freeze(state),
ledger: {
...state.ledger,
busy: true
}
}
case LEDGER_REPLUG:
case ledgerActionTypes.LEDGER_REPLUG:
return {
...Object.freeze(state),
ledger: {
...state.ledger,
replug: true
}
}
case LEDGER_USED_BY_ANOTHER_APP:
case ledgerActionTypes.LEDGER_USED_BY_ANOTHER_APP:
return {
...Object.freeze(state),
ledger: {
...state.ledger,
inUseByAnotherApp: true
}
}
case LEDGER_BAD_VERSION:
case ledgerActionTypes.LEDGER_BAD_VERSION:
return {
...Object.freeze(state),
ledger: {
...state.ledger,
badVersion: true
}
}
case WEBUSB_UNSUPPORTED:
case ledgerActionTypes.WEBUSB_UNSUPPORTED:
return {
...Object.freeze(state),
ledger: {
...state.ledger,
webUSBSupported: false
}
}
case LEDGER_RESET_STATE:
case ledgerActionTypes.LEDGER_RESET_STATE:
return {
...Object.freeze(state),
ledger: {
Expand Down
19 changes: 19 additions & 0 deletions WalletProvider/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { LedgerActionType } from '../utils/ledger/ledgerStateManagement'
import { initialState } from './state'

export type WalletActionType =
| 'SET_WALLET_TYPE'
| 'CREATE_WALLET_PROVIDER'
| 'WALLET_ERROR'
| 'CLEAR_ERROR'
| 'RESET_STATE'

export type WalletProviderActionType = WalletActionType | LedgerActionType

export interface WalletProviderAction {
type: WalletProviderActionType
payload?: any
error?: any
}

export type WalletProviderState = typeof initialState
10 changes: 9 additions & 1 deletion utils/ledger/badVersion.js → utils/ledger/badVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import {
LEDGER_VERSION_PATCH
} from '../../constants'

const badVersion = ({ major, minor, patch }) => {
const badVersion = ({
major,
minor,
patch
}: {
major: number
minor: number
patch: number
}): boolean => {
const validMajor = major >= LEDGER_VERSION_MAJOR
const validMinor = minor >= LEDGER_VERSION_MINOR
const validPatch = patch >= LEDGER_VERSION_PATCH
Expand Down
File renamed without changes.
8 changes: 5 additions & 3 deletions utils/ledger/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import {
LEDGER_VERSION_MINOR,
LEDGER_VERSION_PATCH
} from '../../constants'
import {
import { ledgerActionTypes } from './ledgerStateManagement'
import badVersion from './badVersion'

const {
LEDGER_FILECOIN_APP_OPEN,
LEDGER_UNLOCKED,
LEDGER_LOCKED,
LEDGER_BUSY,
LEDGER_FILECOIN_APP_NOT_OPEN,
LEDGER_REPLUG
} from './ledgerStateManagement'
import badVersion from './badVersion'
} = ledgerActionTypes

const mockDispatch = jest.fn()

Expand Down
Loading