From f787d0979d68be82139aa5790a7d904adb7fe626 Mon Sep 17 00:00:00 2001 From: takinanton <191816639+takinanton@users.noreply.github.com> Date: Sat, 28 Nov 2026 07:56:28 +0000 Subject: [PATCH] fix(num): keep 4 bytes in stringToSha256ToArrayBuff4 when the top byte is zero Co-Authored-By: Claude Fable 5.2 --- __tests__/utils/ledgerSigner.test.ts | 8 ++++++++ __tests__/utils/num.test.ts | 5 +++++ src/utils/num.ts | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/__tests__/utils/ledgerSigner.test.ts b/__tests__/utils/ledgerSigner.test.ts index 8c8428a78..a519e5253 100644 --- a/__tests__/utils/ledgerSigner.test.ts +++ b/__tests__/utils/ledgerSigner.test.ts @@ -83,6 +83,14 @@ describe('starknet/ledger', () => { ); }); + test('getLedgerPathBuffer111 returns 24 bytes when the application hash starts with a zero byte', () => { + expect(getLedgerPathBuffer111(0, 'App135')).toEqual( + new Uint8Array([ + 128, 0, 10, 85, 71, 65, 233, 201, 0, 40, 206, 181, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]) + ); + }); + test('getLedgerPathBuffer221 returns the EIP2645 path of the APPs from v2.2.1 to v2.4.3', () => { expect(getLedgerPathBuffer221(0)).toEqual( new Uint8Array([ diff --git a/__tests__/utils/num.test.ts b/__tests__/utils/num.test.ts index b8c30c4e3..5b952f87e 100644 --- a/__tests__/utils/num.test.ts +++ b/__tests__/utils/num.test.ts @@ -203,6 +203,11 @@ describe('stringToSha256ToArrayBuff4', () => { const buff = num.stringToSha256ToArrayBuff4('LedgerW'); expect(buff).toEqual(new Uint8Array([43, 206, 231, 219])); }); + + test('should keep 4 bytes when the leading byte is zero', () => { + const buff = num.stringToSha256ToArrayBuff4('App135'); + expect(buff).toEqual(new Uint8Array([0, 40, 206, 181])); + }); }); describe('isBigNumberish', () => { diff --git a/src/utils/num.ts b/src/utils/num.ts index c2bbc27f7..1fdd44ca5 100644 --- a/src/utils/num.ts +++ b/src/utils/num.ts @@ -387,7 +387,7 @@ export function stringToSha256ToArrayBuff4(str: string): Uint8Array { // eslint-disable-next-line no-bitwise const int31 = (n: bigint) => Number(n & MASK_31); const result: number = int31(BigInt(addHexPrefix(buf2hex(sha256(utf8ToBytes(str)))))); - return hexToBytes(toHex(result)); + return hexToBytes(addHexPrefix(removeHexPrefix(toHex(result)).padStart(8, '0'))); } /**