<commit_before>// @flow
import React, { Fragment } from 'react'
import { wallet } from '@cityofzion/neon-js'
import { withRouter } from 'react-router-dom'
import { cloneDeep } from 'lodash-es'
import PasswordInput from '../Inputs/PasswordInput'
import StyledReactSelect from '../Inputs/StyledReactSelect/StyledReactSelect'
import TextInput from '../Inputs/TextInput'
import Button from '../Button'
import CheckIcon from '../../assets/icons/check.svg'
import BackArrow from '../../assets/icons/arrow.svg'
import ForwardArrow from '../../assets/icons/forward-arrow.svg'
import styles from './CreateImportSplitWalletForm.scss'

type Props = {
  generateNewWalletAccount: Function,
  history: Object,
  authenticated: boolean,
  accounts: Object,
  showErrorNotification: Object => any,
}

type State = {
  existingPassphrase: string,
  passphrase: string,
  passphrase2: string,
  passphraseValid: boolean,
  passphrase2Valid: boolean,
  passphraseError: string,
  passphrase2Error: string,
  keypart2: string,
  walletName: string,
  submitButtonDisabled: boolean,
  selectedAccount: Object | null,
  mappedAccounts: Array<Object>,
}

const PASS_MIN_LENGTH = 4

class CreateImportSplitWalletForm extends React.Component<Props, State> {
  static defaultProps = {
    accounts: [],
  }

  state = {
    existingPassphrase: '',
    passphrase: '',
    passphrase2: '',
    passphraseValid: false,
    passphrase2Valid: false,
    passphraseError: '',
    passphrase2Error: '',
    keypart2: '',
    walletName: '',
    submitButtonDisabled: false,
    selectedAccount: null,
    step: 1,
    mappedAccounts:
      this.props.accounts.length &&
      this.props.accounts.map(account => {
        const clonedAccount = cloneDeep(account)
        clonedAccount.value = account.label
        return clonedAccount
      }),
  }

  createWalletAccount = async (e: SyntheticMouseEvent<*>) => {
    this.setState({ submitButtonDisabled: true })
    e.preventDefault()
    const {
      passphrase,
      passphrase2,
      keypart2,
      walletName,
      existingPassphrase,
      selectedAccount,
    } = this.state
    const {
      generateNewWalletAccount,
      authenticated,
      history,
      accounts,
    } = this.props

    if (selectedAccount) {
      const accountInStorage = accounts.find(
        account => account.label === selectedAccount.value,
      )

      const key = await wallet.decryptAsync(
        accountInStorage.key,
        existingPassphrase,
      )
      generateNewWalletAccount(
        passphrase,
        passphrase2,
        key,
        keypart2,
        'SPLIT',
        history,
        walletName,
        authenticated,
        () => this.setState({ submitButtonDisabled: false }),
      )
    }
  }

  toggleStep = async (e: SyntheticMouseEvent<*>) => {
    if (this.state.step === 1) {
      const { existingPassphrase, selectedAccount, keypart2 } = this.state
      const { showErrorNotification } = this.props

      if (selectedAccount) {
        const accountInStorage = this.props.accounts.find(
          account => account.label === selectedAccount.value,
        )

        this.setState({ submitButtonDisabled: true })
        e.preventDefault()

        wallet
          .decryptAsync(accountInStorage.key, existingPassphrase)
          .then(() => {
            if (keypart2 && !wallet.isWIF(keypart2)) {
              showErrorNotification({
                message: 'Invalid secondary private key WIF',
              })
            } else {
              this.setState({ step: 2 })
            }
          })
          .catch(err => showErrorNotification({ message: err.message }))
          .finally(() => this.setState({ submitButtonDisabled: false }))
      }
    } else {
      this.setState({ step: 1 })
    }
  }
<commit_msg>remove async declaration and add type for step<commit_after>// @flow
import React, { Fragment } from 'react'
import { wallet } from '@cityofzion/neon-js'
import { withRouter } from 'react-router-dom'
import { cloneDeep } from 'lodash-es'
import PasswordInput from '../Inputs/PasswordInput'
import StyledReactSelect from '../Inputs/StyledReactSelect/StyledReactSelect'
import TextInput from '../Inputs/TextInput'
import Button from '../Button'
import CheckIcon from '../../assets/icons/check.svg'
import BackArrow from '../../assets/icons/arrow.svg'
import ForwardArrow from '../../assets/icons/forward-arrow.svg'
import styles from './CreateImportSplitWalletForm.scss'

type Props = {
  generateNewWalletAccount: Function,
  history: Object,
  authenticated: boolean,
  accounts: Object,
  showErrorNotification: Object => any,
}

type State = {
  existingPassphrase: string,
  passphrase: string,
  passphrase2: string,
  passphraseValid: boolean,
  passphrase2Valid: boolean,
  passphraseError: string,
  passphrase2Error: string,
  keypart2: string,
  walletName: string,
  submitButtonDisabled: boolean,
  selectedAccount: Object | null,
  mappedAccounts: Array<Object>,
  step: number,
}

const PASS_MIN_LENGTH = 4

class CreateImportSplitWalletForm extends React.Component<Props, State> {
  static defaultProps = {
    accounts: [],
  }

  state = {
    existingPassphrase: '',
    passphrase: '',
    passphrase2: '',
    passphraseValid: false,
    passphrase2Valid: false,
    passphraseError: '',
    passphrase2Error: '',
    keypart2: '',
    walletName: '',
    submitButtonDisabled: false,
    selectedAccount: null,
    step: 1,
    mappedAccounts:
      this.props.accounts.length &&
      this.props.accounts.map(account => {
        const clonedAccount = cloneDeep(account)
        clonedAccount.value = account.label
        return clonedAccount
      }),
  }

  createWalletAccount = async (e: SyntheticMouseEvent<*>) => {
    this.setState({ submitButtonDisabled: true })
    e.preventDefault()
    const {
      passphrase,
      passphrase2,
      keypart2,
      walletName,
      existingPassphrase,
      selectedAccount,
    } = this.state
    const {
      generateNewWalletAccount,
      authenticated,
      history,
      accounts,
    } = this.props

    if (selectedAccount) {
      const accountInStorage = accounts.find(
        account => account.label === selectedAccount.value,
      )

      const key = await wallet.decryptAsync(
        accountInStorage.key,
        existingPassphrase,
      )
      generateNewWalletAccount(
        passphrase,
        passphrase2,
        key,
        keypart2,
        'SPLIT',
        history,
        walletName,
        authenticated,
        () => this.setState({ submitButtonDisabled: false }),
      )
    }
  }

  toggleStep = (e: SyntheticMouseEvent<*>) => {
    if (this.state.step === 1) {
      const { existingPassphrase, selectedAccount, keypart2 } = this.state
      const { showErrorNotification } = this.props

      if (selectedAccount) {
        const accountInStorage = this.props.accounts.find(
          account => account.label === selectedAccount.value,
        )

        this.setState({ submitButtonDisabled: true })
        e.preventDefault()

        wallet
          .decryptAsync(accountInStorage.key, existingPassphrase)
          .then(() => {
            if (keypart2 && !wallet.isWIF(keypart2)) {
              showErrorNotification({
                message: 'Invalid secondary private key WIF',
              })
            } else {
              this.setState({ step: 2 })
            }
          })
          .catch(err => showErrorNotification({ message: err.message }))
          .finally(() => this.setState({ submitButtonDisabled: false }))
      }
    } else {
      this.setState({ step: 1 })
    }
  }

{'fraction_non_alphanumeric': 0.11226501906139083, 'fraction_numerical': 0.005258314710135402, 'mean_word_length': 2.614251781472684, 'pattern_counts': {'":': 0, '<': 11, '<?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': '4443628', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2174, 'n_words': 478}