gpt4 book ai didi

node.js - 使用 MtGox 的流 API 进行身份验证的命令

转载 作者:太空宇宙 更新时间:2023-11-03 22:02:32 25 4
gpt4 key购买 nike

我正在使用带有socket.io-client的nodejs来连接到此处描述的MtGox Streaming API:https://en.bitcoin.it/wiki/MtGox/API/Streaming#Authenticated_commands

这些示例是用 php 编写的,我尽力将它们转换为 JS,但我不断收到服务器的响应“无效调用”

var sec_key_buffer = Buffer(secret_key, 'base64');
var hmac = crypto.createHmac('sha512', sec_key_buffer);
var nonce = Date.now() + "";
var id = crypto.createHash('md5').update(nonce).digest('hex');
var query = {
"call": 'private/info',
"id": id,
"nonce": nonce
};
var body = JSON.stringify(query);
var sign = hmac.update(body).digest('binary');

// The 'api_key' field has already stripped of the '-' character
var callBody = new Buffer(api_key + sign + body).toString('base64');

// 'connection' is the socket.io connection to 'https://socketio.mtgox.com/mtgox'
connection.json.send({
"op": "call",
"id": id,
"call": callBody,
"context": 'mtgox.com'
});

如有任何帮助,我们将不胜感激。

最佳答案

这似乎对我有用:

var io = require('socket.io-client');
var crypto = require('crypto');

var socket = io.connect('https://socketio.mtgox.com/mtgox');

var MTGOX_API_INFO = {
key: '<YOUR_KEY>',
secret: '<YOUR_SECRET>'
}

var MTGOX_CHANNELS = {
trade: 'dbf1dee9-4f2e-4a08-8cb7-748919a71b21',
depth: '24e67e0d-1cad-4cc0-9e7a-f8523ef460fe',
ticker: 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f'
}

// unsubscribe from depth and trade messages
socket.emit('message', {
op: 'unsubscribe',
channel: MTGOX_CHANNELS.trade
});
socket.emit('message', {
op: 'unsubscribe',
channel: MTGOX_CHANNELS.depth
});
socket.emit('message', {
op: 'unsubscribe',
channel: MTGOX_CHANNELS.ticker
});


var getNonce = function() {
return ((new Date()).getTime() * 1000).toString();
}

var nonce = getNonce();
var requestId = crypto.createHash('md5').update(nonce).digest('hex');
var query = {
id: requestId,
call: 'private/info',
nonce: nonce
// params: {},
// item: 'BTC',
// currency: 'USD'
};
var queryJSON = JSON.stringify(query);

var signedQuery = crypto.createHmac('sha512', new Buffer(MTGOX_API_INFO.secret, 'base64')).update(queryJSON).digest('binary');
var binKey = (new Buffer(MTGOX_API_INFO.key.replace(/-/g, ''), 'hex')).toString('binary');
var buffer = new Buffer(binKey + signedQuery + queryJSON, 'binary');
var call = buffer.toString('base64');

var command = {
op: 'call',
id: requestId,
call: call,
context: 'mtgox.com'
};

console.log("REQUEST:", command);

socket.emit('message', command);
socket.on('message', function(data) {
console.log(data);
});

更新:我还将其抽象为 a simple node module .

关于node.js - 使用 MtGox 的流 API 进行身份验证的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16293914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com