@coin-voyage/paykit
Version:
Seamless crypto payments. Onboard users from any chain, any coin into your app with one click.
40 lines • 1.82 kB
JavaScript
import { createHash } from "crypto";
/**
* Generates an authorization signature for API requests.
*
* ⚠️ **Warning:** This function should only be run on the server.
* It uses the API secret, which must remain confidential. Running this
* code in a client-side environment risks exposing sensitive information
* to end users or malicious actors.
*
* This function creates a signed authorization string using the provided API key and secret.
* The generated string includes the API key, a timestamp (in seconds since epoch),
* and a SHA-512 hash signature of the concatenated API key, secret, and timestamp.
*
* @param {string} apiKey - The API key used for authentication.
* @param {string} apiSecret - The API secret used to generate the signature.
* @returns {string} - A formatted authorization string in the format:
* `APIKey=<apiKey>,signature=<signature>,timestamp=<timestamp>`.
*
* @example
* const apiKey = 'yourApiKey';
* const apiSecret = 'yourApiSecret';
* const authSignature = generateAuthorizationSignature(apiKey, apiSecret);
* console.log(authSignature);
* // Example output:
* // APIKey=yourApiKey,signature=2ee76043a90a...,timestamp=1737106896
*/
export function generateAuthorizationSignature(apiKey, apiSecret) {
// Get the current timestamp as a string
const timestamp = Math.floor(Date.now() / 1000).toString();
// Create the data string
const data = apiKey + apiSecret + timestamp;
// Create a SHA-512 hash of the data
const hash = createHash('sha512');
hash.update(data);
// Encode the hash as a hexadecimal string
const signature = hash.digest('hex');
// Format the final string
return `APIKey=${apiKey},signature=${signature},timestamp=${timestamp}`;
}
//# sourceMappingURL=generate-signature.js.map