Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

PCSC smartcard reader library for nodejs

License

NotificationsYou must be signed in to change notification settings

tomkp/smartcard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smartcard library.

This is a simple wrapper aroundSantiago Gimeno's greatpcsclite library.

Used byCard Spy

API

The following objects are defined by thesmartcard library, each contains its own set of methods and events.

Class: Devices

A general object that provides access to all smartcard related devices.

Events

Thedevices object emits the following events

Event: 'device-activated'

Emitted when a card reader is attached.Returns:

  • Object
    • Device
    • Array: List of all devices, returned viadevices.listDevices()
Event: 'device-deactivated'

Emitted when a card reader is detached.Returns:

  • Object
    • Device
    • Array: List of all devices, returned viadevices.listDevices()
Event: 'error'

Emitted when an error occursReturnsObject:

  • errorError

Methods

The following methods are available within thedevices class.

Constructor

The constructor for a devices object takes no arguments,

devices=newDevices();
devices.onActivated()

ReturnsPromise

  • Resolves with activationevent
devices.onDeactivated()

ReturnsPromise

  • Resolves with deactivationevent
devices.listDevices()

ReturnsObject a list of the different devices attached, each adevice object

devices.lookup(name)
  • nameString: The text name of a device

  • ReturnsDevice

Class: Device

An object representing a specific card reader (device).

Methods

The following methods are available within thedevice class.

device.getName()

Returns the name of the attached device.

device.transmit(data, res_len, protocol, cb)

Sends a command to the connected device

  • dataBuffer: data to be transmitted
  • res_lenNumber: Maximum length of the expected response, includes the 2 byte response (sw1 and sw2)
  • protocolNumber: Protocol to be used in the transmission
  • cb(error,response)Function: Called when transmit function completes
    • errorError
    • outputBuffer

Events

Thedevice object emits the following events

Event: 'card-inserted'

Emitted when a smartcard is inserted into a card reader

ReturnsObject:

  • deviceDevice
  • cardCard
Event: 'card-removed'

Emitted when a smartcard is removed from a card reader

ReturnsObject:

  • nameString
  • cardCard

Class: Card

An object representing an attached smart card.

Methods

The following methods are available within thecard class.

card.getAtr()

ReturnsString containing the atr of the card

card.issueCommand(commandApdu, callback)

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
    • errorError
    • responseBuffer

ReturnsPromise

  • Resolves withresponseBuffer
  • Rejects witherrorError

If no callback is specified, returns aPromise*

Events

Thecard object emits the following events

Event: 'command-issued'

Emitted when a command is sent to the smartcard.

ReturnsObject:

  • cardCard
  • commandBuffer
Event: 'response-received'

Emitted when a response is received from the card.

ReturnsObject:

  • cardCard
  • commandBuffer
  • responseResponseApdu

Class: CommandApdu

An object representing a command to send to a smart card

Methods

TheCommandApdu class has the following methods.

ConstructorCommandApdu(obj)

Creates a new instance and sets the appropriate items

  • objObject
    • claNumber: The class of the command, typically 0
    • insNumber: The instruction
    • p1Number: The value of p1
    • p2Number: The value of p2
    • dataArray (optional): The value of data
    • leNumber (optional): The value of le

OR

  • objArray: Byte array representing the whole command
CommandApdu.toBuffer()

Converts the command to a Buffer

  • ReturnsBuffer
CommandApdu.toString()

Converts the command to a hex string

  • ReturnsString
CommandApdu.toByteArray()

Converts the command to a byte array

  • ReturnsArray
CommandApdu.setLe(le)

Updates the le value of the command

  • leNumber: The new le value

Class: ResponseApdu

Class representing a response from the card

Methods

TheResponseApdu class has the following methods.

Constructor
ResponseApdu.meaning()

Interprets the return code and attempts to provide a text translation.

  • ReturnsString
ResponseApdu.getDataOnly()

Returns the response data without including the status code

  • ReturnsString
ResponseApdu.getStatusCode()

Returns only the status code

  • ReturnsString
ResponseApdu.isOk()

Check if the status code is 9000

  • ReturnsBoolean
ResponseApdu.buffer()

Returns the whole buffer, status code and data

  • ReturnsBuffer
ResponseApdu.hasMoreBytesAvailable()

Reads the status code and looks for a 61 as sw1, meaning more data is available

  • ReturnsBoolean
ResponseApdu.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.

  • ReturnsNumber
ResponseApdu.isWrongLength()

Checks status code for 6c as sw1

  • ReturnsBoolean
ResponseApdu.correctLength()

If sw1 is 6c, returns the correct length from sw2. A value of 0 means there are more than 256 bytes remaining.

  • ReturnsNumber

Class: Iso7816Application

An object offering general commands to most ISO7816 compliant smart cards.

Methods

ConstructorIso7816Application(card)

Sets up theIso7816Application object

  • cardCard: The card to communicate with using ISO7816 standards
Iso7816Application.issueCommand(commandApdu)

Sends the provided command to the card. Automatically retrieve the full response, even if it requires multiple GET_RESPONSE commands

  • commandApduCommandApdu: Command to send to the card

Returns

  • ResponseApdu Complete response from card
Iso7816Application.selectFile(bytes, p1, p2)

Sends the SELECT command to the card, often called selecting an application

  • bytesBuffer: The resource locater (AID, etc)
  • p1Number: Value to specify as the p1 value
  • p2Number: Value to specify as the p2 value

Returns

  • ResponseApdu Complete response from card
Iso7816Application.getResponse(length)

Sends a single GET_RESPONSE command to the card

  • lengthNumber: The length of the response expected, maximum is 0xFF

Returns

  • ResponseApdu Complete response from card
Iso7816Application.getResponse(sfi,record)

Sends a READ_RECORD command to the card

  • sfiNumber: The sfi
  • recordNumber: The record

Returns

  • ResponseApdu Complete response from card
Iso7816Application.getData(p1, p2)

Sends a GET_DATA command to the card

  • p1Number: Value to specify as the p1 value
  • p2Number: Value to specify as the p2 value

Returns

  • ResponseApdu Complete response from card

Events

TheIso7816Application class emits the following events

Event: 'application-selected'

Emitted when a successful reply to aselectFile() command is received.

ReturnsObject:

  • applicationString

Examples

With event emitter

'use strict';constsmartcard=require('smartcard');constDevices=smartcard.Devices;constdevices=newDevices();devices.on('device-activated',(event=>{console.log(`Device '${event.device}' activated`);event.devices.map((device,index)=>{console.log(`Device #${index+1}: '${device.name}'`);});}));

Using promises

'use strict';constsmartcard=require('smartcard');constDevices=smartcard.Devices;constdevices=newDevices();devices.onActivated().then(event=>{console.log(`Device '${event.device}' activated`);event.devices.map((device,index)=>{console.log(`Device #${index+1}: '${device.name}'`);});});

Selecting the Payment Systems Environment on an EMV (Chip & Pin) card

'use strict';constsmartcard=require('smartcard');constDevices=smartcard.Devices;constIso7816Application=smartcard.Iso7816Application;constdevices=newDevices();devices.on('device-activated',event=>{constcurrentDevices=event.devices;letdevice=event.device;console.log(`Device '${device}' activated, devices:${currentDevices}`);for(letpropincurrentDevices){console.log("Devices: "+currentDevices[prop]);}device.on('card-inserted',event=>{letcard=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}'`);});constapplication=newIso7816Application(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}]`);});

[8]ページ先頭

©2009-2025 Movatter.jp