Table of Contents

Class Utils

Namespace
Thirdweb
Assembly
Thirdweb.dll

Provides utility methods for various operations.

public static class Utils
Inheritance
Utils
Inherited Members

Methods

AdjustDecimals(BigInteger, int, int)

Adjusts the value's decimals.

public static BigInteger AdjustDecimals(this BigInteger value, int fromDecimals, int toDecimals)

Parameters

value BigInteger

The value.

fromDecimals int

The original number of decimals.

toDecimals int

The target number of decimals.

Returns

BigInteger

The value adjusted to the new decimals.

BytesToHex(byte[])

Converts the given bytes to a hex string.

public static string BytesToHex(this byte[] bytes)

Parameters

bytes byte[]

The bytes to convert.

Returns

string

The hex string.

ComputeClientIdFromSecretKey(string)

Computes the client ID from the given secret key.

public static string ComputeClientIdFromSecretKey(string secretKey)

Parameters

secretKey string

The secret key.

Returns

string

The computed client ID.

DecodeAllEvents<TEventDTO>(ThirdwebTransactionReceipt)

Decodes all events of the specified type from the transaction receipt logs.

public static List<EventLog<TEventDTO>> DecodeAllEvents<TEventDTO>(this ThirdwebTransactionReceipt transactionReceipt) where TEventDTO : new()

Parameters

transactionReceipt ThirdwebTransactionReceipt

The transaction receipt.

Returns

List<EventLog<TEventDTO>>

A list of decoded events.

Type Parameters

TEventDTO

The event DTO type.

DecodeAutorizationList(byte[])

Decodes the given RLP-encoded authorization list.

public static List<EIP7702Authorization> DecodeAutorizationList(byte[] authorizationListEncoded)

Parameters

authorizationListEncoded byte[]

Returns

List<EIP7702Authorization>

DecodeTransaction(byte[])

Decodes the given RLP-encoded transaction data.

public static (ThirdwebTransactionInput transactionInput, string signature) DecodeTransaction(byte[] signedRlpData)

Parameters

signedRlpData byte[]

The RLP-encoded signed transaction data.

Returns

(ThirdwebTransactionInput transactionInput, string signature)

The decoded transaction input and signature.

DecodeTransaction(string)

Decodes the given RLP-encoded transaction data.

public static (ThirdwebTransactionInput transactionInput, string signature) DecodeTransaction(string signedRlpData)

Parameters

signedRlpData string

The RLP-encoded signed transaction data.

Returns

(ThirdwebTransactionInput transactionInput, string signature)

The decoded transaction input and signature.

FetchGasFees(ThirdwebClient, BigInteger, bool)

public static Task<(BigInteger maxFeePerGas, BigInteger maxPriorityFeePerGas)> FetchGasFees(ThirdwebClient client, BigInteger chainId, bool withBump = true)

Parameters

client ThirdwebClient
chainId BigInteger
withBump bool

Returns

Task<(BigInteger maxFeePerGas, BigInteger maxPriorityFeePerGas)>

FetchGasPrice(ThirdwebClient, BigInteger, bool)

public static Task<BigInteger> FetchGasPrice(ThirdwebClient client, BigInteger chainId, bool withBump = true)

Parameters

client ThirdwebClient
chainId BigInteger
withBump bool

Returns

Task<BigInteger>

FormatERC20(string, int, int, bool)

Formats the given ERC20 token value.

public static string FormatERC20(this string wei, int decimalsToDisplay = 4, int decimals = 18, bool addCommas = false)

Parameters

wei string

The wei value to format.

decimalsToDisplay int

The number of decimals to display.

decimals int

The number of decimals of the token.

addCommas bool

Whether to add commas to the output.

Returns

string

The formatted token value.

GenerateSIWE(LoginPayloadData)

Generates a Sign-In With Ethereum (SIWE) message.

public static string GenerateSIWE(LoginPayloadData loginPayloadData)

Parameters

loginPayloadData LoginPayloadData

The login payload data.

Returns

string

The generated SIWE message.

GetAddressFromENS(ThirdwebClient, string)

public static Task<string> GetAddressFromENS(ThirdwebClient client, string ensName)

Parameters

client ThirdwebClient
ensName string

Returns

Task<string>

GetChainMetadata(ThirdwebClient, BigInteger)

public static Task<ThirdwebChainData> GetChainMetadata(ThirdwebClient client, BigInteger chainId)

Parameters

client ThirdwebClient
chainId BigInteger

Returns

Task<ThirdwebChainData>

GetENSFromAddress(ThirdwebClient, string)

public static Task<string> GetENSFromAddress(ThirdwebClient client, string address)

Parameters

client ThirdwebClient
address string

Returns

Task<string>

GetEntryPointVersion(string)

public static int GetEntryPointVersion(string address)

Parameters

address string

Returns

int

GetSocialProfiles(ThirdwebClient, string)

Gets the social profiles for the given address or ENS.

public static Task<SocialProfiles> GetSocialProfiles(ThirdwebClient client, string addressOrEns)

Parameters

client ThirdwebClient

The Thirdweb client.

addressOrEns string

The wallet address or ENS.

Returns

Task<SocialProfiles>

A SocialProfiles object containing the social profiles.

Exceptions

ArgumentNullException

Thrown when the address or ENS is null or empty.

ArgumentException

Thrown when the address or ENS is invalid.

Exception

Thrown when the social profiles could not be fetched.

GetUnixTimeStampIn10Years()

Gets the Unix timestamp for 10 years from now.

public static long GetUnixTimeStampIn10Years()

Returns

long

The Unix timestamp for 10 years from now.

GetUnixTimeStampNow()

Gets the current Unix timestamp.

public static long GetUnixTimeStampNow()

Returns

long

The current Unix timestamp.

HashMessage(byte[])

Hashes the given message bytes.

public static byte[] HashMessage(this byte[] messageBytes)

Parameters

messageBytes byte[]

The message bytes to hash.

Returns

byte[]

The hashed message bytes.

HashMessage(string)

Hashes the given message.

public static string HashMessage(this string message)

Parameters

message string

The message to hash.

Returns

string

The hashed message.

HashPrefixedMessage(byte[])

Hashes the given message bytes with a prefixed message.

public static byte[] HashPrefixedMessage(this byte[] messageBytes)

Parameters

messageBytes byte[]

The message bytes to hash.

Returns

byte[]

The hashed message bytes.

HashPrefixedMessage(string)

Hashes the given message with a prefixed message.

public static string HashPrefixedMessage(this string message)

Parameters

message string

The message to hash.

Returns

string

The hashed message.

HexConcat(params string[])

Concatenates the given hex strings.

public static string HexConcat(params string[] hexStrings)

Parameters

hexStrings string[]

The hex strings to concatenate.

Returns

string

The concatenated hex string.

HexToBigInt(string)

Converts the given hex string to a big integer.

[Obsolete("Use HexToNumber instead.")]
public static BigInteger HexToBigInt(this string hex)

Parameters

hex string

The hex string to convert.

Returns

BigInteger

The big integer.

HexToBytes(string)

Converts the given hex string to bytes.

public static byte[] HexToBytes(this string hex)

Parameters

hex string

The hex string to convert.

Returns

byte[]

The bytes.

HexToBytes32(string)

public static byte[] HexToBytes32(this string hex)

Parameters

hex string

Returns

byte[]

HexToNumber(string)

Converts the given hex string to a big integer.

public static BigInteger HexToNumber(this string hex)

Parameters

hex string

The hex string to convert.

Returns

BigInteger

The big integer.

HexToString(string)

Converts the given hex string to a regular string.

public static string HexToString(this string hex)

Parameters

hex string

The hex string to convert.

Returns

string

The regular string.

IsDeployed(ThirdwebClient, BigInteger, string)

public static Task<bool> IsDeployed(ThirdwebClient client, BigInteger chainId, string address)

Parameters

client ThirdwebClient
chainId BigInteger
address string

Returns

Task<bool>

IsEip1559Supported(string)

public static bool IsEip1559Supported(string chainId)

Parameters

chainId string

Returns

bool

IsEip155Enforced(ThirdwebClient, BigInteger)

public static Task<bool> IsEip155Enforced(ThirdwebClient client, BigInteger chainId)

Parameters

client ThirdwebClient
chainId BigInteger

Returns

Task<bool>

IsValidAddress(string)

public static bool IsValidAddress(this string address)

Parameters

address string

Returns

bool

IsZkSync(ThirdwebClient, BigInteger)

Checks if the chain ID corresponds to zkSync.

public static Task<bool> IsZkSync(ThirdwebClient client, BigInteger chainId)

Parameters

client ThirdwebClient

The Thirdweb client.

chainId BigInteger

The chain ID.

Returns

Task<bool>

True if it is a zkSync chain ID, otherwise false.

NumberToHex(int)

Converts the given integer to a hex string.

public static string NumberToHex(this int number)

Parameters

number int

Returns

string

NumberToHex(long)

Converts the given long to a hex string.

public static string NumberToHex(this long number)

Parameters

number long

Returns

string

NumberToHex(BigInteger)

Converts the given big integer to a hex string.

public static string NumberToHex(this BigInteger number)

Parameters

number BigInteger

Returns

string

PacketToBytes(string)

public static byte[] PacketToBytes(string packet)

Parameters

packet string

Returns

byte[]

PreprocessTypedDataJson(string)

Preprocesses the typed data JSON to stringify large numbers.

public static string PreprocessTypedDataJson(string json)

Parameters

json string

The typed data JSON.

Returns

string

The preprocessed typed data JSON.

ReconstructHttpClient(IThirdwebHttpClient, Dictionary<string, string>)

public static IThirdwebHttpClient ReconstructHttpClient(IThirdwebHttpClient httpClient, Dictionary<string, string> defaultHeaders = null)

Parameters

httpClient IThirdwebHttpClient
defaultHeaders Dictionary<string, string>

Returns

IThirdwebHttpClient

ReplaceIPFS(string, string)

Replaces the IPFS URI with a specified gateway.

public static string ReplaceIPFS(this string uri, string gateway = null)

Parameters

uri string

The URI to replace.

gateway string

The gateway to use.

Returns

string

The replaced URI.

SerializeErc6492Signature(string, byte[], byte[])

Serializes a signature for use with ERC-6492. The signature must be generated by a signer for an ERC-4337 Account Factory account with counterfactual deployment addresses.

public static string SerializeErc6492Signature(string address, byte[] data, byte[] signature)

Parameters

address string

The ERC-4337 Account Factory address

data byte[]

Account deployment calldata (if not deployed) for counterfactual verification

signature byte[]

The original signature

Returns

string

The serialized signature hex string.

StringToHex(string)

Converts the given string to a hex string.

public static string StringToHex(this string str)

Parameters

str string

The string to convert.

Returns

string

The hex string.

ToChecksumAddress(string)

Converts an Ethereum address to its checksum format.

public static string ToChecksumAddress(this string address)

Parameters

address string

The Ethereum address.

Returns

string

The checksummed Ethereum address.

ToEth(string, int, bool)

Converts the given wei value to ether.

public static string ToEth(this string wei, int decimalsToDisplay = 4, bool addCommas = false)

Parameters

wei string

The wei value to convert.

decimalsToDisplay int

The number of decimals to display.

addCommas bool

Whether to add commas to the output.

Returns

string

The ether value.

ToJsonExternalWalletFriendly<TMessage, TDomain>(TypedData<TDomain>, TMessage)

public static string ToJsonExternalWalletFriendly<TMessage, TDomain>(TypedData<TDomain> typedData, TMessage message)

Parameters

typedData TypedData<TDomain>
message TMessage

Returns

string

Type Parameters

TMessage
TDomain

ToWei(string)

Converts the given ether value to wei.

public static string ToWei(this string eth)

Parameters

eth string

The ether value to convert.

Returns

string

The wei value.

TrimZeroes(byte[])

Removes leading zeroes from the given byte array.

public static byte[] TrimZeroes(this byte[] bytes)

Parameters

bytes byte[]

Returns

byte[]