Skip to content

Commit

Permalink
Merge pull request #523 from multiversx/fix-code-metadata
Browse files Browse the repository at this point in the history
Fix decoding of code metadata
  • Loading branch information
andreibancioiu authored Oct 30, 2024
2 parents 2b76735 + ee6dd3a commit ea1b063
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/smartcontracts/codeMetadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});

Expand Down
7 changes: 4 additions & 3 deletions src/smartcontracts/codeMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const CodeMetadataLength = 2;

/**
* The metadata of a Smart Contract, as an abstraction.
*/
Expand All @@ -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,
Expand Down Expand Up @@ -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];
Expand Down
8 changes: 3 additions & 5 deletions src/smartcontracts/codec/codemetadata.ts
Original file line number Diff line number Diff line change
@@ -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);
}

Expand Down

0 comments on commit ea1b063

Please sign in to comment.