diff --git a/packages/config/src/Config.ts b/packages/config/src/Config.ts index c96eee4e..c49b910c 100644 --- a/packages/config/src/Config.ts +++ b/packages/config/src/Config.ts @@ -25,6 +25,7 @@ import { cordSignedExtensions, } from '@cord.network/type-definitions' import * as ConfigService from './Service.js' +import { SDKErrors } from '@cord.network/utils' /** * Initializes the CORD SDK configuration and prepares cryptographic modules. @@ -130,3 +131,49 @@ export async function disconnect(): Promise { await api.disconnect() return true } + +/** + * Returns the genesis hash in hexadecimal format for a given network object which is a ApiPromise. + * + * @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 { + const connectData = await connect(networkAddress as string); + const genesisHash = connectData.genesisHash.toHex(); + + return genesisHash; +} catch (error) { + const errorMessage = + error instanceof Error ? error.message : JSON.stringify(error) + + throw new SDKErrors.CordQueryError( + `Error querying asset entry: ${errorMessage}` + ) +} finally { + disconnect(); + } +} diff --git a/packages/config/src/index.ts b/packages/config/src/index.ts index d34cadd8..bb66a0d0 100644 --- a/packages/config/src/index.ts +++ b/packages/config/src/index.ts @@ -1,2 +1,2 @@ export * as ConfigService from './Service.js' -export { connect, disconnect, init } from './Config.js' +export { connect, disconnect, init, getGenesisHash, getGenesisHashWithConnect } from './Config.js' diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index a2b09202..6b075341 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -1,4 +1,4 @@ -export { connect, disconnect, init, ConfigService } from '@cord.network/config' +export { connect, disconnect, init, getGenesisHash, getGenesisHashWithConnect, ConfigService } from '@cord.network/config' export { Chain } from '@cord.network/network' export * as ChainHelpers from '@cord.network/network' export * as Did from '@cord.network/did'