Class Serializer

The serializer class serialize and deserialize data using protocol buffers. It accepts the descriptor in JSON or binary format

NOTE: This class uses the protobufjs library internally, which uses reflection (use of eval and new Function) for the construction of the types. This could cause issues in environments where eval is not allowed, like in browser extensions. In such cases, this class must be confined in a sandbox environment where eval is allowed. This is the principal reason of having the serializer in a separate class.

Example

// using descriptor JSON
const descriptorJson = {
nested: {
awesomepackage: {
nested: {
AwesomeMessage: {
fields: {
awesome_field: {
type: "string",
id: 1,
},
},
},
},
},
},
};
const serializer1 = new Serializer(descriptorJson);
const message1 = await serializer1.deserialize(
"CgZrb2lub3M=",
"AwesomeMessage"
);
console.log(message1);
// { awesome_field: 'koinos' }

// using descriptor binary
const descriptorBinary =
"Cl4KDWF3ZXNvbWUucHJvdG8SDmF3ZXNvbWVwYWN" +
"rYWdlIjUKDkF3ZXNvbWVNZXNzYWdlEiMKDWF3ZX" +
"NvbWVfZmllbGQYASABKAlSDGF3ZXNvbWVGaWVsZ" +
"GIGcHJvdG8z";
const serializer2 = new Serializer(descriptorBinary);
const message2 = await serializer2.deserialize(
"CgZrb2lub3M=",
"AwesomeMessage"
);
console.log(message2);
// { awesome_field: 'koinos' }

Hierarchy

  • Serializer

Constructors

  • Parameters

    • types: string | INamespace
    • Optional opts: { argumentsTypeName?: string; bytesConversion?: boolean; defaultTypeName?: string; returnTypeName?: string }
      • Optional argumentsTypeName?: string

        Type name for arguments when using Provider.invokeSystemCall

      • Optional bytesConversion?: boolean

        Bytes conversion. Option to preformat bytes when "(koinos_bytes_type)" is defined in the type definitions. By default it is true.

      • Optional defaultTypeName?: string

        Default type name. Use this option when you always want to serialize/deserialize the same type

      • Optional returnTypeName?: string

        Type name for the output when using Provider.invokeSystemCall

    Returns Serializer

Properties

argumentsTypeName?: string

Type name for arguments when using Provider.invokeSystemCall

bytesConversion: boolean = true

Preformat bytes for base64url, base58 or hex string

defaultType?: Type

Default type for all serializations

returnTypeName?: string

Type name for the output when using Provider.invokeSystemCall

root: Root

Protobuffer definitions

types: string | INamespace

Protobuffers descriptor in JSON format. See https://www.npmjs.com/package/protobufjs#using-json-descriptors

verifyChecksum: { deserialize: boolean; serialize: boolean } = ...

Verify checksum in addresses during serialization or deserialization

Type declaration

  • deserialize: boolean
  • serialize: boolean

Methods

  • Parameters

    • valueBtypeEncoded: Record<string, unknown> | unknown[]
    • protobufType: Type
    • verifyChecksum: boolean

    Returns Record<string, unknown>

  • Parameters

    • valueBtypeDecoded: Record<string, unknown> | unknown[]
    • protobufType: Type
    • verifyChecksum: boolean

    Returns Record<string, unknown>

  • Function to decode bytes using the protobuffer definitions It also encodes the bytes for special cases (base58, hex string) when bytesConversion param is true.

    Type Parameters

    • T = Record<string, unknown>

    Parameters

    • valueEncoded: string | Uint8Array
    • Optional typeName: string
    • Optional opts: { bytesConversion?: boolean; verifyChecksum?: boolean }
      • Optional bytesConversion?: boolean
      • Optional verifyChecksum?: boolean

    Returns Promise<T>

  • Function to encode a type using the protobuffer definitions It also prepares the bytes for special cases (base58, hex string) when bytesConversion param is true.

    Parameters

    • valueDecoded: Record<string, unknown>
    • Optional typeName: string
    • Optional opts: { bytesConversion?: boolean; verifyChecksum?: boolean }
      • Optional bytesConversion?: boolean
      • Optional verifyChecksum?: boolean

    Returns Promise<Uint8Array>

Generated using TypeDoc