diff --git a/client.js b/client.js index 417de8a..ce94acf 100644 --- a/client.js +++ b/client.js @@ -3,7 +3,7 @@ const https = require('https') const info = require('./package') -const auth = require('./auth') +const util = require('./util') exports.request = (options, env = process.env, { request } = https, { hostnameFor, headersFor, toString } = exports) => { return new Promise((resolve, reject) => { @@ -35,7 +35,7 @@ exports.hostnameFor = ({ npm_config_coinbase_pro_api_hostname, npm_config_coinba : coinbase_pro_api_hostname } -exports.headersFor = (options, { npm_config_coinbase_pro_api_key = String.prototype, npm_config_coinbase_pro_api_passphrase = String.prototype, npm_config_coinbase_pro_api_secret = String.prototype } = process.env, { signatureFor } = auth, { name, version } = info) => { +exports.headersFor = (options, { npm_config_coinbase_pro_api_key = String.prototype, npm_config_coinbase_pro_api_passphrase = String.prototype, npm_config_coinbase_pro_api_secret = String.prototype } = process.env, { signatureFor } = util, { name, version } = info) => { const timestamp = 1e-3 * Date.now() // fixme: not sure if timestamp should be created here return { 'User-Agent': name + '/' + version, diff --git a/package.json b/package.json index 733b85c..3f57d7e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coinbase-pro-api", - "version": "0.1.1", + "version": "0.1.2", "description": "lightweight coinbase pro api implementation", "main": "index.js", "config": { diff --git a/test/auth.js b/test/util.js similarity index 95% rename from test/auth.js rename to test/util.js index 3632a10..6717f7d 100644 --- a/test/auth.js +++ b/test/util.js @@ -1,8 +1,8 @@ -test('coinbase-pro-api/auth', () => { +test('coinbase-pro-api/util', () => { const { ok, strictEqual, deepStrictEqual } = require('assert') test('.configurationFor', () => { - const { configurationFor } = require('../auth') + const { configurationFor } = require('../util') test('is callable', () => { deepStrictEqual(typeof configurationFor, 'function') @@ -37,7 +37,7 @@ test('coinbase-pro-api/auth', () => { }) test('.signatureFor', () => { - const { signatureFor } = require('../auth') + const { signatureFor } = require('../util') test('is callable', () => { deepStrictEqual(typeof signatureFor, 'function') diff --git a/auth.js b/util.js similarity index 100% rename from auth.js rename to util.js index e49f933..8a2c061 100644 --- a/auth.js +++ b/util.js @@ -2,6 +2,13 @@ const crypto = require('crypto') +exports.signatureFor = ({ timestamp, method, path = String.prototype, body = String.prototype }, { npm_config_coinbase_pro_api_secret = String.prototype } = process.env, { createHmac } = crypto) => { + const buffer = Buffer.from(npm_config_coinbase_pro_api_secret, 'base64') + return createHmac('sha256', buffer) + .update(timestamp + method.toUpperCase() + path + body) + .digest('base64') +} + exports.configurationFor = ({ sandbox, hostname, key, passphrase, secret } = Object.prototype) => { return { npm_config_coinbase_pro_api_sandbox: String(sandbox), @@ -11,10 +18,3 @@ exports.configurationFor = ({ sandbox, hostname, key, passphrase, secret } = Obj npm_config_coinbase_pro_api_secret: secret } } - -exports.signatureFor = ({ timestamp, method, path = String.prototype, body = String.prototype }, { npm_config_coinbase_pro_api_secret = String.prototype } = process.env, { createHmac } = crypto) => { - const buffer = Buffer.from(npm_config_coinbase_pro_api_secret, 'base64') - return createHmac('sha256', buffer) - .update(timestamp + method.toUpperCase() + path + body) - .digest('base64') -}