Skip to content

Instantly share code, notes, and snippets.

@cordt-sei
Last active December 21, 2024 02:12
Show Gist options
  • Save cordt-sei/5f483ed25f7131202379d6887942f654 to your computer and use it in GitHub Desktop.
Save cordt-sei/5f483ed25f7131202379d6887942f654 to your computer and use it in GitHub Desktop.

Sei EVM/Cosmos Network Debug Tools

Here is a small collection of scripts and shell aliases designed to perform some very basic but very common and useful functions. These quick simple tools are meant to relieve a bit of the tedious repetative tasks when debugging or exploring information or applications on the Sei network.


Setup Instructions

  1. Clone or Copy Scripts: Save the provided scripts into your preferred directory (e.g., $HOME/scripts/). Ensure each file is executable:

    chmod +x $HOME/scripts/*.sh
  2. Add Aliases to Shell Config: Add the following aliases to your shell configuration file (e.g., ~/.bashrc for Bash or ~/.config/fish/config.fish for Fish shell):

    alias b64='bash $HOME/scripts/b64.sh'
    alias h2d='node $HOME/scripts/js/hextodec.js'
    alias d2h='node $HOME/scripts/js/dectohex.js'
    alias assocwallet='bash $HOME/scripts/associated_wallet.sh'
    alias crosstx='bash $HOME/scripts/cross_txhash.sh'
  3. Reload Shell Configuration: Apply the changes:

    source ~/.bashrc
  4. Define Environment Variables: Set the necessary RPC endpoint environment variables:

    export SEIEVM="<EVM_RPC_URL>"
    export SEIRPC="<TENDERMINT_RPC_URL>"

Tool Descriptions

1. Base64 Decoder

Script: $HOME/scripts/b64.sh

Alias: b64

Purpose: Decodes base64-encoded data, extracting printable characters for easier readability and debugging.

Script Content:

#!/bin/bash

DATA=$1

echo "$DATA" | base64 --decode | strings

Usage:

b64 "<base64_data>"

2. Hex to Decimal Converter

Script: $HOME/scripts/js/hextodec.js

Alias: h2d

Purpose: Converts a hexadecimal string to its decimal equivalent, useful for interpreting numerical data in EVM logs or transactions.

Script Content:

// Function to convert a hexadecimal string to a decimal number
function hexToDecimal(hexString) {
  if (typeof hexString !== 'string' || hexString.trim() === '') {
    console.error("Please provide a valid hexadecimal string.");
    return;
  }

  let decimalValue = parseInt(hexString, 16);

  if (isNaN(decimalValue)) {
    console.error("Invalid hexadecimal string. Could not convert to decimal.");
  } else {
    console.log(`Hexadecimal: ${hexString}`);
    console.log(`Decimal: ${decimalValue}`);
  }
}

const hexInput = process.argv[2];
if (hexInput) {
  hexToDecimal(hexInput);
} else {
  console.log("Usage: node hextodec.js <hexString>");
}

Usage:

h2d <hexadecimal_value>

3. Decimal to Hex Converter

Script: $HOME/scripts/js/dectohex.js

Alias: d2h

Purpose: Converts a decimal number to its hexadecimal representation, often needed for crafting EVM-compatible data.

Script Content:

// Function to convert a decimal number to a hexadecimal string
function decimalToHex(decimalNumber) {
  if (isNaN(decimalNumber)) {
    console.error("Please provide a valid decimal number.");
    return;
  }

  let hexValue = parseInt(decimalNumber).toString(16);

  console.log(`Decimal: ${decimalNumber}`);
  console.log(`Hexadecimal: 0x${hexValue}`);
}

const decimalInput = process.argv[2];
if (decimalInput) {
  decimalToHex(decimalInput);
} else {
  console.log("Usage: node dectohex.js <decimalNumber>");
}

Usage:

d2h <decimal_value>

4. Associated Wallet Query

Script: $HOME/scripts/associated_wallet.sh

Alias: assocwallet

Purpose: Checks for an associated address for a given wallet. Supports conversion between Bech32 (Cosmos) and Hex (EVM) addresses.

Script Content:

#!/bin/bash

ADDRESS=$1

if [[ $ADDRESS == 0x* ]]; then
  curl -X POST "$SEIEVM" -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"sei_getSeiAddress","params":["'$ADDRESS'"],"id":1}'
elif [[ $ADDRESS == sei* ]]; then
  curl -X POST "$SEIEVM" -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"sei_getEVMAddress","params":["'$ADDRESS'"],"id":1}'
else
  echo "Invalid address format. Provide a Bech32 or Hex address."
fi

Usage:

  • Bech32 to Hex:
    assocwallet sei1...
  • Hex to Bech32:
    assocwallet 0x...

5. Cross-Environment Transaction Hash Query

Script: $HOME/scripts/cross_txhash.sh

Alias: crosstx

Purpose: Fetches the corresponding transaction hash when moving between EVM and Cosmos environments.

Script Content:

#!/bin/bash

TX_HASH=$1

if [[ $TX_HASH == 0x* ]]; then
  curl -X POST "$SEIEVM" -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"sei_getCosmosTx","params":["'$TX_HASH'"],"id":1}'
elif [[ $TX_HASH == *txhash* ]]; then
  curl -X POST "$SEIEVM" -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"sei_getEvmTx","params":["'$TX_HASH'"],"id":1}'
else
  echo "Invalid transaction hash format. Provide an EVM or Tendermint transaction hash."
fi

Usage:

  • EVM to Tendermint:
    crosstx 0x...
  • Tendermint to EVM:
    crosstx txhash...

Use Cases

Debugging Address Associations

Quickly identify if a wallet address is associated across environments (Bech32 <-> Hex) to ensure interoperability.

Analyzing Transaction Flows

Trace transaction hashes between EVM and Cosmos environments to diagnose discrepancies or missing links.

Data Conversion

Facilitate conversion of numerical data formats (hex/decimal) to debug logs or transactions more effectively.

Base64 Decoding

Decode encoded data, such as smart contract payloads or debug messages, into human-readable format.


Notes

  • These tools rely on RPC endpoints ($SEIEVM, $SEIRPC). Ensure they are correctly configured and reachable. Any public endpoints should work here.
  • Each script is lightweight and intended for quick troubleshooting, not for production-level automation.
  • Always verify data outputs, especially when debugging critical issues.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment