Skip to content

Commit

Permalink
adds configurationFor utility to auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoputzer committed Jan 14, 2019
1 parent bc53476 commit 8bbf59e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
10 changes: 10 additions & 0 deletions auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

const crypto = require('crypto')

exports.configurationFor = ({ sandbox, hostname, key, passphrase, secret } = Object.prototype) => {
return {
npm_config_coinbase_pro_api_sandbox: String(sandbox),
npm_config_coinbase_pro_api_hostname: hostname,
npm_config_coinbase_pro_api_key: key,
npm_config_coinbase_pro_api_passphrase: passphrase,
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)
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.0.6",
"version": "0.1.0",
"description": "lightweight coinbase pro api implementation",
"main": "index.js",
"config": {
Expand Down
44 changes: 42 additions & 2 deletions test/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
test('coinbase-pro-api/auth', () => {
const { deepStrictEqual } = require('assert')
const { ok, strictEqual, deepStrictEqual } = require('assert')

test('.configurationFor', () => {
const { configurationFor } = require('../auth')

test('is callable', () => {
deepStrictEqual(typeof configurationFor, 'function')
})

test('returns empty configuration object by default', () => {
const configuration = configurationFor(undefined)
ok(configuration.hasOwnProperty('npm_config_coinbase_pro_api_sandbox'))
ok(configuration.hasOwnProperty('npm_config_coinbase_pro_api_hostname'))
ok(configuration.hasOwnProperty('npm_config_coinbase_pro_api_key'))
ok(configuration.hasOwnProperty('npm_config_coinbase_pro_api_passphrase'))
ok(configuration.hasOwnProperty('npm_config_coinbase_pro_api_secret'))
})

test('returns object env compatible object', () => {
const configuration = configurationFor({
sandbox: false,
hostname: 'hostname',
key: 'key',
passphrase: 'passphrase',
secret: 'secret'
})

deepStrictEqual(configuration, {
npm_config_coinbase_pro_api_sandbox: 'false',
npm_config_coinbase_pro_api_hostname: 'hostname',
npm_config_coinbase_pro_api_key: 'key',
npm_config_coinbase_pro_api_passphrase: 'passphrase',
npm_config_coinbase_pro_api_secret: 'secret'
})
})
})

test('.signatureFor', () => {
const { signatureFor } = require('../auth')
Expand All @@ -8,7 +43,7 @@ test('coinbase-pro-api/auth', () => {
deepStrictEqual(typeof signatureFor, 'function')
})

test('creates base64 encoded sha256 hmac signature using crypto', (done) => {
test('creates base64 encoded sha256 hmac signature', (done) => {
signatureFor({ timestamp: 1, method: 'get' }, undefined, {
createHmac (algorithm) {
deepStrictEqual(algorithm, 'sha256')
Expand All @@ -27,6 +62,11 @@ test('coinbase-pro-api/auth', () => {
})
})

test('defaults to crypto library', () => {
const signature = signatureFor({ timestamp: 1, method: 'get' })
strictEqual(Buffer.from(signature, 'base64').toString('base64'), signature)
})

test('takes an optional secret argument', (done) => {
const secret = Buffer.from('secret').toString('base64')
signatureFor({ timestamp: 1, method: 'get' }, { npm_config_coinbase_pro_api_secret: secret }, {
Expand Down

0 comments on commit 8bbf59e

Please sign in to comment.