fix: Use canonical JSON serialization in HMAC signature#191
Open
rinadelph wants to merge 1 commit intoPolymarket:mainfrom
Open
fix: Use canonical JSON serialization in HMAC signature#191rinadelph wants to merge 1 commit intoPolymarket:mainfrom
rinadelph wants to merge 1 commit intoPolymarket:mainfrom
Conversation
Fixes 'Unauthorized/Invalid api key' error when posting orders.
The issue was that the HMAC signature was computed using Python's
str() representation of the body dictionary (e.g., "{'price': '0.3'}")
with single quotes replaced by double quotes. This creates a non-canonical
JSON representation that doesn't match what the server expects.
Changed to use json.dumps(body, separators=(',', ':')) to ensure
canonical, compact JSON serialization that matches Go/TypeScript clients.
This ensures cross-language HMAC consistency and fixes the 401
authentication errors when calling post_order().
Tested and verified working with signature_type=1 (Email/Magic wallet proxy).
Contributor
|
@rinadelph a fix has been merged and version 0.30.0 has been created: #176 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.