Skip to content

Commit 1815469

Browse files
authored
fix(abi): preserve leading NUL bytes in strings (#301)
1 parent 1074bb3 commit 1815469

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

.changeset/calm-pandas-smile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ox': patch
3+
---
4+
5+
Preserved leading NUL bytes when decoding ABI `string` values.

src/core/_test/AbiParameters.decode.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,19 @@ describe('dynamic', () => {
443443
expect(result).toEqual([''])
444444
})
445445

446+
test('leading NUL byte', () => {
447+
const result = AbiParameters.decode(
448+
AbiParameters.from('string'),
449+
'0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030061620000000000000000000000000000000000000000000000000000000000',
450+
)
451+
assertType<readonly [string]>(result)
452+
expect(result).toMatchInlineSnapshot(`
453+
[
454+
"\u0000ab",
455+
]
456+
`)
457+
})
458+
446459
test('default', () => {
447460
expect(
448461
AbiParameters.decode(

src/core/internal/abiParameters.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ export function decodeString(
407407
}
408408

409409
const data = cursor.readBytes(length, 32)
410-
const value = Bytes.toString(Bytes.trimLeft(data))
410+
const value = Bytes.toString(data)
411411

412412
// As we have gone wondering, restore to the original position + next slot.
413413
cursor.setPosition(staticPosition + 32)
@@ -419,7 +419,6 @@ export declare namespace decodeString {
419419
type ErrorType =
420420
| Bytes.toNumber.ErrorType
421421
| Bytes.toString.ErrorType
422-
| Bytes.trimLeft.ErrorType
423422
| Errors.GlobalErrorType
424423
}
425424

0 commit comments

Comments
 (0)