Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #67 from gre/u2f-timeout-in-millis
Browse files Browse the repository at this point in the history
add Transport#setExchangeTimeout and use it in U2F instead of open timeout
  • Loading branch information
gre authored Feb 8, 2018
2 parents c5e70ab + 8601037 commit 115396b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
12 changes: 3 additions & 9 deletions packages/hw-transport-u2f/src/TransportU2F.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,13 @@ export default class TransportU2F extends Transport<null> {
};
};

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<TransportU2F> {
return Promise.resolve(new TransportU2F(timeout));
static open(): Promise<TransportU2F> {
return Promise.resolve(new TransportU2F());
}

exchange(apdu: Buffer): Promise<Buffer> {
Expand All @@ -95,7 +89,7 @@ export default class TransportU2F extends Transport<null> {
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");
Expand Down
24 changes: 16 additions & 8 deletions packages/hw-transport/src/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ TransportStatusError.prototype = new Error();
*/
export default class Transport<Descriptor> {
debug: boolean = false;
exchangeTimeout: number = 30000;

/**
* Statically check if a transport is supported on the user's platform/browser.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -238,7 +246,7 @@ TransportFoo.open(descriptor).then(transport => ...)
* @example
TransportFoo.create().then(transport => ...)
*/
static create(timeout?: number = 5000): Promise<Transport<Descriptor>> {
static create(openTimeout?: number = 5000): Promise<Transport<Descriptor>> {
if (arguments.length > 1) {
console.warn(
this.name +
Expand All @@ -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"));
}
Expand Down

0 comments on commit 115396b

Please sign in to comment.