Last active
October 2, 2020 05:55
-
-
Save c9s/5b35c5ff741c7772eedffe995e3b27b5 to your computer and use it in GitHub Desktop.
MAX Exchange API client in Bash
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
#!/bin/bash | |
base_url="https://max-api.maicoin.com" | |
| |
ts=$(date "+%s") | |
server_ts=$(curl -s $base_url/api/v2/timestamp) | |
| |
offset=$(( $server_ts - $ts )) | |
| |
function nonce() | |
{ | |
local mts=$(($ts * 1000)) | |
echo $mts | |
} | |
| |
| |
function generate_payload() | |
{ | |
local req_path=$1 | |
local -n ref_params=$2 | |
| |
declare -A payload | |
payload[nonce]=$(nonce) | |
payload[path]=$req_path | |
| |
for k in "${!ref_params[@]}" | |
do | |
payload[$k]=${ref_params[$k]} | |
done | |
| |
for k in "${!payload[@]}" | |
do | |
echo "\"$k\"" | |
echo "\"${payload[$k]}\"" | |
done | jq -c -n 'reduce inputs as $i ({}; . + { ($i): (input|(tonumber? // .)) })' | |
} | |
| |
function hmac_sha1() | |
{ | |
local value=$1 | |
local key=$2 | |
echo -n "$value" | openssl dgst -sha256 -hmac "$key" | sed -e 's/^.* //' | |
} | |
| |
function accounts() | |
{ | |
req_path="/api/v2/members/accounts" | |
declare -A params=() | |
payload_json=$(generate_payload $req_path params) | |
| |
encoded_payload=$(echo -n "$payload_json" | base64) | |
| |
payload_sig=$(hmac_sha1 "$encoded_payload" "$MAX_API_SECRET") | |
| |
curl -X GET -H "Content-Type: application/json" \ | |
-H "Accept: application/json" \ | |
-H "X-MAX-ACCESSKEY: $MAX_API_KEY" \ | |
-H "X-MAX-PAYLOAD: $encoded_payload" \ | |
-H "X-MAX-SIGNATURE: $payload_sig" \ | |
--data "$payload_json" \ | |
"${base_url}${req_path}" | |
} | |
| |
| |
| |
accounts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment