"use strict";
// Util functions copied from attestation JS library
Object.defineProperty(exports, "__esModule", { value: true });
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;
