【问题标题】:Nodejs Coinbase V2 REST endpoint returns invalid signatureNodejs Coinbase V2 REST 端点返回无效签名
【发布时间】:2022-06-19 11:46:33
【问题描述】:

无法弄清楚为什么 coinbase v2 REST 端点返回无效签名错误,也许有人看到我做错了什么。我发现的一切都与使用不再维护的旧 NPM 包有关。还有一个 Coinbase Pro 包,但我不想与 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);
  }  
})();

【问题讨论】:

    标签: node.js coinbase-api


    【解决方案1】:

    我要在这里添加一些东西,因为它之前让我发疯了,即。尝试 crypto-js 无济于事,然后不得不进行大量斗争并采取多种变通方法来使教程中使用的“crypto”能够克服目前遇到的所有障碍。

    据我所知,大多数无效签名都回到了 CB-ACCESS-SIGN,最大的挑战是弄清楚 crypto-js 中的等效签名是什么样的,并让所有这些在 Angular 10 中正常工作.

    API 调用的精简版本和访问标志的哈希字符串创建:

    import * as CryptoJS from 'crypto-js';
    
    async getUserCreds(apk: string, aps: string): Promise<any> {
      let access_sign = Access_Sign(getUnixTimestamp(), 'GET', '/v2/user','',aps)
    
      let httpOptions = {
        headers: new HttpHeaders({
          "CB-ACCESS-KEY": apk,
          "CB-ACCESS-SIGN": access_sign,
          "CB-ACCESS-TIMESTAMP": getUnixTimestamp().toString(),
          "Content-Type": "application/json"
        })
      }
    
      return this.http.get<any>('https://api.coinbase.com/v2/user',httpOptions)
        .pipe(shareReplay(), catchError((x) => { return this.handleErrorLog(x) 
        })).toPromise();
    }
    
    
    export function Access_Sign(timestamp: number, method: string, requestPath: string, body: string, secret: string) {
    
      let prehash = timestamp + method.toUpperCase() + requestPath + body;
      return CryptoJS.HmacSHA256(prehash, secret).toString(CryptoJS.enc.Hex);
    }
    
    export function getUnixTimestamp() {
      return Math.floor(Date.now() / 1000)
    }
    

    【讨论】:

    • 感谢您分享您的路径。
    【解决方案2】:

    我在这里找到了答案https://github.com/coinbase/coinbase-node/blob/master/lib/ClientBase.js#L101

    签名的正确代码是

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

    【讨论】:

      【解决方案3】:

      如果您仍然遇到此问题,请确保路径包含 /

      我遇到这个问题是因为message 上使用的path 部分是accounts 而不是/accounts

      【讨论】:

        猜你喜欢
        • 2019-04-07
        • 1970-01-01
        • 2015-10-17
        • 1970-01-01
        • 2020-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-12
        相关资源
        最近更新 更多