2.0.1 • Published 1 year ago

@scale-codec/core v2.0.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

@scale-codec/core build status version license

Low-level tools to perform serialization and deserialization according to SCALE spec.

Installation

Available on NPM:

npm i @scale-codec/core

Example

Encode:

import { createStructEncoder, Encode, encodeStr, encodeU64, WalkerImpl } from '@scale-codec/core'

const foo = { bar: 'baz', foo: 90n }

const encodeFoo: Encode<typeof foo> = createStructEncoder([
  ['bar', encodeStr],
  ['foo', encodeU64],
])

const bytes = WalkerImpl.encode(foo, encodeFoo)

Decode:

import { Decode, WalkerImpl, createStructDecoder, decodeStr, decodeU64 } from '@scale-codec/core'

const bytes = new Uint8Array([12, 98, 97, 122, 90, 0, 0, 0, 0, 0, 0, 0])

interface Foo {
  bar: string
  foo: bigint
}

const decodeFoo: Decode<Foo> = createStructDecoder([
  ['bar', decodeStr],
  ['foo', decodeU64],
])

const foo = WalkerImpl.decode(bytes, decodeFoo)

Supported types

Primitive:

SpecJS TypeDetails
Intnumber, bigintsigned/unsigned, 8/16/32/64/128/etc bits. For integers with 64+ bits, you can only use bigint. For integers with less bits, you can use both number and bigint.
Compactnumber, bigint-
Stringstring-
Boolboolean-
Unit typenull, undefined() in Rust. JavaScript doesn't have zero-cost abstractions, so this codec could be used to handle them.

Higher-order:

SpecJS Type
ArrayArray
VectorArray
TupleArray, but tuple in TypeScript
SetSet
MapMap
StructPlain Object
EnumVariant from @scale-codec/enum

Special:

  • Efficient codec for arrays of bytes ([u8; x] in Rust and Uint8Array in JS)
  • Efficient codec for vectors of bytes (Vec<u8> in Rust and Uint8Array in JS)
  • OptionBool

API

@scale-codec/core API