Smartcard library.
This is a fork of the https://github.com/tomkp/smartcard library, created to solve a problem of fixed length of the expected APDU responses.
The following objects are defined by the smartcard
library, each contains its own set of methods and events.
A general object that provides access to all smartcard related devices.
The devices
object emits the following events
Emitted when a card reader is attached. Returns:
Object
Device
Array
: List of all devices, returned viadevices.listDevices()
Emitted when a card reader is detached. Returns:
Object
Device
Array
: List of all devices, returned viadevices.listDevices()
Emitted when an error occurs
Returns Object
:
- error
Error
The following methods are available within the devices
class.
The constructor for a devices object takes no arguments,
devices = new Devices();
Returns Promise
- Resolves with activation event
Returns Promise
- Resolves with deactivation event
Returns Object
a list of the different devices attached, each a device
object
-
name
String
: The text name of a device -
Returns
Device
An object representing a specific card reader (device).
The following methods are available within the device
class.
Returns the name of the attached device.
Sends a command to the connected device
- data
Buffer
: data to be transmitted - res_len
Number
: Maximum length of the expected response, includes the 2 byte response (sw1 and sw2) - protocol
Number
: Protocol to be used in the transmission - cb(error,response)
Function
: Called when transmit function completes- error
Error
- output
Buffer
- error
The device
object emits the following events
Emitted when a smartcard is inserted into a card reader
Returns Object
:
- device
Device
- card
Card
Emitted when a smartcard is removed from a card reader
Returns Object
:
- name
String
- card
Card
An object representing an attached smart card.
The following methods are available within the card
class.
Returns String
containing the atr of the card
Sends a command to the card
- commandApdu: The command to be sent to the card
String
Buffer
Array
CommandApdu
- callback(error,response): (optional) Function to call upon completion of the command
- error
Error
- response
Buffer
- error
Returns Promise
- Resolves with response
Buffer
- Rejects with error
Error
If no callback is specified, returns a Promise
*
The card
object emits the following events
Emitted when a command is sent to the smartcard.
Returns Object
:
- card
Card
- command
Buffer
Emitted when a response is received from the card.
Returns Object
:
- card
Card
- command
Buffer
- response
ResponseApdu
An object representing a command to send to a smart card
The CommandApdu
class has the following methods.
Creates a new instance and sets the appropriate items
- obj
Object
- cla
Number
: The class of the command, typically 0 - ins
Number
: The instruction - p1
Number
: The value of p1 - p2
Number
: The value of p2 - data
Array
(optional): The value of data - le
Number
(optional): The value of le
- cla
OR
- obj
Array
: Byte array representing the whole command
Converts the command to a Buffer
- Returns
Buffer
Converts the command to a hex string
- Returns
String
Converts the command to a byte array
- Returns
Array
Updates the le value of the command
- le
Number
: The new le value
Class representing a response from the card
The ResponseApdu
class has the following methods.
Interprets the return code and attempts to provide a text translation.
- Returns
String
Returns the response data without including the status code
- Returns
String
Returns only the status code
- Returns
String
Check if the status code is 9000
- Returns
Boolean
Returns the whole buffer, status code and data
- Returns
Buffer
Reads the status code and looks for a 61 as sw1, meaning more data is available
- Returns
Boolean
Reads sw2 staus code to return number of bytes left, when sw1 is 61. A value of 0 means there are more than 256 bytes remaining.
- Returns
Number
Checks status code for 6c as sw1
- Returns
Boolean
If sw1 is 6c, returns the correct length from sw2. A value of 0 means there are more than 256 bytes remaining.
- Returns
Number
An object offering general commands to most ISO7816 compliant smart cards.
Sets up the Iso7816Application
object
- card
Card
: The card to communicate with using ISO7816 standards
Sends the provided command to the card. Automatically retrieve the full response, even if it requires multiple GET_RESPONSE commands
- commandApdu
CommandApdu
: Command to send to the card
Returns
ResponseApdu
Complete response from card
Sends the SELECT command to the card, often called selecting an application
- bytes
Buffer
: The resource locater (AID, etc) - p1
Number
: Value to specify as the p1 value - p2
Number
: Value to specify as the p2 value
Returns
ResponseApdu
Complete response from card
Sends a single GET_RESPONSE command to the card
- length
Number
: The length of the response expected, maximum is 0xFF
Returns
ResponseApdu
Complete response from card
Sends a READ_RECORD command to the card
- sfi
Number
: The sfi - record
Number
: The record
Returns
ResponseApdu
Complete response from card
Sends a GET_DATA command to the card
- p1
Number
: Value to specify as the p1 value - p2
Number
: Value to specify as the p2 value
Returns
ResponseApdu
Complete response from card
The Iso7816Application
class emits the following events
Emitted when a successful reply to a selectFile()
command is received.
Returns Object
:
- application
String
'use strict';
const smartcard = require('smartcard');
const Devices = smartcard.Devices;
const devices = new Devices();
devices.on('device-activated', (event => {
console.log(`Device '${event.device}' activated`);
event.devices.map((device, index) => {
console.log(`Device #${index + 1}: '${device.name}'`);
});
}));
'use strict';
const smartcard = require('smartcard');
const Devices = smartcard.Devices;
const devices = new Devices();
devices.onActivated().then(event => {
console.log(`Device '${event.device}' activated`);
event.devices.map((device, index) => {
console.log(`Device #${index + 1}: '${device.name}'`);
});
});
'use strict';
const smartcard = require('smartcard');
const Devices = smartcard.Devices;
const Iso7816Application = smartcard.Iso7816Application;
const devices = new Devices();
devices.on('device-activated', event => {
const currentDevices = event.devices;
let device = event.device;
console.log(`Device '${device}' activated, devices: ${currentDevices}`);
for (let prop in currentDevices) {
console.log("Devices: " + currentDevices[prop]);
}
device.on('card-inserted', event => {
let card = event.card;
console.log(`Card '${card.getAtr()}' inserted into '${event.device}'`);
card.on('command-issued', event => {
console.log(`Command '${event.command}' issued to '${event.card}' `);
});
card.on('response-received', event => {
console.log(`Response '${event.response}' received from '${event.card}' in response to '${event.command}'`);
});
const application = new Iso7816Application(card);
application.selectFile([0x31, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31])
.then(response => {
console.info(`Select PSE Response: '${response}' '${response.meaning()}'`);
}).catch(error => {
console.error('Error:', error, error.stack);
});
});
device.on('card-removed', event => {
console.log(`Card removed from '${event.name}' `);
});
});
devices.on('device-deactivated', event => {
console.log(`Device '${event.device}' deactivated, devices: [${event.devices}]`);
});