Add address encoding option to web3Accounts (#437)

* add address encoding to web3Accounts

* s/ss58Prefix/ss58Format/ (align with Substrate & keyring)

* cater for web3AccountsSubscribe

* nits

Co-authored-by: Jaco Greeff <jacogr@gmail.com>
This commit is contained in:
Thibaut Sardan
2020-09-11 16:09:02 +02:00
committed by GitHub
parent 3d5f9fffaf
commit e278f89545
3 changed files with 20 additions and 11 deletions

View File

@@ -2,7 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Injected, InjectedAccount, InjectedAccountWithMeta, InjectedExtension, InjectedExtensionInfo, InjectedProviderWithMeta, InjectedWindow, Unsubcall, ProviderList } from '@polkadot/extension-inject/types';
import { Injected, InjectedAccount, InjectedAccountWithMeta, InjectedExtension, InjectedExtensionInfo, InjectedProviderWithMeta, InjectedWindow, ProviderList, Unsubcall, Web3AccountsOptions } from '@polkadot/extension-inject/types';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
import { documentReadyPromise } from './util';
@@ -23,11 +24,15 @@ function throwError (method: string): never {
}
// internal helper to map from Array<InjectedAccount> -> Array<InjectedAccountWithMeta>
function mapAccounts (source: string, list: InjectedAccount[]): InjectedAccountWithMeta[] {
return list.map(({ address, genesisHash, name }): InjectedAccountWithMeta => ({
address,
meta: { genesisHash, name, source }
}));
function mapAccounts (source: string, list: InjectedAccount[], ss58Format?: number): InjectedAccountWithMeta[] {
return list.map(({ address, genesisHash, name }): InjectedAccountWithMeta => {
const encodedAddress = encodeAddress(decodeAddress(address), ss58Format);
return ({
address: encodedAddress,
meta: { genesisHash, name, source }
});
});
}
// have we found a properly constructed window.injectedWeb3
@@ -91,7 +96,7 @@ export function web3Enable (originName: string): Promise<InjectedExtension[]> {
}
// retrieve all the accounts accross all providers
export async function web3Accounts (): Promise<InjectedAccountWithMeta[]> {
export async function web3Accounts ({ ss58Format }: Web3AccountsOptions = {}): Promise<InjectedAccountWithMeta[]> {
if (!web3EnablePromise) {
return throwError('web3Accounts');
}
@@ -103,7 +108,7 @@ export async function web3Accounts (): Promise<InjectedAccountWithMeta[]> {
try {
const list = await accounts.get();
return mapAccounts(source, list);
return mapAccounts(source, list, ss58Format);
} catch (error) {
// cannot handle this one
return [];
@@ -122,7 +127,7 @@ export async function web3Accounts (): Promise<InjectedAccountWithMeta[]> {
return accounts;
}
export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithMeta[]) => void | Promise<void>): Promise<Unsubcall> {
export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithMeta[]) => void | Promise<void>, { ss58Format }: Web3AccountsOptions = {}): Promise<Unsubcall> {
if (!web3EnablePromise) {
return throwError('web3AccountsSubscribe');
}
@@ -133,7 +138,7 @@ export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithM
Object
.entries(accounts)
.reduce((result: InjectedAccountWithMeta[], [source, list]): InjectedAccountWithMeta[] => {
result.push(...mapAccounts(source, list));
result.push(...mapAccounts(source, list, ss58Format));
return result;
}, [])

View File

@@ -1,6 +1,6 @@
# @polkadot/extension-inject
This is a basic extension injector that manages access to the global objects available. As an extension developer, you don't need to manage access to the windo object manually, by just calling enable here, the global object is setup and managed properly. From here any dapp can access it with the `@polkadot/extension-dapp` package;
This is a basic extension injector that manages access to the global objects available. As an extension developer, you don't need to manage access to the window object manually, by just calling enable here, the global object is setup and managed properly. From here any dapp can access it with the `@polkadot/extension-dapp` package;
## Usage

View File

@@ -105,3 +105,7 @@ export interface InjectedWindow extends This {
export type InjectedExtension = InjectedExtensionInfo & Injected;
export type InjectOptions = InjectedExtensionInfo;
export interface Web3AccountsOptions {
ss58Format?: number
}