Created
January 3, 2018 03:44
-
-
Save josephg/8e088d5206e9524785f0efb92367f157 to your computer and use it in GitHub Desktop.
Cryptopia API from nodejs
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
const request = require('request') | |
const crypto = require('crypto') | |
const API_KEY = 'API KEY GOES HERE' | |
const API_SECRET = Buffer.from('SECRET GOES HERE', 'base64') | |
const md5 = str => crypto.createHash('md5').update(str).digest().toString('base64') | |
const hmac = str => crypto.createHmac('sha256', API_SECRET).update(str).digest().toString('base64') | |
const getBalance = (currency) => { | |
const params = {Currency: currency} | |
const url = 'https://www.cryptopia.co.nz/api/GetBalance' | |
const nonce = ''+Date.now() | |
const data = JSON.stringify(params) | |
const digest = md5(data) | |
// WHYYYY? | |
const u2 = encodeURIComponent(url).toLowerCase() | |
const sig = API_KEY + 'POST' + u2 + nonce + digest | |
const signed = hmac(sig) | |
const auth = 'amx ' + API_KEY + ':' + signed + ':' + nonce | |
const headers = { | |
'Authorization': auth, | |
'Content-Type': 'application/json; charset=utf-8', | |
} | |
request.post({ | |
url, | |
headers, | |
body: data, | |
}, (err, result) => { | |
console.log(err, result && result.body) | |
}) | |
} | |
getBalance('BTC') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment