From 2935a8d5d6902e9d6d81c16af20c275817c09fc3 Mon Sep 17 00:00:00 2001 From: KindKillerwhale Date: Sat, 31 May 2025 01:40:31 +0900 Subject: [PATCH] Fix BooleanCoder.decode to error on non-zero padding and invalid boolean values --- src.ts/abi/coders/boolean.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src.ts/abi/coders/boolean.ts b/src.ts/abi/coders/boolean.ts index a1b96d3e60..299b8c6e7c 100644 --- a/src.ts/abi/coders/boolean.ts +++ b/src.ts/abi/coders/boolean.ts @@ -1,5 +1,6 @@ import { Typed } from "../typed.js"; import { Coder } from "./abstract-coder.js"; +import { assert } from "../../utils/index.js"; import type { Reader, Writer } from "./abstract-coder.js"; @@ -22,6 +23,22 @@ export class BooleanCoder extends Coder { } decode(reader: Reader): any { - return !!reader.readValue(); + const bytes = reader.readBytes(32, false); + + for (let i = 0; i < 31; i++) { + assert( + bytes[i] === 0, + `Boolean padding error: byte[${i}]=0x${bytes[i].toString(16)}`, + "INVALID_ARGUMENT" + ); + } + + const v = bytes[31]; + assert( + v === 0 || v === 1, + `Boolean value error: expected 0 or 1 but got 0x${v.toString(16)}`, + "INVALID_ARGUMENT" + ); + return v === 1; } }