Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = {
'COIN-',
'FIAT-',
'ME-',
'ANT-',
'#', // Prefix used by GitHub issues
],
},
Expand Down
8 changes: 6 additions & 2 deletions modules/sdk-hmac/src/hmac.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHmac } from 'crypto';
import { type BinaryLike, createHmac, type KeyObject } from 'crypto';
import * as urlLib from 'url';
import * as sjcl from '@bitgo/sjcl';
import {
Expand All @@ -16,7 +16,7 @@ import {
* @param message {String} - the actual message to HMAC
* @returns {*} - the result of the HMAC operation
*/
export function calculateHMAC(key: string, message: string): string {
export function calculateHMAC(key: string | BinaryLike | KeyObject, message: string | BinaryLike): string {
return createHmac('sha256', key).update(message).digest('hex');
}

Expand All @@ -37,6 +37,10 @@ export function calculateHMACSubject({
method,
authVersion,
}: CalculateHmacSubjectOptions): string {
/* Normalize legacy 'del' to 'delete' for backward compatibility */
if (method === 'del') {
method = 'delete';
}
const urlDetails = urlLib.parse(urlPath);
const queryPath = urlDetails.query && urlDetails.query.length > 0 ? urlDetails.path : urlDetails.pathname;
if (statusCode !== undefined && isFinite(statusCode) && Number.isInteger(statusCode)) {
Expand Down
2 changes: 1 addition & 1 deletion modules/sdk-hmac/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const supportedRequestMethods = ['get', 'post', 'put', 'del', 'patch', 'options'] as const;
export const supportedRequestMethods = ['get', 'post', 'put', 'del', 'patch', 'options', 'delete'] as const;

export type AuthVersion = 2 | 3;

Expand Down
15 changes: 15 additions & 0 deletions modules/sdk-hmac/test/hmac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
verifyResponse,
} from '../src/hmac';
import * as sjcl from '@bitgo/sjcl';
import { createSecretKey } from 'crypto';

// Mock Date.now for consistent timestamp values
const MOCK_TIMESTAMP = 1672531200000; // Example timestamp (e.g., Jan 1, 2023, 00:00:00 UTC)
Expand All @@ -30,6 +31,20 @@ describe('HMAC Utility Functions', () => {
const expectedHmac = 'f8c2bb87c17608c9038eab4e92ef2775e42629c939d6fd3390d42f80af6bb712';
expect(calculateHMAC(key, message)).to.equal(expectedHmac);
});

it('should accept a Buffer key (BinaryLike) and match the string result', () => {
const keyBuffer = Buffer.from('test-key', 'utf8');
const message = Buffer.from('test-message');
const expectedHmac = 'f8c2bb87c17608c9038eab4e92ef2775e42629c939d6fd3390d42f80af6bb712';
expect(calculateHMAC(keyBuffer, message)).to.equal(expectedHmac);
});

it('should accept a KeyObject key and match the string result', () => {
const keyObject = createSecretKey(Buffer.from('test-key', 'utf8'));
const message = 'test-message';
const expectedHmac = 'f8c2bb87c17608c9038eab4e92ef2775e42629c939d6fd3390d42f80af6bb712';
expect(calculateHMAC(keyObject, message)).to.equal(expectedHmac);
});
});

describe('calculateHMACSubject', () => {
Expand Down
Loading