@polymarket/clob-client-v2 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -257,20 +257,34 @@ var buildClobEip712Signature = async (signer, chainId, timestamp, nonce, address
257
257
  };
258
258
 
259
259
  // src/signing/hmac.ts
260
- var import_node_crypto = __toESM(require("crypto"), 1);
261
- function replaceAll(s, search, replace) {
262
- return s.split(search).join(replace);
263
- }
264
- var buildPolyHmacSignature = (secret, timestamp, method, requestPath, body) => {
260
+ var buildPolyHmacSignature = async (secret, timestamp, method, requestPath, body) => {
265
261
  let message = timestamp + method + requestPath;
266
262
  if (body !== void 0) {
267
263
  message += body;
268
264
  }
269
- const base64Secret = Buffer.from(secret, "base64");
270
- const hmac = import_node_crypto.default.createHmac("sha256", base64Secret);
271
- const sig = hmac.update(message).digest("base64");
272
- const sigUrlSafe = replaceAll(replaceAll(sig, "+", "-"), "/", "_");
273
- return sigUrlSafe;
265
+ const binarySecret = atob(secret);
266
+ const keyBytes = new Uint8Array(binarySecret.length);
267
+ for (let i = 0; i < binarySecret.length; i++) {
268
+ keyBytes[i] = binarySecret.charCodeAt(i);
269
+ }
270
+ const key = await globalThis.crypto.subtle.importKey(
271
+ "raw",
272
+ keyBytes,
273
+ { name: "HMAC", hash: "SHA-256" },
274
+ false,
275
+ ["sign"]
276
+ );
277
+ const sigBuffer = await globalThis.crypto.subtle.sign(
278
+ "HMAC",
279
+ key,
280
+ new TextEncoder().encode(message)
281
+ );
282
+ const sigBytes = new Uint8Array(sigBuffer);
283
+ let binary = "";
284
+ for (let i = 0; i < sigBytes.length; i++) {
285
+ binary += String.fromCharCode(sigBytes[i]);
286
+ }
287
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_");
274
288
  };
275
289
 
276
290
  // src/headers/index.ts
@@ -299,7 +313,7 @@ var createL2Headers = async (signer, creds, l2HeaderArgs, timestamp) => {
299
313
  ts = timestamp;
300
314
  }
301
315
  const address = await getSignerAddress(signer);
302
- const sig = buildPolyHmacSignature(
316
+ const sig = await buildPolyHmacSignature(
303
317
  creds.secret,
304
318
  ts,
305
319
  l2HeaderArgs.method,
@@ -351,7 +365,6 @@ var post = async (endpoint, options, retryOnError = false) => {
351
365
  return resp.data;
352
366
  } catch (err) {
353
367
  if (retryOnError && isTransientAxiosError(err)) {
354
- console.log("[CLOB Client-v2] transient error, retrying once after 30 ms");
355
368
  await sleep(30);
356
369
  try {
357
370
  const resp = await request(
@@ -394,15 +407,6 @@ var del = async (endpoint, options) => {
394
407
  var errorHandling = (err) => {
395
408
  if (import_axios.default.isAxiosError(err)) {
396
409
  if (err.response) {
397
- console.error(
398
- "[CLOB Client] request error",
399
- JSON.stringify({
400
- status: err.response?.status,
401
- statusText: err.response?.statusText,
402
- data: err.response?.data,
403
- config: err.response?.config
404
- })
405
- );
406
410
  if (err.response?.data) {
407
411
  if (typeof err.response?.data === "string" || err.response?.data instanceof String) {
408
412
  return { error: err.response?.data, status: err.response?.status };
@@ -414,16 +418,9 @@ var errorHandling = (err) => {
414
418
  }
415
419
  }
416
420
  if (err.message) {
417
- console.error(
418
- "[CLOB Client] request error",
419
- JSON.stringify({
420
- error: err.message
421
- })
422
- );
423
421
  return { error: err.message };
424
422
  }
425
423
  }
426
- console.error("[CLOB Client] request error", err);
427
424
  return { error: err };
428
425
  };
429
426
  var isTransientAxiosError = (err) => {
@@ -1052,7 +1049,6 @@ function isV2Order(order) {
1052
1049
  }
1053
1050
 
1054
1051
  // src/utilities.ts
1055
- var import_node_crypto2 = require("crypto");
1056
1052
  var roundNormal = (num, decimals) => {
1057
1053
  if (decimalPlaces(num) <= decimals) {
1058
1054
  return num;
@@ -1081,9 +1077,12 @@ var decimalPlaces = (num) => {
1081
1077
  }
1082
1078
  return arr[1].length;
1083
1079
  };
1084
- var generateOrderBookSummaryHash = (orderbook) => {
1080
+ var generateOrderBookSummaryHash = async (orderbook) => {
1085
1081
  orderbook.hash = "";
1086
- const hash = (0, import_node_crypto2.createHash)("sha1").update(JSON.stringify(orderbook)).digest("hex");
1082
+ const data = new TextEncoder().encode(JSON.stringify(orderbook));
1083
+ const hashBuffer = await globalThis.crypto.subtle.digest("SHA-1", data);
1084
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
1085
+ const hash = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
1087
1086
  orderbook.hash = hash;
1088
1087
  return hash;
1089
1088
  };
@@ -1591,7 +1590,7 @@ var ClobClient = class {
1591
1590
  * @param orderbook
1592
1591
  * @returns
1593
1592
  */
1594
- getOrderBookHash(orderbook) {
1593
+ async getOrderBookHash(orderbook) {
1595
1594
  return generateOrderBookSummaryHash(orderbook);
1596
1595
  }
1597
1596
  async getMidpoint(tokenID) {