Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
@polkadot/util
Advanced tools
@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.
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
bn.js is a library for arbitrary-precision arithmetic. It is used for handling large integers, similar to the BN.js integration in @polkadot/util. However, bn.js focuses solely on arithmetic operations and does not provide the additional utility functions found in @polkadot/util.
The buffer package provides a way to handle binary data in Node.js. It offers similar functionality to the Uint8Array conversions in @polkadot/util, but it is more general-purpose and not specifically tailored for blockchain data.
ethers is a library for interacting with the Ethereum blockchain. It includes utility functions for encoding/decoding data, similar to @polkadot/util, but it is specifically designed for Ethereum rather than Polkadot/Substrate.
Various useful utility functions that are used across all projects in the @polkadot namespace. It provides utility functions with additional safety checks, allowing not only for consistent coding, but also reducing the general boilerplate.
Installation -
yarn add @polkadot/util
Functions can be imported directly from the package, e.g.
import { isHex } from '@polkadot/util';
FAQs
A collection of useful utilities for @polkadot
The npm package @polkadot/util receives a total of 84,716 weekly downloads. As such, @polkadot/util popularity was classified as popular.
We found that @polkadot/util demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.