@lunch-money/kraken-extension
Version:
A wrapper around the Kraken API for enabling Lunch Money to gather information about a user's account.
73 lines • 2.57 kB
JavaScript
import * as crypto from 'crypto';
import qs from 'qs';
import axios from 'axios';
export default class KrakenAPI {
client;
connection;
config = {
url: 'https://api.kraken.com/',
version: 0,
};
routes = {
PUBLIC_SYSTEM_STATUS: 'public/SystemStatus',
PRIVATE_BALANCE: 'private/Balance',
};
constructor(connection) {
this.client = axios.create({
baseURL: this.config.url + this.config.version,
headers: {
'User-Agent': 'LunchMoney Kraken Client',
'API-Key': connection.apiKey,
'Content-Type': 'application/x-www-form-urlencoded',
},
// always resolve as errors are custom handled
validateStatus: () => {
return true;
},
});
this.connection = connection;
}
// Query Funds permission
async getAccountBalances() {
const nonce = KrakenAPI.genNonce();
let payload = { nonce: nonce };
if (this.connection.otp) {
payload = { ...payload, otp: this.connection.otp };
}
return this.client.post(this.routes.PRIVATE_BALANCE, KrakenAPI.formUrlEncoded(payload), this.options(this.routes.PRIVATE_BALANCE, payload, this.connection.apiSecret, nonce));
}
async getSystemStatus() {
return this.client.get(this.routes.PUBLIC_SYSTEM_STATUS, {
responseType: 'json',
});
}
options(path, payload, secret, nonce) {
return {
headers: {
'API-Sign': KrakenAPI.getMessageSignature('/' + this.config.version + '/' + path, payload, secret, nonce),
},
responseType: 'json',
};
}
static getMessageSignature(path, request, key, nonce) {
const message = qs.stringify(request);
const secret_buffer = new Buffer(key, 'base64');
const hash = crypto.createHash('sha256');
const hmac = crypto.createHmac('sha512', secret_buffer);
const hash_digest = hash.update(nonce + message).digest('binary');
return hmac.update(path + hash_digest, 'binary').digest('base64');
}
static genNonce() {
return Date.now() * 1000;
}
static formUrlEncoded(payload) {
return Object.keys(payload).reduce(function (p, c) {
let prefix = '&';
if (!p) {
prefix = '';
}
return p + prefix + `${c}=${encodeURIComponent(payload[c])}`;
}, '');
}
}
//# sourceMappingURL=krakenAPI.js.map