{"size": 2183, "ext": "js", "max_stars_repo_path": "src/store/migrations/14_account_set_derivation_path.js", "max_stars_repo_name": "john-light/wallet", "max_stars_repo_stars_event_min_datetime": "2020-09-30T06:15:20.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-28T16:34:07.000Z", "max_issues_repo_path": "src/store/migrations/14_account_set_derivation_path.js", "max_issues_repo_name": "rootial/wallet", "max_issues_repo_issues_event_min_datetime": "2020-11-13T15:08:34.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-30T07:50:16.000Z", "max_forks_repo_path": "src/store/migrations/14_account_set_derivation_path.js", "max_forks_repo_name": "rootial/wallet", "max_forks_repo_forks_event_min_datetime": "2020-08-03T17:00:44.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-29T12:36:07.000Z"}
{"max_stars_count": 66, "max_issues_count": 169, "max_forks_count": 54, "avg_line_length": 28.7236842105, "max_line_length": 88, "alphanum_fraction": 0.5249656436}
import { getDerivationPath } from '@/utils/derivationPath'
import { Networks } from '@/utils/networks'
import { accountCreator, getNextAccountColor } from '@/utils/accounts'
import { ChainId, chains } from '@liquality/cryptoassets'

export const accountSetDerivationPath = {
  version: 14,
  migrate: async (state) => {
    const hasAccounts = Object.keys(state.accounts || {}).length > 0

    if (!hasAccounts) {
      return {
        ...state
      }
    }

    const _accounts = {}

    for (const walletId in state.accounts) {
      _accounts[walletId] = {}

      for (const network of Networks) {
        const accounts = state.accounts[walletId][network]
        const updatedAccounts = []
        for (const account of accounts) {
          const derivationPath = getDerivationPath(
            account.chain,
            network,
            account.index,
            account.type
          )
          const updatedAccount = {
            ...account,
            alias: '',
            addresses: [],
            balances: {},
            derivationPath
          }
          updatedAccounts.push(updatedAccount)

          if (account.chain === ChainId.Rootstock && !account.type.includes('ledger')) {
            // get the legacy rsk derivation path
            const coinType = network === 'mainnet' ? '137' : '37310'
            const chain = chains[ChainId.Rootstock]
            const _account = accountCreator({
              walletId,
              network,
              account: {
                name: `Legacy ${chain.name} 1`,
                alias: '',
                chain: ChainId.Rootstock,
                addresses: [],
                assets: [...account.assets],
                balances: {},
                type: 'default',
                index: 0,
                derivationPath: `m/44'/${coinType}'/0'/0/0`,
                color: getNextAccountColor(ChainId.Rootstock, 1)
              }
            })
            updatedAccounts.push(_account)
          }
        }
        _accounts[walletId][network] = updatedAccounts
      }
    }

    delete state.rskLegacyDerivation

    return {
      ...state,
      accounts: _accounts
    }
  }
}
