@ledgerhq/hw-transport
Advanced tools
Comparing version 1.2.0-beta.b4ba11b7 to 1.2.0-beta.df091f88
@@ -32,5 +32,10 @@ "use strict"; | ||
this.send = (() => { | ||
var _ref = _asyncToGenerator(function* (cla, ins, p1, p2, data = Buffer.alloc(0)) { | ||
var _ref = _asyncToGenerator(function* (cla, ins, p1, p2, data = Buffer.alloc(0), statusList = [0x9000]) { | ||
(0, _invariant2.default)(data.length < 256, "data.length exceed 256 bytes limit. Got: %s", data.length); | ||
return Buffer.from((yield _this.exchange(Buffer.concat([Buffer.from([cla, ins, p1, p2]), Buffer.from([data.length]), data]).toString("hex"), [0x9000])), "hex"); | ||
const response = yield _this.exchange(Buffer.concat([Buffer.from([cla, ins, p1, p2]), Buffer.from([data.length]), data])); | ||
const sw = response.readUInt16BE(response.length - 2); | ||
(0, _invariant2.default)(statusList.some(function (s) { | ||
return s === sw; | ||
}), "Invalid status %s", sw.toString(16)); | ||
return response; | ||
}); | ||
@@ -45,3 +50,3 @@ | ||
/** | ||
* List once all available descriptors. For a better granularity, checkout `discover()`. | ||
* List once all available descriptors. For a better granularity, checkout `listen()`. | ||
* @return a promise of descriptors | ||
@@ -54,12 +59,19 @@ * @example | ||
/** | ||
* Listen all descriptors that can be opened. This will call cb() with all available descriptors | ||
* and then the new ones that gets discovered in the future until unsubscribe is called. | ||
* events can come over times, for instance if you plug a USB device after listen() or a bluetooth device become discoverable | ||
* 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 ) | ||
* a DescriptorEvent is a `{ descriptor, type }` object. type can be `"add"` or `"remove"` and descriptor is a value you can pass to `open(descriptor)`. | ||
* each listen() call will first emit all potential device already connected and then will emit events can come over times, | ||
* for instance if you plug a USB device after listen() or a bluetooth device become discoverable. | ||
* @param observer is an object with a next, error and complete function (compatible with observer pattern) | ||
* @return a Subscription object on which you can `.unsubscribe()` to stop discovering descriptors. | ||
* @return a Subscription object on which you can `.unsubscribe()` to stop listening descriptors. | ||
* @example | ||
const sub = TransportFoo.discover(async descriptor => { | ||
sub.unsubscribe(); | ||
const transport = await TransportFoo.open(descriptor); | ||
... | ||
const sub = TransportFoo.listen({ | ||
next: e => { | ||
if (e.type==="add") { | ||
sub.unsubscribe(); | ||
const transport = await TransportFoo.open(e.descriptor); | ||
... | ||
} | ||
}, | ||
error: error => {}, | ||
complete: () => {} | ||
}) | ||
@@ -81,6 +93,6 @@ */ | ||
* low level api to communicate with the device | ||
* TODO: in the future we'll refactor this to be Buffer=>Buffer instead | ||
* @param apduHex hex string of the data to send | ||
* @param statusList an array of accepted status code to be considered successful | ||
* @return a Promise of hex string response data | ||
* This method is for implementations to implement but should not be directly called. | ||
* Instead, the recommanded way is to use send() method | ||
* @param apdu the data to send | ||
* @return a Promise of response data | ||
*/ | ||
@@ -136,2 +148,3 @@ | ||
* @param data | ||
* @param statusList is a list of accepted status code (shorts). [0x9000] by default | ||
* @return a Promise of response buffer | ||
@@ -143,3 +156,3 @@ */ | ||
* create() allows to open the first descriptor available or throw if there is none. | ||
* **DEPRECATED**: use `list()` or `discover()` and `open()` instead. | ||
* **DEPRECATED**: use `list()` or `listen()` and `open()` instead. | ||
*/ | ||
@@ -150,7 +163,5 @@ static create(timeout, debug = false) { | ||
return _asyncToGenerator(function* () { | ||
console.warn(_this2.name + ".create is deprecated. Please use .list()/.discover() and .open() instead"); | ||
console.warn(_this2.name + ".create is deprecated. Please use .list()/.listen() and .open() instead"); | ||
const descriptors = yield _this2.list(); | ||
if (descriptors.length === 0) { | ||
throw "No device found"; | ||
} | ||
(0, _invariant2.default)(descriptors.length !== 0, "No device found"); | ||
const transport = yield _this2.open(descriptors[0], timeout); | ||
@@ -157,0 +168,0 @@ transport.setDebugMode(debug); |
{ | ||
"name": "@ledgerhq/hw-transport", | ||
"version": "1.2.0-beta.b4ba11b7", | ||
"version": "1.2.0-beta.df091f88", | ||
"description": "Ledger Hardware Wallet common interface of the communication layer", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,4 +7,8 @@ //@flow | ||
export type Subscription = { unsubscribe: () => void }; | ||
export type Observer<T> = { | ||
next: (descriptor: T) => void, | ||
export type DescriptorEvent<Descriptor> = { | ||
type: "add" | "remove", | ||
descriptor: Descriptor | ||
}; | ||
export type Observer<Descriptor> = { | ||
next: (event: DescriptorEvent<Descriptor>) => void, | ||
error: (e: ?Error) => void, | ||
@@ -23,3 +27,3 @@ complete: () => void | ||
/** | ||
* List once all available descriptors. For a better granularity, checkout `discover()`. | ||
* List once all available descriptors. For a better granularity, checkout `listen()`. | ||
* @return a promise of descriptors | ||
@@ -32,15 +36,24 @@ * @example | ||
/** | ||
* Listen all descriptors that can be opened. This will call cb() with all available descriptors | ||
* and then the new ones that gets discovered in the future until unsubscribe is called. | ||
* events can come over times, for instance if you plug a USB device after listen() or a bluetooth device become discoverable | ||
* 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 ) | ||
* a DescriptorEvent is a `{ descriptor, type }` object. type can be `"add"` or `"remove"` and descriptor is a value you can pass to `open(descriptor)`. | ||
* each listen() call will first emit all potential device already connected and then will emit events can come over times, | ||
* for instance if you plug a USB device after listen() or a bluetooth device become discoverable. | ||
* @param observer is an object with a next, error and complete function (compatible with observer pattern) | ||
* @return a Subscription object on which you can `.unsubscribe()` to stop discovering descriptors. | ||
* @return a Subscription object on which you can `.unsubscribe()` to stop listening descriptors. | ||
* @example | ||
const sub = TransportFoo.discover(async descriptor => { | ||
sub.unsubscribe(); | ||
const transport = await TransportFoo.open(descriptor); | ||
... | ||
const sub = TransportFoo.listen({ | ||
next: e => { | ||
if (e.type==="add") { | ||
sub.unsubscribe(); | ||
const transport = await TransportFoo.open(e.descriptor); | ||
... | ||
} | ||
}, | ||
error: error => {}, | ||
complete: () => {} | ||
}) | ||
*/ | ||
static +discover: (observer: Observer<Descriptor>) => Subscription; | ||
static +listen: ( | ||
observer: Observer<DescriptorEvent<Descriptor>> | ||
) => Subscription; | ||
@@ -62,8 +75,8 @@ /** | ||
* low level api to communicate with the device | ||
* TODO: in the future we'll refactor this to be Buffer=>Buffer instead | ||
* @param apduHex hex string of the data to send | ||
* @param statusList an array of accepted status code to be considered successful | ||
* @return a Promise of hex string response data | ||
* This method is for implementations to implement but should not be directly called. | ||
* Instead, the recommanded way is to use send() method | ||
* @param apdu the data to send | ||
* @return a Promise of response data | ||
*/ | ||
+exchange: (apduHex: string, statusList: Array<number>) => Promise<string>; | ||
+exchange: (apdu: Buffer) => Promise<Buffer>; | ||
@@ -119,2 +132,3 @@ /** | ||
* @param data | ||
* @param statusList is a list of accepted status code (shorts). [0x9000] by default | ||
* @return a Promise of response buffer | ||
@@ -127,3 +141,4 @@ */ | ||
p2: number, | ||
data: Buffer = Buffer.alloc(0) | ||
data: Buffer = Buffer.alloc(0), | ||
statusList: Array<number> = [0x9000] | ||
): Promise<Buffer> => { | ||
@@ -135,13 +150,16 @@ invariant( | ||
); | ||
return Buffer.from( | ||
await this.exchange( | ||
Buffer.concat([ | ||
Buffer.from([cla, ins, p1, p2]), | ||
Buffer.from([data.length]), | ||
data | ||
]).toString("hex"), | ||
[0x9000] | ||
), | ||
"hex" | ||
const response = await this.exchange( | ||
Buffer.concat([ | ||
Buffer.from([cla, ins, p1, p2]), | ||
Buffer.from([data.length]), | ||
data | ||
]) | ||
); | ||
const sw = response.readUInt16BE(response.length - 2); | ||
invariant( | ||
statusList.some(s => s === sw), | ||
"Invalid status %s", | ||
sw.toString(16) | ||
); | ||
return response; | ||
}; | ||
@@ -151,3 +169,3 @@ | ||
* create() allows to open the first descriptor available or throw if there is none. | ||
* **DEPRECATED**: use `list()` or `discover()` and `open()` instead. | ||
* **DEPRECATED**: use `list()` or `listen()` and `open()` instead. | ||
*/ | ||
@@ -160,8 +178,6 @@ static async create( | ||
this.name + | ||
".create is deprecated. Please use .list()/.discover() and .open() instead" | ||
".create is deprecated. Please use .list()/.listen() and .open() instead" | ||
); | ||
const descriptors = await this.list(); | ||
if (descriptors.length === 0) { | ||
throw "No device found"; | ||
} | ||
invariant(descriptors.length !== 0, "No device found"); | ||
const transport = await this.open(descriptors[0], timeout); | ||
@@ -168,0 +184,0 @@ transport.setDebugMode(debug); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36327
598