Created
November 29, 2021 10:16
-
-
Save ericjeker/47185c9fe9127e9e2d2cefa033165d8e to your computer and use it in GitHub Desktop.
3Commas Helpers for generating a Signature and flatten a JSON body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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