Add a flag to get any account type for InjectedAccounts interface (#572)

* add getAll and deprecate get

* using a withTypes flag

* change for anyType and add filtering

Co-authored-by: Github Actions <action@github.com>
This commit is contained in:
Thibaut Sardan
2020-12-10 14:51:51 +01:00
committed by GitHub
co-authored by Github Actions
parent 87ae7e7414
commit 5df2f974d7
4 changed files with 19 additions and 11 deletions
@@ -6,7 +6,7 @@ import type { KeyringPair } from '@polkadot/keyring/types';
import type { JsonRpcResponse } from '@polkadot/rpc-provider/types';
import type { SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
import type { SubjectInfo } from '@polkadot/ui-keyring/observable/types';
import type { MessageTypes, RequestAuthorizeTab, RequestRpcSend, RequestRpcSubscribe, RequestRpcUnsubscribe, RequestTypes, ResponseRpcListProviders, ResponseSigning, ResponseTypes, SubscriptionMessageTypes } from '../types';
import type { MessageTypes, RequestAccountList, RequestAuthorizeTab, RequestRpcSend, RequestRpcSubscribe, RequestRpcUnsubscribe, RequestTypes, ResponseRpcListProviders, ResponseSigning, ResponseTypes, SubscriptionMessageTypes } from '../types';
import { PHISHING_PAGE_REDIRECT } from '@polkadot/extension-base/defaults';
import { checkIfDenied } from '@polkadot/phishing';
@@ -19,13 +19,17 @@ import RequestExtrinsicSign from '../RequestExtrinsicSign';
import State from './State';
import { createSubscription, unsubscribe } from './subscriptions';
function transformAccounts (accounts: SubjectInfo): InjectedAccount[] {
function transformAccounts (accounts: SubjectInfo, anyType = false): InjectedAccount[] {
return Object
.values(accounts)
.filter(({ json: { meta: { isHidden } } }) => !isHidden)
.filter(({ type }) => anyType ? true : type && ['ed25519', 'sr25519', 'ecdsa'].includes(type))
.sort((a, b) => (a.json.meta.whenCreated || 0) - (b.json.meta.whenCreated || 0))
.map(({ json: { address, meta: { genesisHash, name } } }): InjectedAccount => ({
address, genesisHash, name
.map(({ json: { address, meta: { genesisHash, name } }, type }): InjectedAccount => ({
address,
genesisHash,
name,
type
}));
}
@@ -41,8 +45,8 @@ export default class Tabs {
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
private accountsList (url: string): InjectedAccount[] {
return transformAccounts(accountsObservable.subject.getValue());
private accountsList (url: string, { anyType }: RequestAccountList): InjectedAccount[] {
return transformAccounts(accountsObservable.subject.getValue(), anyType);
}
// FIXME This looks very much like what we have in Extension
@@ -168,7 +172,7 @@ export default class Tabs {
return this.authorize(url, request as RequestAuthorizeTab);
case 'pub(accounts.list)':
return this.accountsList(url);
return this.accountsList(url, request as RequestAccountList);
case 'pub(accounts.subscribe)':
return this.accountsSubscribe(url, id, port);
@@ -223,7 +223,9 @@ export interface RequestAccountExport {
password: string;
}
export type RequestAccountList = null;
export interface RequestAccountList {
anyType?: boolean;
}
export type RequestAccountSubscribe = null;
+2 -2
View File
@@ -12,8 +12,8 @@ export default class Accounts implements InjectedAccounts {
sendRequest = _sendRequest;
}
public get (): Promise<InjectedAccount[]> {
return sendRequest('pub(accounts.list)');
public get (anyType?: boolean): Promise<InjectedAccount[]> {
return sendRequest('pub(accounts.list)', { anyType });
}
public subscribe (cb: (accounts: InjectedAccount[]) => unknown): Unsubcall {
+3 -1
View File
@@ -3,6 +3,7 @@
import type { Signer as InjectedSigner } from '@polkadot/api/types';
import type { ProviderInterface } from '@polkadot/rpc-provider/types';
import type { KeypairType } from '@polkadot/util-crypto/types';
// eslint-disable-next-line no-undef
type This = typeof globalThis;
@@ -13,6 +14,7 @@ export interface InjectedAccount {
address: string;
genesisHash?: string | null;
name?: string;
type?: KeypairType;
}
export interface InjectedAccountWithMeta {
@@ -25,7 +27,7 @@ export interface InjectedAccountWithMeta {
}
export interface InjectedAccounts {
get: () => Promise<InjectedAccount[]>;
get: (anyType?: boolean) => Promise<InjectedAccount[]>;
subscribe: (cb: (accounts: InjectedAccount[]) => void | Promise<void>) => Unsubcall;
}