UNPKG

@sphereon/ssi-sdk.qr-code-generator

Version:

QR Code provider (react)

109 lines • 6.11 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QrCodeProvider = void 0; const utils_1 = require("./utils"); const react_qr_1 = require("./utils/react-qr"); /** * {@inheritDoc IQRCodeGenerator} */ class QrCodeProvider { constructor() { this.methods = { qrDIDCommOobInvitationElement: QrCodeProvider.qrDIDCommOobInvitationElement.bind(this), qrDIDCommOobInvitationValue: QrCodeProvider.qrDIDCommOobInvitationValue.bind(this), qrSIOPv2Element: QrCodeProvider.qrSIOPv2Element.bind(this), qrSIOPv2Value: QrCodeProvider.qrSIOPv2Value.bind(this), qrURIElement: QrCodeProvider.qrURIElement.bind(this), qrOpenID4VCIElement: QrCodeProvider.qrOpenID4VCIElement.bind(this), qrOpenID4VCIValue: QrCodeProvider.qrOpenID4VCIValue.bind(this), }; } /** {@inheritDoc IQRCodeGenerator.uriElement} */ static qrURIElement(args, context) { return __awaiter(this, void 0, void 0, function* () { return (0, utils_1.generateQRCodeReactElement)(args, context); }); } /** {@inheritDoc IQRCodeGenerator.didCommOobInvitationValue} */ static qrDIDCommOobInvitationValue(args, context) { return __awaiter(this, void 0, void 0, function* () { const { object } = args.data; const encoded = utils_1.DidCommOutOfBandMessage.urlEncode(object.oobInvitation); const delim = object.baseURI.includes('?') ? '&' : '?'; return object.baseURI.includes('oob=') ? object.baseURI.replace('oob=', `oob=${encoded}`) : `${object.baseURI}${delim}_oob=${encoded}`; }); } /** {@inheritDoc IQRCodeGenerator.didCommOobInvitationElement} */ static qrDIDCommOobInvitationElement(args, context) { return __awaiter(this, void 0, void 0, function* () { const content = yield QrCodeProvider.qrDIDCommOobInvitationValue(args, context); return (0, react_qr_1.generateQRCodeReactElementImpl)(Object.assign(Object.assign({}, args), { data: Object.assign(Object.assign({}, args.data), { object: content }) }), args, context); }); } /** {@inheritDoc IQRCodeGenerator.siopv2Value} */ static qrSIOPv2Value(args, context) { return __awaiter(this, void 0, void 0, function* () { var _a; const { object } = args.data; if (typeof object === 'string') { return object; } const scheme = ((_a = object.scheme) !== null && _a !== void 0 ? _a : 'openid-vc://').replace('://?', '').replace('://', '') + '://'; const requestUri = `request_uri=${object.requestUri.replace('request_uri=', '')}`; return `${scheme}?${requestUri}`; }); } /** {@inheritDoc IQRCodeGenerator.siopv2Element} */ static qrSIOPv2Element(args, context) { return __awaiter(this, void 0, void 0, function* () { const content = yield QrCodeProvider.qrSIOPv2Value(args, context); return (0, react_qr_1.generateQRCodeReactElementImpl)(Object.assign(Object.assign({}, args), { data: Object.assign(Object.assign({}, args.data), { object: content }) }), args, context); }); } /** {@inheritDoc IQRCodeGenerator.qrOpenID4VCIElement} */ static qrOpenID4VCIElement(args, context) { return __awaiter(this, void 0, void 0, function* () { const content = yield QrCodeProvider.qrOpenID4VCIValue(args, context); return (0, react_qr_1.generateQRCodeReactElementImpl)(Object.assign(Object.assign({}, args), { data: Object.assign(Object.assign({}, args.data), { object: content }) }), args, context); }); } /** {@inheritDoc IQRCodeGenerator.qrOpenID4VCIValue} */ static qrOpenID4VCIValue(args, context) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const { object } = args.data; if (!object.credentialOffer && !object.credentialOfferUri) { throw new Error('Please provide credential_offer or credential_offer_uri'); } const scheme = ((_a = object.scheme) !== null && _a !== void 0 ? _a : 'openid-credential-offer://').replace('://?', '').replace('://', '') + '://'; const baseUri = (_b = object.baseUri) !== null && _b !== void 0 ? _b : ''; let url; const splitBaseUri = baseUri.split('://'); const hasScheme = splitBaseUri.length > 1; if (hasScheme && object.scheme) { if (splitBaseUri[0] !== object.scheme) { throw new Error('The uri must contain the same scheme or omit it'); } url = `${baseUri}`; } else { url = `${scheme}${baseUri}`; } const credentialOfferUri = `?credential_offer_uri=${(_c = object.credentialOfferUri) === null || _c === void 0 ? void 0 : _c.replace('credential_offer_uri=', '')}`; const credentialOffer = `?credential_offer=${(_d = object.credentialOffer) === null || _d === void 0 ? void 0 : _d.replace('credential_offer=', '')}`; const value = object.credentialOffer ? credentialOffer : credentialOfferUri; return `${url}${value}`; }); } } exports.QrCodeProvider = QrCodeProvider; //# sourceMappingURL=QrCodeProvider.js.map