borsher
v3.5.0
Published
Borsh (de)serialization
Downloads
3,679
Readme
Borsher
Support for Borsh (de)serialization
Borsher wraps the Borsh JS and simplifies the creation of schema
Install
pnpm add borsher
Usage
import { BorshSchema, borshSerialize, borshDeserialize, Unit } from 'borsher';
u8
const n: number = 100;
const buffer = borshSerialize(BorshSchema.u8, n);
u16
const n: number = 100;
const buffer = borshSerialize(BorshSchema.u16, n);
u32
const n: number = 100;
const buffer = borshSerialize(BorshSchema.u32, n);
u64
const n: bigint = 100n;
const buffer = borshSerialize(BorshSchema.u64, n);
u128
const n: bigint = 100n;
const buffer = borshSerialize(BorshSchema.u128, n);
i8
const n: number = 100;
const buffer = borshSerialize(BorshSchema.i8, n);
i16
const n: number = 100;
const buffer = borshSerialize(BorshSchema.i16, n);
i32
const n: number = 100;
const buffer = borshSerialize(BorshSchema.i32, n);
i64
const n: bigint = 100n;
const buffer = borshSerialize(BorshSchema.i64, n);
i128
const n: bigint = 100n;
const buffer = borshSerialize(BorshSchema.i128, n);
f32
const n: number = 1.0;
const buffer = borshSerialize(BorshSchema.f32, n);
f64
const n: number = 1.0;
const buffer = borshSerialize(BorshSchema.f64, n);
bool
const b: boolean = true;
const buffer = borshSerialize(BorshSchema.bool, b);
String
const message: string = 'hello world';
const buffer = borshSerialize(BorshSchema.String, message);
Option
const schema = BorshSchema.Option(BorshSchema.String);
const some: string | null = 'hello world';
const someBuffer = borshSerialize(schema, some);
const none: string | null = null;
const noneBuffer = borshSerialize(schema, none);
Array
const schema = BorshSchema.Array(BorshSchema.String, 2);
const messages: string[] = ['hello', 'world'];
const buffer = borshSerialize(schema, messages);
Vec
const schema = BorshSchema.Vec(BorshSchema.String);
const messages: string[] = ['hello', 'world'];
const buffer = borshSerialize(schema, messages);
HashSet
const schema = BorshSchema.HashSet(BorshSchema.String);
const messages: Set<string> = new Set(['hello', 'world']);
const buffer = borshSerialize(schema, messages);
HashMap
const schema = BorshSchema.HashMap(BorshSchema.String, BorshSchema.u128);
const balances: Map<string, bigint> = new Map([
['alice', 1_000_000_000_000_000_000_000_000n],
['bob', 2_000_000_000_000_000_000_000_000n],
]);
const buffer = borshSerialize(schema, balances);
Unit
const unit: Unit = {};
const buffer = borshSerialize(BorshSchema.Unit, unit);
Struct
type Person = {
name: string;
age: number;
};
const schema = BorshSchema.Struct({
name: BorshSchema.String,
age: BorshSchema.u8,
});
const person: Person = {
name: 'alice',
age: 18,
};
const buffer = borshSerialize(schema, person);
Enum Without Associated Type
type Status =
| {
Pending: Unit;
}
| {
Fulfilled: Unit;
}
| {
Rejected: Unit;
};
const schema = BorshSchema.Enum({
Pending: BorshSchema.Unit,
Fulfilled: BorshSchema.Unit,
Rejected: BorshSchema.Unit,
});
const status: Status = {
Pending: {},
};
const buffer = borshSerialize(schema, status);
Enum With Associated Type
type Shape =
| {
Square: number;
}
| {
Rectangle: {
length: number;
width: number;
};
}
| {
Circle: {
radius: number;
};
};
const schema = BorshSchema.Enum({
Square: BorshSchema.u32,
Rectangle: BorshSchema.Struct({
length: BorshSchema.u32,
width: BorshSchema.u32,
}),
Circle: BorshSchema.Struct({
radius: BorshSchema.u32,
}),
});
const shape: Shape = {
Square: 5,
};
const buffer = borshSerialize(schema, shape);