nodejs-cryptomus
Version:
A comprehensive Node.js client for the Cryptomus API
36 lines (31 loc) • 1.04 kB
text/typescript
import * as crypto from 'crypto';
// Add explicit type import for Node.js Buffer
import { Buffer } from 'buffer';
/**
* Generate signature for API requests
*
* @param payload - Request payload
* @param apiKey - API key
* @returns Generated signature
*/
export function generateSignature(payload: Record<string, any>, apiKey: string): string {
const payloadString = JSON.stringify(payload);
const sign = crypto.createHash('md5')
.update(Buffer.from(payloadString).toString('base64') + apiKey)
.digest('hex');
return sign;
}
/**
* Verify webhook signature
*
* @param payload - Webhook payload
* @param signature - Signature from webhook headers
* @param apiKey - API key
* @returns Whether the signature is valid
*/
export function verifyWebhookSignature(payload: string, signature: string, apiKey: string): boolean {
const calculatedSignature = crypto.createHash('md5')
.update(Buffer.from(payload).toString('base64') + apiKey)
.digest('hex');
return calculatedSignature === signature;
}