Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitgetLimited
GitHub Repository: BitgetLimited/V3-bitget-api-sdk
Path: blob/master/bitget-node-sdk-api/src/lib/util.ts
732 views
1
import {stringify} from 'querystring'
2
import {createHmac} from 'crypto'
3
import * as Console from 'console';
4
import {API_CONFIG} from './config';
5
import {BIZ_CONSTANT} from './contant';
6
7
export interface BitgetApiHeader {
8
'ACCESS-SIGN': string
9
'ACCESS-TIMESTAMP': number | string
10
'ACCESS-KEY': string
11
'ACCESS-PASSPHRASE': string
12
'Content-Type': string
13
Cookie: string
14
locale: string
15
}
16
17
/**
18
* 获取签名器
19
* @param apiKey
20
* @param secretKey
21
* @param timestamp
22
* @param passphrase
23
*/
24
export default function getSigner(
25
apiKey: string = '',
26
secretKey: string = '',
27
passphrase: string = ''
28
) {
29
30
return (httpMethod: string, url: string, qsOrBody: NodeJS.Dict<any> | null, locale = 'zh-CN') => {
31
const timestamp = Date.now();
32
let signString = encrypt(httpMethod, url, qsOrBody, timestamp, secretKey);
33
if (API_CONFIG.SIGN_TYPE === BIZ_CONSTANT.RSA) {
34
signString = encryptRSA(httpMethod, url, qsOrBody, timestamp,secretKey)
35
}
36
37
return {
38
'ACCESS-SIGN': signString,
39
'ACCESS-TIMESTAMP': timestamp,
40
'ACCESS-KEY': apiKey,
41
'ACCESS-PASSPHRASE': passphrase,
42
'Content-Type': 'application/json',
43
Cookie: 'locale=' + locale,
44
locale
45
}
46
}
47
}
48
49
/**
50
* 加密算法
51
* @param httpMethod
52
* @param url
53
* @param qsOrBody
54
* @param timestamp
55
* @param secretKey
56
*/
57
export function encrypt(httpMethod: string, url: string, qsOrBody: NodeJS.Dict<string | number> | null, timestamp: number,secretKey:string) {
58
httpMethod = httpMethod.toUpperCase()
59
if (qsOrBody && httpMethod === 'GET') {
60
qsOrBody = sortByKey(qsOrBody);
61
}
62
if (qsOrBody && Object.keys(qsOrBody).length === 0) {
63
qsOrBody = null;
64
}
65
// const qsOrBodyStr = qsOrBody ? httpMethod === 'GET' ? '?' + stringify(qsOrBody) : toJsonString(qsOrBody) : ''
66
const qsOrBodyStr = qsOrBody ? httpMethod === 'GET' ? '?' + unescapedStringify(qsOrBody) : toJsonString(qsOrBody) : ''
67
68
const preHash = String(timestamp) + httpMethod + url + qsOrBodyStr
69
70
const mac = createHmac('sha256', secretKey)
71
const preHashToMacBuffer = mac.update(preHash).digest()
72
return preHashToMacBuffer.toString('base64')
73
}
74
75
76
function unescapedStringify(formDataDict: NodeJS.Dict<string | number>){
77
const encodedData = Object.keys(formDataDict).map((eachKey) => {
78
return eachKey + '=' + formDataDict[eachKey];
79
}).join('&');
80
return encodedData;
81
}
82
83
export function toJsonString(obj: object): string | null {
84
if (obj == null) {
85
return null;
86
}
87
88
let json = JSON.stringify(obj);
89
Object.keys(obj).filter(key => key[0] === '_').forEach(key => {
90
json = json.replace(key, key.substring(1));
91
});
92
const reg = new RegExp('"_', 'g')
93
return json.replace(reg, '"');
94
}
95
96
/**
97
* RSA加密算法
98
* @param httpMethod
99
* @param url
100
* @param qsOrBody
101
* @param timestamp
102
* @param secretKey
103
*/
104
export function encryptRSA(httpMethod: string, url: string, qsOrBody: NodeJS.Dict<string | number> | null, timestamp: number,secretKey:string) {
105
httpMethod = httpMethod.toUpperCase()
106
const qsOrBodyStr = qsOrBody ? httpMethod === 'GET' ? '?' + stringify(qsOrBody) : toJsonString(qsOrBody) : ''
107
const preHash = String(timestamp) + httpMethod + url + qsOrBodyStr
108
const NodeRSA = require('node-rsa')
109
const priKey = new NodeRSA(secretKey)
110
const sign = priKey.sign(preHash, 'base64', 'UTF-8')
111
return sign
112
}
113
114
export function sortByKey(dict:NodeJS.Dict<string | number>) {
115
const sorted = [];
116
for(const key of Object.keys(dict)) {
117
sorted[sorted.length] = key;
118
}
119
sorted.sort();
120
121
const tempDict:any = {};
122
// for(let i = 0; i < sorted.length; i++) {
123
// tempDict[sorted[i]] = dict[sorted[i]];
124
// }
125
for(const item of sorted) {
126
tempDict[item] = dict[item];
127
}
128
return tempDict;
129
}
130