Socket
Socket
Sign inDemoInstall

@polkadot/types-codec

Package Overview
Dependencies
Maintainers
2
Versions
492
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polkadot/types-codec

Implementation of the SCALE codec


Version published
Weekly downloads
109K
increased by9.22%
Maintainers
2
Weekly downloads
 
Created

What is @polkadot/types-codec?

@polkadot/types-codec is a library that provides a set of utilities for encoding and decoding data types used in the Polkadot ecosystem. It is part of the larger Polkadot JS API suite and is essential for interacting with the Polkadot blockchain, particularly for handling the various custom data types defined in the Polkadot runtime.

What are @polkadot/types-codec's main functionalities?

Encoding and Decoding

This feature allows you to encode and decode data types. In this example, a u8 (unsigned 8-bit integer) is encoded to a byte array and then decoded back to its original value.

const { TypeRegistry, u8, u32 } = require('@polkadot/types-codec');

const registry = new TypeRegistry();
const encoded = new u8(registry, 123).toU8a();
const decoded = new u8(registry, encoded);
console.log(decoded.toNumber()); // 123

Type Creation

This feature allows you to create custom types. In this example, a custom struct type with fields 'age' and 'name' is created and instantiated.

const { TypeRegistry, Struct, u32, Text } = require('@polkadot/types-codec');

const registry = new TypeRegistry();
const MyStruct = Struct.with({
  age: u32,
  name: Text
});
const instance = new MyStruct(registry, { age: 30, name: 'Alice' });
console.log(instance.toJSON()); // { age: 30, name: 'Alice' }

Type Conversion

This feature allows you to convert types to different formats. In this example, a u32 (unsigned 32-bit integer) is converted to its hexadecimal and string representations.

const { TypeRegistry, u32 } = require('@polkadot/types-codec');

const registry = new TypeRegistry();
const value = new u32(registry, 12345);
console.log(value.toHex()); // '0x00003039'
console.log(value.toString()); // '12345'

Other packages similar to @polkadot/types-codec

FAQs

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

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc