@lunch-money/kraken-extension
Version:
A wrapper around the Kraken API for enabling Lunch Money to gather information about a user's account.
113 lines • 4.81 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const crypto = __importStar(require("crypto"));
const qs_1 = __importDefault(require("qs"));
const axios_1 = __importDefault(require("axios"));
class KrakenAPI {
constructor(connection) {
this.config = {
url: 'https://api.kraken.com/',
version: 0,
};
this.routes = {
PUBLIC_SYSTEM_STATUS: 'public/SystemStatus',
PRIVATE_BALANCE: 'private/Balance',
};
this.client = axios_1.default.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
getAccountBalances() {
return __awaiter(this, void 0, void 0, function* () {
const nonce = KrakenAPI.genNonce();
let payload = { nonce: nonce };
if (this.connection.otp) {
payload = Object.assign(Object.assign({}, 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));
});
}
getSystemStatus() {
return __awaiter(this, void 0, void 0, function* () {
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_1.default.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])}`;
}, '');
}
}
exports.default = KrakenAPI;
//# sourceMappingURL=krakenAPI.js.map