From e278f8954518922ecb24aaa6f0a6581942e8790d Mon Sep 17 00:00:00 2001 From: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com> Date: Fri, 11 Sep 2020 16:09:02 +0200 Subject: [PATCH] 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 --- packages/extension-dapp/src/index.ts | 25 +++++++++++++++---------- packages/extension-inject/README.md | 2 +- packages/extension-inject/src/types.ts | 4 ++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/extension-dapp/src/index.ts b/packages/extension-dapp/src/index.ts index 26b68895..ad5e255f 100644 --- a/packages/extension-dapp/src/index.ts +++ b/packages/extension-dapp/src/index.ts @@ -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 -> Array -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 { } // retrieve all the accounts accross all providers -export async function web3Accounts (): Promise { +export async function web3Accounts ({ ss58Format }: Web3AccountsOptions = {}): Promise { if (!web3EnablePromise) { return throwError('web3Accounts'); } @@ -103,7 +108,7 @@ export async function web3Accounts (): Promise { 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 { return accounts; } -export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithMeta[]) => void | Promise): Promise { +export async function web3AccountsSubscribe (cb: (accounts: InjectedAccountWithMeta[]) => void | Promise, { ss58Format }: Web3AccountsOptions = {}): Promise { 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; }, []) diff --git a/packages/extension-inject/README.md b/packages/extension-inject/README.md index b000bd6e..460adaf3 100644 --- a/packages/extension-inject/README.md +++ b/packages/extension-inject/README.md @@ -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 diff --git a/packages/extension-inject/src/types.ts b/packages/extension-inject/src/types.ts index 98c2c96f..10a1a17a 100644 --- a/packages/extension-inject/src/types.ts +++ b/packages/extension-inject/src/types.ts @@ -105,3 +105,7 @@ export interface InjectedWindow extends This { export type InjectedExtension = InjectedExtensionInfo & Injected; export type InjectOptions = InjectedExtensionInfo; + +export interface Web3AccountsOptions { + ss58Format?: number +}