Socket
Socket
Sign inDemoInstall

@polkadot/util

Package Overview
Dependencies
Maintainers
2
Versions
1408
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polkadot/util

A collection of useful utilities for @polkadot


Version published
Weekly downloads
154K
increased by9.35%
Maintainers
2
Weekly downloads
 
Created

What is @polkadot/util?

@polkadot/util is a utility library for JavaScript and TypeScript that provides a variety of helper functions for working with Polkadot and Substrate-based blockchain data. It includes functions for encoding/decoding, type conversion, and other common operations needed when interacting with blockchain data.

What are @polkadot/util's main functionalities?

Hexadecimal Conversion

This feature allows you to convert hexadecimal strings to Uint8Array and vice versa. It is useful for handling raw data in blockchain transactions.

const { hexToU8a, u8aToHex } = require('@polkadot/util');

const hex = '0x12345678';
const u8a = hexToU8a(hex);
console.log(u8a); // Uint8Array(4) [ 18, 52, 86, 120 ]

const backToHex = u8aToHex(u8a);
console.log(backToHex); // '0x12345678'

String Conversion

This feature allows you to convert strings to Uint8Array and vice versa. It is useful for encoding and decoding text data.

const { stringToU8a, u8aToString } = require('@polkadot/util');

const str = 'Hello, Polkadot!';
const u8a = stringToU8a(str);
console.log(u8a); // Uint8Array(15) [ 72, 101, 108, 108, 111, 44, 32, 80, 111, 108, 107, 97, 100, 111, 116 ]

const backToString = u8aToString(u8a);
console.log(backToString); // 'Hello, Polkadot!'

BN.js Integration

This feature provides integration with the BN.js library for handling large numbers. It is useful for performing arithmetic operations on large integers, which are common in blockchain applications.

const { BN } = require('@polkadot/util');

const num = new BN('12345678901234567890');
console.log(num.toString()); // '12345678901234567890'

const numHex = num.toString(16);
console.log(numHex); // 'ab54a98ceb1f0ad2'

Type Checking

This feature provides functions for checking the types of various data structures. It is useful for validating input data before processing it.

const { isHex, isU8a } = require('@polkadot/util');

console.log(isHex('0x12345678')); // true
console.log(isHex('12345678')); // false
console.log(isU8a(new Uint8Array([1, 2, 3, 4]))); // true
console.log(isU8a([1, 2, 3, 4])); // false

Other packages similar to @polkadot/util

FAQs

Package last updated on 18 Nov 2023

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