@xrpl-wallet-kit/adapter-ledger 0.1.19 → 0.1.21

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
@@ -35,6 +35,10 @@ const kit = createWalletKit({
35
35
  - Hardware wallets are explicit user-presence devices. `restoreSession()` returns `null` and does not silently reconnect after refresh.
36
36
  - Default derivation path is `44'/144'/0'/0/0`.
37
37
  - `signTransaction()` signs without submit.
38
+ - Default signing verifies the device signature locally before returning or submitting it. RPC errors keep their original message and cause rather than being translated into device errors.
39
+ - Disconnecting or changing the Ledger session while signing prevents subsequent submission.
40
+ - Default single-sign signing autofills network fields and adds `tfFullyCanonicalSig`, preserving transaction-specific flags, for compatibility with XRP app versions such as 2.3.2. It does not change destination, amount or tags.
41
+ - Device signing errors include the prepared transaction and Ledger status code in `details`; `submitAttempted: false` means the adapter did not reach submission. App version is included when available.
38
42
  - `signAndSubmit()` signs and submits when requested by the caller.
39
43
  - `signTransaction()` supports XRPL multisigning when `SigningPubKey` is empty. It returns a signed blob plus the signer contribution under `raw.signer`/`raw.signedTx` for the dApp to combine with other signers.
40
44
  - `signAndSubmit()` rejects multisign payloads with `UNSUPPORTED_METHOD`; the dApp must combine a quorum of signer contributions before submission.
package/dist/index.d.ts CHANGED
@@ -20,6 +20,7 @@ export interface LedgerSession {
20
20
  address: string;
21
21
  publicKey?: string;
22
22
  derivationPath?: string;
23
+ appVersion?: string;
23
24
  signTransaction(tx: unknown, submit?: boolean): Promise<LedgerSignResult>;
24
25
  signMultisignTransaction?(tx: unknown): Promise<LedgerSignResult>;
25
26
  }
@@ -45,6 +46,7 @@ export declare class LedgerAdapter extends BaseWalletAdapter {
45
46
  private transport?;
46
47
  private xrp?;
47
48
  private derivationPath;
49
+ private connectionGeneration;
48
50
  constructor(options?: LedgerAdapterOptions);
49
51
  isAvailable(): Promise<boolean>;
50
52
  connect(options: ConnectOptions): Promise<ConnectResult>;
@@ -57,6 +59,8 @@ export declare class LedgerAdapter extends BaseWalletAdapter {
57
59
  private signWithDefaultLedger;
58
60
  private signMultisignTransaction;
59
61
  private signMultisignWithDefaultLedger;
62
+ private verifyDeviceSignature;
63
+ private signOnDevice;
60
64
  private createTransport;
61
65
  private cleanup;
62
66
  private mapConnectionError;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAmD,MAAM,uBAAuB,CAAC;AAC3G,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;AAUviB,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,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,eAAe,CAAC,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC1E,wBAAwB,CAAC,CAAC,EAAE,EAAE,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;C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AAwB;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,7 +1,8 @@
1
- import { BaseWalletAdapter, createWalletError, validateMultisignTransaction } from "@xrpl-wallet-kit/core";
1
+ import { BaseWalletAdapter, createWalletError, validateMultisignTransaction, WalletKitError, WalletKitErrorCode } from "@xrpl-wallet-kit/core";
2
2
  export const LEDGER_ICON = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyOCIgaGVpZ2h0PSIyOCIgZmlsbD0ibm9uZSI+PHBhdGggZmlsbD0iIzAwMCIgZD0iTTAgMGgyOHYyOEgweiIvPjxwYXRoIGZpbGw9IiNmZmYiIGZpbGwtcnVsZT0iZXZlbm9kZCIgZD0iTTExLjY1IDQuNEg0LjRWOWgxLjFWNS41bDYuMTUtLjA0VjQuNFptLjA1IDUuOTV2Ny4yNWg0LjZ2LTEuMWgtMy41bC0uMDQtNi4xNUgxMS43Wk00LjQgMjMuNmg3LjI1di0xLjA2TDUuNSAyMi41VjE5SDQuNHY0LjZaTTE2LjM1IDQuNGg3LjI1VjloLTEuMVY1LjVsLTYuMTUtLjA0VjQuNFptNy4yNSAxOS4yaC03LjI1di0xLjA2bDYuMTUtLjA0VjE5aDEuMXY0LjZaIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiLz48L3N2Zz4=";
3
3
  const DEFAULT_TIMEOUT = 60000;
4
4
  const DEFAULT_ACCOUNT_INDEX = 0;
5
+ const FULLY_CANONICAL_SIG = 0x80000000;
5
6
  export var LedgerDeviceState;
6
7
  (function (LedgerDeviceState) {
7
8
  LedgerDeviceState["NOT_CONNECTED"] = "NOT_CONNECTED";
@@ -20,6 +21,7 @@ export class LedgerAdapter extends BaseWalletAdapter {
20
21
  signTransaction: true,
21
22
  signAndSubmit: true
22
23
  };
24
+ this.connectionGeneration = 0;
23
25
  this.derivationPath = options.derivationPath ?? getDerivationPath(options.accountIndex ?? DEFAULT_ACCOUNT_INDEX);
24
26
  this.metadata = {
25
27
  id: "ledger",
@@ -34,11 +36,15 @@ export class LedgerAdapter extends BaseWalletAdapter {
34
36
  return getLedgerBrowserSupport().supported;
35
37
  }
36
38
  async connect(options) {
39
+ const generation = ++this.connectionGeneration;
37
40
  this.network = options.network;
38
41
  try {
39
- this.session = this.options.connectLedger
42
+ const session = this.options.connectLedger
40
43
  ? await this.options.connectLedger()
41
- : await this.connectDefaultLedger();
44
+ : await this.connectDefaultLedger(generation);
45
+ if (generation !== this.connectionGeneration)
46
+ throw createWalletError.notConnected();
47
+ this.session = session;
42
48
  return {
43
49
  account: {
44
50
  address: this.session.address,
@@ -57,13 +63,15 @@ export class LedgerAdapter extends BaseWalletAdapter {
57
63
  },
58
64
  connectedAt: Date.now(),
59
65
  metadata: {
60
- derivationPath: this.session.derivationPath ?? this.derivationPath
66
+ derivationPath: this.session.derivationPath ?? this.derivationPath,
67
+ appVersion: this.session.appVersion
61
68
  }
62
69
  }
63
70
  };
64
71
  }
65
72
  catch (error) {
66
- await this.cleanup();
73
+ if (generation === this.connectionGeneration)
74
+ await this.cleanup();
67
75
  throw this.mapConnectionError(error);
68
76
  }
69
77
  }
@@ -72,9 +80,10 @@ export class LedgerAdapter extends BaseWalletAdapter {
72
80
  return null;
73
81
  }
74
82
  async disconnect() {
75
- await this.cleanup();
83
+ this.connectionGeneration++;
76
84
  this.session = undefined;
77
85
  this.network = undefined;
86
+ await this.cleanup();
78
87
  }
79
88
  async signAndSubmit(request) {
80
89
  if (!this.session)
@@ -82,52 +91,54 @@ export class LedgerAdapter extends BaseWalletAdapter {
82
91
  if (this.isMultisignTransaction(request.txJson)) {
83
92
  throw createWalletError.unsupportedMethod("multisign submission", this.metadata.name);
84
93
  }
85
- try {
86
- const result = await this.session.signTransaction(request.txJson, request.submit ?? true);
87
- return {
88
- hash: result.hash,
89
- status: result.status,
90
- signed: result.signed ?? Boolean(result.hash || result.txBlob),
91
- raw: {
92
- txBlob: result.txBlob,
93
- ...asRecord(result.raw ?? result)
94
- }
95
- };
96
- }
97
- catch (error) {
98
- throw this.mapSigningError(error);
99
- }
94
+ // RPC and validation errors must not be translated into device errors.
95
+ const result = await this.session.signTransaction(request.txJson, request.submit ?? true);
96
+ return {
97
+ hash: result.hash,
98
+ status: result.status,
99
+ signed: result.signed ?? Boolean(result.hash || result.txBlob),
100
+ raw: {
101
+ txBlob: result.txBlob,
102
+ ...asRecord(result.raw ?? result)
103
+ }
104
+ };
100
105
  }
101
106
  async signTransaction(request) {
102
107
  if (!this.session)
103
108
  throw createWalletError.notConnected();
104
- try {
105
- const multisign = this.isMultisignTransaction(request.txJson);
106
- const result = multisign
107
- ? await this.signMultisignTransaction(request.txJson)
108
- : await this.session.signTransaction(request.txJson, false);
109
- return {
110
- txBlob: result.txBlob,
111
- signed: result.signed ?? Boolean(result.txBlob),
112
- raw: result.raw ?? result
113
- };
114
- }
115
- catch (error) {
116
- throw this.mapSigningError(error);
117
- }
109
+ const multisign = this.isMultisignTransaction(request.txJson);
110
+ const result = multisign
111
+ ? await this.signMultisignTransaction(request.txJson)
112
+ : await this.session.signTransaction(request.txJson, false);
113
+ return {
114
+ txBlob: result.txBlob,
115
+ signed: result.signed ?? Boolean(result.txBlob),
116
+ raw: result.raw ?? result
117
+ };
118
118
  }
119
119
  async getAccounts(count = 5, startIndex = 0) {
120
- const cleanupAfter = !this.transport;
120
+ if (!Number.isSafeInteger(count) || count < 0 || !Number.isSafeInteger(startIndex) || startIndex < 0 || startIndex + count > 0x80000000) {
121
+ throw new WalletKitError(WalletKitErrorCode.INVALID_REQUEST, "Ledger account range must contain valid non-negative BIP44 indexes");
122
+ }
123
+ if (count === 0)
124
+ return [];
125
+ const generation = this.connectionGeneration;
126
+ let ownedTransport;
127
+ let xrp = this.xrp;
121
128
  try {
122
- if (!this.transport || !this.xrp) {
129
+ if (!xrp) {
123
130
  const Xrp = await loadLedgerXrpApp();
124
- this.transport = await this.createTransport();
125
- this.xrp = new Xrp(this.transport);
131
+ ownedTransport = await this.createTransport();
132
+ xrp = new Xrp(ownedTransport);
126
133
  }
127
134
  const accounts = [];
128
135
  for (let index = startIndex; index < startIndex + count; index += 1) {
136
+ if (generation !== this.connectionGeneration)
137
+ throw createWalletError.notConnected();
129
138
  const path = getDerivationPath(index);
130
- const account = await this.withTimeout(this.xrp.getAddress(path, false, false), "Ledger account lookup timeout. Please check your Ledger device.");
139
+ const account = await this.withTimeout(xrp.getAddress(path, false, false), "Ledger account lookup timeout. Please check your Ledger device.");
140
+ if (generation !== this.connectionGeneration)
141
+ throw createWalletError.notConnected();
131
142
  accounts.push({
132
143
  address: account.address,
133
144
  publicKey: account.publicKey,
@@ -141,28 +152,51 @@ export class LedgerAdapter extends BaseWalletAdapter {
141
152
  throw this.mapConnectionError(error);
142
153
  }
143
154
  finally {
144
- if (cleanupAfter)
145
- await this.cleanup();
155
+ await ownedTransport?.close().catch(() => { });
146
156
  }
147
157
  }
148
- async connectDefaultLedger() {
158
+ async connectDefaultLedger(generation) {
149
159
  const support = getLedgerBrowserSupport();
150
160
  if (!support.supported) {
151
161
  throw createWalletError.walletNotAvailable("Ledger", new Error(support.message));
152
162
  }
153
- this.transport = await this.createTransport();
154
- const Xrp = await loadLedgerXrpApp();
155
- this.xrp = new Xrp(this.transport);
156
- const account = await this.withTimeout(this.xrp.getAddress(this.derivationPath, false, false), "Ledger connection timeout. Please check your Ledger device.");
157
- if (!account.address)
158
- throw new Error("Ledger did not return an XRPL address");
159
- return {
160
- address: account.address,
161
- publicKey: account.publicKey,
162
- derivationPath: this.derivationPath,
163
- signTransaction: (tx, submit) => this.signWithDefaultLedger(tx, submit),
164
- signMultisignTransaction: (tx) => this.signMultisignWithDefaultLedger(tx)
165
- };
163
+ const transport = await this.createTransport();
164
+ try {
165
+ const Xrp = await loadLedgerXrpApp();
166
+ const xrp = new Xrp(transport);
167
+ const account = await this.withTimeout(xrp.getAddress(this.derivationPath, false, false), "Ledger connection timeout. Please check your Ledger device.");
168
+ if (!account.address)
169
+ throw new Error("Ledger did not return an XRPL address");
170
+ // App version is diagnostic only; older apps may not implement this APDU.
171
+ let appVersion;
172
+ try {
173
+ if (xrp.getAppConfiguration) {
174
+ appVersion = (await this.withTimeout(xrp.getAppConfiguration(), "Ledger app version lookup timeout")).version;
175
+ }
176
+ }
177
+ catch {
178
+ appVersion = undefined;
179
+ }
180
+ if (generation !== this.connectionGeneration)
181
+ throw createWalletError.notConnected();
182
+ await this.cleanup();
183
+ if (generation !== this.connectionGeneration)
184
+ throw createWalletError.notConnected();
185
+ this.transport = transport;
186
+ this.xrp = xrp;
187
+ return {
188
+ address: account.address,
189
+ publicKey: account.publicKey,
190
+ derivationPath: this.derivationPath,
191
+ appVersion,
192
+ signTransaction: (tx, submit) => this.signWithDefaultLedger(tx, submit),
193
+ signMultisignTransaction: (tx) => this.signMultisignWithDefaultLedger(tx)
194
+ };
195
+ }
196
+ catch (error) {
197
+ await transport.close().catch(() => { });
198
+ throw error;
199
+ }
166
200
  }
167
201
  async signWithDefaultLedger(tx, submit = true) {
168
202
  if (!this.xrp || !this.session)
@@ -172,24 +206,41 @@ export class LedgerAdapter extends BaseWalletAdapter {
172
206
  if (this.isMultisignTransaction(tx)) {
173
207
  throw createWalletError.unsupportedMethod("XRPL multisigning through the single-sign path", this.metadata.name);
174
208
  }
175
- const { Client, encode } = await import("xrpl");
176
- const client = new Client(this.network.rpcUrl);
177
- await client.connect();
209
+ const session = this.session;
210
+ const device = this.xrp;
211
+ const network = this.network;
212
+ const generation = this.connectionGeneration;
213
+ const assertCurrent = () => {
214
+ if (generation !== this.connectionGeneration || this.session !== session || this.xrp !== device) {
215
+ throw createWalletError.notConnected();
216
+ }
217
+ };
218
+ const { Client, encode, encodeForSigning, verifyKeypairSignature } = await import("xrpl");
219
+ const client = new Client(network.rpcUrl);
178
220
  try {
221
+ assertCurrent();
222
+ await client.connect();
179
223
  const txJson = asRecord(tx);
224
+ if (txJson.TxnSignature !== undefined || txJson.Signers !== undefined) {
225
+ throw new WalletKitError(WalletKitErrorCode.INVALID_REQUEST, "Ledger signing requires an unsigned transaction");
226
+ }
180
227
  const prepared = await client.autofill({
181
228
  ...txJson,
182
- Account: typeof txJson.Account === "string" ? txJson.Account : this.session.address
229
+ Account: typeof txJson.Account === "string" ? txJson.Account : session.address
183
230
  });
184
231
  const txForSigning = { ...prepared };
185
232
  delete txForSigning.TxnSignature;
186
233
  delete txForSigning.Signers;
187
- const publicKey = this.session.publicKey ?? (await this.xrp.getAddress(this.derivationPath, false, false)).publicKey;
234
+ // XRP app 2.3.2 rejects an explicit Flags field without this signature bit.
235
+ txForSigning.Flags = (Number(prepared.Flags ?? 0) | FULLY_CANONICAL_SIG) >>> 0;
236
+ assertCurrent();
237
+ const publicKey = session.publicKey ?? (await device.getAddress(this.derivationPath, false, false)).publicKey;
188
238
  txForSigning.SigningPubKey = publicKey.toUpperCase();
189
239
  const signingBlob = encode(txForSigning).toUpperCase();
190
- const signature = await this.withTimeout(this.xrp.signTransaction(this.derivationPath, signingBlob), "Ledger signing timeout. Please confirm the transaction on your Ledger device.");
191
- if (!signature)
192
- throw new Error("Ledger did not return a transaction signature");
240
+ assertCurrent();
241
+ const signature = await this.signOnDevice(txForSigning, signingBlob, "Ledger signing timeout. Please confirm the transaction on your Ledger device.");
242
+ assertCurrent();
243
+ this.verifyDeviceSignature(encodeForSigning(txForSigning), signature, publicKey, verifyKeypairSignature);
193
244
  const signedTx = {
194
245
  ...txForSigning,
195
246
  TxnSignature: signature.toUpperCase()
@@ -216,7 +267,7 @@ export class LedgerAdapter extends BaseWalletAdapter {
216
267
  };
217
268
  }
218
269
  finally {
219
- await client.disconnect();
270
+ await client.disconnect().catch(() => { });
220
271
  }
221
272
  }
222
273
  async signMultisignTransaction(tx) {
@@ -231,18 +282,66 @@ export class LedgerAdapter extends BaseWalletAdapter {
231
282
  throw createWalletError.notConnected();
232
283
  if (!this.network)
233
284
  throw createWalletError.connectionFailed(this.metadata.name, new Error("XRPL network is required for Ledger signing"));
234
- const { encode, encodeForMultiSigning } = await import("xrpl");
285
+ const session = this.session;
286
+ const device = this.xrp;
287
+ const generation = this.connectionGeneration;
288
+ const assertCurrent = () => {
289
+ if (generation !== this.connectionGeneration || this.session !== session || this.xrp !== device) {
290
+ throw createWalletError.notConnected();
291
+ }
292
+ };
293
+ const { encode, encodeForMultiSigning, verifyKeypairSignature } = await import("xrpl");
235
294
  const prepared = await validateMultisignTransaction(asRecord(tx));
236
- const publicKey = (this.session.publicKey ?? (await this.xrp.getAddress(this.derivationPath, false, false)).publicKey).toUpperCase();
237
- const signingBlob = encodeForMultiSigning(prepared, this.session.address).toUpperCase();
238
- const signature = await this.withTimeout(this.xrp.signTransaction(this.derivationPath, signingBlob), "Ledger multisigning timeout. Please confirm the transaction on your Ledger device.");
239
- if (!signature)
240
- throw new Error("Ledger did not return a transaction signature");
241
- const signer = { Signer: { Account: this.session.address, SigningPubKey: publicKey, TxnSignature: signature.toUpperCase() } };
295
+ assertCurrent();
296
+ const publicKey = (session.publicKey ?? (await device.getAddress(this.derivationPath, false, false)).publicKey).toUpperCase();
297
+ // The XRP app adds the multisign prefix and signer AccountID itself.
298
+ const signingBlob = encode(prepared).toUpperCase();
299
+ assertCurrent();
300
+ const signature = await this.signOnDevice(prepared, signingBlob, "Ledger multisigning timeout. Please confirm the transaction on your Ledger device.");
301
+ assertCurrent();
302
+ this.verifyDeviceSignature(encodeForMultiSigning(prepared, session.address), signature, publicKey, verifyKeypairSignature);
303
+ const signer = { Signer: { Account: session.address, SigningPubKey: publicKey, TxnSignature: signature.toUpperCase() } };
242
304
  const signedTx = { ...prepared, Signers: [signer] };
243
305
  const txBlob = encode(signedTx);
244
306
  return { txBlob, signed: true, raw: { txBlob, signedTx, signer } };
245
307
  }
308
+ verifyDeviceSignature(preimage, signature, publicKey, verify) {
309
+ let valid = false;
310
+ try {
311
+ valid = Boolean(signature) && verify(preimage, signature, publicKey);
312
+ }
313
+ catch {
314
+ valid = false;
315
+ }
316
+ if (!valid)
317
+ throw createWalletError.signFailed(new Error("Ledger returned an invalid transaction signature"));
318
+ }
319
+ async signOnDevice(tx, signingBlob, timeoutMessage) {
320
+ if (!this.xrp)
321
+ throw createWalletError.notConnected();
322
+ try {
323
+ return await this.withTimeout(this.xrp.signTransaction(this.derivationPath, signingBlob), timeoutMessage);
324
+ }
325
+ catch (error) {
326
+ const mapped = this.mapSigningError(error);
327
+ if (!(mapped instanceof WalletKitError))
328
+ throw mapped;
329
+ throw new WalletKitError(mapped.code, mapped.message, {
330
+ cause: error,
331
+ category: mapped.category,
332
+ details: {
333
+ ...mapped.details,
334
+ stage: "device-signing",
335
+ ledgerStatusCode: getLedgerStatusCode(error),
336
+ appVersion: this.session?.appVersion,
337
+ derivationPath: this.derivationPath,
338
+ network: this.network?.id,
339
+ preparedTransaction: { ...tx },
340
+ submitAttempted: false
341
+ }
342
+ });
343
+ }
344
+ }
246
345
  async createTransport() {
247
346
  const support = getLedgerBrowserSupport();
248
347
  if (!support.supported)
@@ -370,7 +469,7 @@ function parseLedgerError(error) {
370
469
  if ([0x6e00, 0x6511, 0x650f].includes(statusCode ?? 0) || /app.*open|xrp.*app|cla_not_supported/.test(lower)) {
371
470
  return { state: LedgerDeviceState.APP_NOT_OPEN, message: "Please open the XRP app on your Ledger device.", rejected: false, timeout: false };
372
471
  }
373
- if (/no device|not found|disconnected|cannot open device|transport/i.test(message)) {
472
+ if (/no device|device.*not found|disconnected|cannot open device|transport/i.test(message)) {
374
473
  return { state: LedgerDeviceState.NOT_CONNECTED, message: "Ledger device not found. Please connect and unlock your Ledger device.", rejected: false, timeout: false };
375
474
  }
376
475
  return { state: LedgerDeviceState.UNKNOWN, message: message || "Unknown Ledger error", 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.19",
3
+ "version": "0.1.21",
4
4
  "description": "Ledger hardware wallet adapter for XRPL Wallet Kit.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -24,7 +24,7 @@
24
24
  "@ledgerhq/hw-app-xrp": "^6.28.0",
25
25
  "@ledgerhq/hw-transport-webhid": "^6.28.0",
26
26
  "@ledgerhq/hw-transport-webusb": "^6.28.0",
27
- "@xrpl-wallet-kit/core": "0.1.19"
27
+ "@xrpl-wallet-kit/core": "0.1.21"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "xrpl": "^4.0.0 || ^5.0.0"