"use strict";
/**
 * Fireblocks API
 * Fireblocks provides a suite of applications to manage digital asset operations and a complete development platform to build your business on the blockchain.  - Visit our website for more information: [Fireblocks Website](https://fireblocks.com) - Visit our developer docs: [Fireblocks DevPortal](https://developers.fireblocks.com)
 *
 * The version of the OpenAPI document: 1.6.2
 * Contact: developers@fireblocks.com
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BearerTokenProvider = void 0;
var jwt = __importStar(require("jsonwebtoken"));
var crypto = __importStar(require("crypto"));
var uuid_1 = require("uuid");
var BearerTokenProvider = /** @class */ (function () {
    /**
    * Configures the http bearer token provider with the required details.
    *
    * @param apiKey the API key to use
    * @param secretKey the secret key to use
    */
    function BearerTokenProvider(apiKey, secretKey) {
        this.apiKey = apiKey;
        this.secretKey = secretKey;
    }
    BearerTokenProvider.prototype.getToken = function (method, path, payload) {
        var url = new URL(path);
        return this.signJwt(this.apiKey, this.secretKey, "".concat(url.pathname).concat(url.search), this.isPayloadSignatureRequired(method) ? payload : undefined);
    };
    BearerTokenProvider.prototype.isPayloadSignatureRequired = function (method) {
        if (['POST', 'PATCH', 'PUT'].includes(method.toUpperCase())) {
            return true;
        }
        return false;
    };
    BearerTokenProvider.prototype.signJwt = function (apiKey, secretKey, path, bodyJson) {
        var token = jwt.sign({
            uri: path,
            nonce: (0, uuid_1.v4)(),
            iat: Math.floor(Date.now() / 1000),
            exp: Math.floor(Date.now() / 1000) + 55,
            sub: apiKey,
            bodyHash: crypto.createHash("sha256").update(bodyJson || "").digest().toString("hex")
        }, secretKey, { algorithm: "RS256" });
        return token;
    };
    return BearerTokenProvider;
}());
exports.BearerTokenProvider = BearerTokenProvider;
//# sourceMappingURL=bearerTokenProvider.js.map