diff --git a/packages/hw-transport-u2f/src/TransportU2F.js b/packages/hw-transport-u2f/src/TransportU2F.js index 44a59117f..9fcfa5029 100644 --- a/packages/hw-transport-u2f/src/TransportU2F.js +++ b/packages/hw-transport-u2f/src/TransportU2F.js @@ -65,19 +65,13 @@ export default class TransportU2F extends Transport { }; }; - timeoutSeconds: number; scrambleKey: Buffer; - constructor(timeoutSeconds?: number = 20) { - super(); - this.timeoutSeconds = timeoutSeconds; - } - /** * static function to create a new Transport from a connected Ledger device discoverable via U2F (browser support) */ - static open(_: *, timeout?: number): Promise { - return Promise.resolve(new TransportU2F(timeout)); + static open(): Promise { + return Promise.resolve(new TransportU2F()); } exchange(apdu: Buffer): Promise { @@ -95,7 +89,7 @@ export default class TransportU2F extends Transport { if (this.debug) { console.log("=> " + apdu.toString("hex")); } - return sign(signRequest, this.timeoutSeconds).then(response => { + return sign(signRequest, this.exchangeTimeout / 1000).then(response => { const { signatureData } = response; if (typeof signatureData === "string") { const data = Buffer.from(normal64(signatureData), "base64"); diff --git a/packages/hw-transport/src/Transport.js b/packages/hw-transport/src/Transport.js index c5cd210d2..ad0e3732b 100644 --- a/packages/hw-transport/src/Transport.js +++ b/packages/hw-transport/src/Transport.js @@ -91,6 +91,7 @@ TransportStatusError.prototype = new Error(); */ export default class Transport { debug: boolean = false; + exchangeTimeout: number = 30000; /** * Statically check if a transport is supported on the user's platform/browser. @@ -193,6 +194,13 @@ TransportFoo.open(descriptor).then(transport => ...) this.debug = debug; } + /** + * Set a timeout (in milliseconds) for the exchange call. Only some transport might implement it. (e.g. U2F) + */ + setExchangeTimeout(exchangeTimeout: number) { + this.exchangeTimeout = exchangeTimeout; + } + /** * wrapper on top of exchange to simplify work of the implementation. * @param cla @@ -238,7 +246,7 @@ TransportFoo.open(descriptor).then(transport => ...) * @example TransportFoo.create().then(transport => ...) */ - static create(timeout?: number = 5000): Promise> { + static create(openTimeout?: number = 5000): Promise> { if (arguments.length > 1) { console.warn( this.name + @@ -247,23 +255,23 @@ TransportFoo.create().then(transport => ...) } return new Promise((resolve, reject) => { let found = false; - const timeoutId = setTimeout(() => { + const openTimeoutId = setTimeout(() => { sub.unsubscribe(); - reject(new TransportError("Transport timeout", "timeout")); - }, timeout); + reject(new TransportError("Transport openTimeout", "OpenTimeout")); + }, openTimeout); const sub = this.listen({ next: e => { found = true; sub.unsubscribe(); - clearTimeout(timeoutId); - this.open(e.descriptor, timeout).then(resolve, reject); + clearTimeout(openTimeoutId); + this.open(e.descriptor, openTimeout).then(resolve, reject); }, error: e => { - clearTimeout(timeoutId); + clearTimeout(openTimeoutId); reject(e); }, complete: () => { - clearTimeout(timeoutId); + clearTimeout(openTimeoutId); if (!found) { reject(new TransportError("No device found", "NoDeviceFound")); }