gsigner 2.0.0

Universal cryptographic signer supporting secp256k1 (Ethereum), ed25519, and sr25519 (Substrate)
Documentation
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

//! Small utility helpers used across the crate.

use alloc::vec::Vec;
use hex::FromHexError;

/// Decode a hex string (with optional `0x` prefix) into bytes.
pub fn decode_hex(s: &str) -> Result<Vec<u8>, FromHexError> {
    let stripped = s.strip_prefix("0x").unwrap_or(s);
    hex::decode(stripped)
}

/// Decode a hex string (with optional `0x` prefix) into a fixed-size array.
pub fn decode_hex_to_array<const N: usize>(s: &str) -> Result<[u8; N], FromHexError> {
    let stripped = s.strip_prefix("0x").unwrap_or(s);
    if stripped.len() != N * 2 {
        return Err(FromHexError::InvalidStringLength);
    }

    let mut buf = [0u8; N];
    hex::decode_to_slice(stripped, &mut buf)?;
    Ok(buf)
}