forked from Modulr-finance/modulr-hmac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature.js
64 lines (50 loc) · 1.78 KB
/
signature.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
'use strict';
const uuid = require('uuid');
const crypto = require('crypto-js');
class Signature {
constructor(apiKey, apiSecret) {
if (this.apikey || this.apiSecret) {
throw new Error('apikey and apiSecret must be suppiled to calculate signature');
}
this.apikey = apiKey;
this.apiSecret = apiSecret;
}
calculate(nonce, timestamp){
if (!nonce){
nonce = uuid.v4();
}
if (!timestamp){
timestamp = new Date().toUTCString();
}
// format raw signature
const signature = `date: ${timestamp}\nx-mod-nonce: ${nonce}`;
console.debug(`Raw signature string is:\n-----------\n${signature}\n-----------`);
// sign and encode signature
const signatureSigned = crypto.HmacSHA1(signature, this.apiSecret);
const signatureEncoded = encodeURIComponent(crypto.enc.Base64.stringify(signatureSigned));
console.debug(`Signed signature is [${signatureSigned}] and encoded is [${signatureEncoded}]`);
return new Result(this.apikey, timestamp, nonce, signatureEncoded);
}
}
class Result {
constructor(key, timestamp, nonce, encodedSignature){
this.timestamp = timestamp;
this.nonce = nonce;
this.encodedSignature = encodedSignature;
this.authorisation = `Signature keyId="${key}",algorithm="hmac-sha1",headers="date x-mod-nonce",signature="${encodedSignature}"`;
}
getTimeStamp(){
return this.timestamp;
}
getHTTPHeaders(){
return {
'Date': this.timestamp,
'x-mod-nonce': this.nonce,
'Authorization': this.authorisation
};
}
getSignature(){
return this.encodedSignature;
}
}
module.exports = Signature, Result;