Gists
By the Google’s update at June 19, 2018, finally, Utilities.computeDigest(), Utilities.computeHmacSha256Signature() and Utilities.computeHmacSignature() got to be able to use the byte arrays. By this, using only native Google Apps Script, the result can be retrieved without using jsSHA. So Cryptopia API can be created using only Google Apps Script. If this is useful for you, I’m glad.
Sample script :
When you use this, at first, please input the requiring values. Now the sample values are used.
Gists
This sample script is for using Zaif API by Google Apps Script.
The following go script is a sample at Zaif API.
package main
import (
"fmt"
"time"
"strconv"
"crypto/hmac"
"crypto/sha512"
"io/ioutil"
"net/http"
"encoding/hex"
"net/url"
"strings"
)
var key = "<your_key>"
var secret = "<your_secret>"
func main() {
uri := "https://api.zaif.jp/tapi"
values := url.Values{}
values.Add("method", "get_info")
values.Add("nonce", strconv.FormatInt(time.Now().Unix(), 10))
encodedParams := values.Encode()
req, _ := http.NewRequest("POST", uri, strings.NewReader(encodedParams))
hash := hmac.New(sha512.New, []byte(secret))
hash.Write([]byte(encodedParams))
signature := hex.EncodeToString(hash.Sum(nil))
req.Header.Add("Key", key)
req.Header.Add("Sign", signature)
client := new(http.Client)
resp, _ := client.Do(req)
defer resp.Body.Close()
byteArray, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(byteArray))
}
When this is converted to GAS, the script is as follows.
Gists
This sample script is for using Bittrex API by Google Apps Script.
The following PHP script is a sample at bittrex.com.
$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);
When this is converted to GAS, the script is as follows.
function main() {
var apikey = '#####'; // Please input your key.
var apisecret = '#####'; // Please input your secret.
var nonce = Number(new Date().getTime() / 1000).toFixed(0);
var uri = 'https://bittrex.com/api/v1.1/market/getopenorders?apikey=' + apikey + '&nonce=' + nonce;
var sign = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, uri, apisecret);
sign = sign.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2);}).join("");
var params = {
method: "get",
headers: {Apisign: sign}
}
var res = UrlFetchApp.fetch(uri, params);
Logger.log(res.getContentText())
}
Gists
This sample script is for using Binance API by Google Apps Script. This script encryptes “signature” like samples. In this script, “Example 1: As a query string” is used, and it retrieves “All orders (SIGNED)” by “GET”.
function main() {
var key = '#####'; // Please input your key.
var secret = '#####'; // Please input your secret.
var api = "/api/v3/allOrders"; // Please input API Endpoint you want.
var timestamp = Number(new Date().getTime()).toFixed(0);
var string = "symbol=LTCBTC×tamp=" + timestamp; // Please input query parameters for the inputterd API.
var baseUrl = "https://api.binance.com";
var signature = Utilities.computeHmacSha256Signature(string, secret);
signature = signature.map(function(e) {
var v = (e < 0 ? e + 256 : e).toString(16);
return v.length == 1 ? "0" + v : v;
}).join("");
var query = "?" + string + "&signature=" + signature;
var params = {
'method': 'get',
'headers': {'X-MBX-APIKEY': key},
'muteHttpExceptions': true
};
var data = UrlFetchApp.fetch(baseUrl + api + query, params);
Logger.log(data.getContentText())
}
Gists
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().