Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions __tests__/krakenClient/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const KrakenClient = require('../../kraken.js')

describe('KrakenClient', async ()=>{
it('[Constructor] null param', ()=>{
try{ new KrakenClient() }
catch (e) {
expect(e.message).toBe('[Error] null key or secret')
}
})

it('[Constructor]', ()=>{
const key = 'mockKey'
const secret = 'mockSecret'
const kc = new KrakenClient(key, secret)
const reqConfig = require('../../config/request.json')

expect(kc.config).toEqual({
...reqConfig,
key,
secret
})
})

})
13 changes: 13 additions & 0 deletions __tests__/lib/signature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const {getMessageSignature} = require('../../lib/signature')

describe('KrakenClient', async ()=>{
it('[Constructor] null param', ()=>{
const path = '/mock/path'
const request = {mock:'foo'}
const secret = 'mockSecret'
const nonce = 'mockNonce'
const result = getMessageSignature(path, request, secret, nonce)

expect(result).toBe('VP8viIhM4IDc0gOPRN4oyDqyBr5/f/rSWMMt/NGjwlq9NcmcSnZESpQ1aT4lETVzmyfzUbR6cIi8+2V1wTWtlQ==')
})
})
4 changes: 4 additions & 0 deletions config/methods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"public" : [ "Time", "Assets", "AssetPairs", "Ticker", "Depth", "Trades", "Spread", "OHLC" ],
"private" : [ "Balance", "TradeBalance", "OpenOrders", "ClosedOrders", "QueryOrders", "TradesHistory", "QueryTrades", "OpenPositions", "Ledgers", "QueryLedgers", "TradeVolume", "AddOrder", "CancelOrder", "DepositMethods", "DepositAddresses", "DepositStatus", "WithdrawInfo", "Withdraw", "WithdrawStatus", "WithdrawCancel" ]
}
5 changes: 5 additions & 0 deletions config/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"url" : "https://api.kraken.com",
"version" : 0,
"timeout" : 5000
}
11 changes: 11 additions & 0 deletions example/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const key = process.env.KRAKEN_API_KEY // replace your key here
const secret = process.env.KRAKEN_API_SECRET // replace your secret here
const KrakenClient = require('../kraken.js')

(async () => {
// Display user's balance
console.log(await kraken.api('Balance'));

// Get Ticker Info
console.log(await kraken.api('Ticker', { pair : 'XXBTZUSD' }));
})();
41 changes: 9 additions & 32 deletions kraken.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
const got = require('got');
const crypto = require('crypto');
const qs = require('qs');
const {getMessageSignature} = require('./lib/signature')

// Public/Private method names
const methods = {
public : [ 'Time', 'Assets', 'AssetPairs', 'Ticker', 'Depth', 'Trades', 'Spread', 'OHLC' ],
private : [ 'Balance', 'TradeBalance', 'OpenOrders', 'ClosedOrders', 'QueryOrders', 'TradesHistory', 'QueryTrades', 'OpenPositions', 'Ledgers', 'QueryLedgers', 'TradeVolume', 'AddOrder', 'CancelOrder', 'DepositMethods', 'DepositAddresses', 'DepositStatus', 'WithdrawInfo', 'Withdraw', 'WithdrawStatus', 'WithdrawCancel' ],
};
const methods = require('./config/methods.json');

// Default options
const defaults = {
url : 'https://api.kraken.com',
version : 0,
timeout : 5000,
};

// Create a signature for a request
const getMessageSignature = (path, request, secret, nonce) => {
const message = qs.stringify(request);
const secret_buffer = new Buffer(secret, 'base64');
const hash = new crypto.createHash('sha256');
const hmac = new crypto.createHmac('sha512', secret_buffer);
const hash_digest = hash.update(nonce + message).digest('binary');
const hmac_digest = hmac.update(path + hash_digest, 'binary').digest('base64');

return hmac_digest;
};
const defaults = require('./config/request.json');

// Send an API request
const rawRequest = async (url, headers, data, timeout) => {
Expand Down Expand Up @@ -67,12 +48,12 @@ const rawRequest = async (url, headers, data, timeout) => {
*/
class KrakenClient {
constructor(key, secret, options) {
if(!key || !secret) throw new Error('[Error] null key or secret')

// Allow passing the OTP as the third argument for backwards compatibility
if(typeof options === 'string') {
options = { otp : options };
}
if(typeof options === 'string') options = { otp : options };

this.config = Object.assign({ key, secret }, defaults, options);
this.config = { ...defaults, key, secret, options};
}

/**
Expand Down Expand Up @@ -107,9 +88,7 @@ class KrakenClient {
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
publicMethod(method, params, callback) {
params = params || {};

publicMethod(method, params = {}, callback) {
// Default params to empty object
if(typeof params === 'function') {
callback = params;
Expand All @@ -136,9 +115,7 @@ class KrakenClient {
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
privateMethod(method, params, callback) {
params = params || {};

privateMethod(method, params = {}, callback) {
// Default params to empty object
if(typeof params === 'function') {
callback = params;
Expand Down
15 changes: 15 additions & 0 deletions lib/signature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const crypto = require('crypto');
const qs = require('qs');

// Create a signature for a request
const getMessageSignature = (path='', request={}, secret='', nonce='') => {
const message = qs.stringify(request);
const secret_buffer = new Buffer(secret, 'base64');
const hash = new crypto.createHash('sha256');
const hmac = new crypto.createHmac('sha512', secret_buffer);
const hash_digest = hash.update(nonce + message).digest('binary');
const hmac_digest = hmac.update(path + hash_digest, 'binary').digest('base64');
return hmac_digest;
};

module.exports = {getMessageSignature};
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "kraken-api",
"version": "1.0.0",
"description": "kraken.com API client library for NodeJS",
"scripts": {
"test": "jest"
},
"keywords": [
"kraken",
"api",
Expand All @@ -27,5 +30,8 @@
"url": "https://github.com/nothingisdead/npm-kraken-api/issues",
"email": "[email protected]"
},
"readmeFilename": "README.md"
"readmeFilename": "README.md",
"devDependencies": {
"jest": "^22.4.3"
}
}