numberToHex static method

String numberToHex(
  1. dynamic number
)

Implementation

static String numberToHex(dynamic number) {
  BigInt bigInt;

  if (number is BigInt) {
    bigInt = number;
  } else if (number is int) {
    bigInt = BigInt.from(number);
  } else if (number is String) {
    if (number.contains('.')) {
      throw WalletException('Decimal numbers not supported');
    }
    try {
      bigInt = BigInt.parse(number);
    } catch (e) {
      throw WalletException('Invalid number string: $number');
    }
  } else {
    throw WalletException('Unsupported number type: ${number.runtimeType}');
  }

  if (bigInt < BigInt.zero) {
    throw WalletException('Negative numbers not supported');
  }

  if (bigInt == BigInt.zero) {
    return '0x0';
  }

  String hex = bigInt.toRadixString(16);
  if (hex.length % 2 != 0) {
    hex = '0$hex';
  }

  return '0x$hex';
}