"use strict";
// Util functions copied from attestation JS library
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAddress = exports.encode = exports.decode = exports.uint8tohex = exports.base64toBase64Url = exports.hexStringToBase64Url = exports.uint8toString = exports.uint8arrayToBase64 = exports.hexStringToBase64 = exports.hexStringToUint8 = exports.hexStringToArray = void 0;
function hexStringToArray(str = '') {
    if (str.substr(0, 2).toLowerCase() === "0x") {
        str = str.substr(2);
    }
    let arr = [];
    let strArr = [...str];
    if (strArr.length % 2)
        strArr.unshift('0');
    let tempStr = '';
    if (!strArr || typeof strArr == "undefined" || !strArr.length)
        return [];
    let tmpVal;
    while (strArr.length) {
        tempStr = '';
        // @ts-ignore
        tempStr += strArr.shift() + strArr.shift();
        tmpVal = parseInt(tempStr, 16);
        if (isNaN(tmpVal)) {
            throw new Error("hexStringToArray input is not a hex string.");
        }
        arr.push(tmpVal);
    }
    return arr;
}
exports.hexStringToArray = hexStringToArray;
function hexStringToUint8(str = '') {
    return Uint8Array.from(hexStringToArray(str));
}
exports.hexStringToUint8 = hexStringToUint8;
function hexStringToBase64(str = '') {
    return uint8arrayToBase64(hexStringToUint8(str));
}
exports.hexStringToBase64 = hexStringToBase64;
function uint8arrayToBase64(bytes) {
    let binary = uint8toString(bytes);
    if (typeof window === 'undefined' || !window.btoa) {
        let buff = new Buffer(binary);
        return buff.toString('base64');
    }
    else {
        return window.btoa(binary);
    }
}
exports.uint8arrayToBase64 = uint8arrayToBase64;
function uint8toString(uint8) {
    if (!uint8)
        return '';
    let binary = '';
    let len = uint8.byteLength;
    for (let i = 0; i < len; i++) {
        binary += String.fromCharCode(uint8[i]);
    }
    return binary;
}
exports.uint8toString = uint8toString;
function hexStringToBase64Url(str = '') {
    return base64toBase64Url(uint8arrayToBase64(hexStringToUint8(str)));
}
exports.hexStringToBase64Url = hexStringToBase64Url;
function base64toBase64Url(base64) {
    return base64.split('/').join('_')
        .split('+').join('-');
    // .split('=').join('.');
}
exports.base64toBase64Url = base64toBase64Url;
function uint8tohex(uint8) {
    if (!uint8 || !uint8.length)
        return '';
    return Array.from(uint8).map(i => ('0' + i.toString(16)).slice(-2)).join('');
}
exports.uint8tohex = uint8tohex;
function decode(key, cyphertextB64) {
    try {
        const buffer = Buffer.from(cyphertextB64, 'base64');
        const cyphertext = buffer.toString('hex');
        let parsed = cyphertext.match(/.{1,2}/g).map(x => parseInt(x, 16));
        let plaintext = [];
        for (let i = 0; i < parsed.length; i++) {
            plaintext.push((parsed[i] ^ key.charCodeAt(Math.floor(i % key.length))).toString(16).padStart(2, '0'));
        }
        return decodeURIComponent('%' + plaintext.join('').match(/.{1,2}/g).join('%'));
    }
    catch (e) {
        return false;
    }
}
exports.decode = decode;
function encode(key, plaintext) {
    let cyphertext = [];
    // Convert to hex to properly handle UTF8
    let convert = Array.from(plaintext).map(function (c) {
        if (c.charCodeAt(0) < 128)
            return c.charCodeAt(0).toString(16).padStart(2, '0');
        else
            return encodeURIComponent(c).replace(/\%/g, '').toLowerCase();
    }).join('');
    // Convert each hex to decimal
    let stripped = convert.match(/.{1,2}/g)?.map(x => parseInt(x, 16));
    // Perform xor operation
    for (let i = 0; i < plaintext.length; i++) {
        cyphertext.push(stripped[i] ^ key.charCodeAt(Math.floor(i % key.length)));
    }
    // Convert to hex
    cyphertext = cyphertext.map(function (x) {
        return x.toString(16).padStart(2, '0');
    });
    return hexToBase64(cyphertext.join(''));
}
exports.encode = encode;
//Can this be done with a library function?
function hexToBase64(hexstring) {
    const matcher = hexstring.match(/\w{2}/g);
    return btoa(matcher.map(function (a) {
        return String.fromCharCode(parseInt(a, 16));
    }).join(""));
}
function isAddress(address) {
    return (/^(0x)?[0-9a-f]{40}$/i.test(address));
    /*if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
        return false;
    } else {
        return true;
    }*/
}
exports.isAddress = isAddress;
