UNPKG

tickethead-sdk

Version:

SDK for the Tickethead API

187 lines 8.43 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.encodeQrPayload = encodeQrPayload; exports.decodeQrPayload = decodeQrPayload; exports.createQrPayloadForTicket = createQrPayloadForTicket; exports.createQrPayloadForTicketAsync = createQrPayloadForTicketAsync; exports.qrToTicketInvalidateCall = qrToTicketInvalidateCall; exports.qrToTicketInvalidateCallAsync = qrToTicketInvalidateCallAsync; exports.qrToTicketData = qrToTicketData; exports.qrToTicketDataAsync = qrToTicketDataAsync; 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}`; } /** * 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, }; } /** * Signs and encodes ticket data into the string which will be put in the QR code * * @deprecated use createQrPayloadForTicketAsync for 35-45x increased performance * @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); } /** * Signs and encodes ticket data into the string which will be put in the QR code. * Uses native library for speedup, if available. * * @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 createQrPayloadForTicketAsync(eventId, ticketConfigId, sequenceNumber, privateKey) { return __awaiter(this, void 0, void 0, function* () { // Encode the ticket ID const ticketId = `E${eventId}TC${ticketConfigId}T${sequenceNumber}`; const payload = JSON.stringify({ id: ticketId, }); const signature = yield (0, certificate_1.signWithFallback)(payload, privateKey); return encodeQrPayload(payload, signature); }); } /** * Decodes, verifies the signature of the QR code payload * and constructs the blockchain function call to invalidate the ticket. * * @deprecated Use qrToTicketInvalidateCallAsync instead, since it will be 150x faster if it can use the native `crypto` library * @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); } /** * 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 qrToTicketInvalidateCallAsync(qrCodePayload, invalidationDate, publicKey) { return __awaiter(this, void 0, void 0, function* () { const qrContent = decodeQrPayload(qrCodePayload); if (publicKey) { const signatureValid = yield (0, certificate_1.verifyWithFallback)(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); }); } /** * Decodes and verifies the signature of the QR code payload and returns the IDs * * @deprecated Use `qrToTicketDataAsync` for speedup * @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); } /** * Decodes and verifies the signature of the QR code payload and returns the IDs. * Uses native libraries for speedup, if available. * * @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 qrToTicketDataAsync(qrCodePayload, publicKey) { return __awaiter(this, void 0, void 0, function* () { const qrContent = decodeQrPayload(qrCodePayload); if (publicKey) { const signatureValid = yield (0, certificate_1.verifyWithFallback)(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); }); } //# sourceMappingURL=qr.js.map