Skip to content

Commit

Permalink
feat(browser-extension-signing-manager): add support for multiple acc…
Browse files Browse the repository at this point in the history
…ount types while creating browser signing manager

`setAccountTypes` and `create` method now takes an array of `KeypairType` instead of a single string
to filter the available web3 accounts

BREAKING CHANGE: - `setAccountType` has been renamed to `setAccountTypes` and takes
`KeypairType[]` as parameter
- `accountType` param of `create` method of `BrowserExtensionSigningManger` has been changed to
`accountTypes` of type `KeypairType[]`
  • Loading branch information
prashantasdeveloper committed Apr 2, 2023
1 parent bc401d9 commit adadfc0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"commitlint",
"hashicorp",
"idempotency",
"Keypair",
"nrwl",
"Parens",
"pathinfo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ describe('BrowserExtensionSigningManager Class', () => {
);
});

it('should return accounts filtered by genesisHash and accountType', async () => {
it('should return accounts filtered by genesisHash and accountTypes', async () => {
accountsGetStub.mockResolvedValue(accounts);

networkAgnosticSigningManager.setGenesisHash('someHash');
networkAgnosticSigningManager.setAccountType('ed25519');
networkAgnosticSigningManager.setAccountTypes(['ed25519']);
const result = await networkAgnosticSigningManager.getAccounts();
expect(result.length).toEqual(1);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InjectedAccount, InjectedAccountWithMeta } from '@polkadot/extension-inject/types';
import { KeypairType } from '@polkadot/util-crypto/types';
import { PolkadotSigner, SigningManager } from '@polymeshassociation/signing-manager-types';

import { Extension, NetworkInfo, UnsubCallback } from '../types';
Expand All @@ -7,7 +8,7 @@ import { changeAddressFormat, enableWeb3Extension, getExtensions, mapAccounts }
export class BrowserExtensionSigningManager implements SigningManager {
private _ss58Format?: number;
private _genesisHash?: string;
private _accountType?: string;
private _accountTypes?: KeypairType[];

/**
* Create a Signing Manager that connects to a browser extension
Expand All @@ -16,7 +17,7 @@ export class BrowserExtensionSigningManager implements SigningManager {
* @param args.extensionName - name of the extension to be used (optional, defaults to 'polywallet')
* @param args.ss58Format - SS58 format for the extension in which the returned addresses will be encoded(optional)
* @param args.genesisHash - genesis hash to be used in filtering the accounts returned by the extension
* @param args.accountType - account type to be used in filtering the accounts returned by the extension
* @param args.accountTypes - account types to be used in filtering the accounts returned by the extension
*
* @note if this is the first time the user is interacting with the dApp using that specific extension,
* they will be prompted by the extension to add the dApp to the allowed list
Expand All @@ -31,9 +32,9 @@ export class BrowserExtensionSigningManager implements SigningManager {
extensionName?: string;
ss58Format?: number;
genesisHash?: string;
accountType?: string;
accountTypes?: KeypairType[];
}): Promise<BrowserExtensionSigningManager> {
const { appName, extensionName = 'polywallet', ss58Format, genesisHash, accountType } = args;
const { appName, extensionName = 'polywallet', ss58Format, genesisHash, accountTypes } = args;
const extension = await enableWeb3Extension(appName, extensionName);

const signingManager = new BrowserExtensionSigningManager(extension as Extension);
Expand All @@ -46,8 +47,8 @@ export class BrowserExtensionSigningManager implements SigningManager {
signingManager.setGenesisHash(genesisHash);
}

if (accountType) {
signingManager.setAccountType(accountType);
if (accountTypes) {
signingManager.setAccountTypes(accountTypes);
}

return signingManager;
Expand All @@ -72,8 +73,8 @@ export class BrowserExtensionSigningManager implements SigningManager {
/**
* Set the account type which will be used in filtering the accounts returned by the extension
*/
public setAccountType(accountType: string): void {
this._accountType = accountType;
public setAccountTypes(accountTypes: KeypairType[]): void {
this._accountTypes = accountTypes;
}

/**
Expand All @@ -82,7 +83,7 @@ export class BrowserExtensionSigningManager implements SigningManager {
private getWeb3Accounts(accounts: InjectedAccount[]): InjectedAccount[] {
return accounts.filter(
account =>
(!account.type || !this._accountType || this._accountType === account.type) &&
(!account.type || !this._accountTypes || this._accountTypes.includes(account.type)) &&
(!account.genesisHash || !this._genesisHash || account.genesisHash === this._genesisHash)
);
}
Expand Down

0 comments on commit adadfc0

Please sign in to comment.