UNPKG

6.51 kBTypeScriptView Raw
1/// <reference types="node" />
2import EventEmitter from "events";
3import type { DeviceModel } from "@ledgerhq/devices";
4import { TransportError, StatusCodes, getAltStatusMessage, TransportStatusError } from "@ledgerhq/errors";
5export { TransportError, TransportStatusError, StatusCodes, getAltStatusMessage, };
6/**
7 */
8export declare type Subscription = {
9 unsubscribe: () => void;
10};
11/**
12 */
13export declare type Device = any;
14/**
15 * type: add or remove event
16 * descriptor: a parameter that can be passed to open(descriptor)
17 * deviceModel: device info on the model (is it a nano s, nano x, ...)
18 * device: transport specific device info
19 */
20export interface DescriptorEvent<Descriptor> {
21 type: "add" | "remove";
22 descriptor: Descriptor;
23 deviceModel?: DeviceModel | null | undefined;
24 device?: Device;
25}
26/**
27 */
28export declare type Observer<Ev> = Readonly<{
29 next: (event: Ev) => unknown;
30 error: (e: any) => unknown;
31 complete: () => unknown;
32}>;
33/**
34 * Transport defines the generic interface to share between node/u2f impl
35 * A **Descriptor** is a parametric type that is up to be determined for the implementation.
36 * it can be for instance an ID, an file path, a URL,...
37 */
38export default class Transport {
39 exchangeTimeout: number;
40 unresponsiveTimeout: number;
41 deviceModel: DeviceModel | null | undefined;
42 /**
43 * Statically check if a transport is supported on the user's platform/browser.
44 */
45 static readonly isSupported: () => Promise<boolean>;
46 /**
47 * List once all available descriptors. For a better granularity, checkout `listen()`.
48 * @return a promise of descriptors
49 * @example
50 * TransportFoo.list().then(descriptors => ...)
51 */
52 static readonly list: () => Promise<Array<any>>;
53 /**
54 * Listen all device events for a given Transport. The method takes an Obverver of DescriptorEvent and returns a Subscription (according to Observable paradigm https://github.com/tc39/proposal-observable )
55 * a DescriptorEvent is a `{ descriptor, type }` object. type can be `"add"` or `"remove"` and descriptor is a value you can pass to `open(descriptor)`.
56 * each listen() call will first emit all potential device already connected and then will emit events can come over times,
57 * for instance if you plug a USB device after listen() or a bluetooth device become discoverable.
58 * @param observer is an object with a next, error and complete function (compatible with observer pattern)
59 * @return a Subscription object on which you can `.unsubscribe()` to stop listening descriptors.
60 * @example
61 const sub = TransportFoo.listen({
62 next: e => {
63 if (e.type==="add") {
64 sub.unsubscribe();
65 const transport = await TransportFoo.open(e.descriptor);
66 ...
67 }
68 },
69 error: error => {},
70 complete: () => {}
71 })
72 */
73 static readonly listen: (observer: Observer<DescriptorEvent<any>>) => Subscription;
74 /**
75 * attempt to create a Transport instance with potentially a descriptor.
76 * @param descriptor: the descriptor to open the transport with.
77 * @param timeout: an optional timeout
78 * @return a Promise of Transport instance
79 * @example
80 TransportFoo.open(descriptor).then(transport => ...)
81 */
82 static readonly open: (descriptor?: any, timeout?: number) => Promise<Transport>;
83 /**
84 * low level api to communicate with the device
85 * This method is for implementations to implement but should not be directly called.
86 * Instead, the recommanded way is to use send() method
87 * @param apdu the data to send
88 * @return a Promise of response data
89 */
90 exchange(_apdu: Buffer): Promise<Buffer>;
91 /**
92 * set the "scramble key" for the next exchanges with the device.
93 * Each App can have a different scramble key and they internally will set it at instanciation.
94 * @param key the scramble key
95 */
96 setScrambleKey(_key: string): void;
97 /**
98 * close the exchange with the device.
99 * @return a Promise that ends when the transport is closed.
100 */
101 close(): Promise<void>;
102 _events: EventEmitter;
103 /**
104 * Listen to an event on an instance of transport.
105 * Transport implementation can have specific events. Here is the common events:
106 * * `"disconnect"` : triggered if Transport is disconnected
107 */
108 on(eventName: string, cb: (...args: Array<any>) => any): void;
109 /**
110 * Stop listening to an event on an instance of transport.
111 */
112 off(eventName: string, cb: (...args: Array<any>) => any): void;
113 emit(event: string, ...args: any): void;
114 /**
115 * Enable or not logs of the binary exchange
116 */
117 setDebugMode(): void;
118 /**
119 * Set a timeout (in milliseconds) for the exchange call. Only some transport might implement it. (e.g. U2F)
120 */
121 setExchangeTimeout(exchangeTimeout: number): void;
122 /**
123 * Define the delay before emitting "unresponsive" on an exchange that does not respond
124 */
125 setExchangeUnresponsiveTimeout(unresponsiveTimeout: number): void;
126 /**
127 * wrapper on top of exchange to simplify work of the implementation.
128 * @param cla
129 * @param ins
130 * @param p1
131 * @param p2
132 * @param data
133 * @param statusList is a list of accepted status code (shorts). [0x9000] by default
134 * @return a Promise of response buffer
135 */
136 send: (cla: number, ins: number, p1: number, p2: number, data?: Buffer, statusList?: Array<number>) => Promise<Buffer>;
137 /**
138 * create() allows to open the first descriptor available or
139 * throw if there is none or if timeout is reached.
140 * This is a light helper, alternative to using listen() and open() (that you may need for any more advanced usecase)
141 * @example
142 TransportFoo.create().then(transport => ...)
143 */
144 static create(openTimeout?: number, listenTimeout?: number): Promise<Transport>;
145 exchangeBusyPromise: Promise<void> | null | undefined;
146 exchangeAtomicImpl: (f: () => Promise<Buffer | void>) => Promise<Buffer | void>;
147 decorateAppAPIMethods(self: Record<string, any>, methods: Array<string>, scrambleKey: string): void;
148 _appAPIlock: string | null;
149 decorateAppAPIMethod<R, A extends any[]>(methodName: string, f: (...args: A) => Promise<R>, ctx: any, scrambleKey: string): (...args: A) => Promise<R>;
150 static ErrorMessage_ListenTimeout: string;
151 static ErrorMessage_NoDeviceFound: string;
152}
153//# sourceMappingURL=Transport.d.ts.map
\No newline at end of file