2.0.0 • Published 1 year ago

borsh v2.0.0

Weekly downloads
3,687
License
Apache-2.0
Repository
github
Last release
1 year ago

Borsh JS

Project license Project license Discord Travis status NPM version Size on NPM

Borsh JS is an implementation of the Borsh binary serialization format for JavaScript and TypeScript projects.

Borsh stands for Binary Object Representation Serializer for Hashing. It is meant to be used in security-critical projects as it prioritizes consistency, safety, speed, and comes with a strict specification.

Examples

(De)serializing a Value

import * as borsh from 'borsh';

const encodedU16 = borsh.serialize('u16', 2);
const decodedU16 = borsh.deserialize('u16', encodedU16);

const encodedStr = borsh.serialize('string', 'testing');
const decodedStr = borsh.deserialize('string', encodedStr);

(De)serializing an Object

import * as borsh from 'borsh';

const value = {x: 255, y: BigInt(20), z: '123', arr: [1, 2, 3]};
const schema = { struct: { x: 'u8', y: 'u64', 'z': 'string', 'arr': { array: { type: 'u8' }}}};

const encoded = borsh.serialize(schema, value);
const decoded = borsh.deserialize(schema, encoded);

API

The package exposes the following functions:

  • serialize(schema: Schema, obj: any, validate: boolean = true): Uint8Array - serializes an object obj according to the schema schema. Setting validate to false will skip the validation of the schema.
  • deserialize(schema: Schema, buffer: Uint8Array, validate: boolean = true): any - deserializes an object according to the schema schema from the buffer buffer. Setting validate to false will skip the validation of the schema.

Schemas

Schemas are used to describe the structure of the data being serialized or deserialized. They are used to validate the data and to determine the order of the fields in the serialized data.

NOTE: You can find examples of valid in the test folder.

Basic Types

Basic types are described by a string. The following types are supported:

  • u8, u16, u32, u64, u128 - unsigned integers of 8, 16, 32, 64, and 128 bits respectively.
  • i8, i16, i32, i64, i128 - signed integers of 8, 16, 32, 64, and 128 bits respectively.
  • f32, f64 - IEEE 754 floating point numbers of 32 and 64 bits respectively.
  • bool - boolean value.
  • string - UTF-8 string.

Arrays, Options, Maps, Sets, Enums, and Structs

More complex objects are described by a JSON object. The following types are supported:

  • { array: { type: Schema, len?: number } } - an array of objects of the same type. The type of the array elements is described by the type field. If the field len is present, the array is fixed-size and the length of the array is len. Otherwise, the array is dynamic-sized and the length of the array is serialized before the elements.
  • { option: Schema } - an optional object. The type of the object is described by the type field.
  • { map: { key: Schema, value: Schema }} - a map. The type of the keys and values are described by the key and value fields respectively.
  • { set: Schema } - a set. The type of the elements is described by the type field.
  • { enum: [ { struct: { className1: structSchema1 } }, { struct: { className2: structSchema2 } }, ... ] } - an enum. The variants of the enum are described by the className1, className2, etc. fields. The variants are structs.
  • { struct: { field1: Schema1, field2: Schema2, ... } } - a struct. The fields of the struct are described by the field1, field2, etc. fields.

Type Mappings

JavascriptBorsh
numberu8 u16 u32 i8 i16 i32
bigintu64 u128 i64 i128
numberf32 f64
numberf32 f64
booleanbool
stringUTF-8 string
type[]fixed-size byte array
type[]dynamic sized array
objectenum
MapHashMap
SetHashSet
null or typeOption

Contributing

Install dependencies:

yarn install

Continuously build with:

yarn dev

Run tests:

yarn test

Run linter

yarn lint

Publish

Prepare dist version by running:

yarn build

When publishing to npm use np.

License

This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). See LICENSE-MIT and LICENSE-APACHE for details.

@debuggor/solana-web3.js@dvstllc/ardata-client@safecoins/web3@phantasia/metaplex-js@solcial/clientref-ui@solcial/vesting@infinitebrahmanuniverse/nolb-borcps_api_client_js@everything-registry/sub-chunk-1255@keval6706/commonaver-tstanya-api-jstest-crema-farmingtest-orderbook-sdkweb3-api-jsweb3-wallet-selectorxiabing-near-api-jsshowgallerysavourlabs-wallet-sdkrerumtemporarich-client-jstedddfgdsst-uistarmap-apivortex-account-client-jsvoluptatemeos@asgardfi/marginfi-client-v2-patchmetapass-sdkml-registry-solanaloading-animatedgolanaiureestmake-agentmeer-api-jsmoney-streamingmpl-commonmorgan-testingksy-metaplex-js-onegrape-art-listing-requestkanaswap-name-servicekanon-auctionhouse-sdkkarneges-sbtkasactrlnear-willem-workspacesnear-workspacesnearcustomnear-api-esmnear-api-external-link-forknear-api-jsnear-api-js-clonenear-api-js-dannynear-api-js-skynear-runnernear-runner-testnecessitatibusveniammultisig-wallet-sdkmultisig-wallet-sdk-v2manxifest-sdk-abmaximeeosmagical_nftspariaturaliquamoyster-commonoyster-common-sotatekproxied-near-api-jsofficiisomnisprism-phoenix-sdknesciuntmagnamnihilidpassport-local-nearpchain-types-jsphoenix-sdkphoenix-orderbook-sdknotspl-token-collectivenotspl-utilsnotstratanotstratareactqtw-wallet-adapterquantum-sdkquaeratabraindrops-clirainbow-bridgequasdeseruntquidemearumspl-stake-poolstrata-foundation-react-2strata-foundation-react-xnftstrata-foundation-spl-token-collective-2strata-foundation-spl-utils-2sitnumquamsolana-bridge-sdksolana-clientsolana-token-registrysolana-nft-metadatasolana-nft-toolsolana-programsolana-helpersolana_magical_mint_nftsolana-txs-sizesolone-sdksolardex-orderbook
1.0.0

1 year ago

2.0.0

1 year ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.2.6

5 years ago

0.1.1

5 years ago