smartcard-interface

Wraps smartcard on mac/linux and pcsc on windows.

Usage no npm install needed!

<script type="module">
  import smartcardInterface from 'https://cdn.skypack.dev/smartcard-interface';
</script>

README

smartcard-interface

A simple package for sending commands into smartcards.

Note: This simply wraps the smartcard package on mac/linux and pcsc on windows.

Installation

$ npm install smartcard-interface

Usage

const { DeviceManager } = require("smartcard-interface");

let devices = new DeviceManager();

// Called when a device is inserted.
devices.on("activate", (e) => {
    let device = e.device;

    console.log("Device activated", device.name);

    device.on("insert", async (e) => {
        let card = e.card;

        console.log("Card inserted", card.getAtr());

        card.connect();

        // Select an applet.
        let selectResponse = await card.sendCommand(Buffer.from([
            0x00,
            0xA4,
            0x04,
            0x00,
            0x09,
            ...Buffer.from("000000000000000000", "hex") // AID
        ]));

        console.log(selectResponse);

        // Send a command into the applet.
        let pubKey = await card.sendCommand(Buffer.from([
            0x00, // CLA
            0x32, // INS
            0x00, // P1
            0x00, // P2
            0x20  // LE
        ]));

        console.log(pubKey);

        card.close();
    });

    device.on("removed", (e) => {
        let card = e.card;

        console.log("Card removed", card.getAtr());
    });
});

Documentation

Classes

Card

Container for cards

Device

Container for card readers

Card

Container for cards

Kind: global class
Properties

Name Type Description
card smartcard.Card The card to wrap
device Device Parent device

new Card()

Wraps a smartcard.Card

card.getAtr() ⇒ string

Returns the card atr.

Kind: instance method of Card
Returns: string - card atr

card.connect()

Connects to the card. Do this before sending commands.

Kind: instance method of Card

card.close()

Closes the current connection.

Kind: instance method of Card

card.sendCommand(APDUBuffer) ⇒ Promise.<Device>

Executes the APDUBuffer on the card.

Kind: instance method of Card
Returns: Promise.<Device> - Output APDU

Param Type Description
APDUBuffer Buffer Buffer of bytes

Example

let aid = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];

card.connect(); // You need to connect in order to issue commands.

// Selects the aid above.
let response = await card.sendCommand(Buffer.from([
        [0x00, 0xA4, 0x04, 0x00, 0x00, 0x09, ...aid]
]));

console.log("Response", response); // Buffer < 90 00 >

card.close(); // Don't forget to close the connection when you're done.

Device

Container for card readers

Kind: global class
Properties

Name Type Description
name string Device name.
index number Device index.

new Device(device, index)

Wraps a smartcard.Device

Param Type Description
device smartcard.Device The device to wrap
index number Raw device index usually 0

"insert"

Fired when a card is inserted into the reader.

Kind: event emitted by Device
Properties

Name Type Description
card Card The card that was inserted.

"remove"

Fired when a card is removed from the reader.

Kind: event emitted by Device
Properties

Name Type Description
card Card The card that was removed.