node.jscoinbase-api

Nodejs Coinbase V2 REST endpoint returns invalid signature


Could not figure out why the coinbase v2 REST endpoint returns an invalid signature error, maybe someone see what I'm doing wrong. Everything I have found relates to the use of an old NPM package that is no longer maintained. There is still a Coinbase Pro package, but I don't want to communicate with the Pro API.

const { createHmac } = require('crypto');
const axios = require('axios');

(async () => {

  const cbApiKey = 'xxx';
  const apiSecret = 'xxx';
  const method = 'GET';
  const path = '/v2/user';
  const body = '';
 
  const timestamp = Math.floor(new Date().getTime() * 1e-3);
  const message = timestamp + method + path + body;

  const key = Buffer.from(apiSecret, 'base64');
  const cbAccessSign = createHmac('sha256', key).update(message).digest('base64');

  const instance = axios.create();

  try {
    const user = await instance.request({
      method,
      url: `https://api.coinbase.com${path}`,
      headers: {
        'CB-ACCESS-KEY': `${cbApiKey}`,
        'CB-ACCESS-SIGN': `${cbAccessSign}`,
        'CB-ACCESS-TIMESTAMP': `${timestamp}`,
        "Content-Type": 'application/json',
      },
    }); 
    console.log(user);
  } catch (error) {
    console.log(error);
  }  
})();


Solution

  • I found the answer here https://github.com/coinbase/coinbase-node/blob/master/lib/ClientBase.js#L101

    The correct code for the signature is

    var signature = crypto.createHmac('sha256', this.apiSecret).update(message).digest('hex');