Skip to content

Commit 2cb66fc

Browse files
authored
refactor!: remove useWalletAPIServer React hook from server package (#494)
BREAKING CHANGE: The useWalletAPIServer React hook has been removed from @ledgerhq/wallet-api-server (v3.0.0). Users should instantiate WalletAPIServer directly using the constructor or implement their own React hook. Changes: - Remove useWalletAPIServer hook and react.ts file from server package - Remove React peer dependencies from package.json - Update documentation to reflect removal and provide migration guide - Update examples to use WalletAPIServer constructor directly - Remove setAccounts() and setCurrencies() references (v2.0.0 changes) - Add callouts explaining new account.list and currency.list handler requirements - Update Ledger Live integration docs to clarify custom hook implementation Migration: - Use WalletAPIServer constructor directly for simple use cases - Implement custom React hook for advanced state management (see Ledger Live example)
1 parent 92ba3a0 commit 2cb66fc

File tree

10 files changed

+129
-191
lines changed

10 files changed

+129
-191
lines changed

.changeset/famous-apricots-tan.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
"@ledgerhq/wallet-api-server": major
3+
---
4+
5+
refactor!: remove useWalletAPIServer React hook from server package
6+
7+
BREAKING CHANGE: The useWalletAPIServer React hook has been removed from
8+
@ledgerhq/wallet-api-server (v3.0.0). Users should instantiate WalletAPIServer
9+
directly using the constructor or implement their own React hook.
10+
11+
Changes:
12+
- Remove useWalletAPIServer hook and react.ts file from server package
13+
- Remove React peer dependencies from package.json
14+
- Update documentation to reflect removal and provide migration guide
15+
- Update examples to use WalletAPIServer constructor directly
16+
- Remove setAccounts() and setCurrencies() references (v2.0.0 changes)
17+
- Add callouts explaining new account.list and currency.list handler requirements
18+
- Update Ledger Live integration docs to clarify custom hook implementation
19+
20+
Migration:
21+
- Use WalletAPIServer constructor directly for simple use cases
22+
- Implement custom React hook for advanced state management (see Ledger Live example)

apps/docs/pages/docs/discover/wallet-api/server/index.mdx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ shows how a server can be used, instantiating it via its constructor
2929

3030
how to instantiate a server via its constructor
3131

32-
### Check out [here](./server/wallet-api-server/react-hook)
33-
34-
(optional, useful if using react) how to instantiate a server via a react hook
35-
3632
### Check out [this example](./server/usage-examples/with-constructor)
3733

3834
shows how a server can be used, instantiating it via its constructor
3935

4036
### Check out [the usage of a server made within Ledger Live](./server/usage-examples/within-ledger-live)
4137

42-
shows how a server can be used, instantiating it via the react hook
38+
shows how Ledger Live implements its own React hook to instantiate and manage the server
4339

4440
</Steps>
4541

apps/docs/pages/docs/discover/wallet-api/server/usage-examples/with-constructor.mdx

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Callout } from "nextra/components";
2+
13
## Example usage of the walletAPIServer
24

35
Here's how you can instantiate a new `WalletAPIServer` using its [constructor](../wallet-api-server#constructor)
@@ -29,43 +31,56 @@ const wapiServer = new WalletAPIServer(transport, config);
2931

3032
two required arguments are passed, [transport](../wallet-api-server#transport) and [config](../wallet-api-server#walletcontextconfig).
3133

32-
### setup accounts, currencies and permissions via setters
34+
### setup permissions
3335

3436
```ts
35-
wapiServer.setAccounts(accounts);
36-
wapiServer.setCurrencies(currencies);
3737
wapiServer.setPermissions({
38-
currencyIds: ["**"],
39-
methodIds: ["account.list", "account.receive", "account.request"],
38+
methodIds: [
39+
"account.list",
40+
"account.receive",
41+
"account.request",
42+
"currency.list",
43+
],
4044
});
4145
```
4246

43-
Call setters to set the [accounts](../wallet-api-server#allaccounts),
44-
[currencies](../wallet-api-server#allcurrencies),
45-
and [permissions](../wallet-api-server#permissions)
47+
Set the [permissions](../wallet-api-server#permissions) to control which methods the app can access.
48+
49+
<Callout type="info" emoji="ℹ️">
50+
**Note:** As of version 2.0.0, `setAccounts()` and `setCurrencies()` have been
51+
removed. You must now implement `account.list` and `currency.list` handlers
52+
instead.
53+
</Callout>
4654

4755
### set wallet handlers
4856

4957
```ts
50-
wapiServer.setHandler("account.request", async ({ accounts$, currencies$ }) => {
51-
return new Promise((resolve, reject) => {
52-
uiAccountRequest({
53-
accounts$,
54-
currencies: currencies.map({id} => id),
55-
onSuccess: (account: AccountLike, parentAccount: Account | undefined) => {
56-
resolve(accountToWalletAPIAccount(account, parentAccount));
57-
},
58-
onCancel: () => {
59-
reject(new Error("Canceled by user"));
60-
},
61-
});
62-
});
63-
})
58+
// Handler to list all accounts
59+
wapiServer.setHandler("account.list", async () => {
60+
return accounts.map((account) => accountToWalletAPIAccount(account));
61+
});
6462

63+
// Handler to list all currencies
64+
wapiServer.setHandler("currency.list", async () => {
65+
return currencies;
66+
});
67+
68+
// Handler to request an account
69+
wapiServer.setHandler("account.request", async ({ accountIds }) => {
70+
return new Promise((resolve, reject) => {
71+
uiAccountRequest({
72+
onSuccess: (account: AccountLike, parentAccount: Account | undefined) => {
73+
resolve(accountToWalletAPIAccount(account, parentAccount));
74+
},
75+
onCancel: () => {
76+
reject(new Error("Canceled by user"));
77+
},
78+
});
79+
});
80+
});
6581
```
6682

67-
Set a [walletHandler](../wallet-api-server#wallethandlers) to handle
68-
actions that would request an account from the wallet.
83+
Set [walletHandlers](../wallet-api-server#wallethandlers) to handle wallet operations. The `account.list` and `currency.list` handlers are now required.
6984

7085
### wire up transport
7186

apps/docs/pages/docs/discover/wallet-api/server/usage-examples/within-ledger-live.mdx

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ import { Steps } from "nextra/components";
33

44
## Usage of the walletAPIServer in Ledger Live
55

6-
The react hook [useWalletAPIServer](../wallet-api-server/react-hook)
7-
is used within [Ledger Live](https://github.com/LedgerHQ/ledger-live/)
8-
to create a walletAPIServer
6+
[Ledger Live](https://github.com/LedgerHQ/ledger-live/) implements its own React hook `useWalletAPIServer` to create and manage a `WalletAPIServer` instance.
97

10-
It is used in _ledger-live-common_ here
11-
**ledger-live-common/src/wallet-api/react.ts**
12-
In this file, a walletAPIServer
13-
is created, and exposed through another hook (also called
14-
`useWalletAPIServer`)
8+
<Callout type="info" emoji="ℹ️">
9+
The `useWalletAPIServer` hook is implemented directly in Ledger Live (in
10+
**ledger-live-common/src/wallet-api/react.ts**), not in the wallet-api
11+
library. This allows Ledger Live to have full control over the server
12+
instantiation and configuration.
13+
</Callout>
1514

16-
This (LLC hook) is in turn used in both
15+
This Ledger Live hook is used in both
1716
[LLD](https://github.com/LedgerHQ/ledger-live/blob/43aeb2a1c650c6231c14149b74e4a42135b15766/apps/ledger-live-desktop/src/renderer/components/Web3AppWebview/WalletAPIWebview.tsx#L10)
1817
and [LLM](https://github.com/LedgerHQ/ledger-live/blob/43aeb2a1c650c6231c14149b74e4a42135b15766/apps/ledger-live-mobile/src/components/Web3AppWebview/helpers.ts#L8)
1918

@@ -74,18 +73,14 @@ here's a list of actions it triggers in LLD:
7473

7574
### LLC useWalletAPIServer() gets called
7675

77-
The goal of LLC's `useWalletAPIServer` is simply to call the walletAPIServer hook (also called `useWalletAPIServer`)
76+
Ledger Live's `useWalletAPIServer` hook instantiates a `WalletAPIServer` directly using its [constructor](../wallet-api-server#constructor).
7877

7978
Before doing so it:
8079

81-
- Extracts [permissions](../wallet-api-server#permissions) From the manifest
82-
83-
- Converts accounts sent from [useWebView](./within-ledger-live#usewebview) (coming from redux store) to [a format readable by walletAPIServer](../wallet-api-server#allaccounts)
80+
- Extracts [permissions](../wallet-api-server#permissions) from the manifest
8481

8582
- Fetches and filters currencies (coming from `libs/ledger-live-common/src/currencies/helpers.ts` `listCurrencies`)
8683

87-
- Converts filtered currencies to a format readable by wallet-api-server. More on that format [here](../wallet-api-server/react-hook#currencies)
88-
8984
- Creates a [transport](../wallet-api-server#transport)
9085

9186
```js
@@ -103,17 +98,16 @@ function useTransport(postMessage: (message: string) => void | undefined): Trans
10398

10499
### walletAPIServer gets instantiated
105100

106-
via the react hook [useWalletAPIServer](../wallet-api-server/react-hook)
101+
The `WalletAPIServer` is instantiated using the [WalletAPIServer constructor](../wallet-api-server#constructor) with the prepared transport, config, logger, and custom handlers.
107102

108103
### post-instantiation transport setup
109104

110105
walletAPIServer sends back its `onMessage` callback, the webview will use
111106
it to send message to it.
112107

113108
```ts {1,12-13, 20}
114-
const { onMessage } = useWalletAPIServer({
109+
const { server, onMessage } = useWalletAPIServer({
115110
manifest,
116-
accounts,
117111
config,
118112
webviewHook,
119113
uiHook,
@@ -126,7 +120,7 @@ const handleMessage = useCallback(
126120
onMessage(event.args[0]);
127121
}
128122
},
129-
[onMessage]
123+
[onMessage],
130124
);
131125

132126
useEffect(() => {
@@ -143,13 +137,9 @@ to setup Ledger Live Wallet/UI/Store callbacks.
143137
<summary>Simplified example of setHandler() call</summary>
144138

145139
```js filename="libs/ledger-live-common/src/wallet-api/react.ts"
146-
server.setHandler("account.request", async ({ accounts$, currencies$ }) => {
147-
const currencies = await firstValueFrom(currencies$);
140+
server.setHandler("account.request", async ({ accountIds }) => {
148141
return new Promise((resolve, reject) => {
149-
let currencyList = currencyList = allCurrenciesAndTokens.filter(({ id }) => currencyIds.includes(id));
150142
uiAccountRequest({
151-
accounts$,
152-
currencies: currencyList,
153143
onSuccess: (account: AccountLike, parentAccount: Account | undefined) => {
154144
resolve(accountToWalletAPIAccount(account, parentAccount));
155145
},
@@ -197,11 +187,10 @@ flowchart TD
197187
SENDS:
198188
199189
transport: so server can communicate TO webview
200-
accounts
201-
currencies
190+
config
202191
permissions
203192
customHandlers
204-
| B{wallet-api-server useWalletAPIServer}
193+
| B{WalletAPIServer constructor}
205194
B --> |
206195
GETS BACK:
207196

apps/docs/pages/docs/discover/wallet-api/server/wallet-api-server/index.mdx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ export type Account = {
5050

5151
</details>
5252

53-
| Created via | name |
54-
| ----------- | --------------- |
55-
| _setter_ | **setAccounts** |
53+
<Callout type="warning" emoji="⚠️">
54+
**Note:** As of version 2.0.0, accounts are no longer managed by the server.
55+
Instead, wallet implementations must provide an `account.list` handler that
56+
returns the list of available accounts.
57+
</Callout>
5658

5759
### allCurrencies$
5860

@@ -76,9 +78,11 @@ type Currency = {
7678

7779
</details>
7880

79-
| Created via | name |
80-
| ----------- | ----------------- |
81-
| _setter_ | **setCurrencies** |
81+
<Callout type="warning" emoji="⚠️">
82+
**Note:** As of version 2.0.0, currencies are no longer managed by the server.
83+
Instead, wallet implementations must provide a `currency.list` handler that
84+
returns the list of available currencies.
85+
</Callout>
8286

8387
---
8488

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,48 @@
11
import { Callout } from "nextra/components";
22

3-
## useWalletAPIServer
3+
## useWalletAPIServer Hook (Removed)
44

5-
[source code](https://github.com/LedgerHQ/wallet-api/blob/main/packages/server/src/react.ts)
5+
<Callout type="warning" emoji="⚠️">
6+
**This React hook has been removed from the wallet-api library as of version
7+
3.0.0** The `useWalletAPIServer` hook is no longer exported from
8+
`@ledgerhq/wallet-api-server`.
9+
</Callout>
610

7-
For convenience in a react environment, the hook `useWalletAPIServer`, is available.
11+
### Migration Guide
812

9-
It is used to setup a WalletAPIServer
13+
If you were using this hook, you have two options:
1014

11-
<Callout type="info" emoji="ℹ️">
12-
[It is how wallet API Server are created in ledger
13-
live](../usage-examples/within-ledger-live)
14-
</Callout>
15+
1. **Use the WalletAPIServer constructor directly** (recommended)
1516

16-
```js filename="packages/server/src/WalletAPIServer.ts"
17-
18-
export function useWalletAPIServer({
19-
transport,
20-
config,
21-
logger,
22-
accounts,
23-
currencies,
24-
permission,
25-
customHandlers,
26-
}){...}
27-
28-
```
29-
30-
| Parameter (destructured from single object) | required? | note |
31-
| ------------------------------------------- | --------- | ------------------------------------------------------------------------------- |
32-
| _transport_ || sets the [transport](../wallet-api-server#transport) |
33-
| _config_ || sets the [walletContext.config](../wallet-api-server#walletcontextconfig) value |
34-
| _logger_ || (optional) sets the logger |
35-
| _accounts_ || sets the [accounts](../wallet-api-server#allaccounts) |
36-
| _currencies_ || sets the [currencies](../wallet-api-server#allcurrencies) |
37-
| _permissions_ || sets the [permissions](../wallet-api-server#permissions) |
38-
| _customHandlers_ || (optional) sets [customHandlers](../wallet-api-server#walletcontextconfig) |
39-
40-
Once instantiated, it
41-
42-
- Sets custom handlers, in all cases (even if no custom handlers are sent from LLC),
43-
it will be called to set the [requestHandlers](../wallet-api-server#requesthandlers)
44-
- Sets config, permissions, currencies and accounts.
45-
46-
Then, it returns:
47-
48-
```ts
49-
return {
50-
server,
51-
onMessage,
52-
};
53-
```
54-
55-
`server` is our walletAPIServer instance.
56-
`onMessage`, which will allow _APP -> SERVER_ communication.
57-
58-
<Callout type="info" emoji="ℹ️">
59-
Note that WalletAPIServer is instantiated as a ref
60-
[useRef](https://react.dev/reference/react/useRef) we actually return
61-
```server: server.current```
62-
</Callout>
17+
Instantiate the server using the [WalletAPIServer constructor](../wallet-api-server#constructor):
18+
19+
```ts
20+
import { WalletAPIServer } from "@ledgerhq/wallet-api-server";
21+
22+
const server = new WalletAPIServer(
23+
transport,
24+
config,
25+
logger,
26+
customHandlers,
27+
);
28+
```
29+
30+
2. **Implement your own React hook** (advanced use case)
31+
32+
You can create your own React hook similar to how [Ledger Live implements it](../usage-examples/within-ledger-live). This gives you full control over the server lifecycle and state management.
33+
34+
### Breaking Changes in v2.0.0
35+
36+
- Removed RxJS dependency from the server package
37+
- Removed observable-based state management (accounts$, currencies$, etc.)
38+
- Removed `setAccounts()` and `setCurrencies()` methods from WalletAPIServer
39+
- Wallet handlers now accept `accountId` strings instead of Account objects
40+
- New required wallet handlers: `account.list` and `currency.list`
41+
42+
For more details, see the [changelog](https://github.com/LedgerHQ/wallet-api/blob/main/packages/server/CHANGELOG.md).
43+
44+
### Reference Implementation
45+
46+
To see how Ledger Live implements server management in a React environment, check out:
47+
48+
- [Ledger Live's usage example](../usage-examples/within-ledger-live)

packages/server/package.json

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
"@types/jest": "^29.5.12",
2323
"@types/node": "^24.10.0",
2424
"@types/picomatch": "^2.3.3",
25-
"@types/react": "^18.2.57",
2625
"eslint": "^8.56.0",
2726
"jest": "^29.7.0",
2827
"jest-environment-jsdom": "^29.7.0",
29-
"react": "^18.2.0",
3028
"ts-jest": "^29.1.2",
3129
"ts-node": "^10.9.2",
3230
"typescript": "^5.3.3"
@@ -35,13 +33,5 @@
3533
"@ledgerhq/wallet-api-core": "workspace:*",
3634
"bignumber.js": "^9.1.2",
3735
"picomatch": "^4.0.1"
38-
},
39-
"peerDependencies": {
40-
"react": "^17.x || ^18.x || ^19.x"
41-
},
42-
"peerDependenciesMeta": {
43-
"react": {
44-
"optional": true
45-
}
4636
}
4737
}

packages/server/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export * from "./react";
21
export * from "./types";
32
export * from "./WalletAPIServer";

0 commit comments

Comments
 (0)