You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

borsher

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

borsher

Borsh (de)serialization


Version published
Weekly downloads
1.1K
increased by9.75%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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);

FAQs

Package last updated on 31 Mar 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc