Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@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';
9.7.1 Jul 1, 2022
Changes:
readonly
arrays are used@polkadot/wasm-crypto
6.2.1FAQs
A collection of useful utilities for @polkadot
The npm package @polkadot/util receives a total of 51,889 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.