ts-scale-codec
A modular, composable, strongly typed and lightweight implementation of the SCALE Codec
Installation
npm install --save @josepot/ts-scale-codec
Usage Example
import { Struct, U32, Vector, Str, Enum, Bool } from "@josepot/ts-scale-codec"
import { bufferToHex } from "./utils"
const [encoder, decoder] = Struct({
id: U32,
name: Str,
friendIds: Vector(U32),
event: Enum({
one: Str,
many: Vector(Str),
allOrNothing: Bool,
})
})
/*
Something very important is the types that are being inferred from this definition,
which both the encoder and the decoder will use. For instance, the input of the
encoder must be compatible with the following interface:
interface SomeData {
id: number;
name: string;
friendIds: number[];
event:
| { tag: "one"; value: string; }
| { tag: "many"; value: string[]; }
| { tag: "allOrNothing"; value: boolean; };
}
Which, as you might expect, it's the same interface that's returned by the
decoder.
*/
const encodedData: ArrayBuffer = encoder({
id: 100,
name: "Some name",
friendIds: [1, 2, 3],
event: { tag: "allOrNothing" as const, value: true }
})
console.log(bufferToHex(encodedData))
// => 0x6400000024536f6d65206e616d650c0100000002000000030000000201
const decodedData = decoder(encodedData).value
// also possible:
// const decodedData = decoder("0x6400000024536f6d65206e616d650c0100000002000000030000000201").value
console.log(JSON.stringify(decodedData, null, 2))
// =>
//{
// "id": 100,
// "name": "Some name",
// "friendIds": [
// 1,
// 2,
// 3
// ],
// "event": {
// "tag": "allOrNothing",
// "value": true
// }
//}
Custom definitions
In this library you won't find common definitions like AccountId
. However,
since the definitions of this library are enhanceable and composable, it's
very easy to create new custom definitions. For instance, the implementation of
the Str
Codec looks like this:
import { enhanceCodec, Vector, U8 } from "@/."
const fromStringToBytes = (value: string) =>
value.split("").map((_, idx) => value.charCodeAt(idx))
const fromBytesToString = (bytes: number[]) => String.fromCharCode(...bytes)
export const Str = enhanceCodec(
Vector(U8),
fromStringToBytes,
fromBytesToString,
)
Similarly, you could implement any other definitions are that based on other
definitions. For instance, a possible implementation of an AccountId
definition could be:
import { enhanceCodec, Bytes } from "@josepot/ts-scale-codec"
import { decodeAddress, encodeAddress } from "@polkadot/util-crypto"
export const AccountId = enhanceCodec(
Bytes(32),
decodeAddress,
encodeAddress,
)