Support for Borsh (de)serialization
Borsher wraps the Borsh JS and simplifies the creation of schema
pnpm add borsher
import { BorshSchema, borshSerialize, borshDeserialize, Unit } from 'borsher';
const n: number = 100;
const buffer = borshSerialize(BorshSchema.u8, n);
const n: number = 100;
const buffer = borshSerialize(BorshSchema.u16, n);
const n: number = 100;
const buffer = borshSerialize(BorshSchema.u32, n);
const n: bigint = 100n;
const buffer = borshSerialize(BorshSchema.u64, n);
const n: bigint = 100n;
const buffer = borshSerialize(BorshSchema.u128, n);
const n: number = 100;
const buffer = borshSerialize(BorshSchema.i8, n);
const n: number = 100;
const buffer = borshSerialize(BorshSchema.i16, n);
const n: number = 100;
const buffer = borshSerialize(BorshSchema.i32, n);
const n: bigint = 100n;
const buffer = borshSerialize(BorshSchema.i64, n);
const n: bigint = 100n;
const buffer = borshSerialize(BorshSchema.i128, n);
const n: number = 1.0;
const buffer = borshSerialize(BorshSchema.f32, n);
const n: number = 1.0;
const buffer = borshSerialize(BorshSchema.f64, n);
const b: boolean = true;
const buffer = borshSerialize(BorshSchema.bool, b);
const message: string = 'hello world';
const buffer = borshSerialize(BorshSchema.String, message);
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);
const schema = BorshSchema.Array(BorshSchema.String, 2);
const messages: string[] = ['hello', 'world'];
const buffer = borshSerialize(schema, messages);
const schema = BorshSchema.Vec(BorshSchema.String);
const messages: string[] = ['hello', 'world'];
const buffer = borshSerialize(schema, messages);
const schema = BorshSchema.HashSet(BorshSchema.String);
const messages: Set<string> = new Set(['hello', 'world']);
const buffer = borshSerialize(schema, messages);
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);
const unit: Unit = {};
const buffer = borshSerialize(BorshSchema.Unit, unit);
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);
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);
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);