Misc Utils
The MiscUtils
class provides a collection of utility functions that are useful for various operations such as hashing, padding hex strings, reading binary data, and converting floats to the XRP Ledger's fixed-point representation.
Generating a Hash
The generateHash
function creates a SHA-512 hash of the input data and returns the first 32 bytes as a hex string.
import { createHash } from 'crypto'
export function generateHash(dataBytes: Buffer) {
const hash = createHash('sha512').update(dataBytes).digest()
return hash.slice(0, 32).toString('hex').toUpperCase()
}
Padding a Hex String
The padHexString
function pads a hex string with leading zeros to reach a target length.
export function padHexString(input: string, targetLength = 64): string {
const paddedString = '0'.repeat(targetLength - input.length) + input
return paddedString
}
Reading Hook Binary Hex from Namespace
The readHookBinaryHexFromNS
function reads a WebAssembly binary file from the specified namespace and returns its content as an uppercase hex string.
import fs from 'fs'
import path from 'path'
export function readHookBinaryHexFromNS(filename: string): string {
const buildPath = process.cwd() + '/' + 'build'
const wasm = fs.readFileSync(
path.resolve(__dirname, `${buildPath}/${filename}.wasm`)
)
return wasm.toString(`hex`).toUpperCase()
}
Generating a Namespace Hex
The hexNamespace
function generates a SHA-256 hash of the provided namespace seed and returns it as an uppercase hex string.
import { SHA256 } from 'crypto-js'
export function hexNamespace(hookNamespaceSeed: string): string {
return SHA256(hookNamespaceSeed).toString().toUpperCase()
}
Converting Float to LE XFL
The floatToLEXfl
function converts a floating-point number represented as a string to the XRP Ledger's fixed-point representation, known as little endian XFL.
export function floatToLEXfl(fl: string): string {
const xfl = floatToXfl(fl)
return flipBeLe(xfl as bigint)
}
These utility functions are essential for developers working with the XRP Ledger, as they facilitate the manipulation and transformation of data in a format that is compatible with XRPL standards.