Currently, I am attempting to showcase various data points from my BitStamp account on a Google Spreadsheet. To accomplish this task, the Google Apps Script (GAS) that utilizes JavaScript is being utilized.
The issue at hand involves an error with code API0005. Upon referencing the BitStamp API documentation, it appears that this error signifies an "invalid signature:".
API0005 Invalid signature Posted signature doesn't match with ours
Despite researching extensively on platforms like Stack Overflow, pinpointing the root cause of this problem has proven to be challenging.
An interesting observation I've made pertains to the variation in the output of the nonce as different steps of the "signature synthesis process" are appended within the spreadsheet itself. The dynamic nature of the nonce output changing slightly between each function call or invocation does not come as a surprise, given its design.
However, one question that arises is whether it's normal for the nonce to alter even when the toUpperCase()
conversion is executed? This may not necessarily be the reason behind the overall issue at hand.
https://i.sstatic.net/4d9Gw.png
var nonce = new (function() {
this.generate = function() {
var now = Date.now();
this.counter = (now === this.last? this.counter + 1 : 0);
this.last = now;
// add padding to nonce
var padding =
this.counter < 10 ? '000' :
this.counter < 100 ? '00' :
this.counter < 1000 ? '0' : '';
return now+padding+this.counter;
};
})();
//write function
function write() {
var cred = {
id:'digit2118',
key:'2RhZfUKIYJbT8CBYk6T27uRSF8Gufre',
secret:'T8CBYk6T274yyR8Z2RhZfUxbRbmZZHJ'
};
//Adding cell outputs to monitor each step of the "conversion" process
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("B14");
cell.setValue(cred.id);
// .. and so forth, refer to screenshot ..
var message = nonce.generate() + cred.id + cred.key;
var res = Utilities.computeHmacSha256Signature(cred.secret, message).map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join("");
var signature = res.toUpperCase();
// Additional data for actual communication
var data = {
key: cred.key,
signature: res,
nonce: nonce.generate()
};
var options = {
'method' : 'post',
//'contentType': 'application/json',
//'payload' : JSON.stringify(data)
'muteHttpExceptions' : true,
'payload' : data
};
var response = UrlFetchApp.fetch('https://www.bitstamp.net/api/v2/balance/', options);
var responseJSON = JSON.parse(response.getContentText());
Logger.log(responseJSON);
return signature;
}//end of write();
Logger.log(write());
Although the exact omission remains elusive, something crucial seems to have slipped through the cracks.
(ps:this is where I got the Nonce code from: Generate Nonce,)
EDIT: Question Solved.
Updated code with problem & solution below.
Special thanks to @Tanaike