Skip to main content

Utility Functions Overview

This page documents the Utils class, which provides a set of helper methods for:

  1. Account/Address Conversion (EVM ↔ Push bech32m ↔ CAIP)
  2. Conversion between CAIP-10 addresses and UniversalAccount
  3. Block Serialization/Deserialization
  4. Transaction Serialization/Deserialization
  5. Transaction Data Serialization/Deserialization (by category)

Below is a complete breakdown of each function in the Utils class.


1. PushChain.utils.account

A namespace dedicated to converting addresses and handling UniversalAccount objects. Each method helps you work with different address formats.

1.1 From CAIP-10 to UniversalAccount

const account = PushChain.utils.account.toUniversal('eip155:1:0x35B84d6848D16415177c64D64504663b998A6ab4');

toUniversal parameters

ParamTypeRemarks
chainAgnosticAddressstringA CAIP-10 address in a chain-agnostic format.
Expected response
{
chain: 'ETHEREUM',
chainId: '1',
address: '0x35B84d6848D16415177c64D64504663b998A6ab4'
}

Response properties description:

PropertyTypeDescription
chainstringThe chain name
chainIdstringThe chain ID
addressstringThe address

Example: Convert Push devnet address to UniversalAccount

// A push-formatted address on Push devnet
const universalAccount = PushChain.utils.account.toUniversal('push:devnet:push1xyz123...');
// {
// chain: 'PUSH',
// chainId: 'devnet',
// address: 'push1xyz123...'
// }

1.2 From UniversalAccount to CAIP-10

const universalAccount = {
chain: CONSTANTS.CHAIN.ETHEREUM,
chainId: CONSTANTS.CHAIN_ID.ETHEREUM.SEPOLIA,
address: '0x35B84d6848D16415177c64D64504663b998A6ab4',
};

const result = PushChain.utils.account.toChainAgnostic(universalAccount);

toChainAgnostic parameters

ParamTypeRemarks
universalAccountUniversalAccountA UniversalAccount object.
Expected response
eip155:1:0x35B84d6848D16415177c64D64504663b998A6ab4

Example: Convert Mainnet Ethereum UniversalAccount to CAIP-10

const chainAgnosticStr = PushChain.utils.account.toChainAgnostic({
chain: 'ETHEREUM',
chainId: '1',
address: '0xabc123...'
});
// => 'eip155:1:0xabc123...'

1.3 Converts an EVM (Ethereum) address to a Push (bech32m) address

const pushAddr = PushChain.utils.account.evmToPushAddress('0x35B84d6848D16415177c64D64504663b998A6ab4');

evmToPush parameters

ParamTypeRemarks
evmAddressstringAn EVM address.
Expected response
push1xkuy66zg69jp29muvnty2prx8wvc5645f9y5ux

Example: Convert Ethereum mainnet address to Push devnet address

// A standard EVM address on Ethereum mainnet
const pushAddr = PushChain.utils.account.evmToPushAddress('0x35B84d6848D16415177c64D64504663b998A6ab4');
// => 'push1xkuy66zg69jp29muvnty2prx8wvc5645f9y5ux'

1.4 Converts a Push (bech32m) address to an EVM (Ethereum) address

const evmAddr = PushChain.utils.account.pushToEvmAddress('push1xkuy66zg69jp29muvnty2prx8wvc5645f9y5ux');

evmToPush parameters

ParamTypeRemarks
evmAddressstringAn EVM address.
Expected response
0x35B84d6848D16415177c64D64504663b998A6ab4

Example: Convert Ethereum mainnet address to Push devnet address

// A standard EVM address on Ethereum mainnet
const pushAddr = PushChain.utils.account.evmToPushAddress('0x35B84d6848D16415177c64D64504663b998A6ab4');
// => 'push1xkuy66zg69jp29muvnty2prx8wvc5645f9y5ux'

2. PushChain.utils.transaction

2.1 Serialize transaction

const serializedTx = PushChain.utils.tx.serialize(myTx);

serialize parameters

ParamTypeRemarks
txTransactionA transaction object.

2.2 Deserialize transaction

const deserializedTx = PushChain.utils.tx.deserialize(serializedTx);

deserialize parameters

ParamTypeRemarks
serializedTxUint8ArrayA serialized transaction.

2.3 Serialize Transaction Payload Data

const initDidData = { /* ...  */ };
const serializedData = PushChain.utils.tx.serializeData(initDidData, TxCategory.INIT_DID);

serializeData parameters

ParamTypeRemarks
dataInitDidThe data to serialize.
categorystringThe category of the transaction.

2.4 Serialize Transaction Payload Data

const deserializedData = PushChain.utils.tx.deserializeData(serializedData, TxCategory.INIT_DID);

deserializeData parameters

ParamTypeRemarks
serializedDataUint8ArrayThe serialized data.
categorystringThe category of the transaction.

3. PushChain.utils.block

3.1 Serialize block

const encodedBlock = PushChain.utils.block.serialize(myBlock);

serialize parameters

ParamTypeRemarks
blockGeneratedBlockA block object.

3.2 Deserialize block

const decodedBlock = PushChain.utils.block.deserialize(encodedBlock);

deserialize parameters

ParamTypeRemarks
blockUint8ArrayAn encoded block.
    1. Devnet Drop S2 is Live!Explore Push Chain Devnet, complete quests, bang out multipliers, and earn airdrops.