Utility Functions Overview
This page documents the Utils class, which provides a set of helper methods for:
- Account/Address Conversion (EVM ↔ Push bech32m ↔ CAIP)
- Conversion between CAIP-10 addresses and UniversalAccount
- Block Serialization/Deserialization
- Transaction Serialization/Deserialization
- 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
- js
- react
- reactnative
const account = PushChain.utils.account.toUniversal('eip155:1:0x35B84d6848D16415177c64D64504663b998A6ab4');
const account = PushChain.utils.account.toUniversal('eip155:1:0x35B84d6848D16415177c64D64504663b998A6ab4');
const account = PushChain.utils.account.toUniversal('eip155:1:0x35B84d6848D16415177c64D64504663b998A6ab4');
toUniversal
parameters
Param | Type | Remarks |
---|---|---|
chainAgnosticAddress | string | A CAIP-10 address in a chain-agnostic format. |
Expected response
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
- js
- react
- reactnative
const universalAccount = {
chain: CONSTANTS.CHAIN.ETHEREUM,
chainId: CONSTANTS.CHAIN_ID.ETHEREUM.SEPOLIA,
address: '0x35B84d6848D16415177c64D64504663b998A6ab4',
};
const result = PushChain.utils.account.toChainAgnostic(universalAccount);
const universalAccount = {
chain: CONSTANTS.CHAIN.ETHEREUM,
chainId: CONSTANTS.CHAIN_ID.ETHEREUM.SEPOLIA,
address: '0x35B84d6848D16415177c64D64504663b998A6ab4',
};
const result = PushChain.utils.account.toChainAgnostic(universalAccount);
const universalAccount = {
chain: CONSTANTS.CHAIN.ETHEREUM,
chainId: CONSTANTS.CHAIN_ID.ETHEREUM.SEPOLIA,
address: '0x35B84d6848D16415177c64D64504663b998A6ab4',
};
const result = PushChain.utils.account.toChainAgnostic(universalAccount);
toChainAgnostic
parameters
Param | Type | Remarks |
---|---|---|
universalAccount | UniversalAccount | A UniversalAccount object. |
Expected response
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
- js
- react
- reactnative
const pushAddr = PushChain.utils.account.evmToPushAddress('0x35B84d6848D16415177c64D64504663b998A6ab4');
const pushAddr = PushChain.utils.account.evmToPushAddress('0x35B84d6848D16415177c64D64504663b998A6ab4');
const pushAddr = PushChain.utils.account.evmToPushAddress('0x35B84d6848D16415177c64D64504663b998A6ab4');
evmToPush
parameters
Param | Type | Remarks |
---|---|---|
evmAddress | string | An EVM address. |
Expected response
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
- js
- react
- reactnative
const evmAddr = PushChain.utils.account.pushToEvmAddress('push1xkuy66zg69jp29muvnty2prx8wvc5645f9y5ux');
const evmAddr = PushChain.utils.account.pushToEvmAddress('push1xkuy66zg69jp29muvnty2prx8wvc5645f9y5ux');
const evmAddr = PushChain.utils.account.pushToEvmAddress('push1xkuy66zg69jp29muvnty2prx8wvc5645f9y5ux');
evmToPush
parameters
Param | Type | Remarks |
---|---|---|
evmAddress | string | An EVM address. |
Expected response
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
- js
- react
- reactnative
const serializedTx = PushChain.utils.tx.serialize(myTx);
const serializedTx = PushChain.utils.tx.serialize(myTx);
const serializedTx = PushChain.utils.tx.serialize(myTx);
serialize
parameters
Param | Type | Remarks |
---|---|---|
tx | Transaction | A transaction object. |
2.2 Deserialize transaction
- js
- react
- reactnative
const deserializedTx = PushChain.utils.tx.deserialize(serializedTx);
const deserializedTx = PushChain.utils.tx.deserialize(serializedTx);
const deserializedTx = PushChain.utils.tx.deserialize(serializedTx);
deserialize
parameters
Param | Type | Remarks |
---|---|---|
serializedTx | Uint8Array | A serialized transaction. |
2.3 Serialize Transaction Payload Data
- js
- react
- reactnative
const initDidData = { /* ... */ };
const serializedData = PushChain.utils.tx.serializeData(initDidData, TxCategory.INIT_DID);
const initDidData = { /* ... */ };
const serializedData = PushChain.utils.tx.serializeData(initDidData, TxCategory.INIT_DID);
const initDidData = { /* ... */ };
const serializedData = PushChain.utils.tx.serializeData(initDidData, TxCategory.INIT_DID);
serializeData
parameters
Param | Type | Remarks |
---|---|---|
data | InitDid | The data to serialize. |
category | string | The category of the transaction. |
2.4 Serialize Transaction Payload Data
- js
- react
- reactnative
const deserializedData = PushChain.utils.tx.deserializeData(serializedData, TxCategory.INIT_DID);
const deserializedData = PushChain.utils.tx.deserializeData(serializedData, TxCategory.INIT_DID);
const deserializedData = PushChain.utils.tx.deserializeData(serializedData, TxCategory.INIT_DID);
deserializeData
parameters
Param | Type | Remarks |
---|---|---|
serializedData | Uint8Array | The serialized data. |
category | string | The category of the transaction. |
3. PushChain.utils.block
3.1 Serialize block
- js
- react
- reactnative
const encodedBlock = PushChain.utils.block.serialize(myBlock);
const encodedBlock = PushChain.utils.block.serialize(myBlock);
const encodedBlock = PushChain.utils.block.serialize(myBlock);
serialize
parameters
Param | Type | Remarks |
---|---|---|
block | GeneratedBlock | A block object. |
3.2 Deserialize block
- js
- react
- reactnative
const decodedBlock = PushChain.utils.block.deserialize(encodedBlock);
const decodedBlock = PushChain.utils.block.deserialize(encodedBlock);
const decodedBlock = PushChain.utils.block.deserialize(encodedBlock);
deserialize
parameters
Param | Type | Remarks |
---|---|---|
block | Uint8Array | An encoded block. |