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
(** Authentication types and header builders for Polymarket APIs.
This module provides credentials types and functions for building
authentication headers for L1 (wallet-based) and L2 (API key-based)
authentication. *)
open Ppx_yojson_conv_lib.Yojson_conv.Primitives
(** {1 Types} *)
type credentials = { api_key : string; secret : string; passphrase : string }
[@@deriving show, eq]
type api_key_response = {
api_key : string; [@key "apiKey"]
secret : string;
passphrase : string;
}
[@@deriving yojson, show, eq]
(** Response from API key endpoints (create or derive). *)
(** {1 Conversion} *)
let credentials_of_api_key_response (resp : api_key_response) : credentials =
{ api_key = resp.api_key; secret = resp.secret; passphrase = resp.passphrase }
(** {1 Header Names} *)
let poly_address = "POLY_ADDRESS"
let poly_signature = "POLY_SIGNATURE"
let poly_timestamp = "POLY_TIMESTAMP"
let poly_nonce = "POLY_NONCE"
let poly_api_key = "POLY_API_KEY"
let poly_passphrase = "POLY_PASSPHRASE"
(** {1 L1 Authentication} *)
let ~private_key ~address ~nonce =
let timestamp = Crypto.current_timestamp_ms () in
let signature =
Crypto.sign_clob_auth_message ~private_key ~address ~timestamp ~nonce
in
[
(poly_address, address);
(poly_signature, signature);
(poly_timestamp, timestamp);
(poly_nonce, string_of_int nonce);
]
(** {1 L2 Authentication} *)
let ~(credentials : credentials) ~address ~method_ ~path ~body
=
let timestamp = Crypto.current_timestamp_ms () in
let signature =
Crypto.sign_l2_request ~secret:credentials.secret ~timestamp ~method_ ~path
~body
in
[
(poly_address, address);
(poly_signature, signature);
(poly_timestamp, timestamp);
(poly_api_key, credentials.api_key);
(poly_passphrase, credentials.passphrase);
]