创建一个Deribit签名字符串和哈希

时间:2019-03-21 13:47:20

标签: javascript google-apps-script authorization

我一直在尝试使用Deribit testnet API连接到我的帐户,但我不知道为什么我的代码无法正常工作。

我的Apps脚本测试功能:

function callderibit() {
  var key = '2YZn85siaUf5A'                                            
  var secret = 'BTMSIAJ8IYQTAV4MLN88UAHLIUNYZ3HN' 
  var nonce = '1452237485895' ;

  var baseUrl = 'https://test.deribit.com';
  var action = '/api/v1/private/account'

  var string = 
  '_=' + nonce                                               
       + '&_ackey=' + key
       + '&_acsec=' + secret
       + '&_action='+ action;

  var hash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, 
  string);

  var hash_encoded = Utilities.base64Encode(hash);     

  var signature = key + '.' + nonce + '.' + hash_encoded ;

  var options = {'headers': {'X-Deribit-Sig': signature}}


  // Call the API
  var response = UrlFetchApp.fetch(baseUrl + action , options  )

  //Parse the JSON reply
  var json = response.getContentText();
  var data = JSON.parse(json);
  Logger.log(data);
  }

这是我正在生成的签名: 2YZn85siaUf5A.1452237485895.KOlc7ELGnz8cjYp614ONxZlngo / z2AHMEjVdlHlW9Oo =

我得到的签名是这个: 2YZn85siaUf5A.1452237485895.LQctRklxPiJDHJj9ZYp78Epilx7N78crGghzr1pvNlI =


新信息

正如我们所发现的,在不满意站点上提供的js和py示例无法正常运行,因为它们产生的哈希值与应有的哈希值不同。

此后,我与deribit联系,并得到了以下答复: ................................................... ...........................

“嗨

我已附上API v1的Python示例。您还可以在我们的github页面https://github.com/deribit

上找到适用于API v1的Python,Nodejs包装器

请注意,API v1的进一步开发已经结束-尽管API v1仍然可用,但我们更专注于API v2 https://docs.deribit.com/v2“ ................................................... ...........................

附加的py:

#!/usr/bin/python
import time
import base64
import hashlib
from urllib.parse import urlencode
from urllib.request import Request, urlopen

api_key = 'API_KEY'
api_secret = 'API_SECRET'
instrument = 'BTC-PERPETUAL'
tstamp = '1485629332052'
price = 100
size = 1
params = (tstamp, api_key, api_secret, instrument, price, size)

data='_=%s&_ackey=%s&_acsec=%s&_action=/api/v1/private/buy&instrument=%s  
      &post_only=true&price=%0.2f&quantity=%d' % params

hashed = base64.b64encode(hashlib.sha256(data.encode()).digest())
signature = '%s.%s.%s' % (api_key, tstamp, hashed.decode())

headers = {"x-deribit-sig": signature, "content-type":"application/x-www-        
form-urlencoded"}

# important: in signature post_only=true, but in params it was True - so     
signature can't match
params={'instrument': instrument, 'post_only': 'true', 'price': 
'%0.2f'%price,'quantity': size}

url = 'https://test.deribit.com/api/v1/private/buy'
data = urlencode(params).encode()
request = Request(url, urlencode(params).encode(), headers)
json = urlopen(request).read().decode()
print(json)

0 个答案:

没有答案