1.0.0 • Published 5 years ago

smartcard-interface v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

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

Kind: global class
Properties

NameTypeDescription
cardsmartcard.CardThe card to wrap
deviceDeviceParent 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

ParamTypeDescription
APDUBufferBufferBuffer 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

NameTypeDescription
namestringDevice name.
indexnumberDevice index.

new Device(device, index)

Wraps a smartcard.Device

ParamTypeDescription
devicesmartcard.DeviceThe device to wrap
indexnumberRaw device index usually 0

"insert"

Fired when a card is inserted into the reader.

Kind: event emitted by Device
Properties

NameTypeDescription
cardCardThe card that was inserted.

"remove"

Fired when a card is removed from the reader.

Kind: event emitted by Device
Properties

NameTypeDescription
cardCardThe card that was removed.