pcsc-mini - v0.1.2
    Preparing search index...

    Class Card

    Represents a connection to an inserted smart card.

    import * as pcsc from "pcsc-mini";
    const { CardDisposition, CardMode, ReaderStatus } = pcsc;

    const client = new pcsc.Client()
    .on("reader", onReader)
    .start();

    function onReader(reader: pcsc.Reader) {
    reader.on("change", async status => {
    if (!status.has(ReaderStatus.PRESENT)) return;
    if (status.hasAny(ReaderStatus.MUTE, ReaderStatus.IN_USE)) return;

    const card = await reader.connect(CardMode.EXCLUSIVE);
    console.log(`${await card.state()}`);

    const resTx = await card.transmit(
    Uint8Array.of(0xca, 0xfe, 0xf0, 0x0d)
    );
    console.log(resTx);

    const codeFeatures = pcsc.controlCode(3400);
    const resCtrl = await card.control(codeFeatures);
    console.log(resCtrl);

    await card.disconnect(CardDisposition.RESET);
    client.stop();
    process.exit(0);
    });
    }
    Index

    Constructors

    Methods

    • Gets an attribute from the IFD Handler.

      Parameters

      • id: number

        ID of the requested attribute. attributes.ids provides a non-exhaustive list of available attribute IDs that may be useful.

      • OptionaloutputBuffer: ArrayBuffer

        Backing buffer for the response. If omitted, a new buffer will be allocated with enough space for the response.

      Returns Promise<Uint8Array<ArrayBufferLike>>

      The attribute value response.

      Err

    • Sets an attribute of the IFD Handler.

      Parameters

      • id: number

        ID of the requested attribute. attributes.ids provides a non-exhaustive list of available attribute IDs that may be useful.

      • value: Uint8Array

        The new byte data value of the attribute.

      Returns Promise<void>

      Err

    • Sends a command directly to the IFD Handler (reader driver).

      This is useful for creating client side reader drivers for functions like PIN pads, biometrics, or other extensions to the normal smart card reader that are not normally handled by PC/SC.

      import * as pcsc from "pcsc-mini";

      const IOCTL_GET_FEATURE_REQUEST = pcsc.controlCode(3400);

      function getFeatures(card: pcsc.Card): Promise<Uint8Array> {
      return card.control(IOCTL_GET_FEATURE_REQUEST);
      }

      Parameters

      • code: number

        The card reader device control code. For cross platform compatibility, use controlCode to construct the control code.

      • Optionalcommand: Uint8Array<ArrayBufferLike>

        Control command data, if any.

      • OptionaloutputBuffer: ArrayBuffer

        Backing buffer for the response. If omitted, a new buffer will be allocated with enough space for the response.

      Returns Promise<Uint8Array<ArrayBufferLike>>

      The response data from the reader.

      Err

    • Closes the connection to the card, rendering this instance invalid.

      Parameters

      Returns Promise<void>

      Err

    • The currently active card communication protocol.

      This is set when the card connection is first established and updated after reconnect calls. It will not be updated if/when a protocol is negotiated via control.

      Returns Protocol

      Err

    • Attempts to re-establish a previously reset connection.

      If connected in CardMode.SHARED, another process may reset the card, causing successive commands to return an error. In those cases, calling reconnect will restore the connection accordingly.

      Note

      For the sake of portability across platforms, the reconnect request is sent with the previously active protocol. To change protocols, first disconnect and then establish a new connection.

      Parameters

      • mode: CardMode

        Access mode for the restored connection.

      • initAction: CardDisposition

        Action to take on the card when reconnecting.

      Returns Promise<Protocol>

      • The active protocol after reconnection.

      Err

    • Establishes a temporary CardMode.EXCLUSIVE connection to the card, if originally connected with CardMode.SHARED, creating a lock for issuing multiple commands without interruption from other processes.

      Important

      Must be followed up with a matching call to Transaction.end() when done with the session.

      import * as pcsc from "pcsc-mini";

      async function longRunningProcess(card: pcsc.Card) {
      const txn = card.transaction();

      try {
      const r1 = await card.transmit(Uint8Array.of(0xca, 0xfe, 0xf0, 0x0d));
      const r2 = await card.transmit(Uint8Array.of(0xfa, 0xde, 0xfa, 0xce));

      return processResults(r1, r2);
      } catch (err) {
      console.error("Something went wrong:", err);
      } finally {
      txn.end(pcsc.CardDisposition.LEAVE);
      }
      }

      Returns Promise<Transaction>

      A handle to the newly active transaction.

      Err

    • Sends an APDU to the card.

      import { Buffer } from "node:buffer";
      import * as pcsc from "pcsc-mini";

      async function getData(card: pcsc.Card) {
      const buf = new ArrayBuffer(pcsc.MAX_BUFFER_LEN);

      try {
      const res1 = await card.transmit(Uint8Array.of(0xca, 0xfe), buf);
      const foo = processRes1(res1);

      const res2 = await card.transmit(Buffer.of(0xf0, 0x0d), buf);
      const bar = processRes2(res2);

      return processResults(foo, bar);
      } catch (err) {
      console.error("Something went wrong:", err);
      return null;
      }
      }

      Parameters

      • input: Uint8Array

        The APDU byte data.

      • OptionaloutputBuffer: ArrayBuffer

        Backing buffer for the response. If omitted, a new buffer will be allocated with enough space for the response.

      Returns Promise<Uint8Array<ArrayBufferLike>>

      The response data from the card.

      Err

    • Sends an APDU to the card with a specific protocol. Useful for CardMode.DIRECT connections, when a specific protocol has not yet been negotiated.

      import { Buffer } from "node:buffer";
      import * as pcsc from "pcsc-mini";

      async function getData(card: pcsc.Card) {

      try {
      const foo = await card.transmit(
      pcsc.Protocol.T1,
      Uint8Array.of(0xca, 0xfe),
      );

      const bar = await card.transmit(
      pcsc.Protocol.T1,
      Buffer.of(0xf0, 0x0d),
      );

      return processResults(foo, bar);
      } catch (err) {
      console.error("Something went wrong:", err);
      return null;
      }
      }

      Parameters

      • protocol: Protocol
      • input: Uint8Array

        The APDU byte data.

      • OptionaloutputBuffer: ArrayBuffer

        Backing buffer for the response. If omitted, a new buffer will be allocated with enough space for the response.

      Returns Promise<Uint8Array<ArrayBufferLike>>

      The response data from the card.

      Err