diff --git a/packages/utils/src/genesis.ts b/packages/utils/src/genesis.ts index 1446a890..a222a8f2 100644 --- a/packages/utils/src/genesis.ts +++ b/packages/utils/src/genesis.ts @@ -1,13 +1,34 @@ -import { connect, disconnect } from '@cord.network/config' -import { SDKErrors } from '.' +import { connect, disconnect } from '@cord.network/config'; +import { SDKErrors } from '.'; +import { ApiPromise } from '@polkadot/api'; /** - * Connects to chain and returnns the genesis hash in hexadecimal format for a given networkAddress. + * Returns the genesis hash in hexadecimal format for a given network object which is a ApiPromise. * - * @param networkAddress Network Address for which the genesis hash is required. + * @param network network object of type ApiPromise for which the genesis hash is required. * @returns Returns the genesis hash in Hexadecimal format. */ export async function getGenesisHash( + network: ApiPromise, +): Promise { +try { + return network.genesisHash.toHex(); +} catch (error) { + const errorMessage = + error instanceof Error ? error.message : JSON.stringify(error) + + throw new SDKErrors.CordQueryError( + `Error querying asset entry: ${errorMessage}` + )} +} + +/** + * Connects to chain and returns the genesis hash in hexadecimal format for a given networkAddress. + * + * @param networkAddress Network Address for which the genesis hash is required. + * @returns Returns the genesis hash in Hexadecimal format. + */ +export async function getGenesisHashWithConnect( networkAddress: string, ): Promise { try { diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 300da419..9f0dc34c 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -8,6 +8,7 @@ export * as DataUtils from './DataUtils.js' export * as DecoderUtils from './Decode.js' export * as SDKErrors from './SDKErrors.js' export * as JsonSchema from './json-schema/index.js' +export * as Genesis from './genesis.js' export { ss58Format } from './ss58Format.js' export { Keyring } from '@polkadot/keyring' export * as Cbor from 'cbor-x'