@simplepay-ai/widget
Version:
SimplePay Payment Widget
1,554 lines • 923 kB
JavaScript
import { q as requireCryptoBrowserify, p as process$1, a as global, I as IEvents, t as fromString, u as toString, v as concat, w as safeJsonParse, x as safeJsonStringify, y as cjs, z as getWindowMetadata_1, A as getDocument_1, C as getNavigator_1, B as Buffer, D as C$2, E as ellipticExports, F as detect, G as getLocation_1, o as eventsExports, H as isWsUrl, J as cjs$1, K as isLocalhostUrl, L as formatJsonRpcError, M as parseConnectionError, N as k, O as A$1, P as E$2, Q as i, R as h$2, S as y$3, T as isJsonRpcError, U as formatJsonRpcRequest, V as r, W as getBigIntRpcId, X as Mg, Y as o$1, Z as isJsonRpcRequest, _ as isJsonRpcResponse, $ as formatJsonRpcResult, a0 as Jg, a1 as isJsonRpcResult, a2 as bs$1, a3 as payloadId, g as getDefaultExportFromCjs, c as commonjsGlobal, a4 as isHttpUrl } from "./main-oDQ_krE7.js";
var queryString = {};
var strictUriEncode = (str) => encodeURIComponent(str).replace(/[!'()*]/g, (x3) => `%${x3.charCodeAt(0).toString(16).toUpperCase()}`);
var token = "%[a-f0-9]{2}";
var singleMatcher = new RegExp("(" + token + ")|([^%]+?)", "gi");
var multiMatcher = new RegExp("(" + token + ")+", "gi");
function decodeComponents(components, split) {
try {
return [decodeURIComponent(components.join(""))];
} catch (err) {
}
if (components.length === 1) {
return components;
}
split = split || 1;
var left = components.slice(0, split);
var right = components.slice(split);
return Array.prototype.concat.call([], decodeComponents(left), decodeComponents(right));
}
function decode(input) {
try {
return decodeURIComponent(input);
} catch (err) {
var tokens = input.match(singleMatcher) || [];
for (var i2 = 1; i2 < tokens.length; i2++) {
input = decodeComponents(tokens, i2).join("");
tokens = input.match(singleMatcher) || [];
}
return input;
}
}
function customDecodeURIComponent(input) {
var replaceMap = {
"%FE%FF": "��",
"%FF%FE": "��"
};
var match = multiMatcher.exec(input);
while (match) {
try {
replaceMap[match[0]] = decodeURIComponent(match[0]);
} catch (err) {
var result = decode(match[0]);
if (result !== match[0]) {
replaceMap[match[0]] = result;
}
}
match = multiMatcher.exec(input);
}
replaceMap["%C2"] = "�";
var entries = Object.keys(replaceMap);
for (var i2 = 0; i2 < entries.length; i2++) {
var key = entries[i2];
input = input.replace(new RegExp(key, "g"), replaceMap[key]);
}
return input;
}
var decodeUriComponent = function(encodedURI) {
if (typeof encodedURI !== "string") {
throw new TypeError("Expected `encodedURI` to be of type `string`, got `" + typeof encodedURI + "`");
}
try {
encodedURI = encodedURI.replace(/\+/g, " ");
return decodeURIComponent(encodedURI);
} catch (err) {
return customDecodeURIComponent(encodedURI);
}
};
var splitOnFirst = (string, separator) => {
if (!(typeof string === "string" && typeof separator === "string")) {
throw new TypeError("Expected the arguments to be of type `string`");
}
if (separator === "") {
return [string];
}
const separatorIndex = string.indexOf(separator);
if (separatorIndex === -1) {
return [string];
}
return [
string.slice(0, separatorIndex),
string.slice(separatorIndex + separator.length)
];
};
var filterObj = function(obj, predicate) {
var ret = {};
var keys = Object.keys(obj);
var isArr = Array.isArray(predicate);
for (var i2 = 0; i2 < keys.length; i2++) {
var key = keys[i2];
var val = obj[key];
if (isArr ? predicate.indexOf(key) !== -1 : predicate(key, val, obj)) {
ret[key] = val;
}
}
return ret;
};
(function(exports) {
const strictUriEncode$1 = strictUriEncode;
const decodeComponent = decodeUriComponent;
const splitOnFirst$1 = splitOnFirst;
const filterObject = filterObj;
const isNullOrUndefined = (value) => value === null || value === void 0;
const encodeFragmentIdentifier = Symbol("encodeFragmentIdentifier");
function encoderForArrayFormat(options) {
switch (options.arrayFormat) {
case "index":
return (key) => (result, value) => {
const index = result.length;
if (value === void 0 || options.skipNull && value === null || options.skipEmptyString && value === "") {
return result;
}
if (value === null) {
return [...result, [encode(key, options), "[", index, "]"].join("")];
}
return [
...result,
[encode(key, options), "[", encode(index, options), "]=", encode(value, options)].join("")
];
};
case "bracket":
return (key) => (result, value) => {
if (value === void 0 || options.skipNull && value === null || options.skipEmptyString && value === "") {
return result;
}
if (value === null) {
return [...result, [encode(key, options), "[]"].join("")];
}
return [...result, [encode(key, options), "[]=", encode(value, options)].join("")];
};
case "colon-list-separator":
return (key) => (result, value) => {
if (value === void 0 || options.skipNull && value === null || options.skipEmptyString && value === "") {
return result;
}
if (value === null) {
return [...result, [encode(key, options), ":list="].join("")];
}
return [...result, [encode(key, options), ":list=", encode(value, options)].join("")];
};
case "comma":
case "separator":
case "bracket-separator": {
const keyValueSep = options.arrayFormat === "bracket-separator" ? "[]=" : "=";
return (key) => (result, value) => {
if (value === void 0 || options.skipNull && value === null || options.skipEmptyString && value === "") {
return result;
}
value = value === null ? "" : value;
if (result.length === 0) {
return [[encode(key, options), keyValueSep, encode(value, options)].join("")];
}
return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
};
}
default:
return (key) => (result, value) => {
if (value === void 0 || options.skipNull && value === null || options.skipEmptyString && value === "") {
return result;
}
if (value === null) {
return [...result, encode(key, options)];
}
return [...result, [encode(key, options), "=", encode(value, options)].join("")];
};
}
}
function parserForArrayFormat(options) {
let result;
switch (options.arrayFormat) {
case "index":
return (key, value, accumulator) => {
result = /\[(\d*)\]$/.exec(key);
key = key.replace(/\[\d*\]$/, "");
if (!result) {
accumulator[key] = value;
return;
}
if (accumulator[key] === void 0) {
accumulator[key] = {};
}
accumulator[key][result[1]] = value;
};
case "bracket":
return (key, value, accumulator) => {
result = /(\[\])$/.exec(key);
key = key.replace(/\[\]$/, "");
if (!result) {
accumulator[key] = value;
return;
}
if (accumulator[key] === void 0) {
accumulator[key] = [value];
return;
}
accumulator[key] = [].concat(accumulator[key], value);
};
case "colon-list-separator":
return (key, value, accumulator) => {
result = /(:list)$/.exec(key);
key = key.replace(/:list$/, "");
if (!result) {
accumulator[key] = value;
return;
}
if (accumulator[key] === void 0) {
accumulator[key] = [value];
return;
}
accumulator[key] = [].concat(accumulator[key], value);
};
case "comma":
case "separator":
return (key, value, accumulator) => {
const isArray = typeof value === "string" && value.includes(options.arrayFormatSeparator);
const isEncodedArray = typeof value === "string" && !isArray && decode2(value, options).includes(options.arrayFormatSeparator);
value = isEncodedArray ? decode2(value, options) : value;
const newValue = isArray || isEncodedArray ? value.split(options.arrayFormatSeparator).map((item) => decode2(item, options)) : value === null ? value : decode2(value, options);
accumulator[key] = newValue;
};
case "bracket-separator":
return (key, value, accumulator) => {
const isArray = /(\[\])$/.test(key);
key = key.replace(/\[\]$/, "");
if (!isArray) {
accumulator[key] = value ? decode2(value, options) : value;
return;
}
const arrayValue = value === null ? [] : value.split(options.arrayFormatSeparator).map((item) => decode2(item, options));
if (accumulator[key] === void 0) {
accumulator[key] = arrayValue;
return;
}
accumulator[key] = [].concat(accumulator[key], arrayValue);
};
default:
return (key, value, accumulator) => {
if (accumulator[key] === void 0) {
accumulator[key] = value;
return;
}
accumulator[key] = [].concat(accumulator[key], value);
};
}
}
function validateArrayFormatSeparator(value) {
if (typeof value !== "string" || value.length !== 1) {
throw new TypeError("arrayFormatSeparator must be single character string");
}
}
function encode(value, options) {
if (options.encode) {
return options.strict ? strictUriEncode$1(value) : encodeURIComponent(value);
}
return value;
}
function decode2(value, options) {
if (options.decode) {
return decodeComponent(value);
}
return value;
}
function keysSorter(input) {
if (Array.isArray(input)) {
return input.sort();
}
if (typeof input === "object") {
return keysSorter(Object.keys(input)).sort((a3, b2) => Number(a3) - Number(b2)).map((key) => input[key]);
}
return input;
}
function removeHash(input) {
const hashStart = input.indexOf("#");
if (hashStart !== -1) {
input = input.slice(0, hashStart);
}
return input;
}
function getHash(url) {
let hash2 = "";
const hashStart = url.indexOf("#");
if (hashStart !== -1) {
hash2 = url.slice(hashStart);
}
return hash2;
}
function extract(input) {
input = removeHash(input);
const queryStart = input.indexOf("?");
if (queryStart === -1) {
return "";
}
return input.slice(queryStart + 1);
}
function parseValue(value, options) {
if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === "string" && value.trim() !== "")) {
value = Number(value);
} else if (options.parseBooleans && value !== null && (value.toLowerCase() === "true" || value.toLowerCase() === "false")) {
value = value.toLowerCase() === "true";
}
return value;
}
function parse(query, options) {
options = Object.assign({
decode: true,
sort: true,
arrayFormat: "none",
arrayFormatSeparator: ",",
parseNumbers: false,
parseBooleans: false
}, options);
validateArrayFormatSeparator(options.arrayFormatSeparator);
const formatter = parserForArrayFormat(options);
const ret = /* @__PURE__ */ Object.create(null);
if (typeof query !== "string") {
return ret;
}
query = query.trim().replace(/^[?#&]/, "");
if (!query) {
return ret;
}
for (const param of query.split("&")) {
if (param === "") {
continue;
}
let [key, value] = splitOnFirst$1(options.decode ? param.replace(/\+/g, " ") : param, "=");
value = value === void 0 ? null : ["comma", "separator", "bracket-separator"].includes(options.arrayFormat) ? value : decode2(value, options);
formatter(decode2(key, options), value, ret);
}
for (const key of Object.keys(ret)) {
const value = ret[key];
if (typeof value === "object" && value !== null) {
for (const k2 of Object.keys(value)) {
value[k2] = parseValue(value[k2], options);
}
} else {
ret[key] = parseValue(value, options);
}
}
if (options.sort === false) {
return ret;
}
return (options.sort === true ? Object.keys(ret).sort() : Object.keys(ret).sort(options.sort)).reduce((result, key) => {
const value = ret[key];
if (Boolean(value) && typeof value === "object" && !Array.isArray(value)) {
result[key] = keysSorter(value);
} else {
result[key] = value;
}
return result;
}, /* @__PURE__ */ Object.create(null));
}
exports.extract = extract;
exports.parse = parse;
exports.stringify = (object, options) => {
if (!object) {
return "";
}
options = Object.assign({
encode: true,
strict: true,
arrayFormat: "none",
arrayFormatSeparator: ","
}, options);
validateArrayFormatSeparator(options.arrayFormatSeparator);
const shouldFilter = (key) => options.skipNull && isNullOrUndefined(object[key]) || options.skipEmptyString && object[key] === "";
const formatter = encoderForArrayFormat(options);
const objectCopy = {};
for (const key of Object.keys(object)) {
if (!shouldFilter(key)) {
objectCopy[key] = object[key];
}
}
const keys = Object.keys(objectCopy);
if (options.sort !== false) {
keys.sort(options.sort);
}
return keys.map((key) => {
const value = object[key];
if (value === void 0) {
return "";
}
if (value === null) {
return encode(key, options);
}
if (Array.isArray(value)) {
if (value.length === 0 && options.arrayFormat === "bracket-separator") {
return encode(key, options) + "[]";
}
return value.reduce(formatter(key), []).join("&");
}
return encode(key, options) + "=" + encode(value, options);
}).filter((x3) => x3.length > 0).join("&");
};
exports.parseUrl = (url, options) => {
options = Object.assign({
decode: true
}, options);
const [url_, hash2] = splitOnFirst$1(url, "#");
return Object.assign(
{
url: url_.split("?")[0] || "",
query: parse(extract(url), options)
},
options && options.parseFragmentIdentifier && hash2 ? { fragmentIdentifier: decode2(hash2, options) } : {}
);
};
exports.stringifyUrl = (object, options) => {
options = Object.assign({
encode: true,
strict: true,
[encodeFragmentIdentifier]: true
}, options);
const url = removeHash(object.url).split("?")[0] || "";
const queryFromUrl = exports.extract(object.url);
const parsedQueryFromUrl = exports.parse(queryFromUrl, { sort: false });
const query = Object.assign(parsedQueryFromUrl, object.query);
let queryString2 = exports.stringify(query, options);
if (queryString2) {
queryString2 = `?${queryString2}`;
}
let hash2 = getHash(object.url);
if (object.fragmentIdentifier) {
hash2 = `#${options[encodeFragmentIdentifier] ? encode(object.fragmentIdentifier, options) : object.fragmentIdentifier}`;
}
return `${url}${queryString2}${hash2}`;
};
exports.pick = (input, filter, options) => {
options = Object.assign({
parseFragmentIdentifier: true,
[encodeFragmentIdentifier]: false
}, options);
const { url, query, fragmentIdentifier } = exports.parseUrl(input, options);
return exports.stringifyUrl({
url,
query: filterObject(query, filter),
fragmentIdentifier
}, options);
};
exports.exclude = (input, filter, options) => {
const exclusionFilter = Array.isArray(filter) ? (key) => !filter.includes(key) : (key, value) => !filter(key, value);
return exports.pick(input, exclusionFilter, options);
};
})(queryString);
var chacha20poly1305 = {};
var chacha = {};
var binary = {};
var int = {};
(function(exports) {
Object.defineProperty(exports, "__esModule", { value: true });
function imulShim(a3, b2) {
var ah = a3 >>> 16 & 65535, al = a3 & 65535;
var bh2 = b2 >>> 16 & 65535, bl = b2 & 65535;
return al * bl + (ah * bl + al * bh2 << 16 >>> 0) | 0;
}
exports.mul = Math.imul || imulShim;
function add(a3, b2) {
return a3 + b2 | 0;
}
exports.add = add;
function sub(a3, b2) {
return a3 - b2 | 0;
}
exports.sub = sub;
function rotl(x3, n2) {
return x3 << n2 | x3 >>> 32 - n2;
}
exports.rotl = rotl;
function rotr(x3, n2) {
return x3 << 32 - n2 | x3 >>> n2;
}
exports.rotr = rotr;
function isIntegerShim(n2) {
return typeof n2 === "number" && isFinite(n2) && Math.floor(n2) === n2;
}
exports.isInteger = Number.isInteger || isIntegerShim;
exports.MAX_SAFE_INTEGER = 9007199254740991;
exports.isSafeInteger = function(n2) {
return exports.isInteger(n2) && (n2 >= -exports.MAX_SAFE_INTEGER && n2 <= exports.MAX_SAFE_INTEGER);
};
})(int);
Object.defineProperty(binary, "__esModule", { value: true });
var int_1 = int;
function readInt16BE(array, offset) {
if (offset === void 0) {
offset = 0;
}
return (array[offset + 0] << 8 | array[offset + 1]) << 16 >> 16;
}
binary.readInt16BE = readInt16BE;
function readUint16BE(array, offset) {
if (offset === void 0) {
offset = 0;
}
return (array[offset + 0] << 8 | array[offset + 1]) >>> 0;
}
binary.readUint16BE = readUint16BE;
function readInt16LE(array, offset) {
if (offset === void 0) {
offset = 0;
}
return (array[offset + 1] << 8 | array[offset]) << 16 >> 16;
}
binary.readInt16LE = readInt16LE;
function readUint16LE(array, offset) {
if (offset === void 0) {
offset = 0;
}
return (array[offset + 1] << 8 | array[offset]) >>> 0;
}
binary.readUint16LE = readUint16LE;
function writeUint16BE(value, out, offset) {
if (out === void 0) {
out = new Uint8Array(2);
}
if (offset === void 0) {
offset = 0;
}
out[offset + 0] = value >>> 8;
out[offset + 1] = value >>> 0;
return out;
}
binary.writeUint16BE = writeUint16BE;
binary.writeInt16BE = writeUint16BE;
function writeUint16LE(value, out, offset) {
if (out === void 0) {
out = new Uint8Array(2);
}
if (offset === void 0) {
offset = 0;
}
out[offset + 0] = value >>> 0;
out[offset + 1] = value >>> 8;
return out;
}
binary.writeUint16LE = writeUint16LE;
binary.writeInt16LE = writeUint16LE;
function readInt32BE(array, offset) {
if (offset === void 0) {
offset = 0;
}
return array[offset] << 24 | array[offset + 1] << 16 | array[offset + 2] << 8 | array[offset + 3];
}
binary.readInt32BE = readInt32BE;
function readUint32BE(array, offset) {
if (offset === void 0) {
offset = 0;
}
return (array[offset] << 24 | array[offset + 1] << 16 | array[offset + 2] << 8 | array[offset + 3]) >>> 0;
}
binary.readUint32BE = readUint32BE;
function readInt32LE(array, offset) {
if (offset === void 0) {
offset = 0;
}
return array[offset + 3] << 24 | array[offset + 2] << 16 | array[offset + 1] << 8 | array[offset];
}
binary.readInt32LE = readInt32LE;
function readUint32LE(array, offset) {
if (offset === void 0) {
offset = 0;
}
return (array[offset + 3] << 24 | array[offset + 2] << 16 | array[offset + 1] << 8 | array[offset]) >>> 0;
}
binary.readUint32LE = readUint32LE;
function writeUint32BE(value, out, offset) {
if (out === void 0) {
out = new Uint8Array(4);
}
if (offset === void 0) {
offset = 0;
}
out[offset + 0] = value >>> 24;
out[offset + 1] = value >>> 16;
out[offset + 2] = value >>> 8;
out[offset + 3] = value >>> 0;
return out;
}
binary.writeUint32BE = writeUint32BE;
binary.writeInt32BE = writeUint32BE;
function writeUint32LE(value, out, offset) {
if (out === void 0) {
out = new Uint8Array(4);
}
if (offset === void 0) {
offset = 0;
}
out[offset + 0] = value >>> 0;
out[offset + 1] = value >>> 8;
out[offset + 2] = value >>> 16;
out[offset + 3] = value >>> 24;
return out;
}
binary.writeUint32LE = writeUint32LE;
binary.writeInt32LE = writeUint32LE;
function readInt64BE(array, offset) {
if (offset === void 0) {
offset = 0;
}
var hi2 = readInt32BE(array, offset);
var lo2 = readInt32BE(array, offset + 4);
return hi2 * 4294967296 + lo2 - (lo2 >> 31) * 4294967296;
}
binary.readInt64BE = readInt64BE;
function readUint64BE(array, offset) {
if (offset === void 0) {
offset = 0;
}
var hi2 = readUint32BE(array, offset);
var lo2 = readUint32BE(array, offset + 4);
return hi2 * 4294967296 + lo2;
}
binary.readUint64BE = readUint64BE;
function readInt64LE(array, offset) {
if (offset === void 0) {
offset = 0;
}
var lo2 = readInt32LE(array, offset);
var hi2 = readInt32LE(array, offset + 4);
return hi2 * 4294967296 + lo2 - (lo2 >> 31) * 4294967296;
}
binary.readInt64LE = readInt64LE;
function readUint64LE(array, offset) {
if (offset === void 0) {
offset = 0;
}
var lo2 = readUint32LE(array, offset);
var hi2 = readUint32LE(array, offset + 4);
return hi2 * 4294967296 + lo2;
}
binary.readUint64LE = readUint64LE;
function writeUint64BE(value, out, offset) {
if (out === void 0) {
out = new Uint8Array(8);
}
if (offset === void 0) {
offset = 0;
}
writeUint32BE(value / 4294967296 >>> 0, out, offset);
writeUint32BE(value >>> 0, out, offset + 4);
return out;
}
binary.writeUint64BE = writeUint64BE;
binary.writeInt64BE = writeUint64BE;
function writeUint64LE(value, out, offset) {
if (out === void 0) {
out = new Uint8Array(8);
}
if (offset === void 0) {
offset = 0;
}
writeUint32LE(value >>> 0, out, offset);
writeUint32LE(value / 4294967296 >>> 0, out, offset + 4);
return out;
}
binary.writeUint64LE = writeUint64LE;
binary.writeInt64LE = writeUint64LE;
function readUintBE(bitLength, array, offset) {
if (offset === void 0) {
offset = 0;
}
if (bitLength % 8 !== 0) {
throw new Error("readUintBE supports only bitLengths divisible by 8");
}
if (bitLength / 8 > array.length - offset) {
throw new Error("readUintBE: array is too short for the given bitLength");
}
var result = 0;
var mul = 1;
for (var i2 = bitLength / 8 + offset - 1; i2 >= offset; i2--) {
result += array[i2] * mul;
mul *= 256;
}
return result;
}
binary.readUintBE = readUintBE;
function readUintLE(bitLength, array, offset) {
if (offset === void 0) {
offset = 0;
}
if (bitLength % 8 !== 0) {
throw new Error("readUintLE supports only bitLengths divisible by 8");
}
if (bitLength / 8 > array.length - offset) {
throw new Error("readUintLE: array is too short for the given bitLength");
}
var result = 0;
var mul = 1;
for (var i2 = offset; i2 < offset + bitLength / 8; i2++) {
result += array[i2] * mul;
mul *= 256;
}
return result;
}
binary.readUintLE = readUintLE;
function writeUintBE(bitLength, value, out, offset) {
if (out === void 0) {
out = new Uint8Array(bitLength / 8);
}
if (offset === void 0) {
offset = 0;
}
if (bitLength % 8 !== 0) {
throw new Error("writeUintBE supports only bitLengths divisible by 8");
}
if (!int_1.isSafeInteger(value)) {
throw new Error("writeUintBE value must be an integer");
}
var div = 1;
for (var i2 = bitLength / 8 + offset - 1; i2 >= offset; i2--) {
out[i2] = value / div & 255;
div *= 256;
}
return out;
}
binary.writeUintBE = writeUintBE;
function writeUintLE(bitLength, value, out, offset) {
if (out === void 0) {
out = new Uint8Array(bitLength / 8);
}
if (offset === void 0) {
offset = 0;
}
if (bitLength % 8 !== 0) {
throw new Error("writeUintLE supports only bitLengths divisible by 8");
}
if (!int_1.isSafeInteger(value)) {
throw new Error("writeUintLE value must be an integer");
}
var div = 1;
for (var i2 = offset; i2 < offset + bitLength / 8; i2++) {
out[i2] = value / div & 255;
div *= 256;
}
return out;
}
binary.writeUintLE = writeUintLE;
function readFloat32BE(array, offset) {
if (offset === void 0) {
offset = 0;
}
var view = new DataView(array.buffer, array.byteOffset, array.byteLength);
return view.getFloat32(offset);
}
binary.readFloat32BE = readFloat32BE;
function readFloat32LE(array, offset) {
if (offset === void 0) {
offset = 0;
}
var view = new DataView(array.buffer, array.byteOffset, array.byteLength);
return view.getFloat32(offset, true);
}
binary.readFloat32LE = readFloat32LE;
function readFloat64BE(array, offset) {
if (offset === void 0) {
offset = 0;
}
var view = new DataView(array.buffer, array.byteOffset, array.byteLength);
return view.getFloat64(offset);
}
binary.readFloat64BE = readFloat64BE;
function readFloat64LE(array, offset) {
if (offset === void 0) {
offset = 0;
}
var view = new DataView(array.buffer, array.byteOffset, array.byteLength);
return view.getFloat64(offset, true);
}
binary.readFloat64LE = readFloat64LE;
function writeFloat32BE(value, out, offset) {
if (out === void 0) {
out = new Uint8Array(4);
}
if (offset === void 0) {
offset = 0;
}
var view = new DataView(out.buffer, out.byteOffset, out.byteLength);
view.setFloat32(offset, value);
return out;
}
binary.writeFloat32BE = writeFloat32BE;
function writeFloat32LE(value, out, offset) {
if (out === void 0) {
out = new Uint8Array(4);
}
if (offset === void 0) {
offset = 0;
}
var view = new DataView(out.buffer, out.byteOffset, out.byteLength);
view.setFloat32(offset, value, true);
return out;
}
binary.writeFloat32LE = writeFloat32LE;
function writeFloat64BE(value, out, offset) {
if (out === void 0) {
out = new Uint8Array(8);
}
if (offset === void 0) {
offset = 0;
}
var view = new DataView(out.buffer, out.byteOffset, out.byteLength);
view.setFloat64(offset, value);
return out;
}
binary.writeFloat64BE = writeFloat64BE;
function writeFloat64LE(value, out, offset) {
if (out === void 0) {
out = new Uint8Array(8);
}
if (offset === void 0) {
offset = 0;
}
var view = new DataView(out.buffer, out.byteOffset, out.byteLength);
view.setFloat64(offset, value, true);
return out;
}
binary.writeFloat64LE = writeFloat64LE;
var wipe$1 = {};
Object.defineProperty(wipe$1, "__esModule", { value: true });
function wipe(array) {
for (var i2 = 0; i2 < array.length; i2++) {
array[i2] = 0;
}
return array;
}
wipe$1.wipe = wipe;
Object.defineProperty(chacha, "__esModule", { value: true });
var binary_1 = binary;
var wipe_1$3 = wipe$1;
var ROUNDS = 20;
function core(out, input, key) {
var j02 = 1634760805;
var j1 = 857760878;
var j2 = 2036477234;
var j3 = 1797285236;
var j4 = key[3] << 24 | key[2] << 16 | key[1] << 8 | key[0];
var j5 = key[7] << 24 | key[6] << 16 | key[5] << 8 | key[4];
var j6 = key[11] << 24 | key[10] << 16 | key[9] << 8 | key[8];
var j7 = key[15] << 24 | key[14] << 16 | key[13] << 8 | key[12];
var j8 = key[19] << 24 | key[18] << 16 | key[17] << 8 | key[16];
var j9 = key[23] << 24 | key[22] << 16 | key[21] << 8 | key[20];
var j10 = key[27] << 24 | key[26] << 16 | key[25] << 8 | key[24];
var j11 = key[31] << 24 | key[30] << 16 | key[29] << 8 | key[28];
var j12 = input[3] << 24 | input[2] << 16 | input[1] << 8 | input[0];
var j13 = input[7] << 24 | input[6] << 16 | input[5] << 8 | input[4];
var j14 = input[11] << 24 | input[10] << 16 | input[9] << 8 | input[8];
var j15 = input[15] << 24 | input[14] << 16 | input[13] << 8 | input[12];
var x02 = j02;
var x1 = j1;
var x22 = j2;
var x3 = j3;
var x4 = j4;
var x5 = j5;
var x6 = j6;
var x7 = j7;
var x8 = j8;
var x9 = j9;
var x10 = j10;
var x11 = j11;
var x12 = j12;
var x13 = j13;
var x14 = j14;
var x15 = j15;
for (var i2 = 0; i2 < ROUNDS; i2 += 2) {
x02 = x02 + x4 | 0;
x12 ^= x02;
x12 = x12 >>> 32 - 16 | x12 << 16;
x8 = x8 + x12 | 0;
x4 ^= x8;
x4 = x4 >>> 32 - 12 | x4 << 12;
x1 = x1 + x5 | 0;
x13 ^= x1;
x13 = x13 >>> 32 - 16 | x13 << 16;
x9 = x9 + x13 | 0;
x5 ^= x9;
x5 = x5 >>> 32 - 12 | x5 << 12;
x22 = x22 + x6 | 0;
x14 ^= x22;
x14 = x14 >>> 32 - 16 | x14 << 16;
x10 = x10 + x14 | 0;
x6 ^= x10;
x6 = x6 >>> 32 - 12 | x6 << 12;
x3 = x3 + x7 | 0;
x15 ^= x3;
x15 = x15 >>> 32 - 16 | x15 << 16;
x11 = x11 + x15 | 0;
x7 ^= x11;
x7 = x7 >>> 32 - 12 | x7 << 12;
x22 = x22 + x6 | 0;
x14 ^= x22;
x14 = x14 >>> 32 - 8 | x14 << 8;
x10 = x10 + x14 | 0;
x6 ^= x10;
x6 = x6 >>> 32 - 7 | x6 << 7;
x3 = x3 + x7 | 0;
x15 ^= x3;
x15 = x15 >>> 32 - 8 | x15 << 8;
x11 = x11 + x15 | 0;
x7 ^= x11;
x7 = x7 >>> 32 - 7 | x7 << 7;
x1 = x1 + x5 | 0;
x13 ^= x1;
x13 = x13 >>> 32 - 8 | x13 << 8;
x9 = x9 + x13 | 0;
x5 ^= x9;
x5 = x5 >>> 32 - 7 | x5 << 7;
x02 = x02 + x4 | 0;
x12 ^= x02;
x12 = x12 >>> 32 - 8 | x12 << 8;
x8 = x8 + x12 | 0;
x4 ^= x8;
x4 = x4 >>> 32 - 7 | x4 << 7;
x02 = x02 + x5 | 0;
x15 ^= x02;
x15 = x15 >>> 32 - 16 | x15 << 16;
x10 = x10 + x15 | 0;
x5 ^= x10;
x5 = x5 >>> 32 - 12 | x5 << 12;
x1 = x1 + x6 | 0;
x12 ^= x1;
x12 = x12 >>> 32 - 16 | x12 << 16;
x11 = x11 + x12 | 0;
x6 ^= x11;
x6 = x6 >>> 32 - 12 | x6 << 12;
x22 = x22 + x7 | 0;
x13 ^= x22;
x13 = x13 >>> 32 - 16 | x13 << 16;
x8 = x8 + x13 | 0;
x7 ^= x8;
x7 = x7 >>> 32 - 12 | x7 << 12;
x3 = x3 + x4 | 0;
x14 ^= x3;
x14 = x14 >>> 32 - 16 | x14 << 16;
x9 = x9 + x14 | 0;
x4 ^= x9;
x4 = x4 >>> 32 - 12 | x4 << 12;
x22 = x22 + x7 | 0;
x13 ^= x22;
x13 = x13 >>> 32 - 8 | x13 << 8;
x8 = x8 + x13 | 0;
x7 ^= x8;
x7 = x7 >>> 32 - 7 | x7 << 7;
x3 = x3 + x4 | 0;
x14 ^= x3;
x14 = x14 >>> 32 - 8 | x14 << 8;
x9 = x9 + x14 | 0;
x4 ^= x9;
x4 = x4 >>> 32 - 7 | x4 << 7;
x1 = x1 + x6 | 0;
x12 ^= x1;
x12 = x12 >>> 32 - 8 | x12 << 8;
x11 = x11 + x12 | 0;
x6 ^= x11;
x6 = x6 >>> 32 - 7 | x6 << 7;
x02 = x02 + x5 | 0;
x15 ^= x02;
x15 = x15 >>> 32 - 8 | x15 << 8;
x10 = x10 + x15 | 0;
x5 ^= x10;
x5 = x5 >>> 32 - 7 | x5 << 7;
}
binary_1.writeUint32LE(x02 + j02 | 0, out, 0);
binary_1.writeUint32LE(x1 + j1 | 0, out, 4);
binary_1.writeUint32LE(x22 + j2 | 0, out, 8);
binary_1.writeUint32LE(x3 + j3 | 0, out, 12);
binary_1.writeUint32LE(x4 + j4 | 0, out, 16);
binary_1.writeUint32LE(x5 + j5 | 0, out, 20);
binary_1.writeUint32LE(x6 + j6 | 0, out, 24);
binary_1.writeUint32LE(x7 + j7 | 0, out, 28);
binary_1.writeUint32LE(x8 + j8 | 0, out, 32);
binary_1.writeUint32LE(x9 + j9 | 0, out, 36);
binary_1.writeUint32LE(x10 + j10 | 0, out, 40);
binary_1.writeUint32LE(x11 + j11 | 0, out, 44);
binary_1.writeUint32LE(x12 + j12 | 0, out, 48);
binary_1.writeUint32LE(x13 + j13 | 0, out, 52);
binary_1.writeUint32LE(x14 + j14 | 0, out, 56);
binary_1.writeUint32LE(x15 + j15 | 0, out, 60);
}
function streamXOR(key, nonce, src, dst, nonceInplaceCounterLength) {
if (nonceInplaceCounterLength === void 0) {
nonceInplaceCounterLength = 0;
}
if (key.length !== 32) {
throw new Error("ChaCha: key size must be 32 bytes");
}
if (dst.length < src.length) {
throw new Error("ChaCha: destination is shorter than source");
}
var nc;
var counterLength;
if (nonceInplaceCounterLength === 0) {
if (nonce.length !== 8 && nonce.length !== 12) {
throw new Error("ChaCha nonce must be 8 or 12 bytes");
}
nc = new Uint8Array(16);
counterLength = nc.length - nonce.length;
nc.set(nonce, counterLength);
} else {
if (nonce.length !== 16) {
throw new Error("ChaCha nonce with counter must be 16 bytes");
}
nc = nonce;
counterLength = nonceInplaceCounterLength;
}
var block = new Uint8Array(64);
for (var i2 = 0; i2 < src.length; i2 += 64) {
core(block, nc, key);
for (var j2 = i2; j2 < i2 + 64 && j2 < src.length; j2++) {
dst[j2] = src[j2] ^ block[j2 - i2];
}
incrementCounter(nc, 0, counterLength);
}
wipe_1$3.wipe(block);
if (nonceInplaceCounterLength === 0) {
wipe_1$3.wipe(nc);
}
return dst;
}
chacha.streamXOR = streamXOR;
function stream(key, nonce, dst, nonceInplaceCounterLength) {
if (nonceInplaceCounterLength === void 0) {
nonceInplaceCounterLength = 0;
}
wipe_1$3.wipe(dst);
return streamXOR(key, nonce, dst, dst, nonceInplaceCounterLength);
}
chacha.stream = stream;
function incrementCounter(counter, pos, len) {
var carry = 1;
while (len--) {
carry = carry + (counter[pos] & 255) | 0;
counter[pos] = carry & 255;
carry >>>= 8;
pos++;
}
if (carry > 0) {
throw new Error("ChaCha: counter overflow");
}
}
var poly1305 = {};
var constantTime = {};
Object.defineProperty(constantTime, "__esModule", { value: true });
function select(subject, resultIfOne, resultIfZero) {
return ~(subject - 1) & resultIfOne | subject - 1 & resultIfZero;
}
constantTime.select = select;
function lessOrEqual(a3, b2) {
return (a3 | 0) - (b2 | 0) - 1 >>> 31 & 1;
}
constantTime.lessOrEqual = lessOrEqual;
function compare(a3, b2) {
if (a3.length !== b2.length) {
return 0;
}
var result = 0;
for (var i2 = 0; i2 < a3.length; i2++) {
result |= a3[i2] ^ b2[i2];
}
return 1 & result - 1 >>> 8;
}
constantTime.compare = compare;
function equal(a3, b2) {
if (a3.length === 0 || b2.length === 0) {
return false;
}
return compare(a3, b2) !== 0;
}
constantTime.equal = equal;
(function(exports) {
Object.defineProperty(exports, "__esModule", { value: true });
var constant_time_12 = constantTime;
var wipe_12 = wipe$1;
exports.DIGEST_LENGTH = 16;
var Poly1305 = (
/** @class */
function() {
function Poly13052(key) {
this.digestLength = exports.DIGEST_LENGTH;
this._buffer = new Uint8Array(16);
this._r = new Uint16Array(10);
this._h = new Uint16Array(10);
this._pad = new Uint16Array(8);
this._leftover = 0;
this._fin = 0;
this._finished = false;
var t02 = key[0] | key[1] << 8;
this._r[0] = t02 & 8191;
var t1 = key[2] | key[3] << 8;
this._r[1] = (t02 >>> 13 | t1 << 3) & 8191;
var t2 = key[4] | key[5] << 8;
this._r[2] = (t1 >>> 10 | t2 << 6) & 7939;
var t3 = key[6] | key[7] << 8;
this._r[3] = (t2 >>> 7 | t3 << 9) & 8191;
var t4 = key[8] | key[9] << 8;
this._r[4] = (t3 >>> 4 | t4 << 12) & 255;
this._r[5] = t4 >>> 1 & 8190;
var t5 = key[10] | key[11] << 8;
this._r[6] = (t4 >>> 14 | t5 << 2) & 8191;
var t6 = key[12] | key[13] << 8;
this._r[7] = (t5 >>> 11 | t6 << 5) & 8065;
var t7 = key[14] | key[15] << 8;
this._r[8] = (t6 >>> 8 | t7 << 8) & 8191;
this._r[9] = t7 >>> 5 & 127;
this._pad[0] = key[16] | key[17] << 8;
this._pad[1] = key[18] | key[19] << 8;
this._pad[2] = key[20] | key[21] << 8;
this._pad[3] = key[22] | key[23] << 8;
this._pad[4] = key[24] | key[25] << 8;
this._pad[5] = key[26] | key[27] << 8;
this._pad[6] = key[28] | key[29] << 8;
this._pad[7] = key[30] | key[31] << 8;
}
Poly13052.prototype._blocks = function(m2, mpos, bytes) {
var hibit = this._fin ? 0 : 1 << 11;
var h0 = this._h[0], h1 = this._h[1], h22 = this._h[2], h3 = this._h[3], h4 = this._h[4], h5 = this._h[5], h6 = this._h[6], h7 = this._h[7], h8 = this._h[8], h9 = this._h[9];
var r02 = this._r[0], r1 = this._r[1], r2 = this._r[2], r3 = this._r[3], r4 = this._r[4], r5 = this._r[5], r6 = this._r[6], r7 = this._r[7], r8 = this._r[8], r9 = this._r[9];
while (bytes >= 16) {
var t02 = m2[mpos + 0] | m2[mpos + 1] << 8;
h0 += t02 & 8191;
var t1 = m2[mpos + 2] | m2[mpos + 3] << 8;
h1 += (t02 >>> 13 | t1 << 3) & 8191;
var t2 = m2[mpos + 4] | m2[mpos + 5] << 8;
h22 += (t1 >>> 10 | t2 << 6) & 8191;
var t3 = m2[mpos + 6] | m2[mpos + 7] << 8;
h3 += (t2 >>> 7 | t3 << 9) & 8191;
var t4 = m2[mpos + 8] | m2[mpos + 9] << 8;
h4 += (t3 >>> 4 | t4 << 12) & 8191;
h5 += t4 >>> 1 & 8191;
var t5 = m2[mpos + 10] | m2[mpos + 11] << 8;
h6 += (t4 >>> 14 | t5 << 2) & 8191;
var t6 = m2[mpos + 12] | m2[mpos + 13] << 8;
h7 += (t5 >>> 11 | t6 << 5) & 8191;
var t7 = m2[mpos + 14] | m2[mpos + 15] << 8;
h8 += (t6 >>> 8 | t7 << 8) & 8191;
h9 += t7 >>> 5 | hibit;
var c2 = 0;
var d02 = c2;
d02 += h0 * r02;
d02 += h1 * (5 * r9);
d02 += h22 * (5 * r8);
d02 += h3 * (5 * r7);
d02 += h4 * (5 * r6);
c2 = d02 >>> 13;
d02 &= 8191;
d02 += h5 * (5 * r5);
d02 += h6 * (5 * r4);
d02 += h7 * (5 * r3);
d02 += h8 * (5 * r2);
d02 += h9 * (5 * r1);
c2 += d02 >>> 13;
d02 &= 8191;
var d1 = c2;
d1 += h0 * r1;
d1 += h1 * r02;
d1 += h22 * (5 * r9);
d1 += h3 * (5 * r8);
d1 += h4 * (5 * r7);
c2 = d1 >>> 13;
d1 &= 8191;
d1 += h5 * (5 * r6);
d1 += h6 * (5 * r5);
d1 += h7 * (5 * r4);
d1 += h8 * (5 * r3);
d1 += h9 * (5 * r2);
c2 += d1 >>> 13;
d1 &= 8191;
var d22 = c2;
d22 += h0 * r2;
d22 += h1 * r1;
d22 += h22 * r02;
d22 += h3 * (5 * r9);
d22 += h4 * (5 * r8);
c2 = d22 >>> 13;
d22 &= 8191;
d22 += h5 * (5 * r7);
d22 += h6 * (5 * r6);
d22 += h7 * (5 * r5);
d22 += h8 * (5 * r4);
d22 += h9 * (5 * r3);
c2 += d22 >>> 13;
d22 &= 8191;
var d3 = c2;
d3 += h0 * r3;
d3 += h1 * r2;
d3 += h22 * r1;
d3 += h3 * r02;
d3 += h4 * (5 * r9);
c2 = d3 >>> 13;
d3 &= 8191;
d3 += h5 * (5 * r8);
d3 += h6 * (5 * r7);
d3 += h7 * (5 * r6);
d3 += h8 * (5 * r5);
d3 += h9 * (5 * r4);
c2 += d3 >>> 13;
d3 &= 8191;
var d4 = c2;
d4 += h0 * r4;
d4 += h1 * r3;
d4 += h22 * r2;
d4 += h3 * r1;
d4 += h4 * r02;
c2 = d4 >>> 13;
d4 &= 8191;
d4 += h5 * (5 * r9);
d4 += h6 * (5 * r8);
d4 += h7 * (5 * r7);
d4 += h8 * (5 * r6);
d4 += h9 * (5 * r5);
c2 += d4 >>> 13;
d4 &= 8191;
var d5 = c2;
d5 += h0 * r5;
d5 += h1 * r4;
d5 += h22 * r3;
d5 += h3 * r2;
d5 += h4 * r1;
c2 = d5 >>> 13;
d5 &= 8191;
d5 += h5 * r02;
d5 += h6 * (5 * r9);
d5 += h7 * (5 * r8);
d5 += h8 * (5 * r7);
d5 += h9 * (5 * r6);
c2 += d5 >>> 13;
d5 &= 8191;
var d6 = c2;
d6 += h0 * r6;
d6 += h1 * r5;
d6 += h22 * r4;
d6 += h3 * r3;
d6 += h4 * r2;
c2 = d6 >>> 13;
d6 &= 8191;
d6 += h5 * r1;
d6 += h6 * r02;
d6 += h7 * (5 * r9);
d6 += h8 * (5 * r8);
d6 += h9 * (5 * r7);
c2 += d6 >>> 13;
d6 &= 8191;
var d7 = c2;
d7 += h0 * r7;
d7 += h1 * r6;
d7 += h22 * r5;
d7 += h3 * r4;
d7 += h4 * r3;
c2 = d7 >>> 13;
d7 &= 8191;
d7 += h5 * r2;
d7 += h6 * r1;
d7 += h7 * r02;
d7 += h8 * (5 * r9);
d7 += h9 * (5 * r8);
c2 += d7 >>> 13;
d7 &= 8191;
var d8 = c2;
d8 += h0 * r8;
d8 += h1 * r7;
d8 += h22 * r6;
d8 += h3 * r5;
d8 += h4 * r4;
c2 = d8 >>> 13;
d8 &= 8191;
d8 += h5 * r3;
d8 += h6 * r2;
d8 += h7 * r1;
d8 += h8 * r02;
d8 += h9 * (5 * r9);
c2 += d8 >>> 13;
d8 &= 8191;
var d9 = c2;
d9 += h0 * r9;
d9 += h1 * r8;
d9 += h22 * r7;
d9 += h3 * r6;
d9 += h4 * r5;
c2 = d9 >>> 13;
d9 &= 8191;
d9 += h5 * r4;
d9 += h6 * r3;
d9 += h7 * r2;
d9 += h8 * r1;
d9 += h9 * r02;
c2 += d9 >>> 13;
d9 &= 8191;
c2 = (c2 << 2) + c2 | 0;
c2 = c2 + d02 | 0;
d02 = c2 & 8191;
c2 = c2 >>> 13;
d1 += c2;
h0 = d02;
h1 = d1;
h22 = d22;
h3 = d3;
h4 = d4;
h5 = d5;
h6 = d6;
h7 = d7;
h8 = d8;
h9 = d9;
mpos += 16;
bytes -= 16;
}
this._h[0] = h0;
this._h[1] = h1;
this._h[2] = h22;
this._h[3] = h3;
this._h[4] = h4;
this._h[5] = h5;
this._h[6] = h6;
this._h[7] = h7;
this._h[8] = h8;
this._h[9] = h9;
};
Poly13052.prototype.finish = function(mac, macpos) {
if (macpos === void 0) {
macpos = 0;
}
var g3 = new Uint16Array(10);
var c2;
var mask;
var f3;
var i2;
if (this._leftover) {
i2 = this._leftover;
this._buffer[i2++] = 1;
for (; i2 < 16; i2++) {
this._buffer[i2] = 0;
}
this._fin = 1;
this._blocks(this._buffer, 0, 16);
}
c2 = this._h[1] >>> 13;
this._h[1] &= 8191;
for (i2 = 2; i2 < 10; i2++) {
this._h[i2] += c2;
c2 = this._h[i2] >>> 13;
this._h[i2] &= 8191;
}
this._h[0] += c2 * 5;
c2 = this._h[0] >>> 13;
this._h[0] &= 8191;
this._h[1] += c2;
c2 = this._h[1] >>> 13;
this._h[1] &= 8191;
this._h[2] += c2;
g3[0] = this._h[0] + 5;
c2 = g3[0] >>> 13;
g3[0] &= 8191;
for (i2 = 1; i2 < 10; i2++) {
g3[i2] = this._h[i2] + c2;
c2 = g3[i2] >>> 13;
g3[i2] &= 8191;
}
g3[9] -= 1 << 13;
mask = (c2 ^ 1) - 1;
for (i2 = 0; i2 < 10; i2++) {
g3[i2] &= mask;
}
mask = ~mask;
for (i2 = 0; i2 < 10; i2++) {
this._h[i2] = this._h[i2] & mask | g3[i2];
}
this._h[0] = (this._h[0] | this._h[1] << 13) & 65535;
this._h[1] = (this._h[1] >>> 3 | this._h[2] << 10) & 65535;
this._h[2] = (this._h[2] >>> 6 | this._h[3] << 7) & 65535;
this._h[3] = (this._h[3] >>> 9 | this._h[4] << 4) & 65535;
this._h[4] = (this._h[4] >>> 12 | this._h[5] << 1 | this._h[6] << 14) & 65535;
this._h[5] = (this._h[6] >>> 2 | this._h[7] << 11) & 65535;
this._h[6] = (this._h[7] >>> 5 | this._h[8] << 8) & 65535;
this._h[7] = (this._h[8] >>> 8 | this._h[9] << 5) & 65535;
f3 = this._h[0] + this._pad[0];
this._h[0] = f3 & 65535;
for (i2 = 1; i2 < 8; i2++) {
f3 = (this._h[i2] + this._pad[i2] | 0) + (f3 >>> 16) | 0;
this._h[i2] = f3 & 65535;
}
mac[macpos + 0] = this._h[0] >>> 0;
mac[macpos + 1] = this._h[0] >>> 8;
mac[macpos + 2] = this._h[1] >>> 0;
mac[macpos + 3] = this._h[1] >>> 8;
mac[macpos + 4] = this._h[2] >>> 0;
mac[macpos + 5] = this._h[2] >>> 8;
mac[macpos + 6] = this._h[3] >>> 0;
mac[macpos + 7] = this._h[3] >>> 8;
mac[macpos + 8] = this._h[4] >>> 0;
mac[macpos + 9] = this._h[4] >>> 8;
mac[macpos + 10] = this._h[5] >>> 0;
mac[macpos + 11] = this._h[5] >>> 8;
mac[macpos + 12] = this._h[6] >>> 0;
mac[macpos + 13] = this._h[6] >>> 8;
mac[macpos + 14] = this._h[7] >>> 0;
mac[macpos + 15] = this._h[7] >>> 8;
this._finished = true;
return this;
};
Poly13052.prototype.update = function(m2) {
var mpos = 0;
var bytes = m2.length;
var want;
if (this._leftover) {
want = 16 - this._leftover;
if (want > bytes) {
want = bytes;
}
for (var i2 = 0; i2 < want; i2++) {
this._buffer[this._leftover + i2] = m2[mpos + i2];
}
bytes -= want;
mpos += want;
this._leftover += want;
if (this._leftover < 16) {
return this;
}
this._blocks(this._buffer, 0, 16);
this._leftover = 0;
}
if (bytes >= 16) {
want = bytes - bytes % 16;
this._blocks(m2, mpos, want);
mpos += want;
bytes -= want;
}
if (bytes) {
for (var i2 = 0; i2 < bytes; i2++) {
this._buffer[this._leftover + i2] = m2[mpos + i2];
}
this._leftover += bytes;
}
return this;
};
Poly13052.prototype.digest = function() {
if (this._finished) {
throw new Error("Poly1305 was finished");
}
var mac = new Uint8Array(16);
this.finish(mac);
return mac;
};
Poly13052.prototype.clean = function() {
wipe_12.wipe(this._buffer);
wipe_12.wipe(this._r);
wipe_12.wipe(this._h);
wipe_12.wipe(this._pad);
this._leftover = 0;
this._fin = 0;
this._finished = true;
return this;
};
return Poly13052;
}()
);
exports.Poly1305 = Poly1305;
function oneTimeAuth(key, data) {
var h3 = new Poly1305(key);
h3.update(data);
var digest = h3.digest();
h3.clean();
return digest;
}
exports.oneTimeAuth = oneTimeAuth;
function equal2(a3, b2) {
if (a3.length !== exports.DIGEST_LENGTH || b2.length !== exports.DIGEST_LENGTH) {
return false;
}
return constant_time_12.equal(a3, b2);
}
exports.equal = equal2;
})(poly1305);
(function(exports) {
Object.defineProperty(exports, "__esModule", { value: true });
var chacha_1 = chacha;
var poly1305_1 = poly1305;
var wipe_12 = wipe$1;
var binary_12 = binary;
var constant_time_12 = constantTime;
exports.KEY_LENGTH = 32;
exports.NONCE_LENGTH = 12;
exports.TAG_LENGTH = 16;
var ZEROS = new Uint8Array(16);
var ChaCha20Poly1305 = (
/** @class */
function() {
function ChaCha20Poly13052(key) {
this.nonceLength = exports.NONCE_LENGTH;
this.tagLength = exports.TAG_LENGTH;
if (key.length !== exports.KEY_LENGTH) {
throw new Error("ChaCha20Poly1305 needs 32-byte key");
}
this._key = new Uint8Array(key);
}
ChaCha20Poly13052.prototype.seal = function(nonce, plaintext, associatedData, dst) {
if (nonce.length > 16) {
throw new Error("ChaCha20Poly1305: incorrect nonce length");
}
var counter = new Uint8Array(16);
counter.set(nonce, counter.length - nonce.length);
var authKey = new Uint8Array(32);
chacha_1.stream(this._key, counter, authKey, 4);
var resultLength = plaintext.length + this.tagLength;
var result;
if (dst) {
if (dst.length !== resultLength) {
throw new Error("ChaCha20Poly1305: incorrect destination length");
}
result = dst;
} else {
result = new Uint8Array(resultLength);
}
chacha_1.streamXOR(this._key, counter, plaintext, result, 4);
this._authenticate(result.subarray(result.length - this.tagLength, result.length), authKey, result.subarray(0, result.length - this.tagLength), associatedData);
wipe_12.wipe(counter);
return result;
};
ChaCha20Poly13052.prototype.open = function(nonce, sealed, associatedData, dst) {
if (nonce.length > 16) {
throw new Error("ChaCha20Poly1305: incorrect nonce length");
}
if (sealed.length < this.tagLength) {
return null;
}
var counter = new Uint8Array(16);
counter.set(nonce, counter.length - nonce.length);
var authKey = new Uint8Array(32);
chacha_1.stream(this._key, counter, authKey, 4);
var calculatedTag = new Uint8Array(this.tagLength);
this._authenticate(calculatedTag, authKey, sealed.subarray(0, sealed.length - this.tagLength), associatedData);
if (!constant_time_12.equal(calculatedTag, sealed.subarray(sealed.length - this.tagLength, sealed.length))) {
return null;
}
var resultLength = sealed.length - this.tagLength;
var result;
if (dst) {
if (dst.length !== resultLength) {
throw new Error("ChaCha20Poly1305: incorrect destination length");
}
result = dst;
} else {
result = new Uint8Array(resultLength);
}
chacha_1.streamXOR(this._key, counter, sealed.subarray(0, sealed.length - this.tagLength), result, 4);
wipe_12.wipe(counter);
return result;
};
ChaCha20Poly13052.prototype.clean = function() {
wipe_12.wipe(this._key);
return this;
};
ChaCha20Poly13052.prototype._authenticate = function(tagOut, authKey, ciphertext, associatedData) {
var h3 = new poly1305_1.Poly1305(authKey);
if (associatedData) {
h3.update(associatedData);
if (associatedData.length % 16 > 0) {
h3.update(ZEROS.subarray