Skip to content

A lightweight JavaScript library to validate Solidity data types like int, uint, bytes, and Ethereum-specific types such as addresses and transaction hashes.

License

Notifications You must be signed in to change notification settings

zororaka00/sol-type-check

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sol-type-check

A lightweight JavaScript library to validate Solidity data types like int, uint, bytes, and Ethereum-specific types such as addresses and transaction hashes.

Features

  • Validate signed and unsigned integers (int, uint) with customizable bit sizes.
  • Validate Solidity bytes types (1, 2, 4, 8, 16, 32).
  • Validate Ethereum addresses and transaction hashes.
  • Provides simple and easy-to-use functions.

Installation

To install the library, run the following command:

npm install sol-type-check

Usage

Importing the Library

You can import the library in your project like this:

const { isInt, isUint, isBytes, isAddress, isHash } = require('sol-type-check');

Example Functions

isInt(bits, value)

Checks if a value is a signed integer within the range for a given bit size.

isInt(32, 123); // true
isInt(8, -130); // false

isUint(bits, value)

Checks if a value is an unsigned integer within the range for a given bit size.

isUint(32, 4294967295); // true
isUint(8, -1); // false

isBytes(bytes, value)

Checks if a value is a valid bytes array of the specified size.

isBytes(4, "0xabcdef"); // true
isBytes(2, "0x12345"); // false

isAddress(value)

Checks if a value is a valid Ethereum address.

isAddress("0x32Be343B94f860124dC4fEe278FDCBD38C102D88"); // true
isAddress("0xInvalidAddress"); // false

isHash(value)

Checks if a value is a valid Ethereum transaction hash.

isHash("0x5c504ed7d413568f857eeb70f9f70e0d8b6b7469c9a7f325d9776de08e850635"); // true
isHash("0xInvalidHash"); // false

API

  • isInt(bits, value)
    Validates a signed integer for the specified bit size.

    • bits: An integer (8, 16, 32, 64, 128, or 256) representing the bit size.
    • value: A number to check if it is within the valid range.
  • isUint(bits, value)
    Validates an unsigned integer for the specified bit size.

    • bits: An integer (8, 16, 32, 64, 128, or 256) representing the bit size.
    • value: A number to check if it is within the valid range.
  • isBytes(bytes, value)
    Validates a bytes array of a specified size.

    • bytes: An integer (1, 2, 4, 8, 16, or 32) representing the expected byte size.
    • value: A string representing the bytes in hexadecimal format (prefixed with 0x).
  • isAddress(value)
    Validates an Ethereum address.

    • value: A string representing the Ethereum address (must start with 0x and be 40 hexadecimal characters).
  • isHash(value)
    Validates an Ethereum transaction hash.

    • value: A string representing the Ethereum transaction hash (must start with 0x and be 64 hexadecimal characters).

Contributing

If you would like to contribute to this project, feel free to fork the repository and create a pull request. We welcome bug reports, suggestions, and improvements.

🛠 Support

If you encounter any issues or have questions, please open an issue in this repository or contact us at rakawidhiantoro@gmail.com.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A lightweight JavaScript library to validate Solidity data types like int, uint, bytes, and Ethereum-specific types such as addresses and transaction hashes.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published