@xrpl-wallet-kit/adapter-ledger 0.1.22 → 0.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -31,7 +31,8 @@ const kit = createWalletKit({
31
31
  ## Runtime Notes
32
32
 
33
33
  - Requires a browser with WebHID or WebUSB support.
34
- - Requires a connected Ledger device with the XRP app open.
34
+ - Requires a connected, unlocked Ledger device with XRP installed. During explicit connection, `autoOpenApp` requests XRP directly on the hardware (default `true`); confirm on the device when prompted. It never launches or requires Ledger Live desktop. This option is available since 0.1.23.
35
+ - Already-open XRP is left alone. Another device app is closed before requesting XRP. App switches can disconnect USB temporarily: reconnection attempts use only the originally selected device handle, never another authorized Ledger. If browser permission/re-enumeration prevents recovery, open XRP manually and select the device again.
35
36
  - Hardware wallets are explicit user-presence devices. `restoreSession()` returns `null` and does not silently reconnect after refresh.
36
37
  - Default derivation path is `44'/144'/0'/0/0`.
37
38
  - `signTransaction()` signs without submit.
@@ -52,6 +53,8 @@ createLedgerAdapter({
52
53
  accountIndex: 0,
53
54
  derivationPath: "44'/144'/0'/0/0",
54
55
  preferWebHID: true,
56
+ autoOpenApp: true,
57
+ onDeviceState: state => console.log(state), // OPENING_APP / READY
55
58
  timeout: 60000
56
59
  });
57
60
  ```
@@ -70,6 +73,8 @@ Hardware transport should be mocked in unit tests. Manual smoke testing should c
70
73
  - device disconnected;
71
74
  - device locked;
72
75
  - XRP app not open;
76
+ - XRP launch confirmation/rejection on the device, and return from another app;
77
+ - USB re-enumeration requiring manual device reselection;
73
78
  - connect success;
74
79
  - disconnect cleanup;
75
80
  - `submit:false` signed transaction result;
@@ -0,0 +1,17 @@
1
+ import { Buffer } from "buffer";
2
+ export interface LedgerAppTransport {
3
+ close(): Promise<void>;
4
+ send?(cla: number, ins: number, p1: number, p2: number, data?: Buffer): Promise<Uint8Array>;
5
+ }
6
+ interface OpenAppOptions {
7
+ signal?: AbortSignal;
8
+ timeoutMs: number;
9
+ assertCurrent(): void;
10
+ onOpening(): void;
11
+ onTransport(transport: LedgerAppTransport): void;
12
+ reopen(transport: LedgerAppTransport): Promise<LedgerAppTransport>;
13
+ }
14
+ /** Ledger OS APDUs, not Ledger Live: B0/01 query, B0/A7 close, E0/D8 open. */
15
+ export declare function ensureLedgerXrpApp(initial: LedgerAppTransport, options: OpenAppOptions): Promise<LedgerAppTransport>;
16
+ export {};
17
+ //# sourceMappingURL=device-app.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"device-app.d.ts","sourceRoot":"","sources":["../src/device-app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGhC,MAAM,WAAW,kBAAkB;IACjC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC7F;AAED,UAAU,cAAc;IACtB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,IAAI,IAAI,CAAC;IACtB,SAAS,IAAI,IAAI,CAAC;IAClB,WAAW,CAAC,SAAS,EAAE,kBAAkB,GAAG,IAAI,CAAC;IACjD,MAAM,CAAC,SAAS,EAAE,kBAAkB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;CACpE;AAcD,8EAA8E;AAC9E,wBAAsB,kBAAkB,CAAC,OAAO,EAAE,kBAAkB,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAsE1H"}
@@ -0,0 +1,110 @@
1
+ import { Buffer } from "buffer";
2
+ import { waitForWalletRequest, WalletKitError, WalletKitErrorCode } from "@xrpl-wallet-kit/core";
3
+ const manual = () => new Error("Open the XRP app on your Ledger device, then connect again. Ledger Live desktop is not required.");
4
+ const status = (error) => error?.statusCode;
5
+ const unsupported = (error) => [0x6d00, 0x6e00, 0x6511, 0x650f, 0x6807].includes(status(error) ?? 0);
6
+ const disconnected = (error) => status(error) === undefined && /disconnect|device.*lost|device.*not.*found/i.test(String(error));
7
+ const isDashboard = (name) => ["BOLOS", "OLOS", "OLOS\0", "DASHBOARD"].includes(name);
8
+ function appName(response) {
9
+ const length = response[1];
10
+ if (response[0] !== 1 || !length || length + 4 > response.length)
11
+ throw manual();
12
+ return Buffer.from(response.subarray(2, length + 2)).toString("ascii").toUpperCase();
13
+ }
14
+ /** Ledger OS APDUs, not Ledger Live: B0/01 query, B0/A7 close, E0/D8 open. */
15
+ export async function ensureLedgerXrpApp(initial, options) {
16
+ if (!initial.send)
17
+ return initial;
18
+ let transport = initial;
19
+ const deadline = Date.now() + options.timeoutMs;
20
+ const active = () => {
21
+ options.assertCurrent();
22
+ if (options.signal?.aborted)
23
+ throw new WalletKitError(WalletKitErrorCode.REQUEST_CANCELLED, "Ledger connection cancelled.");
24
+ if (Date.now() >= deadline)
25
+ throw new WalletKitError(WalletKitErrorCode.REQUEST_TIMEOUT, "Ledger app opening timed out. Check your Ledger device.");
26
+ };
27
+ const bounded = async (operation) => {
28
+ active();
29
+ const value = await waitForWalletRequest(operation, { signal: options.signal, timeoutMs: Math.max(1, deadline - Date.now()) });
30
+ active();
31
+ return value;
32
+ };
33
+ const send = (cla, ins, data) => bounded(() => transport.send(cla, ins, 0, 0, data));
34
+ const reopen = async () => {
35
+ // Closing can wait for a cancelled exchange; never let cleanup extend our deadline.
36
+ void transport.close().catch(() => { });
37
+ const previous = transport;
38
+ await bounded(async () => {
39
+ const next = await options.reopen(previous);
40
+ try {
41
+ active();
42
+ }
43
+ catch (error) {
44
+ void next.close().catch(() => { });
45
+ throw error;
46
+ }
47
+ transport = next;
48
+ options.onTransport(next);
49
+ });
50
+ };
51
+ const waitForApp = async (expected) => {
52
+ const readyDeadline = Math.min(deadline, Date.now() + 5000);
53
+ while (true) {
54
+ active();
55
+ try {
56
+ const name = appName(await send(0xb0, 0x01));
57
+ if (name === expected || expected === "BOLOS" && isDashboard(name))
58
+ return;
59
+ }
60
+ catch (error) {
61
+ if (!disconnected(error) && !unsupported(error))
62
+ throw error;
63
+ if (disconnected(error)) {
64
+ try {
65
+ await reopen();
66
+ }
67
+ catch (reopenError) {
68
+ active();
69
+ if (!disconnected(reopenError))
70
+ throw reopenError;
71
+ }
72
+ }
73
+ }
74
+ if (Date.now() >= readyDeadline)
75
+ throw manual();
76
+ await bounded(() => new Promise(resolve => setTimeout(resolve, 250)));
77
+ }
78
+ };
79
+ let name;
80
+ try {
81
+ name = appName(await send(0xb0, 0x01));
82
+ }
83
+ catch (error) {
84
+ // Older apps may lack the OS query. Preserve the existing XRP connection path.
85
+ if (unsupported(error))
86
+ return initial;
87
+ throw error;
88
+ }
89
+ if (name === "XRP")
90
+ return transport;
91
+ options.onOpening();
92
+ const switchApp = async (cla, ins, data) => {
93
+ try {
94
+ await send(cla, ins, data);
95
+ }
96
+ catch (error) {
97
+ if (unsupported(error))
98
+ throw manual();
99
+ if (!disconnected(error))
100
+ throw error; // Never retry a rejection or locked-device error.
101
+ }
102
+ };
103
+ if (!isDashboard(name)) {
104
+ await switchApp(0xb0, 0xa7);
105
+ await waitForApp("BOLOS");
106
+ }
107
+ await switchApp(0xe0, 0xd8, Buffer.from("XRP", "ascii"));
108
+ await waitForApp("XRP");
109
+ return transport;
110
+ }
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export declare enum LedgerDeviceState {
5
5
  NOT_CONNECTED = "NOT_CONNECTED",
6
6
  LOCKED = "LOCKED",
7
7
  APP_NOT_OPEN = "APP_NOT_OPEN",
8
+ OPENING_APP = "OPENING_APP",
8
9
  READY = "READY",
9
10
  UNKNOWN = "UNKNOWN"
10
11
  }
@@ -14,6 +15,9 @@ export interface LedgerAdapterOptions {
14
15
  derivationPath?: string;
15
16
  preferWebHID?: boolean;
16
17
  timeout?: number;
18
+ /** Request XRP on the hardware device during explicit connect. Default true. */
19
+ autoOpenApp?: boolean;
20
+ onDeviceState?: (state: LedgerDeviceState) => void;
17
21
  icon?: string;
18
22
  }
19
23
  export interface LedgerSession {
@@ -47,6 +51,7 @@ export declare class LedgerAdapter extends BaseWalletAdapter {
47
51
  private xrp?;
48
52
  private derivationPath;
49
53
  private connectionGeneration;
54
+ private connectionAbort?;
50
55
  constructor(options?: LedgerAdapterOptions);
51
56
  isAvailable(): Promise<boolean>;
52
57
  connect(options: ConnectOptions): Promise<ConnectResult>;
@@ -56,6 +61,7 @@ export declare class LedgerAdapter extends BaseWalletAdapter {
56
61
  signTransaction(request: SignTransactionRequest): Promise<SignTransactionResult>;
57
62
  getAccounts(count?: number, startIndex?: number): Promise<LedgerAccount[]>;
58
63
  private connectDefaultLedger;
64
+ private reopenSameDevice;
59
65
  private signWithDefaultLedger;
60
66
  private signMultisignTransaction;
61
67
  private signMultisignWithDefaultLedger;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAuF,MAAM,uBAAuB,CAAC;AAC/I,OAAO,KAAK,EACV,cAAc,EACd,aAAa,EACb,oBAAoB,EACpB,sBAAsB,EACtB,qBAAqB,EACrB,QAAQ,EACR,kBAAkB,EAClB,cAAc,EACd,aAAa,EAEd,MAAM,uBAAuB,CAAC;AAE/B,eAAO,MAAM,WAAW,uiBAC8gB,CAAC;AAYviB,oBAAY,iBAAiB;IAC3B,aAAa,kBAAkB;IAC/B,MAAM,WAAW;IACjB,YAAY,iBAAiB;IAC7B,KAAK,UAAU;IACf,OAAO,YAAY;CACpB;AAED,MAAM,WAAW,oBAAoB;IACnC,aAAa,CAAC,EAAE,MAAM,OAAO,CAAC,aAAa,CAAC,CAAC;IAC7C,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,cAAc,CAA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AAO,CAAC,OAAO,CAAC,CAAc;IAC9B,OAAO,CAAC,SAAS,CAAC,CAAkB;IACpC,OAAO,CAAC,GAAG,CAAC,CAAe;IAC3B,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,oBAAoB,CAAK;IACjC,OAAO,CAAC,eAAe,CAAC,CAAkB;gBAEtB,OAAO,GAAE,oBAAyB;IAahD,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IAI/B,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC;IAiDxD,cAAc,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAKtE,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ3B,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAkB/D,eAAe,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAahF,WAAW,CAAC,KAAK,SAAI,EAAE,UAAU,SAAI,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;YAuCxD,oBAAoB;YA+DpB,gBAAgB;YAMhB,qBAAqB;YAgFrB,wBAAwB;YAMxB,8BAA8B;IA+B5C,OAAO,CAAC,qBAAqB;YAWf,YAAY;YAwBZ,eAAe;YAwBf,OAAO;IAYrB,OAAO,CAAC,kBAAkB;IAW1B,OAAO,CAAC,eAAe;YAQT,WAAW;IAezB,OAAO,CAAC,sBAAsB;CAI/B;AAED,wBAAgB,mBAAmB,CAAC,OAAO,CAAC,EAAE,oBAAoB,iBAEjE"}
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { BaseWalletAdapter, createWalletError, validateMultisignTransaction, WalletKitError, WalletKitErrorCode } from "@xrpl-wallet-kit/core";
2
+ import { ensureLedgerXrpApp } from "./device-app.js";
2
3
  export const LEDGER_ICON = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyOCIgaGVpZ2h0PSIyOCIgZmlsbD0ibm9uZSI+PHBhdGggZmlsbD0iIzAwMCIgZD0iTTAgMGgyOHYyOEgweiIvPjxwYXRoIGZpbGw9IiNmZmYiIGZpbGwtcnVsZT0iZXZlbm9kZCIgZD0iTTExLjY1IDQuNEg0LjRWOWgxLjFWNS41bDYuMTUtLjA0VjQuNFptLjA1IDUuOTV2Ny4yNWg0LjZ2LTEuMWgtMy41bC0uMDQtNi4xNUgxMS43Wk00LjQgMjMuNmg3LjI1di0xLjA2TDUuNSAyMi41VjE5SDQuNHY0LjZaTTE2LjM1IDQuNGg3LjI1VjloLTEuMVY1LjVsLTYuMTUtLjA0VjQuNFptNy4yNSAxOS4yaC03LjI1di0xLjA2bDYuMTUtLjA0VjE5aDEuMXY0LjZaIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiLz48L3N2Zz4=";
3
4
  const DEFAULT_TIMEOUT = 60000;
4
5
  const DEFAULT_ACCOUNT_INDEX = 0;
@@ -8,6 +9,7 @@ export var LedgerDeviceState;
8
9
  LedgerDeviceState["NOT_CONNECTED"] = "NOT_CONNECTED";
9
10
  LedgerDeviceState["LOCKED"] = "LOCKED";
10
11
  LedgerDeviceState["APP_NOT_OPEN"] = "APP_NOT_OPEN";
12
+ LedgerDeviceState["OPENING_APP"] = "OPENING_APP";
11
13
  LedgerDeviceState["READY"] = "READY";
12
14
  LedgerDeviceState["UNKNOWN"] = "UNKNOWN";
13
15
  })(LedgerDeviceState || (LedgerDeviceState = {}));
@@ -36,13 +38,22 @@ export class LedgerAdapter extends BaseWalletAdapter {
36
38
  return getLedgerBrowserSupport().supported;
37
39
  }
38
40
  async connect(options) {
41
+ this.connectionAbort?.abort();
42
+ const controller = new AbortController();
43
+ this.connectionAbort = controller;
44
+ const abort = () => controller.abort();
45
+ options.signal?.addEventListener("abort", abort, { once: true });
46
+ if (options.signal?.aborted)
47
+ controller.abort();
39
48
  const generation = ++this.connectionGeneration;
40
49
  this.network = options.network;
41
50
  try {
51
+ if (controller.signal.aborted)
52
+ throw new WalletKitError(WalletKitErrorCode.REQUEST_CANCELLED, "Ledger connection cancelled.");
42
53
  const session = this.options.connectLedger
43
54
  ? await this.options.connectLedger()
44
- : await this.connectDefaultLedger(generation);
45
- if (generation !== this.connectionGeneration)
55
+ : await this.connectDefaultLedger(generation, { ...options, signal: controller.signal });
56
+ if (generation !== this.connectionGeneration || controller.signal.aborted)
46
57
  throw createWalletError.notConnected();
47
58
  this.session = session;
48
59
  return {
@@ -74,12 +85,18 @@ export class LedgerAdapter extends BaseWalletAdapter {
74
85
  await this.cleanup();
75
86
  throw this.mapConnectionError(error);
76
87
  }
88
+ finally {
89
+ options.signal?.removeEventListener("abort", abort);
90
+ if (this.connectionAbort === controller)
91
+ this.connectionAbort = undefined;
92
+ }
77
93
  }
78
94
  async restoreSession(_session) {
79
95
  // Hardware wallets should not be restored as connected without a fresh device prompt.
80
96
  return null;
81
97
  }
82
98
  async disconnect() {
99
+ this.connectionAbort?.abort();
83
100
  this.connectionGeneration++;
84
101
  this.session = undefined;
85
102
  this.network = undefined;
@@ -155,13 +172,30 @@ export class LedgerAdapter extends BaseWalletAdapter {
155
172
  await ownedTransport?.close().catch(() => { });
156
173
  }
157
174
  }
158
- async connectDefaultLedger(generation) {
175
+ async connectDefaultLedger(generation, options) {
159
176
  const support = getLedgerBrowserSupport();
160
177
  if (!support.supported) {
161
178
  throw createWalletError.walletNotAvailable("Ledger", new Error(support.message));
162
179
  }
163
- const transport = await this.createTransport();
180
+ let transport = await this.createTransport();
164
181
  try {
182
+ const assertCurrent = () => {
183
+ if (generation !== this.connectionGeneration || options.signal?.aborted)
184
+ throw createWalletError.notConnected();
185
+ };
186
+ assertCurrent();
187
+ if (this.options.autoOpenApp !== false) {
188
+ transport = await ensureLedgerXrpApp(transport, {
189
+ signal: options.signal, timeoutMs: options.timeoutMs ?? this.options.timeout ?? DEFAULT_TIMEOUT, assertCurrent,
190
+ onOpening: () => {
191
+ this.options.onDeviceState?.(LedgerDeviceState.OPENING_APP);
192
+ options.onRequestProgress?.({ state: "opened" });
193
+ },
194
+ onTransport: next => { transport = next; },
195
+ reopen: previous => this.reopenSameDevice(previous)
196
+ });
197
+ }
198
+ assertCurrent();
165
199
  const Xrp = await loadLedgerXrpApp();
166
200
  const xrp = new Xrp(transport);
167
201
  const account = await this.withTimeout(xrp.getAddress(this.derivationPath, false, false), "Ledger connection timeout. Please check your Ledger device.");
@@ -184,6 +218,7 @@ export class LedgerAdapter extends BaseWalletAdapter {
184
218
  throw createWalletError.notConnected();
185
219
  this.transport = transport;
186
220
  this.xrp = xrp;
221
+ this.options.onDeviceState?.(LedgerDeviceState.READY);
187
222
  return {
188
223
  address: account.address,
189
224
  publicKey: account.publicKey,
@@ -194,10 +229,16 @@ export class LedgerAdapter extends BaseWalletAdapter {
194
229
  };
195
230
  }
196
231
  catch (error) {
197
- await transport.close().catch(() => { });
232
+ void transport.close().catch(() => { });
198
233
  throw error;
199
234
  }
200
235
  }
236
+ async reopenSameDevice(previous) {
237
+ const native = previous;
238
+ if (!native.device || !native.constructor.open)
239
+ throw new Error("Open XRP on your Ledger device and select it again in the browser.");
240
+ return native.constructor.open(native.device);
241
+ }
201
242
  async signWithDefaultLedger(tx, submit = true) {
202
243
  if (!this.xrp || !this.session)
203
244
  throw createWalletError.notConnected();
@@ -467,7 +508,8 @@ function parseLedgerError(error) {
467
508
  return { state: LedgerDeviceState.LOCKED, message: "Ledger is locked. Please unlock your Ledger device.", rejected: false, timeout: false };
468
509
  }
469
510
  if ([0x6e00, 0x6511, 0x650f].includes(statusCode ?? 0) || /app.*open|xrp.*app|cla_not_supported/.test(lower)) {
470
- return { state: LedgerDeviceState.APP_NOT_OPEN, message: "Please open the XRP app on your Ledger device.", rejected: false, timeout: false };
511
+ return { state: LedgerDeviceState.APP_NOT_OPEN, message: /connect again|select.*again/.test(lower)
512
+ ? message : "Please open the XRP app on your Ledger device.", rejected: false, timeout: false };
471
513
  }
472
514
  if (/no device|device.*not found|disconnected|cannot open device|transport/i.test(message)) {
473
515
  return { state: LedgerDeviceState.NOT_CONNECTED, message: "Ledger device not found. Please connect and unlock your Ledger device.", rejected: false, timeout: false };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xrpl-wallet-kit/adapter-ledger",
3
- "version": "0.1.22",
3
+ "version": "0.1.23",
4
4
  "description": "Ledger hardware wallet adapter for XRPL Wallet Kit.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -21,10 +21,11 @@
21
21
  "build": "tsc -p tsconfig.json"
22
22
  },
23
23
  "dependencies": {
24
+ "buffer": "^6.0.3",
24
25
  "@ledgerhq/hw-app-xrp": "^6.28.0",
25
26
  "@ledgerhq/hw-transport-webhid": "^6.28.0",
26
27
  "@ledgerhq/hw-transport-webusb": "^6.28.0",
27
- "@xrpl-wallet-kit/core": "0.1.22"
28
+ "@xrpl-wallet-kit/core": "0.1.23"
28
29
  },
29
30
  "peerDependencies": {
30
31
  "xrpl": "^4.0.0 || ^5.0.0"