Path: blob/master/bitget-node-sdk-api/src/lib/util.ts
732 views
import {stringify} from 'querystring'1import {createHmac} from 'crypto'2import * as Console from 'console';3import {API_CONFIG} from './config';4import {BIZ_CONSTANT} from './contant';56export interface BitgetApiHeader {7'ACCESS-SIGN': string8'ACCESS-TIMESTAMP': number | string9'ACCESS-KEY': string10'ACCESS-PASSPHRASE': string11'Content-Type': string12Cookie: string13locale: string14}1516/**17* 获取签名器18* @param apiKey19* @param secretKey20* @param timestamp21* @param passphrase22*/23export default function getSigner(24apiKey: string = '',25secretKey: string = '',26passphrase: string = ''27) {2829return (httpMethod: string, url: string, qsOrBody: NodeJS.Dict<any> | null, locale = 'zh-CN') => {30const timestamp = Date.now();31let signString = encrypt(httpMethod, url, qsOrBody, timestamp, secretKey);32if (API_CONFIG.SIGN_TYPE === BIZ_CONSTANT.RSA) {33signString = encryptRSA(httpMethod, url, qsOrBody, timestamp,secretKey)34}3536return {37'ACCESS-SIGN': signString,38'ACCESS-TIMESTAMP': timestamp,39'ACCESS-KEY': apiKey,40'ACCESS-PASSPHRASE': passphrase,41'Content-Type': 'application/json',42Cookie: 'locale=' + locale,43locale44}45}46}4748/**49* 加密算法50* @param httpMethod51* @param url52* @param qsOrBody53* @param timestamp54* @param secretKey55*/56export function encrypt(httpMethod: string, url: string, qsOrBody: NodeJS.Dict<string | number> | null, timestamp: number,secretKey:string) {57httpMethod = httpMethod.toUpperCase()58if (qsOrBody && httpMethod === 'GET') {59qsOrBody = sortByKey(qsOrBody);60}61if (qsOrBody && Object.keys(qsOrBody).length === 0) {62qsOrBody = null;63}64// const qsOrBodyStr = qsOrBody ? httpMethod === 'GET' ? '?' + stringify(qsOrBody) : toJsonString(qsOrBody) : ''65const qsOrBodyStr = qsOrBody ? httpMethod === 'GET' ? '?' + unescapedStringify(qsOrBody) : toJsonString(qsOrBody) : ''6667const preHash = String(timestamp) + httpMethod + url + qsOrBodyStr6869const mac = createHmac('sha256', secretKey)70const preHashToMacBuffer = mac.update(preHash).digest()71return preHashToMacBuffer.toString('base64')72}737475function unescapedStringify(formDataDict: NodeJS.Dict<string | number>){76const encodedData = Object.keys(formDataDict).map((eachKey) => {77return eachKey + '=' + formDataDict[eachKey];78}).join('&');79return encodedData;80}8182export function toJsonString(obj: object): string | null {83if (obj == null) {84return null;85}8687let json = JSON.stringify(obj);88Object.keys(obj).filter(key => key[0] === '_').forEach(key => {89json = json.replace(key, key.substring(1));90});91const reg = new RegExp('"_', 'g')92return json.replace(reg, '"');93}9495/**96* RSA加密算法97* @param httpMethod98* @param url99* @param qsOrBody100* @param timestamp101* @param secretKey102*/103export function encryptRSA(httpMethod: string, url: string, qsOrBody: NodeJS.Dict<string | number> | null, timestamp: number,secretKey:string) {104httpMethod = httpMethod.toUpperCase()105const qsOrBodyStr = qsOrBody ? httpMethod === 'GET' ? '?' + stringify(qsOrBody) : toJsonString(qsOrBody) : ''106const preHash = String(timestamp) + httpMethod + url + qsOrBodyStr107const NodeRSA = require('node-rsa')108const priKey = new NodeRSA(secretKey)109const sign = priKey.sign(preHash, 'base64', 'UTF-8')110return sign111}112113export function sortByKey(dict:NodeJS.Dict<string | number>) {114const sorted = [];115for(const key of Object.keys(dict)) {116sorted[sorted.length] = key;117}118sorted.sort();119120const tempDict:any = {};121// for(let i = 0; i < sorted.length; i++) {122// tempDict[sorted[i]] = dict[sorted[i]];123// }124for(const item of sorted) {125tempDict[item] = dict[item];126}127return tempDict;128}129130