From 1613d0b525ffc5d49e0d726a213c4ca02512d33d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Wed, 30 Oct 2024 14:53:41 +0200 Subject: [PATCH 1/2] Fix decoding of code metadata. --- package-lock.json | 4 ++-- package.json | 2 +- src/smartcontracts/codeMetadata.ts | 7 ++++--- src/smartcontracts/codec/codemetadata.ts | 8 +++----- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 23228e33..96c74793 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@multiversx/sdk-core", - "version": "13.13.0", + "version": "13.13.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@multiversx/sdk-core", - "version": "13.13.0", + "version": "13.13.1", "license": "MIT", "dependencies": { "@multiversx/sdk-transaction-decoder": "1.0.2", diff --git a/package.json b/package.json index a68e5f55..0bc0b4cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-core", - "version": "13.13.0", + "version": "13.13.1", "description": "MultiversX SDK for JavaScript and TypeScript", "author": "MultiversX", "homepage": "https://multiversx.com", diff --git a/src/smartcontracts/codeMetadata.ts b/src/smartcontracts/codeMetadata.ts index 9d2fa2bc..7efff036 100644 --- a/src/smartcontracts/codeMetadata.ts +++ b/src/smartcontracts/codeMetadata.ts @@ -1,3 +1,5 @@ +export const CodeMetadataLength = 2; + /** * The metadata of a Smart Contract, as an abstraction. */ @@ -6,7 +8,6 @@ export class CodeMetadata { public readable: boolean; public payable: boolean; public payableBySc: boolean; - private static readonly codeMetadataLength = 2; static ByteZero = { Upgradeable: 1, @@ -48,8 +49,8 @@ export class CodeMetadata { * Creates a metadata object from a buffer. */ static fromBuffer(buffer: Buffer): CodeMetadata { - if (buffer.length < this.codeMetadataLength) { - throw new Error("Buffer is too short."); + if (buffer.length != CodeMetadataLength) { + throw new Error(`code metadata buffer has length ${buffer.length}, expected ${CodeMetadataLength}`); } const byteZero = buffer[0]; diff --git a/src/smartcontracts/codec/codemetadata.ts b/src/smartcontracts/codec/codemetadata.ts index a631502d..e5502ad8 100644 --- a/src/smartcontracts/codec/codemetadata.ts +++ b/src/smartcontracts/codec/codemetadata.ts @@ -1,16 +1,14 @@ -import { CodeMetadata } from "../codeMetadata"; +import { CodeMetadata, CodeMetadataLength } from "../codeMetadata"; import { CodeMetadataValue } from "../typesystem/codeMetadata"; export class CodeMetadataCodec { decodeNested(buffer: Buffer): [CodeMetadataValue, number] { - const codeMetadata = CodeMetadata.fromBuffer(buffer); - - return [new CodeMetadataValue(codeMetadata), length]; + const codeMetadata = CodeMetadata.fromBuffer(buffer.slice(0, CodeMetadataLength)); + return [new CodeMetadataValue(codeMetadata), CodeMetadataLength]; } decodeTopLevel(buffer: Buffer): CodeMetadataValue { const codeMetadata = CodeMetadata.fromBuffer(buffer); - return new CodeMetadataValue(codeMetadata); } From ee6dd3a9eab4672e74ef2c98c3125e0b96a841f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Wed, 30 Oct 2024 14:57:06 +0200 Subject: [PATCH 2/2] Fix error message. --- src/smartcontracts/codeMetadata.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/smartcontracts/codeMetadata.spec.ts b/src/smartcontracts/codeMetadata.spec.ts index 7b0eab88..7d184c4e 100644 --- a/src/smartcontracts/codeMetadata.spec.ts +++ b/src/smartcontracts/codeMetadata.spec.ts @@ -63,7 +63,7 @@ describe("CodeMetadata Class Tests", function () { CodeMetadata.fromBuffer(buffer); }, Error, - "Buffer is too short.", + "code metadata buffer has length 1, expected 2", ); });