Skip to content

Commit 5542a00

Browse files
committed
fix: preserve leading zeros for cairo bytes data
1 parent 2c2e1a2 commit 5542a00

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

__tests__/utils/cairoDataTypes/CairoByteArray.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,5 +701,19 @@ describe('CairoByteArray Unit Tests', () => {
701701
expect(apiRequest1).toEqual(apiRequest2);
702702
});
703703
});
704+
705+
test('should preserve data leading zeros for toApiRequest()', () => {
706+
const content =
707+
'0x' +
708+
'000000019900000000000002222222374206275726e206d65737aaaa000001' +
709+
'000000029900000000000002222222374206275726e206d65737aaaa000002' +
710+
'000000039900000000000002222222374206275726e206d65737aaaa000003';
711+
const buffer = Buffer.from(content.slice(2), 'hex');
712+
const byteArray = new CairoByteArray(buffer);
713+
const apiRequest = byteArray.toApiRequest();
714+
const reconstructedByteArray = CairoByteArray.factoryFromApiResponse(apiRequest.values());
715+
716+
expect(reconstructedByteArray.toHexString()).toEqual(content);
717+
});
704718
});
705719
});

__tests__/utils/cairoDataTypes/CairoBytes31.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('CairoBytes31 class Unit Tests', () => {
126126
});
127127
});
128128

129-
describe('toUnicode method', () => {
129+
describe('decodeUtf8 method', () => {
130130
test('should convert ASCII text back to original string', () => {
131131
const text = 'hello world';
132132
const bytes31 = new CairoBytes31(text);
@@ -200,7 +200,7 @@ describe('CairoBytes31 class Unit Tests', () => {
200200
test('should convert Uint8Array to hex', () => {
201201
const array = new Uint8Array([1, 2, 3, 4]);
202202
const bytes31 = new CairoBytes31(array);
203-
expect(bytes31.toHexString()).toBe('0x1020304');
203+
expect(bytes31.toHexString()).toBe('0x01020304');
204204
});
205205

206206
test('should handle maximum length data', () => {
@@ -209,6 +209,12 @@ describe('CairoBytes31 class Unit Tests', () => {
209209
const expectedHex = `0x${'ff'.repeat(31)}`;
210210
expect(bytes31.toHexString()).toBe(expectedHex);
211211
});
212+
213+
test('should preserve leading zero values', () => {
214+
const buffer = Buffer.from([0, 0, 1]);
215+
const bytes31 = new CairoBytes31(buffer);
216+
expect(bytes31.toHexString()).toEqual(`0x${buffer.toString('hex')}`);
217+
});
212218
});
213219

214220
describe('toApiRequest method', () => {
@@ -230,7 +236,7 @@ describe('CairoBytes31 class Unit Tests', () => {
230236
test('should return hex string array for Buffer input', () => {
231237
const buffer = Buffer.from([1, 0]); // 0x0100 = 256
232238
const bytes31 = new CairoBytes31(buffer);
233-
expect(bytes31.toApiRequest()).toEqual(['0x100']);
239+
expect(bytes31.toApiRequest()).toEqual(['0x0100']);
234240
});
235241

236242
test('should return hex string array for large values', () => {

src/utils/cairoDataTypes/bytes31.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-underscore-dangle */
2-
import { addHexPrefix, stringToUint8Array, uint8ArrayToBigInt } from '../encode';
2+
import { addHexPrefix, buf2hex, stringToUint8Array, uint8ArrayToBigInt } from '../encode';
33
import { getNext } from '../num';
44
import assert from '../assert';
55
import { addCompiledFlag } from '../helpers';
@@ -22,7 +22,7 @@ export class CairoBytes31 {
2222
return stringToUint8Array(data);
2323
}
2424
if (isBuffer(data)) {
25-
return new Uint8Array(data as Buffer);
25+
return new Uint8Array(data);
2626
}
2727
if (data instanceof Uint8Array) {
2828
return new Uint8Array(data);
@@ -43,7 +43,11 @@ export class CairoBytes31 {
4343
}
4444

4545
toHexString() {
46-
return addHexPrefix(this.toBigInt().toString(16));
46+
// TODO: revisit empty data handling for CairoBytes31 and CairoByteArray
47+
// how to differentiate empty and zero input
48+
const hexValue = this.data.length === 0 ? '0' : buf2hex(this.data);
49+
50+
return addHexPrefix(hexValue);
4751
}
4852

4953
static validate(data: Uint8Array | string | Buffer | unknown): void {

0 commit comments

Comments
 (0)