Skip to content

Commit

Permalink
feat: injected wallet DApp name (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
tien authored Oct 17, 2024
1 parent da23a94 commit 42d6d34
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-schools-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactive-dot/core": minor
---

Added support for specifying DApp name used by injected wallets.
2 changes: 1 addition & 1 deletion examples/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@reactive-dot/wallet-walletconnect": "workspace:^",
"date-fns": "^4.1.0",
"jotai-devtools": "^0.10.1",
"polkadot-api": "^1.5.1",
"polkadot-api": "^1.6.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-error-boundary": "^4.0.13",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const config = {
},
},
wallets: [
new InjectedWalletAggregator(),
new InjectedWalletAggregator({ originName: "ReactiveDOT React Example" }),
new LedgerWallet(),
new WalletConnect({
projectId: "68f5b7e972a51cf379b127f51a791c34",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@tsconfig/recommended": "^1.0.7",
"@tsconfig/strictest": "^2.0.5",
"eslint": "^9.12.0",
"polkadot-api": "^1.5.1",
"polkadot-api": "^1.6.1",
"typescript": "^5.6.3"
},
"peerDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/wallets/deep-link-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Wallet } from "./wallet.js";
import { Wallet, type WalletOptions } from "./wallet.js";

type ConnectionHandshake = {
uri: string;
settled: Promise<void>;
};

export abstract class DeepLinkWallet<
TOptions extends WalletOptions = WalletOptions,
TStorageKey extends string = string,
> extends Wallet<TStorageKey> {
> extends Wallet<TOptions, TStorageKey> {
abstract initiateConnectionHandshake():
| ConnectionHandshake
| Promise<ConnectionHandshake>;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export { initializeWallets } from "./initialize-wallets.js";
export { InjectedWalletAggregator } from "./injected/aggregator.js";
export { InjectedWallet } from "./injected/wallet.js";
export { LocalWallet } from "./local-wallet.js";
export { Wallet } from "./wallet.js";
export { Wallet, type WalletOptions } from "./wallet.js";
10 changes: 3 additions & 7 deletions packages/core/src/wallets/injected/aggregator.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { WalletAggregator } from "../aggregator.js";
import type { WalletOptions } from "../wallet.js";
import { InjectedWallet } from "./wallet.js";
import { InjectedWallet, type InjectedWalletOptions } from "./wallet.js";
import { getInjectedExtensions } from "polkadot-api/pjs-signer";
import { BehaviorSubject } from "rxjs";
import { map } from "rxjs/operators";

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

constructor(options?: WalletOptions) {
constructor(private readonly options?: InjectedWalletOptions) {
super();
this.#walletOptions = options;
}

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

for (const name of injectedNames) {
if (!current.has(name)) {
current.set(name, new InjectedWallet(name, this.#walletOptions));
current.set(name, new InjectedWallet(name, this.options));
}
}

Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/wallets/injected/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
import { BehaviorSubject, Observable } from "rxjs";
import { map, switchMap } from "rxjs/operators";

export class InjectedWallet extends Wallet<"connected"> {
export type InjectedWalletOptions = WalletOptions & { originName?: string };

export class InjectedWallet extends Wallet<InjectedWalletOptions, "connected"> {
readonly #extension$ = new BehaviorSubject<InjectedExtension | undefined>(
undefined,
);
Expand All @@ -20,7 +22,7 @@ export class InjectedWallet extends Wallet<"connected"> {

constructor(
public readonly name: string,
options?: WalletOptions,
options?: InjectedWalletOptions,
) {
super(options);
}
Expand All @@ -37,7 +39,9 @@ export class InjectedWallet extends Wallet<"connected"> {

async connect() {
if (this.#extension$.getValue() === undefined) {
this.#extension$.next(await connectInjectedExtension(this.name));
this.#extension$.next(
await connectInjectedExtension(this.name, this.options?.originName),
);
this.storage.setItem("connected", JSON.stringify(true));
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/wallets/local-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { PolkadotSignerAccount } from "./account.js";
import { Wallet } from "./wallet.js";
import { Wallet, type WalletOptions } from "./wallet.js";

/**
* @experimental
*/
export abstract class LocalWallet<
TAccount extends Pick<PolkadotSignerAccount, "id">,
TOptions extends WalletOptions,
TStorageKey extends string,
> extends Wallet<TStorageKey> {
> extends Wallet<TOptions, TStorageKey> {
/**
* @experimental
*/
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/wallets/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export type WalletOptions = {
storage?: PrefixedStorage | undefined;
};

export abstract class Wallet<TStorageKey extends string = string> {
export abstract class Wallet<
TOptions extends WalletOptions = WalletOptions,
TStorageKey extends string = string,
> {
abstract readonly id: string;

abstract readonly name: string;
Expand All @@ -17,7 +20,7 @@ export abstract class Wallet<TStorageKey extends string = string> {
return this.#storage.join<TStorageKey>(this.id);
}

constructor(options?: WalletOptions | undefined) {
constructor(protected readonly options?: TOptions | undefined) {
this.#storage = (options?.storage ?? defaultStorage).join("wallet");
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@tsconfig/strictest": "^2.0.5",
"@types/react": "^18.3.11",
"eslint": "^9.12.0",
"polkadot-api": "^1.5.1",
"polkadot-api": "^1.6.1",
"react": "^18.3.1",
"typescript": "^5.6.3"
},
Expand Down
6 changes: 5 additions & 1 deletion packages/wallet-ledger/src/ledger-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ type JsonLedgerAccount = Omit<LedgerAccount, "publicKey" | "id"> & {
publicKey: string;
};

export class LedgerWallet extends LocalWallet<LedgerAccount, "accounts"> {
export class LedgerWallet extends LocalWallet<
LedgerAccount,
WalletOptions,
"accounts"
> {
readonly id = "ledger";

readonly name = "Ledger";
Expand Down
58 changes: 29 additions & 29 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4530,18 +4530,18 @@ __metadata:
languageName: node
linkType: hard

"@polkadot-api/cli@npm:0.9.7":
version: 0.9.7
resolution: "@polkadot-api/cli@npm:0.9.7"
"@polkadot-api/cli@npm:0.9.9":
version: 0.9.9
resolution: "@polkadot-api/cli@npm:0.9.9"
dependencies:
"@commander-js/extra-typings": "npm:^12.1.0"
"@polkadot-api/codegen": "npm:0.12.5"
"@polkadot-api/ink-contracts": "npm:0.1.0"
"@polkadot-api/json-rpc-provider": "npm:0.0.4"
"@polkadot-api/known-chains": "npm:0.5.4"
"@polkadot-api/metadata-compatibility": "npm:0.1.9"
"@polkadot-api/observable-client": "npm:0.5.9"
"@polkadot-api/polkadot-sdk-compat": "npm:2.2.2"
"@polkadot-api/observable-client": "npm:0.5.10"
"@polkadot-api/polkadot-sdk-compat": "npm:2.3.1"
"@polkadot-api/sm-provider": "npm:0.1.3"
"@polkadot-api/smoldot": "npm:0.3.3"
"@polkadot-api/substrate-bindings": "npm:0.9.2"
Expand All @@ -4563,7 +4563,7 @@ __metadata:
bin:
papi: dist/main.js
polkadot-api: dist/main.js
checksum: 10c0/835ca3603cdef95e336b23728727e1784ed0f5892cd3014aaa4d89df418cf7fde2f07382126e97b774ad35e370260b8cddf8482d19cc7e3c4e62ef74454daa34
checksum: 10c0/808cd5c49a9c4ba921956513df66540b7f7a505a14e7a318a9ee3a7e9ced1a9f3d71c91055f52bfbbd1d877223b6cb0f59c5d5590b3209568218fa74d57e020f
languageName: node
linkType: hard

Expand Down Expand Up @@ -4685,38 +4685,38 @@ __metadata:
languageName: node
linkType: hard

"@polkadot-api/observable-client@npm:0.5.9":
version: 0.5.9
resolution: "@polkadot-api/observable-client@npm:0.5.9"
"@polkadot-api/observable-client@npm:0.5.10":
version: 0.5.10
resolution: "@polkadot-api/observable-client@npm:0.5.10"
dependencies:
"@polkadot-api/metadata-builders": "npm:0.8.2"
"@polkadot-api/substrate-bindings": "npm:0.9.2"
"@polkadot-api/utils": "npm:0.1.2"
peerDependencies:
"@polkadot-api/substrate-client": 0.2.2
rxjs: ">=7.8.0"
checksum: 10c0/045ba6138ca3f34e863ec261142f0a6a69b01494ecff84ccea47ef3867163de6d8e55288a4762342efcb987b1e639d2a09102676b5d5d3fba165623ffba4c18b
checksum: 10c0/60b14381abf4574f0251d1c6788ade8ea9313b4accfd25cea4f25f684fd45bf7b4cfc90b0d40f770af781e947081451d64d8245b8c47360632884f2aff95ac4c
languageName: node
linkType: hard

"@polkadot-api/pjs-signer@npm:0.4.5":
version: 0.4.5
resolution: "@polkadot-api/pjs-signer@npm:0.4.5"
"@polkadot-api/pjs-signer@npm:0.5.0":
version: 0.5.0
resolution: "@polkadot-api/pjs-signer@npm:0.5.0"
dependencies:
"@polkadot-api/metadata-builders": "npm:0.8.2"
"@polkadot-api/polkadot-signer": "npm:0.1.6"
"@polkadot-api/substrate-bindings": "npm:0.9.2"
"@polkadot-api/utils": "npm:0.1.2"
checksum: 10c0/f9e4027ced40cf62fe15a0ecb9a999a1a4315b0a10f36cd1877713627918460e916088006d418b87626967a1303add8bce26aa5fce573d39584656f8f5cb8638
checksum: 10c0/1eb6fb00253f601bbe45774373c90c489dfc1776e5a5e8be844194ff048a3f3040ca818d017fa33e2e31174d541eab2af915ae60c6d58ba0a1d78f52a90e6934
languageName: node
linkType: hard

"@polkadot-api/polkadot-sdk-compat@npm:2.2.2":
version: 2.2.2
resolution: "@polkadot-api/polkadot-sdk-compat@npm:2.2.2"
"@polkadot-api/polkadot-sdk-compat@npm:2.3.1":
version: 2.3.1
resolution: "@polkadot-api/polkadot-sdk-compat@npm:2.3.1"
dependencies:
"@polkadot-api/json-rpc-provider": "npm:0.0.4"
checksum: 10c0/3b3cecccbdb5cccbc632ecb5d4d66a318a7bbe4fd1e0266b2934b3bbc51a09fe3065b238cd6b1d6b325b11b936c285e4ed7e90d61cf3d37d6d01ba23b086ed0a
checksum: 10c0/19f55588fbac5b72a8e0d54e7c5958b3de76907217bba94118aa409e86b1dc1a86f392da724a258a409678fd00ae21c07d0ade566a6238a84abd10cbed6952c9
languageName: node
linkType: hard

Expand Down Expand Up @@ -4828,7 +4828,7 @@ __metadata:
"@tsconfig/recommended": "npm:^1.0.7"
"@tsconfig/strictest": "npm:^2.0.5"
eslint: "npm:^9.12.0"
polkadot-api: "npm:^1.5.1"
polkadot-api: "npm:^1.6.1"
typescript: "npm:^5.6.3"
peerDependencies:
polkadot-api: 1.x
Expand Down Expand Up @@ -4889,7 +4889,7 @@ __metadata:
date-fns: "npm:^4.1.0"
eslint: "npm:^9.12.0"
jotai-devtools: "npm:^0.10.1"
polkadot-api: "npm:^1.5.1"
polkadot-api: "npm:^1.6.1"
react: "npm:^18.3.1"
react-dom: "npm:^18.3.1"
react-error-boundary: "npm:^4.0.13"
Expand All @@ -4910,7 +4910,7 @@ __metadata:
"@types/react": "npm:^18.3.11"
eslint: "npm:^9.12.0"
jotai: "npm:^2.10.0"
polkadot-api: "npm:^1.5.1"
polkadot-api: "npm:^1.6.1"
react: "npm:^18.3.1"
typescript: "npm:^5.6.3"
peerDependencies:
Expand Down Expand Up @@ -15259,20 +15259,20 @@ __metadata:
languageName: node
linkType: hard

"polkadot-api@npm:^1.5.1":
version: 1.5.1
resolution: "polkadot-api@npm:1.5.1"
"polkadot-api@npm:^1.6.1":
version: 1.6.1
resolution: "polkadot-api@npm:1.6.1"
dependencies:
"@polkadot-api/cli": "npm:0.9.7"
"@polkadot-api/cli": "npm:0.9.9"
"@polkadot-api/ink-contracts": "npm:0.1.0"
"@polkadot-api/json-rpc-provider": "npm:0.0.4"
"@polkadot-api/known-chains": "npm:0.5.4"
"@polkadot-api/logs-provider": "npm:0.0.6"
"@polkadot-api/metadata-builders": "npm:0.8.2"
"@polkadot-api/metadata-compatibility": "npm:0.1.9"
"@polkadot-api/observable-client": "npm:0.5.9"
"@polkadot-api/pjs-signer": "npm:0.4.5"
"@polkadot-api/polkadot-sdk-compat": "npm:2.2.2"
"@polkadot-api/observable-client": "npm:0.5.10"
"@polkadot-api/pjs-signer": "npm:0.5.0"
"@polkadot-api/polkadot-sdk-compat": "npm:2.3.1"
"@polkadot-api/polkadot-signer": "npm:0.1.6"
"@polkadot-api/signer": "npm:0.1.8"
"@polkadot-api/sm-provider": "npm:0.1.3"
Expand All @@ -15286,7 +15286,7 @@ __metadata:
bin:
papi: bin/cli.mjs
polkadot-api: bin/cli.mjs
checksum: 10c0/b177954b87f89c5c9721dd0fdf060f69b45698fa45d01d615d884845a5b66a927184b17ed2723275599d7b53b9442d0698d6538af32b12802f8f70ee4b2eaa40
checksum: 10c0/496b097b4ccfa6c0b7e8c189e1093584a68473c87d64f901c0dd251ae8e06c508d0da49fee202e57ff48cb639b7d1b7a4e579acfdcabb50e0c517957a1249cc1
languageName: node
linkType: hard

Expand Down

0 comments on commit 42d6d34

Please sign in to comment.