Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mysten/bcs

Package Overview
Dependencies
Maintainers
2
Versions
520
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mysten/bcs

BCS - Canonical Binary Serialization implementation for JavaScript

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
77K
decreased by-4.6%
Maintainers
2
Weekly downloads
 
Created
Source

Move BCS

This library implements Binary Canonical Serialization (BCS) in JavaScript, making BCS available in both Browser and NodeJS environments.

Feature set

  • Move's primitive types de/serialization: u8, u64, u128, bool
  • Ability to define custom types such as vector or struct
  • Extendable and allows registering any custom types (e.g. vectors of structs)
  • Custom addresses length. Example: BCS.registerAddressType('Address', 20, 'hex') - 20 bytes;

Examples

Working with primitive types

To deserialize data, use a BCS.de(type: string, data: string). Type parameter is a name of the type; data is a BCS encoded as hex.

import { BCS } from '@mysten/bcs';

// BCS has a set of built ins:
// U8, U32, U64, U128, BOOL, STRING
console.assert(BCS.U64 === 'u64');
console.assert(BCS.BOOL === 'bool');
console.assert(BCS.STRING === 'string');

const hex = BCS.util.fromHex;

// De/serialization of primitives is included by default;
let u8 = BCS.de(BCS.U8, hex('00')); // '0'
let u32 = BCS.de(BCS.U32, hex('78563412')); // '78563412'
let u64 = BCS.de(BCS.U64, hex('ffffffffffffffff')); // '18446744073709551615'
let u128 = BCS.de(BCS.U128, hex('FFFFFFFF000000000000000000000000')); // '4294967295'
let bool = BCS.de(BCS.BOOL, hex('00')); // false

// There's also a handy built-in for ASCII strings (which are `vector<u8>` under the hood)
let str = BCS.de(BCS.STRING, hex('0a68656c6c6f5f6d6f7665')); // hello_move

// Address support TBD once the best API is figured out;
// let addr = BCS.de(Move.ADDRESS, '0a68656c6c6f5f6d6f7665'); // 0a68656c6c6f5f6d6f7665

To serialize any type, use BCS.ser(type: string, data: any). Type parameter is a name of the type to serialize, data is any data, depending on the type (can be object for structs or string for big integers - such as u128).

import { BCS } from '@mysten/bcs';

let bcs_u8 = BCS.ser('u8', 255).toBytes(); // uint Array

console.assert(BCS.util.toHex(bcs_u8) === 'ff');

let bcs_ascii = BCS.ser('string', 'hello_move').toBytes();

console.assert(BCS.util.toHex(bcs_ascii) === '0a68656c6c6f5f6d6f7665');

Adding custom types

import { BCS } from '@mysten/bcs';

// Move / Rust struct
// struct Coin {
//   value: u64,
//   owner: vector<u8>, // name // Vec<u8> in Rust
//   is_locked: bool,
// }

BCS.registerStructType('Coin', {
    value: BCS.U64,
    owner: BCS.STRING,
    is_locked: BCS.BOOL
});



// Created in Rust with diem/bcs
let rust_bcs_str = '80d1b105600000000e4269672057616c6c65742047757900';

console.log(BCS.de('Coin', BCS.util.fromHex(rust_bcs_str)));

// Let's encode the value as well
let test_ser = BCS.ser('Coin', {
    owner: 'Big Wallet Guy',
    value: '412412400000',
    is_locked: false
});

console.log(test_ser.toBytes());
console.assert(BCS.util.toHex(test_ser.toBytes()) === rust_bcs_str, 'Whoopsie, result mismatch');

Keywords

FAQs

Package last updated on 01 Jul 2022

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

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc