应如何生成签名参数以打开与 Coinbase Websocket 的经过身份验证的连接?我在任何地方都找不到任何简洁的描述。
对于GET/PUTAPI调用,我使用下面的代码成功生成了它,但是对于Websocket,既没有“方法”也没有“path_url”,那么什么应该包含“消息”?
timestamp = str(time.time())
message = timestamp + request.method + request.path_url + (request.body or '')
hmac_key = base64.b64decode(self.secret_key)
signature = hmac.new(hmac_key, message.encode('utf-8'), hashlib.sha256)
signature_b64 = base64.b64encode(signature.digest())
Run Code Online (Sandbox Code Playgroud)
我终于能够解决这个问题了。当然,代码看起来并不那么有趣。
我将以下内容与CCXT库结合使用。更具体地说,这里是 Websockets 的分支实现。
const id = this.marketId (symbol)
const timestamp = Date.now() / 1000
const p_passphrase = this.safeValue(params, 'password')
const p_apiKey = this.safeValue(params, 'apiKey')
const p_secret = this.safeValue(params, 'secret')
const what = timestamp + 'GET' + '/users/self/verify'
const key = Buffer.from(p_secret, 'base64')
const hmac = require('crypto').createHmac('sha256', key)
const signature = hmac.update(what).digest('base64')
this.websocketSendJson({
'type': 'subscribe',
'product_ids': [id],
'channels': ['user'],
'key': p_apiKey,
'signature': signature,
'timestamp': timestamp,
'passphrase': p_passphrase,
})
Run Code Online (Sandbox Code Playgroud)
希望这有帮助!