@xrpl-wallet-kit/adapter-ledger 0.1.22 → 0.2.0
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 +14 -1
- package/dist/device-app.d.ts +19 -0
- package/dist/device-app.d.ts.map +1 -0
- package/dist/device-app.js +112 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +113 -21
- package/package.json +3 -2
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
|
|
34
|
+
- Requires a connected, unlocked Ledger with the selected app installed. During explicit connect, `autoOpenApp` (default `true`) requests XRP for XRPL or XAH (Xahau) for Xahau directly on the hardware; confirm on the device when prompted. It never launches Ledger Live desktop or installs an app. XRP auto-opening is available since 0.1.23; XAH auto-opening is unreleased.
|
|
35
|
+
- An already-open matching app is left alone. Another device app is closed before requesting the selected app. App switches can disconnect USB temporarily: reconnection attempts use only the originally selected device handle, never another authorized Ledger. If recovery or OS app launch is unsupported, open the selected app manually and select the device again. No automatic XAH-to-XRP fallback occurs.
|
|
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,12 +53,22 @@ 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
|
```
|
|
58
61
|
|
|
59
62
|
Use `connectLedger` in tests or custom integrations to inject a prebuilt Ledger session.
|
|
60
63
|
|
|
64
|
+
For controlled Xahau compatibility testing with the XRP app, explicitly use
|
|
65
|
+
`deviceApp: "XRP"`; default Xahau connections request `"XAH"`. Use
|
|
66
|
+
`autoOpenApp: false` to manage the app manually. Connection metadata records
|
|
67
|
+
`requestedDeviceApp`, not a claim of independently verified device app identity.
|
|
68
|
+
Default client Ledger remains XRPL-only until explicitly opted into Xahau.
|
|
69
|
+
Launching XAH successfully does not establish support for every transaction,
|
|
70
|
+
Hook field, device or app version, and never enables blind signing.
|
|
71
|
+
|
|
61
72
|
## Testing
|
|
62
73
|
|
|
63
74
|
On 2026-10-13, the maintainer reported that documented Ledger transaction
|
|
@@ -70,6 +81,8 @@ Hardware transport should be mocked in unit tests. Manual smoke testing should c
|
|
|
70
81
|
- device disconnected;
|
|
71
82
|
- device locked;
|
|
72
83
|
- XRP app not open;
|
|
84
|
+
- XRP launch confirmation/rejection on the device, and return from another app;
|
|
85
|
+
- USB re-enumeration requiring manual device reselection;
|
|
73
86
|
- connect success;
|
|
74
87
|
- disconnect cleanup;
|
|
75
88
|
- `submit:false` signed transaction result;
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
app?: "XRP" | "XAH";
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
timeoutMs: number;
|
|
10
|
+
assertCurrent(): void;
|
|
11
|
+
onOpening(): void;
|
|
12
|
+
onTransport(transport: LedgerAppTransport): void;
|
|
13
|
+
reopen(transport: LedgerAppTransport): Promise<LedgerAppTransport>;
|
|
14
|
+
}
|
|
15
|
+
/** Ledger OS APDUs, not Ledger Live: B0/01 query, B0/A7 close, E0/D8 open. */
|
|
16
|
+
export declare function ensureLedgerApp(initial: LedgerAppTransport, options: OpenAppOptions): Promise<LedgerAppTransport>;
|
|
17
|
+
export declare const ensureLedgerXrpApp: (initial: LedgerAppTransport, options: OpenAppOptions) => Promise<LedgerAppTransport>;
|
|
18
|
+
export {};
|
|
19
|
+
//# 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,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC;IACpB,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,eAAe,CAAC,OAAO,EAAE,kBAAkB,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAuEvH;AAED,eAAO,MAAM,kBAAkB,YAAa,kBAAkB,WAAW,cAAc,gCACjC,CAAC"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Buffer } from "buffer";
|
|
2
|
+
import { waitForWalletRequest, WalletKitError, WalletKitErrorCode } from "@xrpl-wallet-kit/core";
|
|
3
|
+
const manual = (app) => new Error(`Open ${app} on Ledger. Reconnect.`);
|
|
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, target) {
|
|
9
|
+
const length = response[1];
|
|
10
|
+
if (response[0] !== 1 || !length || length + 4 > response.length)
|
|
11
|
+
throw manual(target);
|
|
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 ensureLedgerApp(initial, options) {
|
|
16
|
+
const target = options.app ?? "XRP";
|
|
17
|
+
if (!initial.send)
|
|
18
|
+
return initial;
|
|
19
|
+
let transport = initial;
|
|
20
|
+
const deadline = Date.now() + options.timeoutMs;
|
|
21
|
+
const active = () => {
|
|
22
|
+
options.assertCurrent();
|
|
23
|
+
if (options.signal?.aborted)
|
|
24
|
+
throw new WalletKitError(WalletKitErrorCode.REQUEST_CANCELLED, "Ledger connection cancelled.");
|
|
25
|
+
if (Date.now() >= deadline)
|
|
26
|
+
throw new WalletKitError(WalletKitErrorCode.REQUEST_TIMEOUT, "Ledger timed out. Check device.");
|
|
27
|
+
};
|
|
28
|
+
const bounded = async (operation) => {
|
|
29
|
+
active();
|
|
30
|
+
const value = await waitForWalletRequest(operation, { signal: options.signal, timeoutMs: Math.max(1, deadline - Date.now()) });
|
|
31
|
+
active();
|
|
32
|
+
return value;
|
|
33
|
+
};
|
|
34
|
+
const send = (cla, ins, data) => bounded(() => transport.send(cla, ins, 0, 0, data));
|
|
35
|
+
const reopen = async () => {
|
|
36
|
+
// Closing can wait for a cancelled exchange; never let cleanup extend our deadline.
|
|
37
|
+
void transport.close().catch(() => { });
|
|
38
|
+
const previous = transport;
|
|
39
|
+
await bounded(async () => {
|
|
40
|
+
const next = await options.reopen(previous);
|
|
41
|
+
try {
|
|
42
|
+
active();
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
void next.close().catch(() => { });
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
transport = next;
|
|
49
|
+
options.onTransport(next);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const waitForApp = async (expected) => {
|
|
53
|
+
const readyDeadline = Math.min(deadline, Date.now() + 5000);
|
|
54
|
+
while (true) {
|
|
55
|
+
active();
|
|
56
|
+
try {
|
|
57
|
+
const name = appName(await send(0xb0, 0x01), target);
|
|
58
|
+
if (name === expected || expected === "BOLOS" && isDashboard(name))
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (!disconnected(error) && !unsupported(error))
|
|
63
|
+
throw error;
|
|
64
|
+
if (disconnected(error)) {
|
|
65
|
+
try {
|
|
66
|
+
await reopen();
|
|
67
|
+
}
|
|
68
|
+
catch (reopenError) {
|
|
69
|
+
active();
|
|
70
|
+
if (!disconnected(reopenError))
|
|
71
|
+
throw reopenError;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (Date.now() >= readyDeadline)
|
|
76
|
+
throw manual(target);
|
|
77
|
+
await bounded(() => new Promise(resolve => setTimeout(resolve, 250)));
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
let name;
|
|
81
|
+
try {
|
|
82
|
+
name = appName(await send(0xb0, 0x01), target);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
// Older apps may lack the OS query. Let the selected app's address API report readiness.
|
|
86
|
+
if (unsupported(error))
|
|
87
|
+
return initial;
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
if (name === target)
|
|
91
|
+
return transport;
|
|
92
|
+
options.onOpening();
|
|
93
|
+
const switchApp = async (cla, ins, data) => {
|
|
94
|
+
try {
|
|
95
|
+
await send(cla, ins, data);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (unsupported(error))
|
|
99
|
+
throw manual(target);
|
|
100
|
+
if (!disconnected(error))
|
|
101
|
+
throw error; // Never retry a rejection or locked-device error.
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
if (!isDashboard(name)) {
|
|
105
|
+
await switchApp(0xb0, 0xa7);
|
|
106
|
+
await waitForApp("BOLOS");
|
|
107
|
+
}
|
|
108
|
+
await switchApp(0xe0, 0xd8, Buffer.from(target, "ascii"));
|
|
109
|
+
await waitForApp(target);
|
|
110
|
+
return transport;
|
|
111
|
+
}
|
|
112
|
+
export const ensureLedgerXrpApp = (initial, options) => ensureLedgerApp(initial, { ...options, app: "XRP" });
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
import { BaseWalletAdapter } from "@xrpl-wallet-kit/core";
|
|
2
|
+
import type { WalletChainFilter } from "@xrpl-wallet-kit/core";
|
|
2
3
|
import type { ConnectOptions, ConnectResult, SignAndSubmitRequest, SignTransactionRequest, SignTransactionResult, TxResult, WalletCapabilities, WalletMetadata, WalletSession } from "@xrpl-wallet-kit/core";
|
|
3
4
|
export declare const LEDGER_ICON = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyOCIgaGVpZ2h0PSIyOCIgZmlsbD0ibm9uZSI+PHBhdGggZmlsbD0iIzAwMCIgZD0iTTAgMGgyOHYyOEgweiIvPjxwYXRoIGZpbGw9IiNmZmYiIGZpbGwtcnVsZT0iZXZlbm9kZCIgZD0iTTExLjY1IDQuNEg0LjRWOWgxLjFWNS41bDYuMTUtLjA0VjQuNFptLjA1IDUuOTV2Ny4yNWg0LjZ2LTEuMWgtMy41bC0uMDQtNi4xNUgxMS43Wk00LjQgMjMuNmg3LjI1di0xLjA2TDUuNSAyMi41VjE5SDQuNHY0LjZaTTE2LjM1IDQuNGg3LjI1VjloLTEuMVY1LjVsLTYuMTUtLjA0VjQuNFptNy4yNSAxOS4yaC03LjI1di0xLjA2bDYuMTUtLjA0VjE5aDEuMXY0LjZaIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiLz48L3N2Zz4=";
|
|
4
5
|
export declare enum LedgerDeviceState {
|
|
5
6
|
NOT_CONNECTED = "NOT_CONNECTED",
|
|
6
7
|
LOCKED = "LOCKED",
|
|
7
8
|
APP_NOT_OPEN = "APP_NOT_OPEN",
|
|
9
|
+
OPENING_APP = "OPENING_APP",
|
|
8
10
|
READY = "READY",
|
|
9
11
|
UNKNOWN = "UNKNOWN"
|
|
10
12
|
}
|
|
11
13
|
export interface LedgerAdapterOptions {
|
|
14
|
+
supportedChains?: WalletChainFilter;
|
|
12
15
|
connectLedger?: () => Promise<LedgerSession>;
|
|
13
16
|
accountIndex?: number;
|
|
14
17
|
derivationPath?: string;
|
|
15
18
|
preferWebHID?: boolean;
|
|
16
19
|
timeout?: number;
|
|
20
|
+
/** Request the chain's app on the hardware device during explicit connect. Default true. */
|
|
21
|
+
autoOpenApp?: boolean;
|
|
22
|
+
/** Defaults to XAH on Xahau, XRP on XRPL. Override only for controlled device compatibility testing. */
|
|
23
|
+
deviceApp?: "XRP" | "XAH";
|
|
24
|
+
onDeviceState?: (state: LedgerDeviceState) => void;
|
|
17
25
|
icon?: string;
|
|
18
26
|
}
|
|
19
27
|
export interface LedgerSession {
|
|
@@ -47,6 +55,8 @@ export declare class LedgerAdapter extends BaseWalletAdapter {
|
|
|
47
55
|
private xrp?;
|
|
48
56
|
private derivationPath;
|
|
49
57
|
private connectionGeneration;
|
|
58
|
+
private connectionAbort?;
|
|
59
|
+
private protocolContext?;
|
|
50
60
|
constructor(options?: LedgerAdapterOptions);
|
|
51
61
|
isAvailable(): Promise<boolean>;
|
|
52
62
|
connect(options: ConnectOptions): Promise<ConnectResult>;
|
|
@@ -56,6 +66,7 @@ export declare class LedgerAdapter extends BaseWalletAdapter {
|
|
|
56
66
|
signTransaction(request: SignTransactionRequest): Promise<SignTransactionResult>;
|
|
57
67
|
getAccounts(count?: number, startIndex?: number): Promise<LedgerAccount[]>;
|
|
58
68
|
private connectDefaultLedger;
|
|
69
|
+
private reopenSameDevice;
|
|
59
70
|
private signWithDefaultLedger;
|
|
60
71
|
private signMultisignTransaction;
|
|
61
72
|
private signMultisignWithDefaultLedger;
|
|
@@ -67,6 +78,7 @@ export declare class LedgerAdapter extends BaseWalletAdapter {
|
|
|
67
78
|
private mapSigningError;
|
|
68
79
|
private withTimeout;
|
|
69
80
|
private isMultisignTransaction;
|
|
81
|
+
private getDeviceApp;
|
|
70
82
|
}
|
|
71
83
|
export declare function createLedgerAdapter(options?: LedgerAdapterOptions): LedgerAdapter;
|
|
72
84
|
export {};
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAA6K,MAAM,uBAAuB,CAAC;AACrO,OAAO,KAAK,EAAE,iBAAiB,EAAyB,MAAM,uBAAuB,CAAC;AAGtF,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,iBAAi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AAmB,CAAC,OAAO,CAAC,EAAE,oBAAoB,iBAEjE"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { BaseWalletAdapter, createWalletError, validateMultisignTransaction, WalletKitError, WalletKitErrorCode } from "@xrpl-wallet-kit/core";
|
|
1
|
+
import { BaseWalletAdapter, createWalletError, validateMultisignTransaction, WalletKitError, WalletKitErrorCode, getProviderHint, isLegacyNetwork, adapterSupportsNetwork, prepareProtocolTransaction } from "@xrpl-wallet-kit/core";
|
|
2
|
+
import { ensureLedgerApp } 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 = {}));
|
|
@@ -22,6 +24,10 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
22
24
|
signAndSubmit: true
|
|
23
25
|
};
|
|
24
26
|
this.connectionGeneration = 0;
|
|
27
|
+
if (options.supportedChains) {
|
|
28
|
+
this.adapterApiVersion = "1.2";
|
|
29
|
+
this.capabilities.details = { supportedChains: structuredClone(options.supportedChains), transactionModes: ["sign-only", "sign-and-submit"] };
|
|
30
|
+
}
|
|
25
31
|
this.derivationPath = options.derivationPath ?? getDerivationPath(options.accountIndex ?? DEFAULT_ACCOUNT_INDEX);
|
|
26
32
|
this.metadata = {
|
|
27
33
|
id: "ledger",
|
|
@@ -36,13 +42,29 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
36
42
|
return getLedgerBrowserSupport().supported;
|
|
37
43
|
}
|
|
38
44
|
async connect(options) {
|
|
45
|
+
if (options.network && !adapterSupportsNetwork(this, options.network))
|
|
46
|
+
throw createWalletError.networkNotSupported("Ledger", options.network.id);
|
|
47
|
+
if (options.network && !isLegacyNetwork(options.network) && !options.protocolContext)
|
|
48
|
+
throw createWalletError.networkNotSupported("Ledger (runtime required)", options.network.id);
|
|
49
|
+
this.protocolContext = options.network && !isLegacyNetwork(options.network) ? options.protocolContext : undefined;
|
|
50
|
+
if (options.network && !isLegacyNetwork(options.network))
|
|
51
|
+
getProviderHint(options.network, "ledgerApp");
|
|
52
|
+
this.connectionAbort?.abort();
|
|
53
|
+
const controller = new AbortController();
|
|
54
|
+
this.connectionAbort = controller;
|
|
55
|
+
const abort = () => controller.abort();
|
|
56
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
57
|
+
if (options.signal?.aborted)
|
|
58
|
+
controller.abort();
|
|
39
59
|
const generation = ++this.connectionGeneration;
|
|
40
60
|
this.network = options.network;
|
|
41
61
|
try {
|
|
62
|
+
if (controller.signal.aborted)
|
|
63
|
+
throw new WalletKitError(WalletKitErrorCode.REQUEST_CANCELLED, "Ledger connection cancelled.");
|
|
42
64
|
const session = this.options.connectLedger
|
|
43
65
|
? await this.options.connectLedger()
|
|
44
|
-
: await this.connectDefaultLedger(generation);
|
|
45
|
-
if (generation !== this.connectionGeneration)
|
|
66
|
+
: await this.connectDefaultLedger(generation, { ...options, signal: controller.signal });
|
|
67
|
+
if (generation !== this.connectionGeneration || controller.signal.aborted)
|
|
46
68
|
throw createWalletError.notConnected();
|
|
47
69
|
this.session = session;
|
|
48
70
|
return {
|
|
@@ -64,7 +86,8 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
64
86
|
connectedAt: Date.now(),
|
|
65
87
|
metadata: {
|
|
66
88
|
derivationPath: this.session.derivationPath ?? this.derivationPath,
|
|
67
|
-
appVersion: this.session.appVersion
|
|
89
|
+
appVersion: this.session.appVersion,
|
|
90
|
+
requestedDeviceApp: this.getDeviceApp()
|
|
68
91
|
}
|
|
69
92
|
}
|
|
70
93
|
};
|
|
@@ -74,18 +97,30 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
74
97
|
await this.cleanup();
|
|
75
98
|
throw this.mapConnectionError(error);
|
|
76
99
|
}
|
|
100
|
+
finally {
|
|
101
|
+
options.signal?.removeEventListener("abort", abort);
|
|
102
|
+
if (this.connectionAbort === controller)
|
|
103
|
+
this.connectionAbort = undefined;
|
|
104
|
+
}
|
|
77
105
|
}
|
|
78
106
|
async restoreSession(_session) {
|
|
79
107
|
// Hardware wallets should not be restored as connected without a fresh device prompt.
|
|
80
108
|
return null;
|
|
81
109
|
}
|
|
82
110
|
async disconnect() {
|
|
111
|
+
this.protocolContext = undefined;
|
|
112
|
+
this.connectionAbort?.abort();
|
|
83
113
|
this.connectionGeneration++;
|
|
84
114
|
this.session = undefined;
|
|
85
115
|
this.network = undefined;
|
|
86
116
|
await this.cleanup();
|
|
87
117
|
}
|
|
88
118
|
async signAndSubmit(request) {
|
|
119
|
+
const context = this.protocolContext;
|
|
120
|
+
if (context) {
|
|
121
|
+
const result = await this.signTransaction(request);
|
|
122
|
+
return request.submit === false ? { ...result, status: "signed" } : context.protocol.submit(result.txBlob, context.network, request);
|
|
123
|
+
}
|
|
89
124
|
if (!this.session)
|
|
90
125
|
throw createWalletError.notConnected();
|
|
91
126
|
if (this.isMultisignTransaction(request.txJson)) {
|
|
@@ -106,6 +141,33 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
106
141
|
async signTransaction(request) {
|
|
107
142
|
if (!this.session)
|
|
108
143
|
throw createWalletError.notConnected();
|
|
144
|
+
const context = this.protocolContext;
|
|
145
|
+
if (context) {
|
|
146
|
+
const generation = this.connectionGeneration;
|
|
147
|
+
const session = this.session;
|
|
148
|
+
const account = { address: session.address, publicKey: session.publicKey, network: context.network };
|
|
149
|
+
const tx = await prepareProtocolTransaction(context, request, account);
|
|
150
|
+
const current = () => {
|
|
151
|
+
if (generation !== this.connectionGeneration || this.session !== session || request.signal?.aborted)
|
|
152
|
+
throw createWalletError.notConnected();
|
|
153
|
+
};
|
|
154
|
+
current();
|
|
155
|
+
let blob;
|
|
156
|
+
if (this.xrp) {
|
|
157
|
+
const signature = await this.signOnDevice(tx, context.protocol.encode(tx), "Confirm Xahau transaction on Ledger.");
|
|
158
|
+
current();
|
|
159
|
+
blob = context.protocol.encode({ ...tx, TxnSignature: signature.toUpperCase() });
|
|
160
|
+
}
|
|
161
|
+
else
|
|
162
|
+
blob = (await session.signTransaction(tx, false)).txBlob;
|
|
163
|
+
current();
|
|
164
|
+
if (!blob)
|
|
165
|
+
throw createWalletError.signFailed(new Error("Ledger returned no signed blob"));
|
|
166
|
+
const verified = context.protocol.verify(blob, tx, account, context.network, request);
|
|
167
|
+
await context.protocol.prepare(tx, account, context.network, request);
|
|
168
|
+
current();
|
|
169
|
+
return { txBlob: verified.txBlob, signed: true, raw: { hash: verified.hash, appVersion: session.appVersion } };
|
|
170
|
+
}
|
|
109
171
|
const multisign = this.isMultisignTransaction(request.txJson);
|
|
110
172
|
const result = multisign
|
|
111
173
|
? await this.signMultisignTransaction(request.txJson)
|
|
@@ -136,7 +198,7 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
136
198
|
if (generation !== this.connectionGeneration)
|
|
137
199
|
throw createWalletError.notConnected();
|
|
138
200
|
const path = getDerivationPath(index);
|
|
139
|
-
const account = await this.withTimeout(xrp.getAddress(path, false, false), "Ledger
|
|
201
|
+
const account = await this.withTimeout(xrp.getAddress(path, false, false), "Ledger lookup timed out. Check device.");
|
|
140
202
|
if (generation !== this.connectionGeneration)
|
|
141
203
|
throw createWalletError.notConnected();
|
|
142
204
|
accounts.push({
|
|
@@ -155,18 +217,36 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
155
217
|
await ownedTransport?.close().catch(() => { });
|
|
156
218
|
}
|
|
157
219
|
}
|
|
158
|
-
async connectDefaultLedger(generation) {
|
|
220
|
+
async connectDefaultLedger(generation, options) {
|
|
159
221
|
const support = getLedgerBrowserSupport();
|
|
160
222
|
if (!support.supported) {
|
|
161
223
|
throw createWalletError.walletNotAvailable("Ledger", new Error(support.message));
|
|
162
224
|
}
|
|
163
|
-
|
|
225
|
+
let transport = await this.createTransport();
|
|
164
226
|
try {
|
|
227
|
+
const assertCurrent = () => {
|
|
228
|
+
if (generation !== this.connectionGeneration || options.signal?.aborted)
|
|
229
|
+
throw createWalletError.notConnected();
|
|
230
|
+
};
|
|
231
|
+
assertCurrent();
|
|
232
|
+
if (this.options.autoOpenApp !== false) {
|
|
233
|
+
transport = await ensureLedgerApp(transport, {
|
|
234
|
+
app: this.getDeviceApp(),
|
|
235
|
+
signal: options.signal, timeoutMs: options.timeoutMs ?? this.options.timeout ?? DEFAULT_TIMEOUT, assertCurrent,
|
|
236
|
+
onOpening: () => {
|
|
237
|
+
this.options.onDeviceState?.(LedgerDeviceState.OPENING_APP);
|
|
238
|
+
options.onRequestProgress?.({ state: "opened" });
|
|
239
|
+
},
|
|
240
|
+
onTransport: next => { transport = next; },
|
|
241
|
+
reopen: previous => this.reopenSameDevice(previous)
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
assertCurrent();
|
|
165
245
|
const Xrp = await loadLedgerXrpApp();
|
|
166
246
|
const xrp = new Xrp(transport);
|
|
167
|
-
const account = await this.withTimeout(xrp.getAddress(this.derivationPath, false, false), "Ledger
|
|
247
|
+
const account = await this.withTimeout(xrp.getAddress(this.derivationPath, false, false), "Ledger timed out. Check device.");
|
|
168
248
|
if (!account.address)
|
|
169
|
-
throw new Error("Ledger did not return an
|
|
249
|
+
throw new Error("Ledger did not return an address");
|
|
170
250
|
// App version is diagnostic only; older apps may not implement this APDU.
|
|
171
251
|
let appVersion;
|
|
172
252
|
try {
|
|
@@ -184,6 +264,7 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
184
264
|
throw createWalletError.notConnected();
|
|
185
265
|
this.transport = transport;
|
|
186
266
|
this.xrp = xrp;
|
|
267
|
+
this.options.onDeviceState?.(LedgerDeviceState.READY);
|
|
187
268
|
return {
|
|
188
269
|
address: account.address,
|
|
189
270
|
publicKey: account.publicKey,
|
|
@@ -194,15 +275,21 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
194
275
|
};
|
|
195
276
|
}
|
|
196
277
|
catch (error) {
|
|
197
|
-
|
|
278
|
+
void transport.close().catch(() => { });
|
|
198
279
|
throw error;
|
|
199
280
|
}
|
|
200
281
|
}
|
|
282
|
+
async reopenSameDevice(previous) {
|
|
283
|
+
const native = previous;
|
|
284
|
+
if (!native.device || !native.constructor.open)
|
|
285
|
+
throw new Error(`Open ${this.getDeviceApp()}. Reselect Ledger.`);
|
|
286
|
+
return native.constructor.open(native.device);
|
|
287
|
+
}
|
|
201
288
|
async signWithDefaultLedger(tx, submit = true) {
|
|
202
289
|
if (!this.xrp || !this.session)
|
|
203
290
|
throw createWalletError.notConnected();
|
|
204
291
|
if (!this.network)
|
|
205
|
-
throw createWalletError.connectionFailed(this.metadata.name, new Error("
|
|
292
|
+
throw createWalletError.connectionFailed(this.metadata.name, new Error("Select a network for Ledger signing"));
|
|
206
293
|
if (this.isMultisignTransaction(tx)) {
|
|
207
294
|
throw createWalletError.unsupportedMethod("XRPL multisigning through the single-sign path", this.metadata.name);
|
|
208
295
|
}
|
|
@@ -238,7 +325,7 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
238
325
|
txForSigning.SigningPubKey = publicKey.toUpperCase();
|
|
239
326
|
const signingBlob = encode(txForSigning).toUpperCase();
|
|
240
327
|
assertCurrent();
|
|
241
|
-
const signature = await this.signOnDevice(txForSigning, signingBlob, "
|
|
328
|
+
const signature = await this.signOnDevice(txForSigning, signingBlob, "Signing timed out. Check Ledger.");
|
|
242
329
|
assertCurrent();
|
|
243
330
|
this.verifyDeviceSignature(encodeForSigning(txForSigning), signature, publicKey, verifyKeypairSignature);
|
|
244
331
|
const signedTx = {
|
|
@@ -281,7 +368,7 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
281
368
|
if (!this.xrp || !this.session)
|
|
282
369
|
throw createWalletError.notConnected();
|
|
283
370
|
if (!this.network)
|
|
284
|
-
throw createWalletError.connectionFailed(this.metadata.name, new Error("
|
|
371
|
+
throw createWalletError.connectionFailed(this.metadata.name, new Error("Select a network for Ledger signing"));
|
|
285
372
|
const session = this.session;
|
|
286
373
|
const device = this.xrp;
|
|
287
374
|
const generation = this.connectionGeneration;
|
|
@@ -297,7 +384,7 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
297
384
|
// The XRP app adds the multisign prefix and signer AccountID itself.
|
|
298
385
|
const signingBlob = encode(prepared).toUpperCase();
|
|
299
386
|
assertCurrent();
|
|
300
|
-
const signature = await this.signOnDevice(prepared, signingBlob, "
|
|
387
|
+
const signature = await this.signOnDevice(prepared, signingBlob, "Multisign timed out. Check Ledger.");
|
|
301
388
|
assertCurrent();
|
|
302
389
|
this.verifyDeviceSignature(encodeForMultiSigning(prepared, session.address), signature, publicKey, verifyKeypairSignature);
|
|
303
390
|
const signer = { Signer: { Account: session.address, SigningPubKey: publicKey, TxnSignature: signature.toUpperCase() } };
|
|
@@ -384,7 +471,7 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
384
471
|
mapConnectionError(error) {
|
|
385
472
|
if (isWalletKitErrorLike(error))
|
|
386
473
|
return error;
|
|
387
|
-
const parsed = parseLedgerError(error);
|
|
474
|
+
const parsed = parseLedgerError(error, this.getDeviceApp());
|
|
388
475
|
if (parsed.timeout)
|
|
389
476
|
return createWalletError.requestTimeout(parsed.message, error);
|
|
390
477
|
if (parsed.rejected)
|
|
@@ -397,7 +484,7 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
397
484
|
mapSigningError(error) {
|
|
398
485
|
if (isWalletKitErrorLike(error))
|
|
399
486
|
return error;
|
|
400
|
-
const parsed = parseLedgerError(error);
|
|
487
|
+
const parsed = parseLedgerError(error, this.getDeviceApp());
|
|
401
488
|
if (parsed.timeout)
|
|
402
489
|
return createWalletError.requestTimeout(parsed.message, error);
|
|
403
490
|
if (parsed.rejected)
|
|
@@ -424,6 +511,9 @@ export class LedgerAdapter extends BaseWalletAdapter {
|
|
|
424
511
|
const txJson = asRecord(tx);
|
|
425
512
|
return txJson.SigningPubKey === "" || Array.isArray(txJson.Signers);
|
|
426
513
|
}
|
|
514
|
+
getDeviceApp() {
|
|
515
|
+
return this.options.deviceApp ?? (this.network ? getProviderHint(this.network, "ledgerApp") : "XRP");
|
|
516
|
+
}
|
|
427
517
|
}
|
|
428
518
|
export function createLedgerAdapter(options) {
|
|
429
519
|
return new LedgerAdapter(options);
|
|
@@ -453,7 +543,7 @@ function getLedgerBrowserSupport() {
|
|
|
453
543
|
message: webHID || webUSB ? "Ledger transport is supported" : "This browser does not support Ledger WebHID/WebUSB transport"
|
|
454
544
|
};
|
|
455
545
|
}
|
|
456
|
-
function parseLedgerError(error) {
|
|
546
|
+
function parseLedgerError(error, app = "XRP") {
|
|
457
547
|
const message = getLedgerErrorMessage(error);
|
|
458
548
|
const statusCode = getLedgerStatusCode(error);
|
|
459
549
|
const lower = message.toLowerCase();
|
|
@@ -464,13 +554,15 @@ function parseLedgerError(error) {
|
|
|
464
554
|
return { state: LedgerDeviceState.UNKNOWN, message: message || "Ledger request timed out", rejected: false, timeout: true };
|
|
465
555
|
}
|
|
466
556
|
if (statusCode === 0x6804 || /locked|pin/.test(lower)) {
|
|
467
|
-
return { state: LedgerDeviceState.LOCKED, message: "
|
|
557
|
+
return { state: LedgerDeviceState.LOCKED, message: "Unlock your Ledger.", rejected: false, timeout: false };
|
|
468
558
|
}
|
|
469
|
-
if ([0x6e00, 0x6511, 0x650f].includes(statusCode ?? 0) || /app.*open|xrp.*app|cla_not_supported/.test(lower)) {
|
|
470
|
-
return { state: LedgerDeviceState.APP_NOT_OPEN, message:
|
|
559
|
+
if ([0x6e00, 0x6511, 0x650f].includes(statusCode ?? 0) || /app.*open|(?:xrp|xah|xahau).*app|open (?:the )?(?:xrp|xah)|cla_not_supported/.test(lower)) {
|
|
560
|
+
return { state: LedgerDeviceState.APP_NOT_OPEN, message: /select.*again|reselect/.test(lower)
|
|
561
|
+
? `Open ${app}. Reselect Ledger.` : /connect again|reconnect/.test(lower)
|
|
562
|
+
? `Open ${app} on Ledger. Reconnect.` : `Open ${app} on Ledger.`, rejected: false, timeout: false };
|
|
471
563
|
}
|
|
472
564
|
if (/no device|device.*not found|disconnected|cannot open device|transport/i.test(message)) {
|
|
473
|
-
return { state: LedgerDeviceState.NOT_CONNECTED, message: "
|
|
565
|
+
return { state: LedgerDeviceState.NOT_CONNECTED, message: "Connect and unlock Ledger.", rejected: false, timeout: false };
|
|
474
566
|
}
|
|
475
567
|
return { state: LedgerDeviceState.UNKNOWN, message: message || "Unknown Ledger error", rejected: false, timeout: false };
|
|
476
568
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xrpl-wallet-kit/adapter-ledger",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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.
|
|
28
|
+
"@xrpl-wallet-kit/core": "0.2.0"
|
|
28
29
|
},
|
|
29
30
|
"peerDependencies": {
|
|
30
31
|
"xrpl": "^4.0.0 || ^5.0.0"
|