-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhmac.ts
More file actions
35 lines (32 loc) · 911 Bytes
/
hmac.ts
File metadata and controls
35 lines (32 loc) · 911 Bytes
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
import crypto from "node:crypto";
function replaceAll(s: string, search: string, replace: string) {
return s.split(search).join(replace);
}
/**
* Builds the canonical Polymarket CLOB HMAC signature
* @param signer
* @param key
* @param secret
* @param passphrase
* @returns string
*/
export const buildPolyHmacSignature = (
secret: string,
timestamp: number,
method: string,
requestPath: string,
body?: string,
): string => {
let message = timestamp + method + requestPath;
if (body !== undefined) {
message += body;
}
const base64Secret = Buffer.from(secret, "base64");
const hmac = crypto.createHmac("sha256", base64Secret);
const sig = hmac.update(message).digest("base64");
// NOTE: Must be url safe base64 encoding, but keep base64 "=" suffix
// Convert '+' to '-'
// Convert '/' to '_'
const sigUrlSafe = replaceAll(replaceAll(sig, "+", "-"), "/", "_");
return sigUrlSafe;
};