keepkey / keepkey.js

Superseded by https://github.com/shapeshift/hdwallet
MIT License
13 stars 9 forks source link

Fix errors caused by overlapping reads #8

Closed majorhayes closed 5 years ago

majorhayes commented 5 years ago

Issue

Since transferIn is asynchronous, it's possible to have multiple async loops (see webUsbDevice.read()) to call transferIn, causing some packets to end up in the wrong read loop.

This manifests by a look expecting the first packet to have a header with a messageLength, but since the packet it receives is not really a first packet, but the middle of some other read, the read length is incorrect and causes an error when attempting to create a massively large array.

Resolution

Use p-queue to add an async queue with a concurrency of 1 to only allow one message and response to the device at a time.

Extra

This PR also simplifies some code around reading message length and concatenating packets.