@mobile-wallet-protocol/client
Version:
Client SDK for the Mobile Wallet Protocol
63 lines (62 loc) • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeResponseURLParams = decodeResponseURLParams;
exports.encodeRequestURLParams = encodeRequestURLParams;
const buffer_1 = require("buffer");
function decodeResponseURLParams(params) {
const parseParam = (paramName) => {
const encodedValue = params.get(paramName);
if (!encodedValue)
throw new Error(`Missing parameter: ${paramName}`);
return JSON.parse(encodedValue);
};
const contentParam = parseParam('content');
let content;
if ('failure' in contentParam) {
content = contentParam;
}
if ('encrypted' in contentParam) {
const { iv, cipherText } = contentParam.encrypted;
content = {
encrypted: {
iv: new Uint8Array(buffer_1.Buffer.from(iv, 'base64')),
cipherText: new Uint8Array(buffer_1.Buffer.from(cipherText, 'base64')),
},
};
}
return {
id: parseParam('id'),
sender: parseParam('sender'),
requestId: parseParam('requestId'),
timestamp: new Date(parseParam('timestamp')),
content: content,
};
}
function encodeRequestURLParams(request) {
const urlParams = new URLSearchParams();
const appendParam = (key, value) => {
if (value)
urlParams.append(key, JSON.stringify(value));
};
appendParam('id', request.id);
appendParam('sender', request.sender);
appendParam('sdkVersion', request.sdkVersion);
appendParam('callbackUrl', request.callbackUrl);
appendParam('customScheme', request.customScheme);
appendParam('timestamp', request.timestamp);
if ('handshake' in request.content) {
appendParam('content', request.content);
}
if ('encrypted' in request.content) {
const encrypted = request.content.encrypted;
const ivBytes = new Uint8Array(encrypted.iv);
const cipherTextBytes = new Uint8Array(encrypted.cipherText);
appendParam('content', {
encrypted: {
iv: buffer_1.Buffer.from(ivBytes).toString('base64'),
cipherText: buffer_1.Buffer.from(cipherTextBytes).toString('base64'),
},
});
}
return urlParams.toString();
}