@scale-codec/core
v2.0.1
Published
Low-level tools to perform serialization and deserialization via SCALE spec
Downloads
466
Maintainers
Readme
@scale-codec/core
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 andUint8Array
in JS) - Efficient codec for vectors of bytes (
Vec<u8>
in Rust andUint8Array
in JS) OptionBool