-1

I am trying to convert python kraken API auth to Google Script to use it in a Google Spreadsheet, but with no luck.

# python sample    
def get_kraken_signature(urlpath, data, secret):

    postdata = urllib.parse.urlencode(data)
    encoded = (str(data['nonce']) + postdata).encode()
    message = urlpath.encode() + hashlib.sha256(encoded).digest()

    mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
    sigdigest = base64.b64encode(mac.digest())
    return sigdigest.decode()

api_sec = "kQH5HW/8p1uGOVjbgWA7FunAmGO8lsSUXNsu3eow76sz84Q18fWxnyRzBHCd3pd5nE9qa99HAZtuZuj6F1huXg=="

data = {
    "nonce": "1616492376594", 
    "ordertype": "limit", 
    "pair": "XBTUSD",
    "price": 37500, 
    "type": "buy",
    "volume": 1.25
}

signature = get_kraken_signature("/0/private/AddOrder", data, api_sec)
print("API-Sign: {}".format(signature))
# prints API-Sign: 4/dpxb3iT4tp/ZCVEwSnEsLxx0bqyhLpdfOpc6fn7OR8+UClSV5n9E6aSS8MPtnRfp32bAb0nmbRn6H8ndwLUQ==

I ended up with this, but it's not returning the same output.

# google script sample from my sheet
function get_kraken_sinature(url, data, nonce, secret) {
  var message = url + Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, Utilities.base64Encode(nonce + data));
  var base64Secret = Utilities.base64Decode(secret);
  var mac = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, message, secret);
  return Utilities.base64Encode(mac);
}

signature = get_kraken_signature("/0/private/AddOrder", data, api_sec)
print("API-Sign: {}".format(signature))
# prints API-Sign: Jn6Zk8v41uvMWOY/RTBTrb7zhGxyAOTclFFe7lySodBnEnXErfJgIcQb90opFwccuKDd0Nt1l71HT3V9+P8pUQ==

Both code samples should do the same, and are supposed to output an identical API-Sign key. They are not at this stage, and I am wondering why is that.

mArtinko5MB
  • 756
  • 8
  • 18
  • When your python script works and you want to convert your python script to Google Apps Script, can you provide the sample input and output values for your python script? And, it seems that in your Google Apps Script, the request cannot be seen. In this case, your script for requesting to the API works fine when the function `get_kraken_sinature` can return the correct value. Is my understanding correct? – Tanaike Oct 21 '21 at 06:43
  • Thanks for response, i have added param values and outputs. And yes, you are right ! – mArtinko5MB Oct 21 '21 at 11:18
  • Thank you for replying. From your replying, I would like to answer it. But in the current stage, unfortunately, I cannot answer your question because your question is closed. Now I voted to reopen your question. But it is required to also vote from other users. So can you wait for reopening your question? When your question is reopened, I would like to answer it. – Tanaike Oct 21 '21 at 11:57
  • I have no other option, i'll wait ;) – mArtinko5MB Oct 21 '21 at 12:47
  • 2
    Please avoid non specific terms like "not working". Always be specific about what's not working. I've edited that term out. – TheMaster Oct 21 '21 at 16:36
  • 1
    @Tanaike Feel free to make a reopen request in chat room or socvr if it isn't opened. – TheMaster Oct 22 '21 at 00:36
  • @mArtinko5MB I meant this line: `I ended up with this, but it's not working.` and the title, which is not specific. See differences from revision 3 to revision 4. If we want to be more specific, You should be able to figure out, at which step there's a difference between python version and apps script: the ``message`` or base64 or mac. I did cast a reopen vote after my editing. But unfortunately, reopening is a big issue, We haven't found a third guy willing to cast a reopen vote yet. Give it another 24 hours. In the mean time, you could try the debugging steps, I mentioned before. – TheMaster Oct 22 '21 at 18:25
  • @TheMaster i somehow missed the sentence. Sorry for that, frustration got the worst out of me. – mArtinko5MB Oct 24 '21 at 10:43

1 Answers1

1

I believe your goal is as follows.

  • You want to convert your python script to Google Apps Script.
  • In your Google Apps Script, you have already confirmed that the script for requesting works fine. You want to convert get_kraken_signature of your python script to Google Apps Script.

In this case, how about the following modified script?

Modified script:

function get_kraken_sinature(url, data, nonce, secret) {
  var str = Object.entries(data).map(([k, v]) => `${k}=${v}`).join("&");
  var message = Utilities.newBlob(url).getBytes().concat(Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, nonce + str));
  var mac = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, message, Utilities.base64Decode(secret));
  return Utilities.base64Encode(mac);
}

// Please run this function.
function main() {
  const data = {
    "nonce": "1616492376594",
    "ordertype": "limit",
    "pair": "XBTUSD",
    "price": 37500,
    "type": "buy",
    "volume": 1.25
  };
  var url = "/0/private/AddOrder";
  var nonce = "1616492376594";
  var secret = "kQH5HW/8p1uGOVjbgWA7FunAmGO8lsSUXNsu3eow76sz84Q18fWxnyRzBHCd3pd5nE9qa99HAZtuZuj6F1huXg==";
  var res = get_kraken_sinature(url, data, nonce, secret);
  console.log(res)
}

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Man, you are my savior. I knew i was using methods wrongly. This encoding shenanigans is beyond my brain so i very appreciate your response. I am gonna look at your solution to get it to my head. Again, thanks :) – mArtinko5MB Oct 24 '21 at 10:44