From 5b3ee51c8651c00646de4417bf82f4d3d4a545a7 Mon Sep 17 00:00:00 2001 From: Micha Vie Date: Fri, 30 Aug 2024 18:03:34 +0200 Subject: [PATCH] add codemetadata codec --- src/smartcontracts/codec/codemetadata.ts | 24 +++++++++++++++++++++++ src/smartcontracts/codec/primitive.ts | 8 ++++++++ src/smartcontracts/typesystem/index.ts | 1 + src/smartcontracts/typesystem/matchers.ts | 20 +++++++++++++------ 4 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 src/smartcontracts/codec/codemetadata.ts diff --git a/src/smartcontracts/codec/codemetadata.ts b/src/smartcontracts/codec/codemetadata.ts new file mode 100644 index 00000000..a631502d --- /dev/null +++ b/src/smartcontracts/codec/codemetadata.ts @@ -0,0 +1,24 @@ +import { CodeMetadata } 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]; + } + + decodeTopLevel(buffer: Buffer): CodeMetadataValue { + const codeMetadata = CodeMetadata.fromBuffer(buffer); + + return new CodeMetadataValue(codeMetadata); + } + + encodeNested(codeMetadata: CodeMetadataValue): Buffer { + return codeMetadata.valueOf().toBuffer(); + } + + encodeTopLevel(codeMetadata: CodeMetadataValue): Buffer { + return codeMetadata.valueOf().toBuffer(); + } +} diff --git a/src/smartcontracts/codec/primitive.ts b/src/smartcontracts/codec/primitive.ts index f761c0df..2a6cd9e2 100644 --- a/src/smartcontracts/codec/primitive.ts +++ b/src/smartcontracts/codec/primitive.ts @@ -1,4 +1,5 @@ import { + CodeMetadataValue, PrimitiveType, PrimitiveValue, onPrimitiveTypeSelect, @@ -19,6 +20,7 @@ import { H256Value } from "../typesystem/h256"; import { BytesBinaryCodec } from "./bytes"; import { TokenIdentifierCodec } from "./tokenIdentifier"; import { TokenIdentifierValue } from "../typesystem/tokenIdentifier"; +import { CodeMetadataCodec } from "./codemetadata"; import { NothingCodec } from "./nothing"; import { StringBinaryCodec } from "./string"; @@ -32,6 +34,7 @@ export class PrimitiveBinaryCodec { private readonly bytesCodec: BytesBinaryCodec; private readonly stringCodec: StringBinaryCodec; private readonly tokenIdentifierCodec: TokenIdentifierCodec; + private readonly codeMetadataCodec: CodeMetadataCodec; private readonly nothingCodec: NothingCodec; constructor(binaryCodec: BinaryCodec) { @@ -44,6 +47,7 @@ export class PrimitiveBinaryCodec { this.bytesCodec = new BytesBinaryCodec(); this.stringCodec = new StringBinaryCodec(); this.tokenIdentifierCodec = new TokenIdentifierCodec(); + this.codeMetadataCodec = new CodeMetadataCodec(); this.nothingCodec = new NothingCodec(); } @@ -56,6 +60,7 @@ export class PrimitiveBinaryCodec { onString: () => this.stringCodec.decodeNested(buffer), onH256: () => this.h256Codec.decodeNested(buffer), onTokenIndetifier: () => this.tokenIdentifierCodec.decodeNested(buffer), + onCodeMetadata: () => this.codeMetadataCodec.decodeNested(buffer), onNothing: () => this.nothingCodec.decodeNested(), }); } @@ -69,6 +74,7 @@ export class PrimitiveBinaryCodec { onString: () => this.stringCodec.decodeTopLevel(buffer), onH256: () => this.h256Codec.decodeTopLevel(buffer), onTokenIndetifier: () => this.tokenIdentifierCodec.decodeTopLevel(buffer), + onCodeMetadata: () => this.codeMetadataCodec.decodeTopLevel(buffer), onNothing: () => this.nothingCodec.decodeTopLevel(), }); } @@ -82,6 +88,7 @@ export class PrimitiveBinaryCodec { onString: () => this.stringCodec.encodeNested(value), onH256: () => this.h256Codec.encodeNested(value), onTypeIdentifier: () => this.tokenIdentifierCodec.encodeNested(value), + onCodeMetadata: () => this.codeMetadataCodec.encodeNested(value), onNothing: () => this.nothingCodec.encodeNested(), }); } @@ -95,6 +102,7 @@ export class PrimitiveBinaryCodec { onString: () => this.stringCodec.encodeTopLevel(value), onH256: () => this.h256Codec.encodeTopLevel(value), onTypeIdentifier: () => this.tokenIdentifierCodec.encodeTopLevel(value), + onCodeMetadata: () => this.codeMetadataCodec.encodeTopLevel(value), onNothing: () => this.nothingCodec.encodeTopLevel(), }); } diff --git a/src/smartcontracts/typesystem/index.ts b/src/smartcontracts/typesystem/index.ts index f7bc39d4..85cc3b44 100644 --- a/src/smartcontracts/typesystem/index.ts +++ b/src/smartcontracts/typesystem/index.ts @@ -8,6 +8,7 @@ export * from "./address"; export * from "./algebraic"; export * from "./boolean"; export * from "./bytes"; +export * from "./codeMetadata"; export * from "./composite"; export * from "./endpoint"; export * from "./enum"; diff --git a/src/smartcontracts/typesystem/matchers.ts b/src/smartcontracts/typesystem/matchers.ts index 43957d53..2b2f2fea 100644 --- a/src/smartcontracts/typesystem/matchers.ts +++ b/src/smartcontracts/typesystem/matchers.ts @@ -2,18 +2,18 @@ import * as errors from "../../errors"; import { AddressType, AddressValue } from "./address"; import { BooleanType, BooleanValue } from "./boolean"; import { BytesType, BytesValue } from "./bytes"; +import { CodeMetadataType, CodeMetadataValue } from "./codeMetadata"; import { EnumType, EnumValue } from "./enum"; -import { OptionType, OptionValue, List, ListType } from "./generic"; +import { List, ListType, OptionType, OptionValue } from "./generic"; +import { ArrayVec, ArrayVecType } from "./genericArray"; import { H256Type, H256Value } from "./h256"; -import { NumericalType, NumericalValue } from "./numerical"; import { NothingType, NothingValue } from "./nothing"; +import { NumericalType, NumericalValue } from "./numerical"; +import { StringType, StringValue } from "./string"; import { Struct, StructType } from "./struct"; import { TokenIdentifierType, TokenIdentifierValue } from "./tokenIdentifier"; import { Tuple, TupleType } from "./tuple"; -import { Type, PrimitiveType, PrimitiveValue } from "./types"; -import { ArrayVec, ArrayVecType } from "./genericArray"; -import { TypedValue } from "./types"; -import { StringType, StringValue } from "./string"; +import { PrimitiveType, PrimitiveValue, Type, TypedValue } from "./types"; // TODO: Extend functionality or rename wrt. restricted / reduced functionality (not all types are handled: composite, variadic). export function onTypeSelect( @@ -110,6 +110,7 @@ export function onPrimitiveValueSelect( onString: () => TResult; onH256: () => TResult; onTypeIdentifier: () => TResult; + onCodeMetadata: () => TResult; onNothing: () => TResult; onOther?: () => TResult; }, @@ -135,6 +136,9 @@ export function onPrimitiveValueSelect( if (value.hasExactClass(TokenIdentifierValue.ClassName)) { return selectors.onTypeIdentifier(); } + if (value.hasExactClass(CodeMetadataValue.ClassName)) { + return selectors.onCodeMetadata(); + } if (value.hasExactClass(NothingValue.ClassName)) { return selectors.onNothing(); } @@ -155,6 +159,7 @@ export function onPrimitiveTypeSelect( onString: () => TResult; onH256: () => TResult; onTokenIndetifier: () => TResult; + onCodeMetadata: () => TResult; onNothing: () => TResult; onOther?: () => TResult; }, @@ -180,6 +185,9 @@ export function onPrimitiveTypeSelect( if (type.hasExactClass(TokenIdentifierType.ClassName)) { return selectors.onTokenIndetifier(); } + if (type.hasExactClass(CodeMetadataType.ClassName)) { + return selectors.onCodeMetadata(); + } if (type.hasExactClass(NothingType.ClassName)) { return selectors.onNothing(); }