Skip to content

Instantly share code, notes, and snippets.

@cordt-sei
Created January 7, 2025 18:15
Show Gist options
  • Save cordt-sei/971193db0d560590f7e464b07a295c69 to your computer and use it in GitHub Desktop.
Save cordt-sei/971193db0d560590f7e464b07a295c69 to your computer and use it in GitHub Desktop.

Simple Sei Network Helper Functions

Environment variables, aliases, and scripts to simplify basic operations on the Sei network.


Environment Variables

Set these variables in your .bashrc or .bash_profile:

# RPC endpoint for Sei Tendermint queries
export SEIRPC="https://rpc.sei.basementnodes.ca"

# REST endpoint for Sei REST API queries
export SEIREST="https://api.sei.basementnodes.ca"

# EVM-compatible endpoint for Sei
export SEIEVM="https://evm-rpc.sei.basementnodes.ca"

# gRPC endpoint for archival data
export SEIGRPC="https://grpc.sei.basementnodes.ca"

Reload the configuration:

source ~/.bashrc

Aliases

Add these to .bashrc or .bash_profile for quick script access:

alias b64='bash ~/scripts/b64.sh'
alias h2d='node ~/scripts/js/hextodec.js'
alias d2h='node ~/scripts/js/dectohex.js'
alias assocwallet='bash ~/scripts/associated_wallet.sh'
alias crosstx='bash ~/scripts/cross_txhash.sh'

Reload the configuration:

source ~/.bashrc

Directory Setup

Before creating the scripts, ensure the necessary directories exist:

mkdir -p ~/scripts/js

Script Descriptions

1. b64.sh: Base64 Decoder

  • Decodes a Base64 string and extracts printable characters.
  • Create the file:
cat > ~/scripts/b64.sh << 'EOF'
#!/bin/bash
DATA=$1

# Decode the base64 input and extract printable characters
base64 -d <<< "$DATA" | strings
EOF
chmod +x ~/scripts/b64.sh

2. hextodec.js: Hexadecimal to Decimal Converter

  • Converts a hex string to its decimal representation.
  • Create the file:
cat > ~/scripts/js/hextodec.js << 'EOF'
// 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>");
}
EOF

3. dectohex.js: Decimal to Hexadecimal Converter

  • Converts a decimal number to a hex string.
  • Create the file:
cat > ~/scripts/js/dectohex.js << 'EOF'
  
// 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>");
}
EOF

4. associated_wallet.sh: Wallet Address Converter

  • Converts between Hex (EVM) and Bech32 (Sei) wallet addresses using Sei’s EVM JSON-RPC.
  • Create the file:
cat > ~/scripts/associated_wallet.sh << 'EOF'
#!/bin/bash
ADDRESS=$1

if [[ $ADDRESS == 0x* ]]; then
  # Hex to Bech32
  curl -X POST "$SEIEVM" -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"sei_getSeiAddress","params":["'"$ADDRESS"'"],"id":1}'
elif [[ $ADDRESS == sei* ]]; then
  # Bech32 to Hex
  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
EOF
     chmod +x ~/scripts/associated_wallet.sh

5. cross_txhash.sh: Transaction Hash Converter

  • Converts a transaction hash between EVM and Tendermint formats using Sei’s EVM JSON-RPC.
  • Create the file:
cat > ~/scripts/cross_txhash.sh << 'EOF'
#!/bin/bash
TX_HASH=$1

if [[ $TX_HASH == 0x* ]]; then
  # EVM to Tendermint
  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
  # Tendermint to EVM
  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
EOF
     chmod +x ~/scripts/cross_txhash.sh

Key Notes

  • The environment variables must be set for the scripts associated_wallet.sh and cross_txhash.sh to work, as they use $SEIEVM.
  • Ensure ~/scripts and ~/scripts/js directories exist before running the setup commands.
  • Reload aliases and environment variables with:
    source ~/.bashrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment