Authentication 为exchange btc alpha实施基于加密的身份验证

Authentication 为exchange btc alpha实施基于加密的身份验证,authentication,exchange-server,Authentication,Exchange Server,收缩▲ 复制代码 我想创建一个wrap来调用exchange btc aplha的API,因为它实现了DCY货币,这是意大利项目中唯一的一种货币。我的困难在于理解实现基于密钥的授权的规则。文档非常稀疏,我不理解phyton或node.js中的示例 有人能帮我实现我从node.js示例中获得的以下方法吗 const hmacSha256 = require('crypto-js/hmac-sha256'); // sha256 hash. or use you favorite u like

收缩▲ 复制代码 我想创建一个wrap来调用exchange btc aplha的API,因为它实现了DCY货币,这是意大利项目中唯一的一种货币。我的困难在于理解实现基于密钥的授权的规则。文档非常稀疏,我不理解phyton或node.js中的示例

有人能帮我实现我从node.js示例中获得的以下方法吗

const hmacSha256 = require('crypto-js/hmac-sha256'); // sha256 hash. or use you favorite u like
const request = require('request'); // for http requests. or use you favorite u like

const BASE_URL = 'https://btc-alpha.com/api/v1';
const API_KEY = '0000-0000...';
const SECRET = 'Z%2........';

//Serialize for singing only. Can be used in request body if u like urlencoded form format instead of json
function serializePayload(payload) {
  return Object
    .keys(payload) // get keys of payload object
    .sort() // sort keys
    .map((key) => key + "=" + encodeURIComponent(payload[key])) // each value should be url encoded. the most sensitive part for sign checking
    .join('&'); // to sting, separate with ampersand
}

// Generates auth headers
function getAuthHeaders(payload) {
  // get SHA256 of <API_KEY><sorted urlencoded payload string><SECRET>
  const sign = hmacSha256(API_KEY + serializePayload(payload), SECRET).toString();

  return {
    'X-KEY': API_KEY,
    'X-SIGN': sign,
    'X-NONCE': Date.now()
  };
}

function getWallets(callback) {
  payload = {};

  const options = {
    method: 'get',
    url: `${BASE_URL}/wallets/`,
    headers: getAuthHeaders(payload)
  };

  request(options, callback);
}

function createOrder(order, callback) {
  const options = {
    method: 'post',
    url: `${BASE_URL}/order/`,
    headers: getAuthHeaders(order),
    form: order, // API accepts urlencoded form or json. Use appropriate headers!
  };

  request(options, callback);
}

// test
getWallets((error, response, body) => {
  console.log('error', error);
  console.log('body', body);
});

const order = {
  type: 'buy',
  pair: 'BTC_USD',
  amount: '0.0001',
  price: '0.1'
};

createOrder(order, (error, response, body) => {
  // get json, etc
  console.log('error', error);
  console.log('body', body);
});
const hmacSha256=require('crypto-js/hmac-sha256');//sha256散列。或者用你喜欢的最爱
const request=require('request');//用于http请求。或者用你喜欢的最爱
常数基函数https://btc-alpha.com/api/v1';
常量API_键='0000-0000…';
常量机密='Z%2……';
//只为唱歌而连载。如果您喜欢URL编码的表单格式而不是json,则可以在请求正文中使用
功能有效负载(有效负载){
返回对象
.keys(有效负载)//获取有效负载对象的密钥
.sort()//排序键
.map((key)=>key+“=”+encodeURIComponent(payload[key]))//每个值都应该是url编码的。用于签名检查的最敏感部分
.join('&');//到sting,用符号AND分隔
}
//生成身份验证标头
函数getAuthHeaders(有效负载){
//弄到
const sign=hmacSha256(API_密钥+序列化有效负载(有效负载),SECRET).toString();
返回{
“X键”:API_键,
“X符号”:符号,
“X-NONCE”:Date.now()
};
}
函数getWallet(回调){
有效载荷={};
常量选项={
方法:“get”,
网址:`${BASE_url}/wallets/`,
标题:getAuthHeaders(有效负载)
};
请求(选项、回调);
}
函数createOrder(订单,回调){
常量选项={
方法:“post”,
url:`${BASE_url}/order/`,
标题:getAuthHeaders(顺序),
form:order,//API接受URL编码的表单或json。使用适当的头!
};
请求(选项、回调);
}
//试验
GetWallet((错误、响应、正文)=>{
console.log('error',error);
console.log('body',body);
});
常量顺序={
输入:“购买”,
对:'BTC_USD',
金额:“0.0001”,
价格:'0.1'
};
createOrder(顺序,(错误、响应、正文)=>{
//获取json等
console.log('error',error);
console.log('body',body);
});