fix: Use canonical JSON serialization in HMAC signature #191
+3
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The Python client was throwing
PolyApiException[status_code=401, error_message={'error': 'Unauthorized/Invalid api key'}]when callingpost_order(), even with valid API credentials.Root Cause
The HMAC signature was computed using Python's
str()representation of the body dictionary, which created non-canonical JSON that didn't match the canonical JSON used by Go/TypeScript servers, causing signature validation to fail.Solution
Changed to use canonical JSON serialization with
json.dumps(body, separators=(',', ':'))to ensure compact, canonical JSON that matches across all languages.Changes
py_clob_client/signing/hmac.pyimport jsonstr(body).replace("'", '"')withjson.dumps(body, separators=(',', ':'))Testing
✅ Successfully tested with signature_type=1 (Email/Magic wallet proxy)
✅ Order posting now works - placed and filled order successfully
✅ Order response:
{'status': 'matched', 'success': True}This is the same fix confirmed working by multiple users in related discussions.
Note
Switch HMAC body serialization to canonical JSON (json.dumps with compact separators) to ensure cross-language signature consistency.
build_hmac_signatureinpy_clob_client/signing/hmac.pyto serializebodywithjson.dumps(..., separators=(",", ":"))for canonical JSON.import jsonand remove reliance on Pythonstr()representation.Written by Cursor Bugbot for commit 4e87b8a. This will update automatically on new commits. Configure here.