Skip to content

Instantly share code, notes, and snippets.

@ericjeker
Created November 29, 2021 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericjeker/47185c9fe9127e9e2d2cefa033165d8e to your computer and use it in GitHub Desktop.
Save ericjeker/47185c9fe9127e9e2d2cefa033165d8e to your computer and use it in GitHub Desktop.
3Commas Helpers for generating a Signature and flatten a JSON body
/**
* Create a proper 3Commas signature (HMACSHA256)
*
* @param str string
* @param secret string
* @return {string}
*/
export function sign(str, secret) {
return crypto.createHmac('sha256', secret).update(str).digest('hex');
}
/**
* Take an object, parse it and stringify it and render a flat string.
* Careful: this will remove all spaces in your JSON strings.
*
* @param body object
* @return {string}
*/
export function transformBody(body) {
return JSON.stringify(body)
.replace(/(\r\n|\n|\r)/gm, "")
.replace(/\s+/g, "");
}
/**
* EXAMPLE
*/
export function main() {
const uri = '/public/api/v2/smart_trades';
const body = {foo: 'bar'};
const fullParams = `${uri}?${transformBody(body)}`;
const signature = sign(fullParams, process.env.THREECOMMAS_SECRET);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment