This sample script is converted this sample script (javascript) to Google Apps Script. The point for converting is signature
as shown in the following sample script.
- At Bitfinex API, after
"/api/" + apiPath + nonce + rawBody
is encrypted using HMAC SHA-384, the data of byte array is converted to HEX.- In Google Apps Script, there is no the method for this.
- The data which was encrypted by
Utilities.computeHmacSignature()
is the bytes array of the signed hexadecimal. - On the other hand, at this sample script for javascript, the data which was encrypted by
crypto.createHmac('sha384', apiSecret).update(signature).digest('hex')
is the string of the unsigned hexadecimal.
In order to achieve above, I made the method of bytesToHex()
.
function bytesToHex(data) {
return data.map(function(e) {
var v = (e < 0 ? e + 256 : e).toString(16);
return v.length == 1 ? "0" + v : v;
}).join("");
}
function main() {
const apiKey = '<Your API key here>';
const apiSecret = '<Your API secret here>';
const apiPath = "v2/auth/r/alerts";
const nonce = Date.now().toString();
const body = { "type": "price" };
const rawBody = JSON.stringify(body);
var signature = "/api/" + apiPath + nonce + rawBody;
signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_384, signature, apiSecret);
signature = bytesToHex(signature);
const url = "https://api.bitfinex.com/" + apiPath;
const options = {
method: 'POST',
contentType: "application/json",
headers: {
'bfx-nonce': nonce,
'bfx-apikey': apiKey,
'bfx-signature': signature
},
payload: rawBody
};
var res = UrlFetchApp.fetch(url, options);
Logger.log(res.getContentText())
}