Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Go back to root on import
  • Loading branch information
jacogr committed May 30, 2020
commit 4b5d662e1bbb7bc499926d23859f40f93320e4a1
8 changes: 3 additions & 5 deletions packages/extension-base/src/background/handlers/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,13 @@ export default class Extension {
const pair = keyring.restoreAccount(json, password);

if (pair) {
const { address } = pair;

return { message: `Successfully added ${address}` };
return { error: null };
}
} catch (error) {
return { message: (error as Error).message };
return { error: (error as Error).message };
}

return { message: 'Could not restore account.' };
return { error: 'Could not restore account.' };
}

private jsonVerifyFile ({ json }: RequestJsonRestore): boolean {
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-base/src/background/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,5 @@ export interface RequestJsonRestore {
}

export interface ResponseJsonRestore {
message: string;
error: string | null;
}
35 changes: 22 additions & 13 deletions packages/extension-ui/src/Popup/RestoreJson.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// of the Apache-2.0 license. See the LICENSE file for details.

import { KeyringPair$Json } from '@polkadot/keyring/types';
import React, { useCallback, useState } from 'react';
import { InputWithLabel, InputFileWithLabel, Button, Address } from '../components';
import React, { useCallback, useContext, useState } from 'react';
import { ActionContext, InputWithLabel, InputFileWithLabel, Button, Address } from '../components';
import { u8aToString } from '@polkadot/util';
import styled from 'styled-components';
import { jsonRestore, jsonVerifyPassword, jsonVerifyFile } from '../messaging';
Expand Down Expand Up @@ -39,15 +39,15 @@ async function parseFile (file: Uint8Array): Promise<FileState> {
}

export default function Upload (): React.ReactElement {
const onAction = useContext(ActionContext);
const [{ address, isFileValid, json }, setJson] = useState<FileState>({ address: null, isFileValid: false, json: null });
const [{ isPassValid, password }, setPass] = useState<PassState>({ isPassValid: false, password: '' });
const [message, setMessage] = useState<string>('');

const _onChangePass = useCallback(
async (password: string): Promise<void> => {
const isPassValid = await jsonVerifyPassword(password);

setPass({ isPassValid, password });
(password: string): void => {
jsonVerifyPassword(password)
.then((isPassValid) => setPass({ isPassValid, password }))
.catch(console.error);
}, []
);

Expand All @@ -58,12 +58,22 @@ export default function Upload (): React.ReactElement {
);

const _onRestore = useCallback(
async (): Promise<void> => {
if (!json || !password) { return; }

setMessage((await jsonRestore(json, password)).message);
(): void => {
if (!json || !password) {
return;
}

jsonRestore(json, password)
.then(({ error }): void => {
if (error) {
setPass(({ password }) => ({ isPassValid: false, password }));
} else {
onAction('/');
}
})
.catch(console.error);
},
[json, password]
[json, onAction, password]
);

return (
Expand Down Expand Up @@ -95,7 +105,6 @@ export default function Upload (): React.ReactElement {
>
Restore
</Button>
{message}
</div>
</>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-ui/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.

import { Message } from '@polkadot/extension-base/types';
import { AccountJson, AuthorizeRequest, SigningRequest, RequestTypes, MessageTypes, ResponseTypes, SeedLengths, SubscriptionMessageTypes, MetadataRequest, MessageTypesWithNullRequest, MessageTypesWithNoSubscriptions, MessageTypesWithSubscriptions, ResponseDeriveValidate } from '@polkadot/extension-base/background/types';
import { AccountJson, AuthorizeRequest, SigningRequest, RequestTypes, MessageTypes, ResponseTypes, SeedLengths, SubscriptionMessageTypes, MetadataRequest, MessageTypesWithNullRequest, MessageTypesWithNoSubscriptions, MessageTypesWithSubscriptions, ResponseDeriveValidate, ResponseJsonRestore } from '@polkadot/extension-base/background/types';
import { Chain } from '@polkadot/extension-chains/types';
import { KeypairType } from '@polkadot/util-crypto/types';

Expand Down Expand Up @@ -169,6 +169,6 @@ export async function jsonVerifyPassword (password: string): Promise<boolean> {
return sendMessage('pri(json.verify.password)', password);
}

export async function jsonRestore (json: KeyringPair$Json, password: string): Promise<{ message: string }> {
export async function jsonRestore (json: KeyringPair$Json, password: string): Promise<ResponseJsonRestore> {
return sendMessage('pri(json.restore)', { json, password });
}