npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

encoding-helper

v1.2.15

Published

Utility libraries for Solidity smart contract

Downloads

170

Readme

Encoding & Hashing Helper

Utility libraries for Solidity smart contract


I. Usage

1. Installation

yarn add -D encoding-helper

2. Import the libraries in your script

const { encodingHelper, hashingHelper, txUtils } = require('encoding-helper');

II. Function list

A. EncodingHelper

1. toBytes10Name(name: string): string

Convert a string with maximum 10 characters into a 10-bytes hex string. If the input string length is less than 10 characters, the result will be padded with trailing zeros, while with input string greater than 10 characters, the excess will be truncated before encoding.

  • Example:
encodingHelper.toBytes10Name("Lorem")
==> '0x4c6f72656d0000000000'

encodingHelper.toBytes10Name("LoremIpsumDolorSit")
==> '0x4c6f72656d497073756d'

2. first10(address: string): string

Get the first 10-byte string from an address string.

  • Example:
encodingHelper.first10('0xECEfEf2074f56ced5A5e68De3Aff44ed5798B35E')
==> '0xecefef2074f56ced5a5e'

3. getId(name: string, index: uint16, token: string, product: string): BigNumber

Generate a BigNumber id from a name, an index, a token address, and a product contract address.

  • Example:
encodingHelper.getId('Lorem', 65535, '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972','0x6846ceF88276D453e38B92F4BFe70D29643571B8')

==> BigNumber {
        _hex: '0x4c6f72656d0000000000ffffdef0e171272f6f9061636846cef88276d453e38b',
        _isBigNumber: true
    }
==> '34572686050035833475315598151429145236062344499814420574865813453149958300555'

4. last10(id: BigNumber): string

Get the last 10-byte string from an id. Usually used for validating the product contract address.

  • Example:
let id = encodingHelper.getId('Lorem', 65535,'0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972','0x6846ceF88276D453e38B92F4BFe70D29643571B8');

encodingHelper.last10(id);
==> '0x6846cef88276d453e38b'

5. decodeId(id: BigNumber | string): string[]

Recover the information used to generate the id.

  • Example:
let id = encodingHelper.getId('Lorem', 65535,'0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972','0x6846ceF88276D453e38B92F4BFe70D29643571B8');
==> BigNumber {
        _hex: '0x4c6f72656d0000000000ffffdef0e171272f6f9061636846cef88276d453e38b',
        _isBigNumber: true
    }
==> '34572686050035833475315598151429145236062344499814420574865813453149958300555'

encodingHelper.decodeId('0x4c6f72656d0000000000ffffdef0e171272f6f9061636846cef88276d453e38b');
==> [
        'Lorem', 65535,
        '0xdef0e171272f6f906163',
        '0x6846cef88276d453e38b'
    ]

encodingHelper.decodeId('34572686050035833475315598151429145236062344499814420574865813453149958300555');
==> [
        'Lorem', 65535,
        '0xdef0e171272f6f906163',
        '0x6846cef88276d453e38b'
    ]

encodingHelper.decodeId(id);
==> [
        'Lorem', 65535,
        '0xdef0e171272f6f906163',
        '0x6846cef88276d453e38b'
    ]

6. getProductId(name: string, token: string, product: string): string

Get the productId generated by combining name, token address, and product address. If the name's length is lesser or equal to 10, it is converted to a bytes10 value. If name is longer than 10 characters, it is hashed using keccak256 function and take the first 10 bytes.

  • Example
// Name <= 10 characters
encodingHelper.getProductId('LoremIpsum', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> '0x4c6f72656d497073756d0000def0e171272f6f9061636846cef88276d453e38b'

// Name > 10 characters
encodingHelper.getProductId('LoremIpsumDolorsitAmet', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> '0x872b837047e81dd64a950000def0e171272f6f9061636846cef88276d453e38b'

7. getNameHashFromId(hexId: string): string

Return the encoded name (first 10 bytes) from hexId

  • Example
encodingHelper.getNameHashFromId('0x872b837047e81dd64a950000def0e171272f6f9061636846cef88276d453e38b')
==> '0x872b837047e81dd64a95'

8. verifyProductId(id: string, name: string, token: string, product: string): boolean

Return true if the id encoded from 6. can be decoded and matches the value of name, token, and product

  • Example:
// Name <= 10 characters
encodingHelper.verifyProductId('0x4c6f72656d497073756d0000def0e171272f6f9061636846cef88276d453e38b', 'LoremIpsum', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> true

// Name > 10 characters
encodingHelper.verifyProductId('0x872b837047e81dd64a950000def0e171272f6f9061636846cef88276d453e38b', 'LoremIpsumDolorsitAmet', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> true

// Name > 10 characters and does not match
encodingHelper.verifyProductId('0x872b837047e81dd64a950000def0e171272f6f9061636846cef88276d453e38b', 'LoremIpsumDolorsit', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> false

// Name, token, and product don't match
encodingHelper.verifyProductId('0x872b837047e81dd64a950000def0e171272f6f9061636846cef88276d453e38b', 'LoremIpsumDolorsit', '0x6846ceF88276D453e38B92F4BFe70D29643571B8', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972')
==> false

9. keccak256(input: string): string

Returns the keccak256 hash of the input string. This function is a shortcut to ethers.utils.keccak256(BytesLike) from ethers.js library.

  • Example:
encodingHelper.keccak256("Lorem");
==> '0x53e60f9a9472b58c06a7620335d85ec3c20c4f93c0ac735d7a6622f2ef5b67c6'

10. pack(types: string[], values: any[]): string

This is a short cut to ethers.utils.solidityPack(string[], any[]).

  • Example:
hashingHelper.pack(["address", "string"], ['0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', "LoremIpsum"]);
==> '0xdef0e171272f6f906163a2ab4cd82a3ff85a39724c6f72656d497073756d'

11. stringToBytes10orHash(input: string): string

Convert the UTF-8 input string into a 10-bytes hex string. If the input string is lesser than or equal to 10 characters in length, the output is the direct hex-encoding of the input. If the input has more than 10 characters, it is hashed using keccak256 and take the first 10 bytes.

  • Example
// Input <= 10 characters
encodingHelper.stringToBytes10orHash('LoremIpsum')
==> '0x4c6f72656d497073756d'

// Input > 10 characters
encodingHelper.stringToBytes10orHash('LoremIpsumDolorsitAmet')
==> '0x872b837047e81dd64a95'

12. stringToBytes(input: string, length: uint): string

Convert the UTF-8 input string into a hex string representation of a bytes value with specified length. The length must not exceed input's length.

  • Example:
encodingHelper.stringToBytes('Lorem Ipsum dolor sit amet. Consectetur adispicing elit', 10);
==> '0x4c6f72656d2049707375'

13. hexToString(hexString: string): string

Convert a hexString input into UTF-8 string value and remove any null characters (\x00) from the result.

  • Example:
encodingHelper.hexToString('0x4c6f72656d2049707375');
==> 'Lorem Ipsu'

encodingHelper.hexToString('0x4c6f72656d0000000000');
==> 'Lorem'

14. encodeCryptoPayload(name: string, index: uint16, token: address, product: address, autoswap: bool | false): string

Encode a payload data that consists of name, index, token, product, autoswap into a hex string for Crypto currency deposit.

  • name: product name
  • index: account index
  • token: target token address
  • product: target product address
  • autoswap: enable/disable autoswap
encodeCryptoPayload('LOREM', 10, '0x1CBDD079370932F90A19902bA12D20c5D3716833', '0x625384874d83fccfFe55778F72B7C7f4dAb1CdBd', true)
==> '0x4c4f52454d0000000000000a1cbdd079370932f90a19625384874d83fccffe550000000000000000000000001cbdd079370932f90a19902ba12d20c5d3716833000000000000000000000000625384874d83fccffe55778f72b7c7f4dab1cdbd0000000000000000000000000000000000000000000000000000000000000001'

15. decodeCryptoPayload(payloadHex: string): : (string | (string | number)[])[]

Decode a payload hex string to its original values.

decodeCryptoPayload('0x4c4f52454d0000000000000a1cbdd079370932f90a19625384874d83fccffe550000000000000000000000001cbdd079370932f90a19902ba12d20c5d3716833000000000000000000000000625384874d83fccffe55778f72b7c7f4dab1cdbd0000000000000000000000000000000000000000000000000000000000000001')
==> [
        [ 'LOREM', 10, '0x1cbdd079370932f90a19', '0x625384874d83fccffe55' ],
        '0x4c4f52454d0000000000000a1cbdd079370932f90a19625384874d83fccffe55',
        '0x1CBDD079370932F90A19902bA12D20c5D3716833',
        '0x625384874d83fccfFe55778F72B7C7f4dAb1CdBd',
        true
    ]

16. encodeBankPayload(name: string, index: uint16, token: address, product: address, account: address): string

Encode a payload data that consists of name, index, token, product, account into a hex string

  • name: product name

  • index: account index

  • token: target token address

  • product: target product address

  • account: recipient

  • Example:

encodingHelper.encodeBankPayload('Lorem', 65535,'0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972','0x6846ceF88276D453e38B92F4BFe70D29643571B8', '0xB34C74d889177ec905FEB41791936BCce1d7A5df')
==> '0x4c6f72656d0000000000ffffdef0e171272f6f9061636846cef88276d453e38b000000000000000000000000def0e171272f6f906163a2ab4cd82a3ff85a39720000000000000000000000006846cef88276d453e38b92f4bfe70d29643571b8000000000000000000000000b34c74d889177ec905feb41791936bcce1d7a5df'

17. decodeBankPayload(payloadHex: string): (string | (string | number)[])[]

Decode a payload hex string to its original values.

  • Example:
let encodedPayload = encodingHelper.encodePayload('Lorem', 0, '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8', '0xB34C74d889177ec905FEB41791936BCce1d7A5df')
==> '0x4c6f72656d00000000000000def0e171272f6f9061636846cef88276d453e38b000000000000000000000000def0e171272f6f906163a2ab4cd82a3ff85a39720000000000000000000000006846cef88276d453e38b92f4bfe70d29643571b8000000000000000000000000b34c74d889177ec905feb41791936bcce1d7a5df'

encodingHelper.decodePayload(encodedPayload)
==> [
        [ 'Lorem', 0, '0xdef0e171272f6f906163', '0x6846cef88276d453e38b' ],
        '0x4c6f72656d00000000000000def0e171272f6f9061636846cef88276d453e38b',
        '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972',
        '0x6846ceF88276D453e38B92F4BFe70D29643571B8',
        '0xB34C74d889177ec905FEB41791936BCce1d7A5df'
    ]

B. HashingHelper

1. toRoleHash(address: string, name: string): string

Calculate a Hash based on current account/contract address and a name string.

  • Example:
hashingHelper.toRoleHash('0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', 'DEFAULT_SETTER_ROLE');
==> '0x6346e2e51a432d68c7d12ad11ff4bdf844f7e26e092c0d9787d0397d116098e1'

3. toBytes32Key(keyString: string): string

Convert keyString string into a 32-bytes Hex string.

  • Example:
hashingHelper.toBytes32Key('Lorem Ipsum dolor sit amet. Cons');
==> '0x4c6f72656d20497073756d20646f6c6f722073697420616d65742e20436f6e73'

If the input string is longer than 32 bytes, only the first 32 bytes will be used to create the bytes32 value.

hashingHelper.toBytes32Key('Lorem Ipsum dolor sit amet. Consectetur adispicing elit')
==> '0x4c6f72656d20497073756d20646f6c6f722073697420616d65742e20436f6e73'

4. toBytes32Value(nameString: string, index: uint16, address: string): string

Pack the first 10 bytes of the nameString, the uint16 representation of index, and the address string into a bytes32 value.

  • Example:
hashingHelper.toBytes32Value('LoremIpsum', 65535, '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972');
==> '0x4c6f72656d497073756dffffdef0e171272f6f906163a2ab4cd82a3ff85a3972'

5. recoverOriginalValues(bytes32HexValue: hexString): (string | numbere)[]

Recover the data that was packed in the previous function toBytes32Value

  • Example:
hashingHelper.recoverOriginalValues('0x4c6f72656d497073756dffffdef0e171272f6f906163a2ab4cd82a3ff85a3972');
==> [ 
        'LoremIpsum', 
        65535, 
        '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972' 
    ]

C. TxUtils

1. getRevertReason(provider: Provider | string, txnHash: string): string

Get the revert reason from a failed transaction by the provided txnHash. Note: provider can be a Provider object or an RPC url string

  • Example:
await txUtils.getRevertReason(provider, '0x7e9e75da5aed836d34d961d61d66b3108115c395cff03aa058fd794e800ba68d')
==> Failed to submit transaction: cannot estimate transaction: AccessControl: account 0xdec0da618b4fc8ff14028a5e27ca7e6a6d6b68d7 is missing role 0x0000000000000000000000000000000000000000000000000000000000000000.

2. getImplementationAddress(provider: Provider, proxyAddress: string)

Get the implementation address from a proxy contract with proxyAddress

  • Example:
> await txUtils.getImplementationAddress(provider, '0xc4999A3631c017C45B5Ff3363eda6e8B9d81208E')
==> '0xa2BE51DBc0C901b75C7AD6a96a0BF66947ba20ba'