
Security News
PyPI Expands Trusted Publishing to GitLab Self-Managed as Adoption Passes 25 Percent
PyPI adds Trusted Publishing support for GitLab Self-Managed as adoption reaches 25% of uploads
smartcard2
Advanced tools
Smartcard library.
This is a simple wrapper around Santiago Gimeno's great pcsclite library.
Used by Card Spy
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
DeviceArray: List of all devices, returned via devices.listDevices()Emitted when a card reader is detached. Returns:
Object
DeviceArray: List of all devices, returned via devices.listDevices()Emitted when an error occurs
Returns Object:
ErrorThe following methods are available within the devices class.
The constructor for a devices object takes no arguments,
devices = new Devices();
devices.onActivated()Returns Promise
devices.onDeactivated()Returns Promise
devices.listDevices()Returns Object a list of the different devices attached, each a device object
devices.lookup(name)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.
device.getName()Returns the name of the attached device.
device.transmit(data, res_len, protocol, cb)Sends a command to the connected device
Buffer: data to be transmittedNumber: Maximum length of the expected response, includes the 2 byte response (sw1 and sw2)Number: Protocol to be used in the transmissionFunction: Called when transmit function completes
ErrorBufferThe device object emits the following events
Emitted when a smartcard is inserted into a card reader
Returns Object:
DeviceCardEmitted when a smartcard is removed from a card reader
Returns Object:
StringCardAn object representing an attached smart card.
The following methods are available within the card class.
card.getAtr()Returns String containing the atr of the card
card.issueCommand(commandApdu, callback)Sends a command to the card
StringBufferArrayCommandApduErrorBufferReturns Promise
BufferErrorIf 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:
CardBufferEmitted when a response is received from the card.
Returns Object:
CardBufferResponseApduAn object representing a command to send to a smart card
The CommandApdu class has the following methods.
CommandApdu(obj)Creates a new instance and sets the appropriate items
Object
Number: The class of the command, typically 0Number: The instructionNumber: The value of p1Number: The value of p2Array (optional): The value of dataNumber (optional): The value of leOR
Array: Byte array representing the whole commandCommandApdu.toBuffer()Converts the command to a Buffer
BufferCommandApdu.toString()Converts the command to a hex string
StringCommandApdu.toByteArray()Converts the command to a byte array
ArrayCommandApdu.setLe(le)Updates the le value of the command
Number: The new le valueClass representing a response from the card
The ResponseApdu class has the following methods.
ResponseApdu.meaning()Interprets the return code and attempts to provide a text translation.
StringResponseApdu.getDataOnly()Returns the response data without including the status code
StringResponseApdu.getStatusCode()Returns only the status code
StringResponseApdu.isOk()Check if the status code is 9000
BooleanResponseApdu.buffer()Returns the whole buffer, status code and data
BufferResponseApdu.hasMoreBytesAvailable()Reads the status code and looks for a 61 as sw1, meaning more data is available
BooleanResponseApdu.numberOfBytesAvailable()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.
NumberResponseApdu.isWrongLength()Checks status code for 6c as sw1
BooleanResponseApdu.correctLength()If sw1 is 6c, returns the correct length from sw2. A value of 0 means there are more than 256 bytes remaining.
NumberAn object offering general commands to most ISO7816 compliant smart cards.
Iso7816Application(card)Sets up the Iso7816Application object
Card: The card to communicate with using ISO7816 standardsIso7816Application.issueCommand(commandApdu)Sends the provided command to the card. Automatically retrieve the full response, even if it requires multiple GET_RESPONSE commands
CommandApdu: Command to send to the cardReturns
ResponseApdu Complete response from cardIso7816Application.selectFile(bytes, p1, p2)Sends the SELECT command to the card, often called selecting an application
Buffer: The resource locater (AID, etc)Number: Value to specify as the p1 valueNumber: Value to specify as the p2 valueReturns
ResponseApdu Complete response from cardIso7816Application.getResponse(length)Sends a single GET_RESPONSE command to the card
Number: The length of the response expected, maximum is 0xFFReturns
ResponseApdu Complete response from cardIso7816Application.getResponse(sfi,record)Sends a READ_RECORD command to the card
Number: The sfiNumber: The recordReturns
ResponseApdu Complete response from cardIso7816Application.getData(p1, p2)Sends a GET_DATA command to the card
Number: Value to specify as the p1 valueNumber: Value to specify as the p2 valueReturns
ResponseApdu Complete response from cardThe Iso7816Application class emits the following events
Emitted when a successful reply to a selectFile() command is received.
Returns Object:
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}]`);
});
FAQs
Unknown package
The npm package smartcard2 receives a total of 0 weekly downloads. As such, smartcard2 popularity was classified as not popular.
We found that smartcard2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
PyPI adds Trusted Publishing support for GitLab Self-Managed as adoption reaches 25% of uploads

Research
/Security News
A malicious Chrome extension posing as an Ethereum wallet steals seed phrases by encoding them into Sui transactions, enabling full wallet takeover.

Security News
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.