Introduce the enable() interface (prompts user) (#20)

* Introduce the enable() interface

* Provider

* Add auth requests to state

* injector comments

* Background extension list, spprove & reject

* Cleanups

* Untested, but the UI parts are there (test next)

* linting

* Not pretty, but seems to auth

* Small styling adjustments

* Adjust Box layout slightly

* Small styling adjustments

* signing -> transactions

* isDanger for links

* Styling defaults
This commit is contained in:
Jaco Greeff
2019-05-23 12:43:55 +02:00
committed by GitHub
parent 4a2768cee8
commit f241e7191d
34 changed files with 582 additions and 247 deletions
@@ -5,9 +5,8 @@
import { OnActionFromCtx } from '../../components/types';
import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { ActionBar, Address, withAction } from '../../components';
import { ActionBar, Address, Link, withOnAction } from '../../components';
import { editAccount } from '../../messaging';
import { Name } from '../../partials';
@@ -53,11 +52,11 @@ function Account ({ address, className, onAction }: Props) {
}
>
<ActionBar>
<a href='#' onClick={toggleEdit}>Edit</a>
<Link onClick={toggleEdit}>Edit</Link>
<Link to={`/account/forget/${address}`}>Forget</Link>
</ActionBar>
</Address>
);
}
export default withAction(Account);
export default withOnAction(Account);
@@ -0,0 +1,56 @@
// Copyright 2019 @polkadot/extension-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MessageAuthorize } from '@polkadot/extension/background/types';
import { OnActionFromCtx } from '../../components/types';
import React from 'react';
import styled from 'styled-components';
import { ActionBar, Box, Button, Link, defaults, withOnAction } from '../../components';
import { approveAuthRequest, rejectAuthRequest } from '../../messaging';
type Props = {
authId: number,
className?: string,
isFirst: boolean,
onAction: OnActionFromCtx,
request: MessageAuthorize,
url: string
};
function Request ({ authId, className, isFirst, onAction, request: { origin }, url }: Props) {
const onApprove = (): Promise<void> =>
approveAuthRequest(authId)
.then(() => onAction())
.catch(console.error);
const onReject = (): void => {
rejectAuthRequest(authId)
.then(() => onAction())
.catch(console.error);
};
return (
<Box className={className}>
<div>The application, identified as <div className='tab-name'>{origin}</div> is requesting access from <div className='tab-url'>{url}</div> to the accounts and signing capabilities of this extension. Only approve the request if you trust the application.</div>
<ActionBar>
<Link isDanger onClick={onReject}>Reject</Link>
</ActionBar>
{isFirst && (
<Button
label='Yes, allow this application access'
onClick={onApprove}
/>
)}
</Box>
);
}
export default withOnAction(styled(Request)`
.tab-name,
.tab-url {
color: ${defaults.linkColor};
display: inline-block;
}
`);
@@ -0,0 +1,33 @@
// Copyright 2019 @polkadot/extension-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AuthRequestsFromCtx } from '../../components/types';
import React from 'react';
import { Header, withAuthRequests } from '../../components';
import Request from './Request';
type Props = {
requests: AuthRequestsFromCtx
};
function Authorize ({ requests }: Props) {
return (
<div>
<Header label='authorize' />
{requests.map(([id, request, url], index) => (
<Request
authId={id}
isFirst={index === 0}
key={id}
request={request}
url={url}
/>
))}
</div>
);
}
export default withAuthRequests(Authorize);
+2 -2
View File
@@ -6,7 +6,7 @@ import { OnActionFromCtx } from '../components/types';
import React, { useState, useEffect } from 'react';
import { Address, Button, Header, Loading, TextArea, withAction } from '../components';
import { Address, Button, Header, Loading, TextArea, withOnAction } from '../components';
import { createAccount, createSeed } from '../messaging';
import { Back, Name, Password } from '../partials';
@@ -69,4 +69,4 @@ function Create ({ onAction }: Props) {
);
}
export default withAction(Create);
export default withOnAction(Create);
+2 -2
View File
@@ -7,7 +7,7 @@ import { OnActionFromCtx } from '../components/types';
import React from 'react';
import { RouteComponentProps, withRouter } from 'react-router';
import { Address, Button, Header, Tip, withAction } from '../components';
import { Address, Button, Header, Tip, withOnAction } from '../components';
import { forgetAccount } from '../messaging';
import { Back } from '../partials';
@@ -37,4 +37,4 @@ function Forget ({ match: { params: { address } }, onAction }: Props) {
);
}
export default withAction(withRouter(Forget));
export default withOnAction(withRouter(Forget));
+2 -2
View File
@@ -6,7 +6,7 @@ import { OnActionFromCtx } from '../components/types';
import React, { useState } from 'react';
import { Address, Button, Header, TextArea, withAction } from '../components';
import { Address, Button, Header, TextArea, withOnAction } from '../components';
import { createAccount, validateSeed } from '../messaging';
import { Back, Name, Password } from '../partials';
@@ -67,4 +67,4 @@ function Import ({ onAction }: Props) {
);
}
export default withAction(Import);
export default withOnAction(Import);
@@ -7,8 +7,8 @@ import { OnActionFromCtx } from '../../components/types';
import React from 'react';
import { ActionBar, Address, withAction } from '../../components';
import { approveRequest, cancelRequest } from '../../messaging';
import { ActionBar, Address, Link, withOnAction } from '../../components';
import { approveSignRequest, cancelSignRequest } from '../../messaging';
import Details from './Details';
import Unlock from './Unlock';
@@ -22,12 +22,12 @@ type Props = {
function Request ({ isFirst, onAction, request: { address, genesisHash, method, nonce }, signId, url }: Props) {
const onCancel = (): void => {
cancelRequest(signId)
cancelSignRequest(signId)
.then(() => onAction())
.catch(console.error);
};
const onSign = (password: string): Promise<void> =>
approveRequest(signId, password).then(() => onAction());
approveSignRequest(signId, password).then(() => onAction());
return (
<Address address={address}>
@@ -38,11 +38,11 @@ function Request ({ isFirst, onAction, request: { address, genesisHash, method,
url={url}
/>
<ActionBar>
<a href='#' onClick={onCancel}>Cancel</a>
<Link isDanger onClick={onCancel}>Cancel</Link>
</ActionBar>
{isFirst && <Unlock onSign={onSign} />}
</Address>
);
}
export default withAction(Request);
export default withOnAction(Request);
@@ -48,4 +48,5 @@ function Unlock ({ className, onSign }: Props) {
export default styled(Unlock)`
margin-bottom: -0.75rem;
margin-left: -3rem;
`;
@@ -2,21 +2,21 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RequestsFromCtx } from '../../components/types';
import { SignRequestsFromCtx } from '../../components/types';
import React from 'react';
import { Header, withRequests } from '../../components';
import { Header, withSignRequests } from '../../components';
import Request from './Request';
type Props = {
requests: RequestsFromCtx
requests: SignRequestsFromCtx
};
function Signing ({ requests }: Props) {
return (
<div>
<Header label='requests' />
<Header label='transactions' />
{requests.map(([id, request, url], index) => (
<Request
isFirst={index === 0}
@@ -30,4 +30,4 @@ function Signing ({ requests }: Props) {
);
}
export default withRequests(Signing);
export default withSignRequests(Signing);
+27 -16
View File
@@ -2,16 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MessageExtrinsicSign } from '@polkadot/extension/background/types';
import { AuthorizeRequest, SigningRequest } from '@polkadot/extension/background/types';
import { KeyringJson } from '@polkadot/ui-keyring/types';
import React, { useEffect, useState } from 'react';
import { Route, Switch } from 'react-router';
import { Loading } from '../components';
import { AccountContext, ActionContext, SigningContext } from '../components/contexts';
import { getAccounts, getRequests } from '../messaging';
import { AccountContext, ActionContext, AuthorizeContext, SigningContext } from '../components/contexts';
import { getAccounts, getAuthRequests, getSignRequests } from '../messaging';
import Accounts from './Accounts';
import Authorize from './Authorize';
import Create from './Create';
import Forget from './Forget';
import Import from './Import';
@@ -21,15 +22,17 @@ type Props = {};
export default function Popup (props: Props) {
const [accounts, setAccounts] = useState(null as null | Array<KeyringJson>);
const [requests, setRequests] = useState(null as null | Array<[number, MessageExtrinsicSign, string]>);
const [authRequests, setAuthRequests] = useState(null as null | Array<AuthorizeRequest>);
const [signRequests, setSignRequests] = useState(null as null | Array<SigningRequest>);
const onAction = (to?: string): void => {
// loads all accounts & requests (this is passed through to children to trigger changes)
Promise
.all([getAccounts(), getRequests()])
.then(([accounts, requests]) => {
.all([getAccounts(), getAuthRequests(), getSignRequests()])
.then(([accounts, authRequests, signRequests]) => {
setAccounts(accounts);
setRequests(requests);
setAuthRequests(authRequests);
setSignRequests(signRequests);
})
.catch(console.error);
@@ -43,18 +46,26 @@ export default function Popup (props: Props) {
onAction();
}, []);
const Root = authRequests && authRequests.length
? Authorize
: signRequests && signRequests.length
? Signing
: Accounts;
return (
<Loading>{accounts && requests && (
<Loading>{accounts && authRequests && signRequests && (
<ActionContext.Provider value={onAction}>
<AccountContext.Provider value={accounts}>
<SigningContext.Provider value={requests}>
<Switch>
<Route path='/account/create' component={Create} />
<Route path='/account/forget/:address' component={Forget} />
<Route path='/account/import' component={Import} />
<Route exact path='/' component={requests.length ? Signing : Accounts} />
</Switch>
</SigningContext.Provider>
<AuthorizeContext.Provider value={authRequests}>
<SigningContext.Provider value={signRequests}>
<Switch>
<Route path='/account/create' component={Create} />
<Route path='/account/forget/:address' component={Forget} />
<Route path='/account/import' component={Import} />
<Route exact path='/' component={Root} />
</Switch>
</SigningContext.Provider>
</AuthorizeContext.Provider>
</AccountContext.Provider>
</ActionContext.Provider>
)}</Loading>
+3 -2
View File
@@ -21,9 +21,10 @@ function Box ({ children, className }: Props) {
}
export default styled(Box)`
background: transparent;
border-top: ${defaults.hdrBorder};
background: white;
border: ${defaults.boxBorder};
border-radius: ${defaults.borderRadius};
box-shadow: ${defaults.boxShadow};
color: ${defaults.color};
font-family: ${defaults.fontFamily};
font-size: ${defaults.fontSize};
@@ -23,7 +23,6 @@ function Header ({ children, className, label }: Props) {
export default styled(Header)`
background: ${defaults.hdrBg};
border-top: ${defaults.hdrBorder};
box-sizing: border-box;
color: ${defaults.hdrColor};
font-family: ${defaults.fontFamily};
@@ -0,0 +1,52 @@
// Copyright 2019 @polkadot/extension-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import React from 'react';
import styled from 'styled-components';
import { Link as RouterLink } from 'react-router-dom';
import defaults from './defaults';
type Props = {
children?: React.ReactNode,
className?: string,
isDanger?: boolean,
onClick?: () => void,
to?: string
};
function Link ({ children, className, onClick, to }: Props) {
return (
to
? <RouterLink className={className} onClick={onClick} to={to}>{children}</RouterLink>
: <a href='#' className={className} onClick={onClick} >{children}</a>
);
}
export default styled(Link)`
color: ${({ isDanger }) =>
isDanger
? defaults.linkColorDanger
: defaults.linkColor
};
opacity: 0.9;
text-decoration: none;
&:hover {
color: ${({ isDanger }) =>
isDanger
? defaults.linkColorDanger
: defaults.linkColor
};
opacity: 1.0;
}
&:visited {
color: ${({ isDanger }) =>
isDanger
? defaults.linkColorDanger
: defaults.linkColor
};
}
`;
@@ -42,6 +42,7 @@ export default styled(Tip)`
border-left: 0.25rem solid ${(props) =>
getColor(props).border
};
box-shadow: ${defaults.boxShadow};
color: ${(props) =>
getColor(props).color
};
@@ -26,21 +26,6 @@ export default styled(View)`
font-size: ${defaults.fontSize};
line-height: ${defaults.lineHeight};
a {
color: ${defaults.linkColor};
opacity: 0.9;
text-decoration: none;
&:hover {
color: ${defaults.linkColor};
opacity: 1.0;
}
&:visited {
color: ${defaults.linkColor};
}
}
h3 {
margin: 0 0 0.75rem;
text-transform: uppercase;
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountsFromCtx, OnActionFromCtx, RequestsFromCtx, SubtractProps } from './types';
import { AccountsFromCtx, OnActionFromCtx, AuthRequestsFromCtx, SignRequestsFromCtx, SubtractProps } from './types';
import React from 'react';
@@ -12,11 +12,13 @@ const noop = (to?: string) => {
const AccountContext = React.createContext([] as AccountsFromCtx);
const ActionContext = React.createContext(noop as OnActionFromCtx);
const SigningContext = React.createContext([] as RequestsFromCtx);
const AuthorizeContext = React.createContext([] as AuthRequestsFromCtx);
const SigningContext = React.createContext([] as SignRequestsFromCtx);
export {
AccountContext,
ActionContext,
AuthorizeContext,
SigningContext
};
@@ -36,7 +38,7 @@ export function withAccounts<P extends { accounts: AccountsFromCtx }> (Component
};
}
export function withAction<P extends { onAction: OnActionFromCtx }> (Component: React.ComponentType<P>): React.ComponentType<SubtractProps<P, { onAction: OnActionFromCtx }>> {
export function withOnAction<P extends { onAction: OnActionFromCtx }> (Component: React.ComponentType<P>): React.ComponentType<SubtractProps<P, { onAction: OnActionFromCtx }>> {
return (props: SubtractProps<P, { onAction: OnActionFromCtx }>) => {
return (
<ActionContext.Consumer>
@@ -52,8 +54,24 @@ export function withAction<P extends { onAction: OnActionFromCtx }> (Component:
};
}
export function withRequests<P extends { requests: RequestsFromCtx }> (Component: React.ComponentType<P>): React.ComponentType<SubtractProps<P, { requests: RequestsFromCtx }>> {
return (props: SubtractProps<P, { requests: RequestsFromCtx }>) => {
export function withAuthRequests<P extends { requests: AuthRequestsFromCtx }> (Component: React.ComponentType<P>): React.ComponentType<SubtractProps<P, { requests: AuthRequestsFromCtx }>> {
return (props: SubtractProps<P, { requests: AuthRequestsFromCtx }>) => {
return (
<AuthorizeContext.Consumer>
{(requests) => (
// @ts-ignore Something here with the props are going wonky
<Component
{...props}
requests={requests}
/>
)}
</AuthorizeContext.Consumer>
);
};
}
export function withSignRequests<P extends { requests: SignRequestsFromCtx }> (Component: React.ComponentType<P>): React.ComponentType<SubtractProps<P, { requests: SignRequestsFromCtx }>> {
return (props: SubtractProps<P, { requests: SignRequestsFromCtx }>) => {
return (
<SigningContext.Consumer>
{(requests) => (
@@ -14,19 +14,21 @@ const defaults: { [index: string]: any } = {
btnColor: LINK_COLOR,
btnColorDanger: DANGER_COLOR,
btnPadding: '0.75rem 1rem',
boxBorder: '0.25rem solid #e2e1e0',
boxMargin: '0.75rem 0',
boxPadding: '0 0.25rem',
boxShadow: 'none', // '0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24)',
color: TEXT_COLOR,
colorLabel: LABEL_COLOR,
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif',
fontSize: '1rem',
hdrBg: 'transparent',
hdrBorder: '0.25rem solid #eee',
hdrColor: '#ccc',
hdrBg: '#e2e1e0',
hdrColor: '#878786',
inputBorder: '#ccc',
inputPadding: '0.5rem 0.75rem',
lineHeight: '1.25',
linkColor: LINK_COLOR,
linkColorDanger: DANGER_COLOR,
box: {
error: {
background: '#ffe6e6',
@@ -8,6 +8,7 @@ export { default as Box } from './Box';
export { default as Button } from './Button';
export { default as Header } from './Header';
export { default as Input } from './Input';
export { default as Link } from './Link';
export { default as Loading } from './Loading';
export { default as TextArea } from './TextArea';
export { default as Tip } from './Tip';
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SigningRequest } from '@polkadot/extension/background/types';
import { AuthorizeRequest, SigningRequest } from '@polkadot/extension/background/types';
import { KeyringJson } from '@polkadot/ui-keyring/types';
export type OmitProps<T, K> = Pick<T, Exclude<keyof T, K>>;
@@ -10,4 +10,5 @@ export type SubtractProps<T, K> = OmitProps<T, keyof K>;
export type AccountsFromCtx = Array<KeyringJson>;
export type OnActionFromCtx = (to?: string) => void;
export type RequestsFromCtx = Array<SigningRequest>;
export type AuthRequestsFromCtx = Array<AuthorizeRequest>;
export type SignRequestsFromCtx = Array<SigningRequest>;
+16 -4
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MessageTypes, MessageExtrinsicSign } from '@polkadot/extension/background/types';
import { AuthorizeRequest, MessageTypes, SigningRequest } from '@polkadot/extension/background/types';
import { KeyringJson } from '@polkadot/ui-keyring/types';
import { KeypairType } from '@polkadot/util-crypto/types';
@@ -34,15 +34,27 @@ export async function getAccounts (): Promise<Array<KeyringJson>> {
return sendMessage('accounts.list');
}
export async function getRequests (): Promise<Array<[number, MessageExtrinsicSign, string]>> {
export async function getAuthRequests (): Promise<Array<AuthorizeRequest>> {
return sendMessage('authorize.requests');
}
export async function rejectAuthRequest (id: number): Promise<boolean> {
return sendMessage('authorize.reject', { id });
}
export async function approveAuthRequest (id: number): Promise<boolean> {
return sendMessage('authorize.approve', { id });
}
export async function getSignRequests (): Promise<Array<SigningRequest>> {
return sendMessage('signing.requests');
}
export async function cancelRequest (id: number): Promise<boolean> {
export async function cancelSignRequest (id: number): Promise<boolean> {
return sendMessage('signing.cancel', { id });
}
export async function approveRequest (id: number, password: string): Promise<boolean> {
export async function approveSignRequest (id: number, password: string): Promise<boolean> {
return sendMessage('signing.approve', { id, password });
}
+1 -2
View File
@@ -3,10 +3,9 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import React from 'react';
import { Link } from 'react-router-dom';
import styled from 'styled-components';
import { unicode } from '../components';
import { Link, unicode } from '../components';
type Props = {
className?: string,