Skip to content

Commit

Permalink
refactor: create addLocalKeyAndFinishSetup thunk (#2379)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbodnar authored and esaminu committed Feb 16, 2022
1 parent fc93de9 commit 01e3c30
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions packages/frontend/src/redux/slices/account/createAccountThunks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createAsyncThunk } from '@reduxjs/toolkit';
import { KeyPair } from 'near-api-js';

import { setReleaseNotesClosed } from '../../../utils/localStorage';
import { RELEASE_NOTES_MODAL_VERSION, wallet } from '../../../utils/wallet';
import { WalletError } from '../../../utils/walletError';
import { finishAccountSetup } from '../../actions/account';
import { SLICE_NAME } from './';

export const addLocalKeyAndFinishSetup = createAsyncThunk(
`${SLICE_NAME}/addLocalKeyAndFinishSetup`,
async ({
accountId,
recoveryMethod,
publicKey,
previousAccountId
}, { dispatch }) => {
if (recoveryMethod === 'ledger') {
await wallet.addLedgerAccountId(accountId);
await wallet.postSignedJson('/account/ledgerKeyAdded', { accountId, publicKey: publicKey.toString() });
} else {
const newKeyPair = KeyPair.fromRandom('ed25519');
const newPublicKey = newKeyPair.publicKey;
if (recoveryMethod !== 'phrase') {
await wallet.addNewAccessKeyToAccount(accountId, newPublicKey);
await wallet.saveAccount(accountId, newKeyPair);
} else {
const contractName = null;
const fullAccess = true;
await wallet.postSignedJson('/account/seedPhraseAdded', { accountId, publicKey: publicKey.toString() });
try {
await wallet.addAccessKey(accountId, contractName, newPublicKey, fullAccess);
await wallet.saveAccount(accountId, newKeyPair);
} catch (error) {
if (previousAccountId) {
await wallet.saveAndMakeAccountActive(previousAccountId);
}
throw new WalletError(error, 'addAccessKey.error');
}
}
}

setReleaseNotesClosed(RELEASE_NOTES_MODAL_VERSION);
await dispatch(finishAccountSetup());
}
);
2 changes: 1 addition & 1 deletion packages/frontend/src/redux/slices/account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createSelector } from "reselect";

import { isImplicitAccount } from "../../../utils/account";

const SLICE_NAME = 'account';
export const SLICE_NAME = 'account';

// Top level selectors
export const selectAccountSlice = (state) => state[SLICE_NAME];
Expand Down

0 comments on commit 01e3c30

Please sign in to comment.