首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MtGox的流API验证的命令

使用MtGox的流API验证的命令
EN

Stack Overflow用户
提问于 2013-04-30 14:50:14
回答 2查看 562关注 0票数 2

我正在使用nodejs和socket.io-client来连接到这里描述的MtGox流API:https://en.bitcoin.it/wiki/MtGox/API/Streaming#Authenticated_commands

示例是用php编写的,我尽了最大努力将它们转换成JS,但不断收到来自服务器的响应“无效调用”。

代码语言:javascript
复制
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'
});

任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-08 01:33:19

以下是对我来说似乎有效的方法:

代码语言:javascript
复制
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

票数 2
EN

Stack Overflow用户

发布于 2013-05-05 21:48:23

在追加哈希和消息内容之前,必须将api密钥十六进制解码为原始字节。

对node.js还不够熟悉,无法提供代码,但在Java语言中(使用Guava):

代码语言:javascript
复制
byte[] apiKey = BaseEncoding.base16().lowerCase().decode(API_KEY.replace("-", ""));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16293914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档