/** * 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. */ import * as jwt from "jsonwebtoken"; import * as crypto from "crypto"; import { v4 as uuid } from "uuid" export class BearerTokenProvider { /** * Configures the http bearer token provider with the required details. * * @param apiKey the API key to use * @param secretKey the secret key to use */ constructor(private apiKey: string, private secretKey: string) {} public getToken(method: string, path: string, payload?: any): string { const url = new URL(path); return this.signJwt(this.apiKey, this.secretKey, `${url.pathname}${url.search}`, this.isPayloadSignatureRequired(method) ? payload: undefined); } private isPayloadSignatureRequired(method: string): boolean { if (['POST', 'PATCH','PUT'].includes(method.toUpperCase())) { return true; } return false; } private signJwt(apiKey: string, secretKey: string, path: string, bodyJson?: any): string { const token = jwt.sign({ uri: path, nonce: uuid(), iat: Math.floor(Date.now() / 1000), exp: Math.floor(Date.now() / 1000) + 55, sub: apiKey, bodyHash: crypto.createHash("sha256").update(bodyJson || "").digest().toString("hex") } as any, secretKey, {algorithm: "RS256"}); return token; } }