UNPKG

bam-ticketing-sdk

Version:

SDK for B.A.M Ticketing API

106 lines 4.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.qrToTicketData = exports.qrToTicketInvalidateCall = exports.createQrPayloadForTicket = exports.decodeQrPayload = exports.encodeQrPayload = void 0; const base45_1 = __importDefault(require("base45")); const utils_1 = require("../blockchain/utils"); const certificate_1 = require("../utils/certificate"); const __1 = require(".."); /** * Encodes the payload and the signature into the expected QR code payload * * @param payload JSON stringified payload * @param signatureHex Hex encoded signature of the payload * @returns base 45 encoded string */ function encodeQrPayload(payload, signatureHex) { const payloadBuffer = Buffer.from(payload); const signatureBuffer = Buffer.from(signatureHex, 'hex'); const payloadEncoded = base45_1.default.encode(payloadBuffer); const signatureEncoded = base45_1.default.encode(signatureBuffer); return `${payloadEncoded}:::${signatureEncoded}`; } exports.encodeQrPayload = encodeQrPayload; /** * Decode a base 45 encoded QR code payload * * @param encodedPayload what you scanned from the QR code * @returns payload and its signature */ function decodeQrPayload(encodedPayload) { const [payloadEncoded, signatureEncoded] = encodedPayload.split(':::'); const payloadBuffer = base45_1.default.decode(payloadEncoded); const signatureBuffer = base45_1.default.decode(signatureEncoded); const payload = payloadBuffer.toString(); const signature = signatureBuffer.toString('hex'); return { payload, signature, }; } exports.decodeQrPayload = decodeQrPayload; /** * Signs and encodes ticket data into the string which will be put in the QR code * * @param eventId event to which the ticket belongs * @param ticketConfigId ticket config of the ticket * @param sequenceNumber the serial number of the ticket * @param privateKey the private key which is used to sign the ticket * @returns string to display as a tickets QR code */ function createQrPayloadForTicket(eventId, ticketConfigId, sequenceNumber, privateKey) { // Encode the ticket ID const ticketId = `E${eventId}TC${ticketConfigId}T${sequenceNumber}`; const payload = JSON.stringify({ id: ticketId, }); const signature = (0, certificate_1.sign)(payload, privateKey); return encodeQrPayload(payload, signature); } exports.createQrPayloadForTicket = createQrPayloadForTicket; /** * Decodes, verifies the signature of the QR code payload and constructs the blockchain * function call to invalidate the ticket * * @param qrCodePayload content of the QR code * @param invalidationDate the time at which the ticket was scanned * @param publicKey key used to verify the ticket signature, if specified * @returns blockchain function call for ticket invalidation * @throws Error if the signature does not match the payload */ function qrToTicketInvalidateCall(qrCodePayload, invalidationDate, publicKey) { const qrContent = decodeQrPayload(qrCodePayload); if (publicKey) { const signatureValid = (0, certificate_1.verify)(qrContent.payload, qrContent.signature, publicKey); if (!signatureValid) { throw new Error('Invalid ticket signature'); } } const payload = JSON.parse(qrContent.payload); const args = (0, __1.createInvalidatePayload)(payload.id, invalidationDate); return (0, __1.createInvalidateCall)(args); } exports.qrToTicketInvalidateCall = qrToTicketInvalidateCall; /** * Decodes and verifies the signature of the QR code payload and returns the IDs * * @param qrCodePayload content of the QR code * @param publicKey key used to verify the ticket signature, if specified * @returns ticket IDs * @throws Error if the signature does not match the payload */ function qrToTicketData(qrCodePayload, publicKey) { const qrContent = decodeQrPayload(qrCodePayload); if (publicKey) { const signatureValid = (0, certificate_1.verify)(qrContent.payload, qrContent.signature, publicKey); if (!signatureValid) { throw new Error('Invalid ticket signature'); } } const payload = JSON.parse(qrContent.payload); return (0, utils_1.parseTicketId)(payload.id); } exports.qrToTicketData = qrToTicketData; //# sourceMappingURL=qr.js.map