@scale-codec/core
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

@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:

Spec JS Type Details
Int number, bigint signed/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.
Compact number, bigint -
String string -
Bool boolean -
Unit type null, undefined () in Rust. JavaScript doesn't have zero-cost abstractions, so this codec could be used to handle them.

Higher-order:

Spec JS Type
Array Array
Vector Array
Tuple Array, but tuple in TypeScript
Set Set
Map Map
Struct Plain Object
Enum Variant 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

Package Sidebar

Install

npm i @scale-codec/core

Weekly Downloads

177

Version

2.0.1

License

Apache-2.0

Unpacked Size

84.7 kB

Total Files

7

Last publish

Collaborators

  • soramitsu-admin