Skip to content

State machine

card.createT0Machine tracks T=0 command boundaries for you — mounted straight onto a Communicator, it turns raw bytes into parsed ApduCommands and sends back whatever you return.

js
import {link, protocol, card} from 'yolocard-sdk';

// TS T0 [historical: 80 31 e0 73], no interface bytes, T=0 only (no TCK).
const ATR = '3B 04 80 31 E0 73';

const INS_SELECT = 0xa4;
const SW_OK = [0x90, 0x00];
const SW_INS_NOT_SUPPORTED = [0x6d, 0x00];

const comm = protocol.createCommunicator(await link.requestUsbTransport());

const ctx = {selectCount: 0};
const t0 = card.createT0Machine({
    onCommand({cla, ins, p1, p2, data}, ctx) {
        console.log(`C-APDU  ${card.hex([cla, ins, p1, p2])}  ${card.hex(data)}`);

        if (ins === INS_SELECT) {
            ctx.selectCount++;
            return SW_OK;
        }
        return SW_INS_NOT_SUPPORTED;
    },

    onColdReset(ctx) {
        console.log('Cold reset');
        ctx.selectCount = 0;
    },

    onWarmReset(ctx) {
        console.log('Warm reset');
    },
}, ctx);

t0.mount(comm);
await comm.connect(); // may raise Firmware Unsupported
await comm.setAtr(ATR);
// Ready to insert!

// comm.disconnect()
Console output appears here. Needs Chromium and the YoloCard plugged in.

No Le/GET RESPONSE dance — onCommand answers with whatever bytes go back (data + SW1SW2), sent as-is.