| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- const https = require('https');
- const crypto = require('crypto');
- const querystring = require('querystring');
- // 定义 API 凭证
- const api_config = {
- "api_key": '4d6b9d92-c43b-4355-94c0-3fc9bb36b0ee',
- "secret_key": '3C342C0709D461582A140497A5F6E70C',
- "passphrase": 'Qwe123123.',
- };
- function preHash(timestamp, method, request_path, params) {
- // 根据字符串和参数创建预签名
- let query_string = '';
- if (method === 'GET' && params) {
- query_string = '?' + querystring.stringify(params);
- }
- if (method === 'POST' && params) {
- query_string = JSON.stringify(params);
- }
- return timestamp + method + request_path + query_string;
- }
- function sign(message, secret_key) {
- // 使用 HMAC-SHA256 对预签名字符串进行签名
- const hmac = crypto.createHmac('sha256', secret_key);
- hmac.update(message);
- return hmac.digest('base64');
- }
- function createSignature(method, request_path, params) {
- // 获取 ISO 8601 格式时间戳
- const timestamp = new Date().toISOString().slice(0, -5) + 'Z';
- // 生成签名
- const message = preHash(timestamp, method, request_path, params);
- const signature = sign(message, api_config['secret_key']);
- return { signature, timestamp };
- }
- function sendGetRequest(request_path, params) {
- // 生成签名
- const { signature, timestamp } = createSignature("GET", request_path, params);
- // 生成请求头
- const headers = {
- 'OK-ACCESS-KEY': api_config['api_key'],
- 'OK-ACCESS-SIGN': signature,
- 'OK-ACCESS-TIMESTAMP': timestamp,
- 'OK-ACCESS-PASSPHRASE': api_config['passphrase'],
- };
- const options = {
- hostname: 'web3.okx.com',
- path: request_path + (params ? `?${querystring.stringify(params)}` : ''),
- method: 'GET',
- headers: headers
- };
- const req = https.request(options, (res) => {
- let data = '';
- res.on('data', (chunk) => {
- data += chunk;
- });
- res.on('end', () => {
- console.log(data);
- });
- });
- req.end();
- }
- function sendPostRequest(request_path, params) {
- // 生成签名
- const { signature, timestamp } = createSignature("POST", request_path, params);
- // 生成请求头
- const headers = {
- 'OK-ACCESS-KEY': api_config['api_key'],
- 'OK-ACCESS-SIGN': signature,
- 'OK-ACCESS-TIMESTAMP': timestamp,
- 'OK-ACCESS-PASSPHRASE': api_config['passphrase'],
- 'Content-Type': 'application/json'
- };
- const options = {
- hostname: 'web3.okx.com',
- path: request_path,
- method: 'POST',
- headers: headers
- };
- const req = https.request(options, (res) => {
- let data = '';
- res.on('data', (chunk) => {
- data += chunk;
- });
- res.on('end', () => {
- console.log(data);
- });
- });
- if (params) {
- req.write(JSON.stringify(params));
- }
- req.end();
- }
- // GET 请求示例
- const getRequestPath = '/api/v5/dex/aggregator/quote';
- const getParams = {
- 'chainId': 42161,
- 'amount': 1000000000000,
- 'toTokenAddress': '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8',
- 'fromTokenAddress': '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1'
- };
- console.log(sendGetRequest(getRequestPath, getParams));
- // // POST 请求示例
- // const postRequestPath = '/api/v5/mktplace/nft/ordinals/listings';
- // const postParams = {
- // 'slug': 'sats'
- // };
- // sendPostRequest(postRequestPath, postParams);
|