Skip to content

Commit

Permalink
refactor: simplify wallet options
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jun 22, 2024
1 parent d54486c commit 9492c24
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
18 changes: 5 additions & 13 deletions packages/core/src/wallets/aggregator/injected.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { KeyedStorage } from "../../storage.js";
import InjectedWallet from "../injected.js";
import type { WalletOptions } from "../wallet.js";
import WalletAggregator from "./aggregator.js";
import { getInjectedExtensions } from "polkadot-api/pjs-signer";
import { BehaviorSubject } from "rxjs";
import { map } from "rxjs/operators";

export default class InjectedWalletAggregator extends WalletAggregator {
#storage: KeyedStorage | undefined;
#walletOptions: WalletOptions | undefined;

constructor(options?: { storage?: KeyedStorage }) {
constructor(options?: WalletOptions) {
super();
this.#storage = options?.storage;
this.#walletOptions = options;
}

readonly #walletMap$ = new BehaviorSubject(new Map<string, InjectedWallet>());
Expand All @@ -26,15 +26,7 @@ export default class InjectedWalletAggregator extends WalletAggregator {

for (const name of injectedNames) {
if (!current.has(name)) {
current.set(
name,
new InjectedWallet(
name,
this.#storage === undefined
? undefined
: { storage: this.#storage },
),
);
current.set(name, new InjectedWallet(name, this.#walletOptions));
}
}

Expand Down
11 changes: 3 additions & 8 deletions packages/core/src/wallets/injected.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ReDotError } from "../errors.js";
import type { KeyedStorage } from "../storage.js";
import Wallet from "./wallet.js";
import Wallet, { type WalletOptions } from "./wallet.js";
import {
connectInjectedExtension,
type InjectedExtension,
Expand All @@ -20,13 +19,9 @@ export default class InjectedWallet extends Wallet {

constructor(
public readonly name: string,
options?: { storage?: KeyedStorage },
options?: WalletOptions,
) {
super();

if (options?.storage !== undefined) {
this.storage = options?.storage;
}
super(options);
}

override readonly initialize = async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/wallets/wallet-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class WalletConnect extends DeepLinkWallet {
chainIds: string[];
optionalChainIds?: string[];
}) {
super();
super(undefined);

this.#providerOptions =
options.projectId === undefined
Expand Down
18 changes: 14 additions & 4 deletions packages/core/src/wallets/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ import { KeyedStorage } from "../storage.js";
import type { InjectedPolkadotAccount } from "polkadot-api/pjs-signer";
import type { Observable } from "rxjs";

export type WalletOptions = {
storage?: KeyedStorage | undefined;
};

export default abstract class Wallet {
abstract readonly id: string;

abstract readonly name: string;

protected storage = new KeyedStorage({
key: "@reactive-dot",
storage: globalThis.localStorage,
});
protected readonly storage: KeyedStorage;

constructor(options: WalletOptions | undefined) {
this.storage =
options?.storage ??
new KeyedStorage({
key: "@reactive-dot",
storage: globalThis.localStorage,
});
}

abstract readonly initialize: () => void | Promise<void>;

Expand Down

0 comments on commit 9492c24

Please sign in to comment.