ok_chain_request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const https = require('https');
  2. const crypto = require('crypto');
  3. const querystring = require('querystring');
  4. // 定义 API 凭证
  5. const api_config = {
  6. "api_key": '4d6b9d92-c43b-4355-94c0-3fc9bb36b0ee',
  7. "secret_key": '3C342C0709D461582A140497A5F6E70C',
  8. "passphrase": 'Qwe123123.',
  9. };
  10. function preHash(timestamp, method, request_path, params) {
  11. // 根据字符串和参数创建预签名
  12. let query_string = '';
  13. if (method === 'GET' && params) {
  14. query_string = '?' + querystring.stringify(params);
  15. }
  16. if (method === 'POST' && params) {
  17. query_string = JSON.stringify(params);
  18. }
  19. return timestamp + method + request_path + query_string;
  20. }
  21. function sign(message, secret_key) {
  22. // 使用 HMAC-SHA256 对预签名字符串进行签名
  23. const hmac = crypto.createHmac('sha256', secret_key);
  24. hmac.update(message);
  25. return hmac.digest('base64');
  26. }
  27. function createSignature(method, request_path, params) {
  28. // 获取 ISO 8601 格式时间戳
  29. const timestamp = new Date().toISOString().slice(0, -5) + 'Z';
  30. // 生成签名
  31. const message = preHash(timestamp, method, request_path, params);
  32. const signature = sign(message, api_config['secret_key']);
  33. return { signature, timestamp };
  34. }
  35. function sendGetRequest(request_path, params) {
  36. // 生成签名
  37. const { signature, timestamp } = createSignature("GET", request_path, params);
  38. // 生成请求头
  39. const headers = {
  40. 'OK-ACCESS-KEY': api_config['api_key'],
  41. 'OK-ACCESS-SIGN': signature,
  42. 'OK-ACCESS-TIMESTAMP': timestamp,
  43. 'OK-ACCESS-PASSPHRASE': api_config['passphrase'],
  44. };
  45. const options = {
  46. hostname: 'web3.okx.com',
  47. path: request_path + (params ? `?${querystring.stringify(params)}` : ''),
  48. method: 'GET',
  49. headers: headers
  50. };
  51. const req = https.request(options, (res) => {
  52. let data = '';
  53. res.on('data', (chunk) => {
  54. data += chunk;
  55. });
  56. res.on('end', () => {
  57. console.log(data);
  58. });
  59. });
  60. req.end();
  61. }
  62. function sendPostRequest(request_path, params) {
  63. // 生成签名
  64. const { signature, timestamp } = createSignature("POST", request_path, params);
  65. // 生成请求头
  66. const headers = {
  67. 'OK-ACCESS-KEY': api_config['api_key'],
  68. 'OK-ACCESS-SIGN': signature,
  69. 'OK-ACCESS-TIMESTAMP': timestamp,
  70. 'OK-ACCESS-PASSPHRASE': api_config['passphrase'],
  71. 'Content-Type': 'application/json'
  72. };
  73. const options = {
  74. hostname: 'web3.okx.com',
  75. path: request_path,
  76. method: 'POST',
  77. headers: headers
  78. };
  79. const req = https.request(options, (res) => {
  80. let data = '';
  81. res.on('data', (chunk) => {
  82. data += chunk;
  83. });
  84. res.on('end', () => {
  85. console.log(data);
  86. });
  87. });
  88. if (params) {
  89. req.write(JSON.stringify(params));
  90. }
  91. req.end();
  92. }
  93. // GET 请求示例
  94. const getRequestPath = '/api/v5/dex/aggregator/quote';
  95. const getParams = {
  96. 'chainId': 42161,
  97. 'amount': 1000000000000,
  98. 'toTokenAddress': '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8',
  99. 'fromTokenAddress': '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1'
  100. };
  101. console.log(sendGetRequest(getRequestPath, getParams));
  102. // // POST 请求示例
  103. // const postRequestPath = '/api/v5/mktplace/nft/ordinals/listings';
  104. // const postParams = {
  105. // 'slug': 'sats'
  106. // };
  107. // sendPostRequest(postRequestPath, postParams);