javascript - Kraken API 无效签名
问题描述
我在 javascript 中有下一个代码,可以使用 REST API 登录 kraken。当我调用“getDepositMethods”时,它会显示我和错误:“EAPI:Invalid signatura”。
class Kraken{
constructor(key, secret){
this.key = key;
this.secret = secret;
}
getDepositMethods(){
var nonce = new Date() * 1000; // spoof microsecond
const signature = getMessageSignature("/0/private/DepositMethods", {}, this.secret, nonce);
$.ajax({
method: "POST",
url: "https://api.kraken.com/0/private/DepositMethods",
headers: { "API-Key" : this.key,
"API-Sign" : signature},
data: {nonce: nonce,
asset: "xbt"},
dataType: 'json',
success: function(data_response){
console.log(data_response);
},
error: function(data){
console.log(data);
}
});
}
}
function main(){
const kraken = new Kraken(key,secret);
kraken.getDepositMethods();
}
// Create a signature for a request
function getMessageSignature(path, request, secret, nonce){
const crypto = require('crypto');
var qs = require('qs');
const message = qs.stringify(request);
const secret_buffer = new Buffer(secret, 'base64');
const hash = new crypto.createHash('sha256');
const hmac = new crypto.createHmac('sha512', secret_buffer);
const hash_digest = hash.update(nonce + message).digest('binary');;
const hmac_digest = hmac.update(path + hash_digest, 'binary').digest('base64');
console.log(hmac);
return hmac;
}
有人可以帮我吗?我需要资金来调用私有方法吗?
解决方案
使用库 CryptoJS ( https://github.com/brix/crypto-js )
getMessageSignature : function(api_path, api_post, apiSecret, api_nonce) {
api_secret = CryptoJS.enc.Base64.parse(apiSecret);
api_sha256 = CryptoJS.SHA256(api_nonce + api_post);
api_sign = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA512, api_secret);
api_sign.update(api_path, api_secret);
api_sign.update(api_sha256, api_secret);
api_sign = api_sign.finalize().toString(CryptoJS.enc.Base64);
return api_sign;
}
推荐阅读
- python - 如何从数据框中绘制折线图上的最大 Y 值
- python - 尝试在 Python 套接字中将数据从服务器发送到客户端时出现管道损坏错误
- flutter - Flutter Web 中的 SelectableText.rich
- javascript - Mongoose 返回所有数组数据
- mysql - 当我尝试运行我的代码时出现错误。本地端口 127.0.0.1:3310 无法绑定
- gcloud - Google Cloud Run Service to Service Communication 内部是否像 k8s 的 cluster.local?
- python - 如何遍历 CSV 文件并使用 Pandas 从第一个元素和其余元素创建一个对象?
- arrays - 我无法在 OPL 脚本中达到数组的先前迭代值
- python - 如何让用户在连接四游戏中选择行数和列数
- python - 使用 gunicorn ModuleNotFoundError 的 Flask 应用程序部署:没有名为“app”的模块