Coinbase Pro API-无效签名



这是从原始帖子中编辑的:

来自文档:

签署消息CB-Access-Sign标头是通过创建一个 SHA256 HMAC使用base64编码的秘密键在Prehash字符串上 时间戳 方法 requestPath 身体(其中 代表字符串 串联(和base64-condode the Output。时间戳值是 与CB-Access-Timestamp标头相同。

这是我删除的密钥中的信息。这是来自Coinbase Pro Sandbox:

publicKey:

06057d5b5e03d0f8587a248330402b21

密码:

gcgs6k6rp0f

SecretKey:EFAToD5heo66GIgZlT2TIZzJf8TYlmxyeRxRYDHTBv3lTt9XN6uaNS0RNAy0os/caR47x6EiPDOV3Ik+YzrfEA==

我正在使用Angular,特别是Node.js Crypto-JS库:

private generateSignaturePro(timestamp: string, method: string, resourceUrl: string, requestBody: string): string {
    var prehash: string = timestamp + method + resourceUrl + requestBody;
    var key = (Buffer.from(this.secretKey, 'base64')).toString();
    return crypto.enc.Base64.stringify(crypto.HmacSHA256(prehash, key));
}

服务器时间是时间:2019-05-20T19:01:38.711Z时代:1558378898.711(来自/时间端点(

这是我的请求,服务器响应:

请求:

Request URL: https://api-public.sandbox.pro.coinbase.com/accounts
Request Method: GET
Status Code: 400 
Remote Address: 104.16.161.226:443
Referrer Policy: no-referrer-when-downgrade

请求标头:

Provisional headers are shown
Accept: application/json, text/plain, */*
CB-ACCESS-KEY: 06057d5b5e03d0f8587a248330402b21
CB-ACCESS-PASSPHRASE: gcgs6k6rp0f
CB-ACCESS-SIGN: 0cc2BnQYdUhLucXSPwMTjpHjJ32G3RXSH44rSsEopvjAtY90uRCMVy6xUrzg/A/aRJBLqx390fcZc7lmJeP++g==
CB-ACCESS-TIMESTAMP: 1558378899
Referer: https://localhost:44342/dashboard
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36

响应标题:

access-control-allow-headers: Content-Type, Accept, cb-session, cb-fp
access-control-allow-methods: GET,POST,DELETE,PUT
access-control-allow-origin: *
access-control-expose-headers: cb-before, cb-after, cb-gdpr
access-control-max-age: 7200
cache-control: no-store
cf-cache-status: MISS
cf-ray: 4da08f74ba97cf68-IAD
content-length: 31
content-type: application/json; charset=utf-8
date: Mon, 20 May 2019 19:01:38 GMT
etag: W/"1f-4RjKVp8I05+xcnQ5/G16yRoMSKU"
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
status: 400
strict-transport-security: max-age=15552000; includeSubDomains
vary: Accept-Encoding
x-content-type-options: nosniff
x-dns-prefetch-control: off
x-download-options: noopen
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block

响应:

{"message":"invalid signature"}

我在做什么错?

编辑:将方法更改为SHA 256版本。仍然不起作用。

我遇到了同一问题,而我的代码基本上与您的代码相同。我更改为以下(C#(,最终奏效了。奇怪的是,Coinbase Pro只是我到目前为止与签名有关的问题。无论如何,这里是对我有用的代码。希望这可以帮助。会节省我的时间

public string ComputeSignature(
        HttpMethod httpMethod,
        string secret,
        double timestamp,
        string requestUri,
        string contentBody = "")
    {
        var convertedString = System.Convert.FromBase64String(secret);
        var prehash = timestamp.ToString("F0", CultureInfo.InvariantCulture) + httpMethod.ToString().ToUpper() + requestUri + contentBody;
        return HashString(prehash, convertedString);
    }
private string HashString(string str, byte[] secret)
{
        var bytes = Encoding.UTF8.GetBytes(str);
        using (var hmaccsha = new HMACSHA256(secret))
        {
            return System.Convert.ToBase64String(hmaccsha.ComputeHash(bytes));
        }
}

来自gdax-java(正如" coinbase pro"之前命名(库生成签名方法是:

 String prehash = timestamp + method.toUpperCase() + requestPath + body;
 byte[] secretDecoded = Base64.getDecoder().decode(secretKey);
 keyspec = new SecretKeySpec(secretDecoded, "HmacSHA256");
 sha256 = (Mac) GdaxConstants.SHARED_MAC.clone();
 sha256.init(keyspec);
 return Base64.getEncoder().encodeToString(sha256.doFinal(prehash.getBytes()));

至少在初次检查时

右手列中的nodejs也有更多帮助,以生成签名。https://docs.pro.coinbase.com/#creating-a-request

在这里遇到相同的问题。对我来说,答案是使用luxon datetime而不是本机JS日期函数,如Coinbase文档所示。

这是对我有用的打字稿。您可以使用此功能的结果来填充请求标题。

import crypto from 'crypto';
import { DateTime } from 'luxon';
export const auth = (
  method: 'GET' | 'POST',
  path: string,
  body?: Record<string, unknown>
) => {
  const timestamp = DateTime.utc().toMillis() / 1000;
  let message = timestamp + method + path;
  if (body) {
    message += JSON.stringify(body);
  }
  const secret = Buffer.from('YOUR_SECRET','base64');
  const hmac = crypto.createHmac('sha256', secret);
  return {
    'CB-ACCESS-KEY': 'YOUR_KEY',
    'CB-ACCESS-PASSPHRASE': 'YOUR_PASSPHRASE',
    'CB-ACCESS-SIGN': hmac.update(message).digest('base64'),
    'CB-ACCESS-TIMESTAMP': timestamp.toString()
  };
};

最新更新