<commit_before>// @flow
import storage from 'electron-json-storage'
import { generateEncryptedWif, getAccountFromWIFKey, generatePrivateKey, getWIFFromPrivateKey, encryptWIF, encryptWifAccount } from 'neon-js'
import { sendEvent, clearTransactionEvent } from './transactions'
import { validatePassphrase } from '../core/wallet'

// Constants
export const NEW_WALLET_KEYS = 'NEW_WALLET_KEYS'
export const NEW_WALLET = 'NEW_WALLET'
export const SET_GENERATING = 'SET_GENERATING'
export const RESET_KEY = 'RESET_KEY'

// Actions
export function newWalletKeys (passphrase: string) {
  return {
    type: NEW_WALLET_KEYS,
    passphrase
  }
}

export function newWallet (account: Object) {
  return {
    type: NEW_WALLET,
    wif: account.wif,
    address: account.address,
    passphrase: <PASSWORD>,
    encryptedWif: account.encryptedWif
  }
}

export function generating (bool: string) {
  return {
    type: SET_GENERATING,
    state: bool
  }
}

export function resetKey () {
  return {
    type: RESET_KEY
  }
}

export const saveKey = (keyName: string, passphraseKey: string) => (dispatch: DispatchType) => {
  if (!keyName) { return null }

  // eslint-disable-next-line
  storage.get('keys', (error, data) => {
    data[keyName] = passphraseKey
    dispatch(sendEvent(true, `Saved key as ${keyName}`))
    storage.set('keys', data)
    setTimeout(() => dispatch(clearTransactionEvent()), 5000)
  })
}

export const generateNewWallet = (passphrase: string, passphrase2: string, wif?: string) => (dispatch: DispatchType): Promise<*> => {
  return new Promise((resolve, reject) => {
    const rejectNewWallet = (error) => {
      dispatch(sendEvent(false, error))
      setTimeout(() => clearTransactionEvent(), 5000)
      reject(new Error(error))
    }
    if (passphrase !== passphrase2) {
      rejectNewWallet('Passphrases do not match')
    }
    if (validatePassphrase(passphrase)) {
      dispatch(sendEvent(true, 'Generating encoded key...'))
      setTimeout(() => {
        const encryptFn = wif ? encryptWifAccount(wif, passphrase) : generateEncryptedWif(passphrase)
        try {
          encryptFn.then((result) => {
            dispatch(newWallet(result))
            dispatch(clearTransactionEvent())
            resolve()
          })
        } catch (e) {
          rejectNewWallet(wif ? 'The private key is not valid' : 'An error occured while trying to generate a new wallet')
        }
      }, 500)
    } else {
      rejectNewWallet('Please choose a longer passphrase')
    }
  })
}

const initialState = {
  wif: null,
  address: null,
  passphrase: null,
  encryptedWif: null,
  generating: false
}

// Reducer used for state necessary to generating a wallet
export default (state: Object = initialState, action: Object) => {
  switch (action.type) {<commit_msg>Fixes check<commit_after>// @flow
import storage from 'electron-json-storage'
import { generateEncryptedWif, getAccountFromWIFKey, generatePrivateKey, getWIFFromPrivateKey, encryptWIF, encryptWifAccount } from 'neon-js'
import { sendEvent, clearTransactionEvent } from './transactions'
import { validatePassphrase } from '../core/wallet'

// Constants
export const NEW_WALLET_KEYS = 'NEW_WALLET_KEYS'
export const NEW_WALLET = 'NEW_WALLET'
export const SET_GENERATING = 'SET_GENERATING'
export const RESET_KEY = 'RESET_KEY'

// Actions
export function newWalletKeys (passphrase: string) {
  return {
    type: NEW_WALLET_KEYS,
    passphrase
  }
}

export function newWallet (account: Object) {
  return {
    type: NEW_WALLET,
    wif: account.wif,
    address: account.address,
    passphrase: <PASSWORD>,
    encryptedWif: account.encryptedWif
  }
}

export function generating (bool: string) {
  return {
    type: SET_GENERATING,
    state: bool
  }
}

export function resetKey () {
  return {
    type: RESET_KEY
  }
}

export const saveKey = (keyName: string, passphraseKey: string) => (dispatch: DispatchType) => {
  if (!keyName) { return null }

  // eslint-disable-next-line
  storage.get('keys', (error, data) => {
    data[keyName] = passphraseKey
    dispatch(sendEvent(true, `Saved key as ${keyName}`))
    storage.set('keys', data)
    setTimeout(() => dispatch(clearTransactionEvent()), 5000)
  })
}

export const generateNewWallet = (passphrase: string, passphrase2: string, wif?: string) => (dispatch: DispatchType): Promise<*> => {
  return new Promise((resolve, reject) => {
    const rejectNewWallet = (error) => {
      dispatch(sendEvent(false, error))
      setTimeout(() => clearTransactionEvent(), 5000)
      reject(new Error(error))
    }

    if (passphrase !== passphrase2) {
      rejectNewWallet('Passphrases do not match')
    } else if (validatePassphrase(passphrase)) {
      dispatch(sendEvent(true, 'Generating encoded key...'))
      setTimeout(() => {
        const encryptFn = wif ? encryptWifAccount(wif, passphrase) : generateEncryptedWif(passphrase)
        try {
          encryptFn.then((result) => {
            dispatch(newWallet(result))
            dispatch(clearTransactionEvent())
            resolve()
          })
        } catch (e) {
          rejectNewWallet(wif ? 'The private key is not valid' : 'An error occured while trying to generate a new wallet')
        }
      }, 500)
    } else {
      rejectNewWallet('Please choose a longer passphrase')
    }
  })
}

const initialState = {
  wif: null,
  address: null,
  passphrase: null,
  encryptedWif: null,
  generating: false
}

// Reducer used for state necessary to generating a wallet
export default (state: Object = initialState, action: Object) => {
  switch (action.type) {
{'fraction_non_alphanumeric': 0.11895378000716589, 'fraction_numerical': 0.004657828735220351, 'mean_word_length': 3.5390243902439025, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3075906', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1717, 'n_words': 440}