New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@solana/buffer-layout

Package Overview
Dependencies
Maintainers
13
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solana/buffer-layout

Translation between JavaScript values and Buffers

4.0.1
latest
Source
npm
Version published
Weekly downloads
479K
-28.09%
Maintainers
13
Weekly downloads
 
Created

What is @solana/buffer-layout?

@solana/buffer-layout is a library for defining and working with binary data layouts in JavaScript. It is particularly useful for serializing and deserializing data structures to and from binary formats, which is a common requirement when working with blockchain data and other low-level protocols.

What are @solana/buffer-layout's main functionalities?

Defining a Layout

This feature allows you to define a binary layout for a data structure. In this example, a structure with an 8-bit unsigned integer and a 32-bit unsigned integer is defined and encoded into a buffer.

const { struct, u8, u32 } = require('@solana/buffer-layout');

const MyStruct = struct([
  u8('field1'),
  u32('field2')
]);

const buffer = Buffer.alloc(MyStruct.span);
MyStruct.encode({ field1: 1, field2: 42 }, buffer);
console.log(buffer);

Decoding a Layout

This feature allows you to decode a binary buffer into a JavaScript object based on the defined layout. In this example, a buffer is decoded into an object with the fields 'field1' and 'field2'.

const { struct, u8, u32 } = require('@solana/buffer-layout');

const MyStruct = struct([
  u8('field1'),
  u32('field2')
]);

const buffer = Buffer.from([1, 0, 0, 0, 42]);
const decoded = MyStruct.decode(buffer);
console.log(decoded);

Nested Layouts

This feature allows you to define nested layouts, where one layout contains another. In this example, an outer structure contains a sequence of inner structures.

const { struct, u8, u32, seq } = require('@solana/buffer-layout');

const InnerStruct = struct([
  u8('innerField1'),
  u32('innerField2')
]);

const OuterStruct = struct([
  u8('outerField1'),
  seq(InnerStruct, 2, 'innerStructs')
]);

const buffer = Buffer.alloc(OuterStruct.span);
OuterStruct.encode({ outerField1: 1, innerStructs: [{ innerField1: 2, innerField2: 3 }, { innerField1: 4, innerField2: 5 }] }, buffer);
console.log(buffer);

Other packages similar to @solana/buffer-layout

Keywords

Buffer

FAQs

Package last updated on 09 Dec 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