util

Utility functions for CESS SDK

Installation

npm install @cessnetwork/util

Overview

The @cessnetwork/util package provides a comprehensive set of utility functions for working with the CESS (Cumulus Encrypted Storage System) network. This package includes utilities for:

  • Account management and address validation

  • Cryptographic operations and signing

  • File hashing and integrity verification

  • Network operations

  • Data formatting and conversion

  • Error handling

Core Modules

Account Utilities

Functions for managing CESS accounts, validating addresses, and converting between different formats.

Signing Utilities

Functions for signing messages and verifying signatures using mnemonics and cryptographic keys.

Hash Utilities

Functions for calculating file and data hashes.

API Reference

Account Functions

  • parsePublicKey(address: string): Uint8Array - Parse public key from address

  • encodePublicKeyAsTestNetAddr(publicKey: Uint8Array): string - Encode public key as testnet address (ss58Format: 11330)

  • encodePublicKeyAsMainNetAddr(publicKey: Uint8Array): string - Encode public key as mainnet address (ss58Format: 11331)

  • validateAddress(address: string, ss58Format?: number): boolean - Validate address with optional ss58Format check

  • convertAddressFormat(address: string, targetSs58Format: number): string - Convert address to specified ss58Format

  • convertAddrToMainNet(address: string): string - Convert address from ss58Format 11330 to 11331 (cX... -> ce...)

  • convertAddrToTestNet(address: string): string - Convert address from ss58Format 11331 to 11330 (ce... -> cX...)

  • evmAddrToCessAddr(evmAddr: string, targetSs58Format: number): string - Convert Ethereum address to CESS address with specified ss58Format

Signing Functions

  • safeSignUnixTime(unixTime: string | number, mnemonic: string): Uint8Array - Sign a current unix time with a mnemonic using SR25519

  • getWrappedMsg(unixTime: string | number): Uint8Array - Wrap a unix time message with tags

  • sr25519SignWithMnemonic(mnemonic: string, msg: string): Uint8Array - Sign a message with a mnemonic using SR25519

  • verifySignature(msg: string | Uint8Array, sign: Uint8Array, account: string): boolean - Verify a signature with a public key

  • parseEvmAccFromEvmSign(message: string, sign: string): string - Parse Ethereum account from Ethereum signature

Hash Functions

  • calcSHA256(data: Buffer): string - Calculate SHA256 hash of buffer data

  • calcPathSHA256(filePath: string): Promise<string> - Calculate SHA256 hash of a file

  • calcFileSHA256(stream: NodeJS.ReadableStream): Promise<string> - Calculate SHA256 hash of a stream

  • calcMD5(data: string): string - Calculate MD5 hash of string data

  • calcPathSHA256Bytes(filePath: string): Promise<Buffer> - Calculate SHA256 hash as Buffer

  • calcFileSHA256Bytes(stream: NodeJS.ReadableStream): Promise<Buffer> - Calculate SHA256 hash of stream as Buffer

File Functions

  • getFileSize(filePath: string): number - Get the size of a file

  • readChunk(filePath: string, start: number, end: number): Promise<Buffer> - Read a chunk of a file

  • ensureDir(dirPath: string): void - Ensure directory exists

Network Functions

  • checkConnectivity(host: string, port: number): Promise<boolean> - Check network connectivity

  • pingHost(host: string): Promise<PingResponse> - Ping a host

String Functions

  • toBase64(data: string): string - Convert string to base64

  • fromBase64(data: string): string - Convert base64 to string

  • toHex(data: string): string - Convert string to hex

  • fromHex(data: string): string - Convert hex to string

Time Functions

  • formatTimestamp(timestamp: number): string - Format timestamp to readable string

  • getCurrentTimestamp(): number - Get current timestamp

Gateway Utilities

The utility functions play a critical role in gateway operations, particularly for authentication and communication with the CESS storage network.

Gateway Authentication Functions

When interacting with CESS gateways, you need specific utilities to sign messages and authenticate:

Last updated

Was this helpful?