Search code examples
node.jsusblibusbsmartcard-readernode-usb

Access ACS chip reader from node.js


I'm trying to access the ACS chip reader (ACR1252) from node.js using node-usb library. But it seems the library is really a usage pain due no documentation. So far I got the device recognized and connect to the interfaces (just on mac, linux still not working). When a chip is identified, the device emits <Buffer 50 03> and <Buffer 50 02> when the chip is removed.

However when sending a command to get the chip's serial number, the transfer call fails with error undefined.

This is my code so far:

import usb, { InEndpoint, OutEndpoint } from 'usb';

usb.on('attach', device => {
   device.__open();
   device.__claimInterface(0);

   device.open();

   const ifc = device.interface(0);
   ifc.claim();

   const outEndpoint: OutEndpoint = <OutEndpoint>ifc.endpoints[0];
   const inEndpoint: InEndpoint = <InEndpoint>ifc.endpoints[2];

   inEndpoint.startPoll();
   inEndpoint.on('data', (buffer: Buffer) => {
      console.warn('-- Received data: ', buffer);

      if (buffer.toString('hex') === '5003') {
         console.warn('Chip recognized!');
         outEndpoint.transfer(Buffer.from('FF CA 00 00 00', 'hex'), error => {
            console.warn('transfer error', error);
         });
      }
   });
   inEndpoint.on('error', error => {
      console.warn('error', error);
   });
});

Output logs:

-- Received data:  <Buffer 50 03>
Chip recognized!
transfer error undefined
-- Received data:  <Buffer 50 02>

Solution

  • You would have to implement CCID protocol to communicate with a smartcard reader (it is not that simple as sending APDU to USB endpoint).

    Consider using node-pcsclite which provides PC/SC API which is a standard way to communicate with smartcard readers -- your code will work with any smartcard reader supported by installed drivers and you won't have to implement CCID.

    See Ludovic Rousseau's blog for an example.

    Good luck with your project!

    EDIT>

    There is a demo CCID implementation for javascript here.