* Start codec implementation * Expose all constants * NodeHeader encoding/decoding * Encoding in NodeHeader * Constants for Node types * Moce codec to trie-codec * Encode/decode in place (untested) * WIP, tests starting to be put in place * One passing test :) * Another test bites the dust... * One more test down... * One more down * Additional tests * Formatting * Long keys passing * Trie tests * Follow the script, no all-in-all encoder * First simple trie test passing * Another one down * Extra checks remove (debug) * 80% there with tests... * One more, one left * Trie tests passing * ISC -> Apache-2.0 * Add compact util functions * Tests to TypeScript * Remove @polkadot/types deps, ethereum stub * Bump * Genesis tests with chainspecs * Cleanup tests (ease of debugging) * Substrate test passes, branch fails (again) * Checks passing, tests passing * Re-add Ethereum & snapshot tests (not passing atm) * Re-add index test (also not 100%) * Mid-way in generic trie-root * Cleanup ethereumjs-testing definitions * Update deps * Less intrusive renames in trie-hash * Self-review cleanups #1 * asNibbles -> toNibbles * Cleanups * Expand tests * Cleanup imports * Fix logger test (fly-by refactor) * Update snapshot test
@polkadot/util-crypto
Various useful cyrpto 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.
Usage
Installation -
yarn add @polkadot/util-crypto
Functions can be imported as follows:
import { mnemonicGenerate } from '@polkadot/util-crypto';
Documentation and Available Utilities
Below is a list of currently exposed methods published at the Polkadot-JS Common Documentation Portal.
- util-crypto
- Functions
- Blake2b
- Keccak
- Mnemonic
- Nacl
- Random
- SHA-512
- xxHash
- Functions
Blake2b
- Creates a blake2b u8a output containing a hash from a u8a input bytes using given bit-length or default bit-length of 256. An optional key u8a of up to 64 bytes may be provided
- Creates a blake2b hex string with optionally specified bit-length from the u8a input or default bit-length used is 256
- Uses the Blake2b hash function of blakejs library
- Note: All hashes used in Polkadot-JS are 32 bytes (i.e. 256 / 8)
- Note: BLAKE hash functions are in the Nacl library
- Note: Only handles up to 2**53 bytes of input
Keccak
- Creates a Keccak-256 hex string from the input (such as for conversion from seed phrase)
- Creates a Keccak-256 Uint8Array from the input
- Uses the Keccak-256 hash function from the js-sha3 library
Mnemonic
- Generate valid mnemonics (for account seed phrase) using BIP39
- Converts mnemonics to a valid seed secret with Uint8Array output
Nacl
- Implements NaCl secret-key authenticated encryption, public-key authenticated encryption, hashing, and public-key signatures
- Decrypts a message that has been encrypted with a secret key using Nacl
- Encrypts a message with a secret key and generates an associated nonce using Nacl
- Signing a message with a secret key to generate a valid signature using Nacl
- Verifies a signed message by providing the message, signature, and public key (associated with the secret key that signed it) using Nacl
Random
- Returns a sequence of secure random bytes in a variety of formats
- Creates a u8a with a specified (optional) bit-length (default 32) filled with random bytes
- Creates a hex string with a specified (optional) bit-length (default 32) filled with random bytes
- Creates a random number from random secure bytes
SHA-512
- Implements SHA-512 hashing functions for a variety of input and outputs
- Creates a SHA-512 hash of u8a input
- Uses the SHA-512 hash function of tweetnacl library
xxHash
- Create xxhash64 values with specified bitlengths
- Creates a xxhash64 u8a from the input with a specified (optional) bit-length (default 64)
- Creates a xxhash64 hex string from the input with a specified (optional) bit-length (default 64)
- Uses xxHash to generate values as BN, hex & number output