as-scale-codec
AS implementation of SCALE Codec
Overview
as-scale-codec is an AssemblyScript implementation of Polkadot SCALE Codec. The codec is used as a communication mechanism between Substrate Hosts and Substrate Runtimes.
This is an integral part of the Subsembly, since it provides SCALE encoding and decoding functionality for the types.
The library is maintained by LimeChain and has improved significantly over the course of Subsembly development.
Codec
Every Subsembly type needs to have encoding and decoding features, which is provided from the Codec interface of as-scale-codec. If you were to create your custom type to use in your Subsembly runtime, type has to implement Codec interface.
Examples
Encoding
Every type has а toU8a function. It encodes type value into an array of bytes
import { Bool, String } from "as-scale-codec"
// Bool
const scaleBool = new Bool(true);
scaleBool.toU8a() // => [0x01]
// String
const scaleString = new ScaleString("a");
scaleString.toU8a() // => [0x04, 0x61]
// UInt64
const uInt64 = new UInt64(10);
uInt64.toU8a(); // => [0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]Decoding
We can decode arbitrary bytes into as-scale-codec using BytesReader:
For more information, please visit as-scale-codec and SCALE docs.
Last updated
Was this helpful?