使用CryptoJS进行签名的React / Native中的Coinbase Pro(GDAX)API密钥身份验证

时间:2019-05-23 10:00:09

标签: reactjs react-native api-key coinbase-api gdax-api

我在React Native for Coinbase中使用了OAuth2身份验证。不幸的是,我意识到Coinbase Pro不支持OAuth2。我一直在尝试使API密钥身份验证正常工作。我看到的所有示例都使用NodeJS的“ crypto”库。我已经尝试过了,它已经贬值了,不能在React和React Native上使用。我通常使用效果很好的CryptoJS库。

我不确定应如何设置CB-ACCESS-SIGN的格式。

const signed = CryptoJS.HmacSHA512(message, secret).toString();

这将返回如下内容:

b24d709a6beb5e7b0b0b763b5af8a10c988d1424ee1ab693618b90f2950b15948e60efaaff0ea0a3e5a759ee7e8ca323a8d890bbe295e25e099d9db32c01c8a1   

这是否需要十六进制或base64编码?

我需要一些帮助来构造CB-ACCESS-SIGN。

我已经尝试了“ crypto”库,该库已被贬值并且无法在React或React Native中使用。

我尝试用不同的方式对密钥进行编码,但还没有完全起作用。

我将它们放在一起以尽可能具体地测试问题...

import CryptoJS from 'crypto-js';
import axios from 'axios';

coinbaseOrders = () => {
    const { key, secret } = this.state;
    const method = 'GET';
    const uri = 'https://api.pro.coinbase.com/orders?status=all';
    const timestamp = Math.floor(Date.now() / 1000);
    const message = `${timestamp}${method}${uri}`;
    const signed = CryptoJS.HmacSHA512(message, secret).toString();
    // const signed = CryptoJS.enc.Hex.parse(CryptoJS.HmacSHA512(message, secret).toString()).toString();
    const headers = {
        'User-Agent': 'reactnative',
        'Content-Type': 'application/json',
        'CB-ACCESS-SIGN': signed,
        'CB-ACCESS-TIMESTAMP': timestamp,
        'CB-ACCESS-KEY': key,
        'CB-VERSION': '2019-05-22'
    };

    console.log('key', key);
    console.log('secret', secret);
    console.log('method', method);
    console.log('uri', uri);
    console.log('timestamp', timestamp);
    console.log('message', message);
    console.log('signed', signed);
    console.log('headers', headers);

    axios
        .get(uri, {
            method,
            headers
        })
        .then((response) => {
            console.log(response);
        })
        .catch((error) => {
            console.log(error);
        });
}

this.coinbaseOrders();

预期结果是API密钥帐户的Coinbase Pro订单列表。

根据我的尝试,我收到了错误代码400和401。

0 个答案:

没有答案
相关问题