-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
76 lines (63 loc) · 2.58 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import crypto from 'crypto'
import dotenv from 'dotenv'
import axios from 'axios'
dotenv.config();
const issue_date=process.env.ISSUE_DATE
export const generateNonce = () => {
const timestamp = Date.now() - issue_date; // Unix timestamp in seconds
const randomChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let randomString = '';
// Generate a random string of 8 characters
for (let i = 0; i < 8; i++) {
randomString += randomChars.charAt(Math.floor(Math.random() * randomChars.length));
}
return `${timestamp}:${randomString}`;
}
export const calculateBodyHash = (requestBody) => {
const sha256Hash = crypto.createHash('sha256').update(requestBody).digest();
const base64Encoded = Buffer.from(sha256Hash, 'utf8').toString('base64');
return base64Encoded;
}
export const buildNormalizedRequest = (nonce, requestMethod, requestPath, host, port, bodyHash, ext) => {
const NEW_LINE = '\n';
let normalizedRequest = '';
normalizedRequest += nonce + NEW_LINE;
normalizedRequest += requestMethod.toUpperCase() + NEW_LINE;
normalizedRequest += requestPath + NEW_LINE;
normalizedRequest += host.toLowerCase() + NEW_LINE;
normalizedRequest += port + NEW_LINE;
normalizedRequest += bodyHash + NEW_LINE;
normalizedRequest += ext + NEW_LINE;
return normalizedRequest;
}
export const generateHMAC_SHA256 = (secretKey, normalizedRequest) => {
const hmac = crypto.createHmac('sha256', Buffer.from(secretKey, 'utf8'));
hmac.update(Buffer.from(normalizedRequest, 'utf8'));
return hmac.digest();
}
export const encodeToBase64 = (data) => {
return Buffer.from(data,'utf-8').toString('base64');
}
export const requestAxios = ({host, path, method, nonce, mac, requestBody, bodyHash=''}) => {
let data = JSON.stringify(requestBody);
return new Promise((resolve, reject) => {
let config = {
method: method,
maxBodyLength: Infinity,
url: 'https://' + host + '/' + path,
headers: {
'Content-Type': 'application/json',
'Authorization': bodyHash ? `MAC id="${process.env.CLIENT_ID}",nonce="${nonce}",bodyhash="${bodyHash}",mac="${mac}"` : `MAC id="${process.env.CLIENT_ID}",nonce="${nonce}",mac="${mac}"`,
'X-GH-PARTNER-KEY': process.env.PARTNER_KEY,
},
data: data
};
axios.request(config)
.then((response) => {
resolve(response);
})
.catch((error) => {
reject(error);
});
})
}