Skip to content

Commit 02b2314

Browse files
committed
fix: use byte array for stark key generation signature message
Replaces string-based signature message with raw bytes in stark key generation to prevent encoding issues in non-English systems. This aligns with the fix for wallet connection message encoding.
1 parent 754efbd commit 02b2314

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

packages/x-client/src/utils/stark/legacy/crypto/constants.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ import BN from 'bn.js';
44
import elliptic from 'elliptic';
55
import hashJS from 'hash.js';
66

7+
import { toUtf8String } from 'ethers';
78
import { constantPointsHex } from './points';
89
import { Instruction, InstructionWithFee } from './types';
910

1011
const DEFAULT_ACCOUNT_MAPPING_KEY = 'STARKWARE_ACCOUNT_MAPPING';
11-
const DEFAULT_SIGNATURE_MESSAGE = 'Only sign this request if you’ve initiated an action with Immutable X.';
12+
// const DEFAULT_SIGNATURE_MESSAGE = 'Only sign this request if you’ve initiated an action with Immutable X.';
13+
// non-english systems may not encode this correctly
14+
const DEFAULT_SIGNATURE_BYTES = new Uint8Array([
15+
79, 110, 108, 121, 32, 115, 105, 103, 110, 32, 116, 104, 105, 115, 32, 114,
16+
101, 113, 117, 101, 115, 116, 32, 105, 102, 32, 121, 111, 117, 226, 128, 153,
17+
118, 101, 32, 105, 110, 105, 116, 105, 97, 116, 101, 100, 32, 97, 110, 32,
18+
97, 99, 116, 105, 111, 110, 32, 119, 105, 116, 104, 32, 73, 109, 109, 117,
19+
116, 97, 98, 108, 101, 32, 88, 46,
20+
]);
21+
const DEFAULT_SIGNATURE_MESSAGE = toUtf8String(DEFAULT_SIGNATURE_BYTES);
1222

1323
const DEFAULT_ACCOUNT_LAYER = 'starkex';
1424
const DEFAULT_ACCOUNT_APPLICATION = 'immutablex';
@@ -95,6 +105,7 @@ export {
95105
DEFAULT_ACCOUNT_INDEX,
96106
DEFAULT_ACCOUNT_LAYER,
97107
DEFAULT_ACCOUNT_MAPPING_KEY,
108+
DEFAULT_SIGNATURE_BYTES,
98109
DEFAULT_SIGNATURE_MESSAGE,
99110
instructionEncodingMap,
100111
MAX_ECDSA_BN,

packages/x-client/src/utils/stark/starkCurve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as encUtils from 'enc-utils';
66
// eslint-disable-next-line @typescript-eslint/naming-convention
77
import BN from 'bn.js';
88
import { hdkey } from '@ethereumjs/wallet';
9-
import { Signature, Signer, toUtf8Bytes } from 'ethers';
9+
import { Signature, Signer } from 'ethers';
1010
import { createStarkSigner } from './starkSigner';
1111
import * as legacy from './legacy/crypto';
1212
import { getStarkPublicKeyFromImx } from './getStarkPublicKeyFromImx';
@@ -286,7 +286,7 @@ export async function generateLegacyStarkPrivateKey(
286286
signer: Signer,
287287
): Promise<string> {
288288
const address = (await signer.getAddress()).toLowerCase();
289-
const signature = await signer.signMessage(toUtf8Bytes(legacy.DEFAULT_SIGNATURE_MESSAGE));
289+
const signature = await signer.signMessage(legacy.DEFAULT_SIGNATURE_BYTES);
290290
const seed = Signature.from(signature).s;
291291
const path = legacy.getAccountPath(
292292
legacy.DEFAULT_ACCOUNT_LAYER,

packages/x-provider/src/imx-wallet/imxWallet.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Environment } from '@imtbl/config';
22
import {
33
BrowserProvider,
4-
getBytes,
5-
toUtf8Bytes,
64
toUtf8String,
75
} from 'ethers';
86
import {
@@ -21,7 +19,14 @@ import { messageResponseListener } from './messageResponseListener';
2119
import { ImxSigner } from './ImxSigner';
2220
import { getOrSetupIFrame } from './imxWalletIFrame';
2321

24-
const DEFAULT_CONNECTION_MESSAGE = 'Only sign this request if you’ve initiated an action with Immutable X.';
22+
// "Only sign this request if you've initiated an action with Immutable X."
23+
const DEFAULT_CONNECTION_BYTES = new Uint8Array([
24+
79, 110, 108, 121, 32, 115, 105, 103, 110, 32, 116, 104, 105, 115, 32, 114,
25+
101, 113, 117, 101, 115, 116, 32, 105, 102, 32, 121, 111, 117, 226, 128, 153,
26+
118, 101, 32, 105, 110, 105, 116, 105, 97, 116, 101, 100, 32, 97, 110, 32,
27+
97, 99, 116, 105, 111, 110, 32, 119, 105, 116, 104, 32, 73, 109, 109, 117,
28+
116, 97, 98, 108, 101, 32, 88, 46,
29+
]);
2530
const CONNECTION_FAILED_ERROR = 'The L2 IMX Wallet connection has failed';
2631

2732
export async function connect(
@@ -31,13 +36,11 @@ export async function connect(
3136
const l1Signer = await l1Provider.getSigner();
3237
const address = await l1Signer.getAddress();
3338

34-
console.log('DEFAULT_CONNECTION_MESSAGE', { message: DEFAULT_CONNECTION_MESSAGE });
35-
console.log('toUtf8Bytes.toString()', { toUtf8Bytes: toUtf8Bytes(DEFAULT_CONNECTION_MESSAGE).toString() });
39+
// log read message here
40+
console.log('DEFAULT_CONNECTION_BYTES.toString()', { bytes: DEFAULT_CONNECTION_BYTES.toString() });
41+
console.log('DEFAULT_CONNECTION_BYTES.toUtf8String()', { bytes: toUtf8String(DEFAULT_CONNECTION_BYTES) });
3642

37-
console.log('toUtf8String 1', { toUtf8String: toUtf8String(toUtf8Bytes(DEFAULT_CONNECTION_MESSAGE)) });
38-
console.log('toUtf8String 2', { toUtf8String: toUtf8String(getBytes(toUtf8Bytes(DEFAULT_CONNECTION_MESSAGE))) });
39-
40-
const signature = await l1Signer.signMessage(toUtf8Bytes(DEFAULT_CONNECTION_MESSAGE));
43+
const signature = await l1Signer.signMessage(DEFAULT_CONNECTION_BYTES);
4144
const iframe = await getOrSetupIFrame(env);
4245

4346
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)