Created
June 27, 2023 13:29
-
-
Save nieldw/74946be3077407fc02da54f4e813476a to your computer and use it in GitHub Desktop.
Postman pre-request script for signing requests to Bittrex V3 api
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
// You can import the Bittrex v3 API collection into Postman: https://github.com/Bittrex/bittrex.github.io/tree/master/src/_data | |
// To use this script ensure the following is available in Postman: | |
// A Collection Variable `baseUrl` | |
// An Environment Variable BITTREX_API_KEY | |
// An Environment Variable BITTREX_API_SECRET | |
// set up vars | |
let timestamp = new Date().getTime() | |
let url = pm.request['url'] | |
let method = pm.request['method'] | |
function getPath(url) { | |
var pathRegex = /(?:.+?\:\/\/.+?)?(\/.+)/; | |
var result = url.match(pathRegex); | |
return result && result.length > 1 ? result[1] : ''; | |
} | |
function getContentHash(httpMethod, requestBody) { | |
if (httpMethod == 'GET' || !requestBody) { | |
requestBody = ''; | |
} else { | |
requestBody = requestBody.toString(); | |
} | |
return CryptoJS.SHA512(requestBody).toString(CryptoJS.enc.Hex); | |
} | |
function getHmacDigest(httpMethod, requestUrl, requestBody) { | |
var requestPath = getPath(requestUrl.toString()); | |
let contentHash = getContentHash(httpMethod, requestBody); | |
let baseUrl = pm.collectionVariables.get('baseUrl') | |
var preSign = [timestamp, baseUrl, requestPath, httpMethod.toUpperCase(), contentHash].join(''); | |
let secret = postman.getEnvironmentVariable("BITTREX_API_SECRET"); | |
var hmacDigest = CryptoJS.enc.Hex.stringify(CryptoJS.HmacSHA512(preSign, secret)); | |
return hmacDigest; | |
} | |
var signature = getHmacDigest(pm.request.method, pm.request.url, pm.request.body) | |
// Api-Key | |
pm.request.headers.add({ | |
key: 'Api-Key', | |
value: postman.getEnvironmentVariable("BITTREX_API_KEY") | |
}); | |
// Api-Timestamp | |
pm.request.headers.add({ | |
key: 'Api-Timestamp', | |
value: timestamp | |
}); | |
// Api-Content-Hash | |
pm.request.headers.add({ | |
key: 'Api-Content-Hash', | |
value: getContentHash(pm.request.method, pm.request.body) | |
}); | |
// Api-Signature | |
pm.request.headers.add({ | |
key: 'Api-Signature', | |
value: signature | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment