🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

borsher

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

borsher

Borsh (de)serialization

4.0.0
latest
npm
Version published
Weekly downloads
1.2K
-29.65%
Maintainers
1
Weekly downloads
 
Created
Source

Borsher

Support for Borsh (de)serialization

Borsher wraps the Borsh JS and simplifies the creation of schema

Install

pnpm add borsher

Usage

import { borshSerialize, borshDeserialize, BorshSchema, Infer, Unit } from 'borsher';

u8

const schema = BorshSchema.u8;
const n: number = 100;
const buffer = borshSerialize(schema, n);

u16

const schema = BorshSchema.u16;
const n: number = 100;
const buffer = borshSerialize(schema, n);

u32

const schema = BorshSchema.u32;
const n: number = 100;
const buffer = borshSerialize(schema, n);

u64

const schema = BorshSchema.u64;
const n: bigint = 100n;
const buffer = borshSerialize(schema, n);

u128

const schema = BorshSchema.u128;
const n: bigint = 100n;
const buffer = borshSerialize(schema, n);

i8

const schema = BorshSchema.i8;
const n: number = 100;
const buffer = borshSerialize(schema, n);

i16

const schema = BorshSchema.i16;
const n: number = 100;
const buffer = borshSerialize(schema, n);

i32

const schema = BorshSchema.i32;
const n: number = 100;
const buffer = borshSerialize(schema, n);

i64

const schema = BorshSchema.i64;
const n: bigint = 100n;
const buffer = borshSerialize(schema, n);

i128

const schema = BorshSchema.i128;
const n: bigint = 100n;
const buffer = borshSerialize(schema, n);

f32

const schema = BorshSchema.f32;
const n: number = 1.0;
const buffer = borshSerialize(schema, n);

f64

const schema = BorshSchema.f64;
const n: number = 1.0;
const buffer = borshSerialize(schema, n);

bool

const schema = BorshSchema.bool;
const b: boolean = true;
const buffer = borshSerialize(schema, b);

String

const schema = BorshSchema.String;
const message: string = 'hello world';
const buffer = borshSerialize(schema, 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 schema = BorshSchema.Unit;
const unit: Unit = {};
const buffer = borshSerialize(schema, unit);

Struct

const schema = BorshSchema.Struct({
  name: BorshSchema.String,
  age: BorshSchema.u8,
});

type Person = Infer<typeof schema>;

// type Person = {
//   name: string;
//   age: number;
// };

const person: Person = {
  name: 'alice',
  age: 18,
};

const buffer = borshSerialize(schema, person);

Enum

const schema = BorshSchema.Enum({
  Pending: BorshSchema.Unit,
  Fulfilled: BorshSchema.Unit,
  Rejected: BorshSchema.Unit,
});

type Status = Infer<typeof schema>;

// type Status =
//   | {
//   Pending: Unit;
// }
//   | {
//   Fulfilled: Unit;
// }
//   | {
//   Rejected: Unit;
// };

const status: Status = {
  Pending: {},
};

const buffer = borshSerialize(schema, status);

Enum Associated

const schema = BorshSchema.Enum({
  Square: BorshSchema.u32,
  Rectangle: BorshSchema.Struct({
    length: BorshSchema.u32,
    width: BorshSchema.u32,
  }),
  Circle: BorshSchema.Struct({
    radius: BorshSchema.u32,
  }),
});

type Shape = Infer<typeof schema>;

// type Shape =
//   | {
//   Square: number;
// }
//   | {
//   Rectangle: {
//     length: number;
//     width: number;
//   };
// }
//   | {
//   Circle: {
//     radius: number;
//   };
// };

const shape: Shape = {
  Square: 5,
};

const buffer = borshSerialize(schema, shape);

FAQs

Package last updated on 30 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts