mirror of
https://github.com/sharevb/it-tools.git
synced 2026-12-01 01:14:13 +03:00
Decode a Matter onboarding payload (the MT:... text of a Matter QR code) into its components: 11-digit manual pairing code (and the 21-digit code for non-standard commissioning flows), passcode and its validity, discriminator and short discriminator, vendor and product IDs, commissioning flow, discovery capabilities, payload version, raw bytes and the optional TLV data. QR codes holding several devices (payloads joined with '*') are decoded one card per device. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: sharevb <sharevb@gmail.com>
135 lines
4.7 KiB
TypeScript
135 lines
4.7 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
base38Decode,
|
|
base38Encode,
|
|
computeManualPairingCode,
|
|
decodeMatterQrPayload,
|
|
encodeMatterQrPayload,
|
|
formatManualPairingCode,
|
|
isValidPasscode,
|
|
verhoeffCheckDigit,
|
|
} from './matter-qr-code-decoder.service';
|
|
|
|
// Default payloads of the Matter SDK example devices.
|
|
const SDK_EXAMPLE = 'MT:-24J0AFN00KA0648G00';
|
|
const SDK_EXAMPLE_BLE = 'MT:Y.K9042C00KA0648G00';
|
|
const SDK_EXAMPLE_FIELDS = {
|
|
vendorId: 0xfff1,
|
|
productId: 0x8001,
|
|
commissioningFlow: 0,
|
|
discoveryCapabilities: 4,
|
|
discriminator: 3840,
|
|
passcode: 20202021,
|
|
};
|
|
|
|
describe('matter-qr-code-decoder', () => {
|
|
it('decodes the Matter SDK example payload', () => {
|
|
const [payload] = decodeMatterQrPayload(SDK_EXAMPLE);
|
|
expect(payload).toMatchObject({
|
|
version: 0,
|
|
vendorId: 0xfff1,
|
|
productId: 0x8001,
|
|
commissioningFlow: 0,
|
|
commissioningFlowName: 'Standard',
|
|
discoveryCapabilities: 4,
|
|
discoveryCapabilityNames: ['On IP network'],
|
|
discriminator: 3840,
|
|
shortDiscriminator: 15,
|
|
passcode: 20202021,
|
|
isPasscodeValid: true,
|
|
manualPairingCode: '34970112332',
|
|
longManualPairingCode: null,
|
|
optionalData: [],
|
|
rawHex: '88FF0F008400E04B846802',
|
|
warnings: [],
|
|
});
|
|
expect(decodeMatterQrPayload(SDK_EXAMPLE_BLE)[0]).toMatchObject({
|
|
productId: 0x8000,
|
|
discoveryCapabilityNames: ['BLE'],
|
|
manualPairingCode: '34970112332',
|
|
});
|
|
});
|
|
|
|
it('accepts lowercase input and surrounding whitespace', () => {
|
|
expect(decodeMatterQrPayload(` ${SDK_EXAMPLE.toLowerCase()}\n`)[0].passcode).toBe(20202021);
|
|
});
|
|
|
|
it('decodes concatenated payloads', () => {
|
|
const second = encodeMatterQrPayload({
|
|
vendorId: 0x1234,
|
|
productId: 0x5678,
|
|
commissioningFlow: 0,
|
|
discoveryCapabilities: 4,
|
|
discriminator: 0x123,
|
|
passcode: 12345679,
|
|
});
|
|
const payloads = decodeMatterQrPayload(`${SDK_EXAMPLE}*${second.slice(3)}`);
|
|
expect(payloads).toHaveLength(2);
|
|
expect(payloads[1]).toMatchObject({ vendorId: 0x1234, productId: 0x5678, discriminator: 0x123 });
|
|
});
|
|
|
|
it('round-trips encode and decode', () => {
|
|
const fields = {
|
|
vendorId: 0xabcd,
|
|
productId: 0x0042,
|
|
commissioningFlow: 2,
|
|
discoveryCapabilities: 0b111,
|
|
discriminator: 0xabc,
|
|
passcode: 99999998,
|
|
};
|
|
expect(encodeMatterQrPayload(SDK_EXAMPLE_FIELDS)).toBe(SDK_EXAMPLE);
|
|
const [payload] = decodeMatterQrPayload(encodeMatterQrPayload(fields));
|
|
expect(payload).toMatchObject({
|
|
...fields,
|
|
commissioningFlowName: 'Custom',
|
|
discoveryCapabilityNames: ['SoftAP', 'BLE', 'On IP network'],
|
|
});
|
|
// 10 digits of discriminator/passcode, vendor ID, product ID, check digit
|
|
expect(payload.longManualPairingCode).toBe('641214610343981000666');
|
|
expect(formatManualPairingCode(payload.longManualPairingCode!)).toBe('6412-146-1034-39810-00666');
|
|
});
|
|
|
|
it('parses optional TLV data', () => {
|
|
const fixed = base38Decode(SDK_EXAMPLE.slice(3));
|
|
const serial = new TextEncoder().encode('SN-001');
|
|
const tlv = [0x15, 0x2c, 0x00, serial.length, ...serial, 0x24, 0x80, 0x2a, 0x18];
|
|
const [payload] = decodeMatterQrPayload(`MT:${base38Encode(Uint8Array.from([...fixed, ...tlv]))}`);
|
|
expect(payload.optionalData).toEqual([
|
|
{ tag: 'Context 0x00', tagNumber: 0, name: 'Serial number', type: 'UTF-8 string', value: 'SN-001' },
|
|
{
|
|
tag: 'Context 0x80',
|
|
tagNumber: 0x80,
|
|
name: 'Vendor-specific (0x80)',
|
|
type: 'Unsigned int (8 bits)',
|
|
value: '42',
|
|
},
|
|
]);
|
|
});
|
|
|
|
it('reports invalid passcodes and versions', () => {
|
|
const [payload] = decodeMatterQrPayload(
|
|
encodeMatterQrPayload({ ...SDK_EXAMPLE_FIELDS, version: 1, passcode: 12345678 }),
|
|
);
|
|
expect(payload.isPasscodeValid).toBe(false);
|
|
expect(payload.warnings).toHaveLength(2);
|
|
});
|
|
|
|
it('rejects malformed input', () => {
|
|
expect(() => decodeMatterQrPayload('')).toThrow();
|
|
expect(() => decodeMatterQrPayload('XX:-24J0AFN00KA0648G00')).toThrow(/MT:/);
|
|
expect(() => decodeMatterQrPayload('MT:')).toThrow();
|
|
expect(() => decodeMatterQrPayload('MT:-24J0AFN00KA0648G0')).toThrow(/length/);
|
|
expect(() => decodeMatterQrPayload('MT:-24J0AFN00KA0648G0_')).toThrow(/character/);
|
|
expect(() => decodeMatterQrPayload('MT:-24J0')).toThrow(/too short/);
|
|
});
|
|
|
|
it('computes manual pairing codes', () => {
|
|
expect(computeManualPairingCode({ discriminator: 3840, passcode: 20202021 })).toBe('34970112332');
|
|
expect(formatManualPairingCode('34970112332')).toBe('3497-011-2332');
|
|
expect(verhoeffCheckDigit('236')).toBe(3);
|
|
expect(isValidPasscode(20202021)).toBe(true);
|
|
expect(isValidPasscode(11111111)).toBe(false);
|
|
expect(isValidPasscode(99999999)).toBe(false);
|
|
});
|
|
});
|