Skip to content

Commit

Permalink
refactoring auth to util
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoputzer committed Jan 15, 2019
1 parent 6d8b5a7 commit d0d66cf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions test/auth.js → test/util.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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')
Expand Down
14 changes: 7 additions & 7 deletions auth.js → util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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')
}

0 comments on commit d0d66cf

Please sign in to comment.