Skip to content

Commit 41b7267

Browse files
Hiksangclaude
andcommitted
fix: validate Lighter API key hex format before WASM signer
Go WASM hex decoder gives cryptic errors on invalid keys (e.g. dots). Now checks for valid hex chars first, logs clear message with fix command, and falls back to read-only mode instead of crashing. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b9921e9 commit 41b7267

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

src/exchanges/lighter.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,22 @@ export class LighterAdapter implements ExchangeAdapter {
126126

127127
// Initialize signer for trading if we have an API key (reuse singleton WASM client)
128128
if (this._apiKey && !signerReady) {
129-
const client = await LighterAdapter.getWasmClient();
130-
await client.createClient({
131-
url: this._baseUrl,
132-
privateKey: this._apiKey,
133-
chainId: this._chainId,
134-
apiKeyIndex: this._apiKeyIndex,
135-
accountIndex: this._accountIndex,
136-
});
137-
this._signer = client;
129+
// Validate API key is valid hex before passing to WASM (Go hex decoder gives cryptic errors otherwise)
130+
const stripped = this._apiKey.startsWith("0x") ? this._apiKey.slice(2) : this._apiKey;
131+
if (!/^[0-9a-fA-F]+$/.test(stripped)) {
132+
console.error(`[lighter] Invalid API key format (expected hex, got non-hex chars). Run 'perp -e lighter manage setup-api-key' to regenerate.`);
133+
this._apiKey = "";
134+
} else {
135+
const client = await LighterAdapter.getWasmClient();
136+
await client.createClient({
137+
url: this._baseUrl,
138+
privateKey: this._apiKey,
139+
chainId: this._chainId,
140+
apiKeyIndex: this._apiKeyIndex,
141+
accountIndex: this._accountIndex,
142+
});
143+
this._signer = client;
144+
}
138145
}
139146
if (this._apiKey) {
140147
this._readOnly = false;

0 commit comments

Comments
 (0)