From 208ce34323c2f243ccd52ce3527d786c703f6543 Mon Sep 17 00:00:00 2001 From: Shreevatsa N Date: Tue, 25 Jun 2024 17:31:36 +0530 Subject: [PATCH 1/3] utils: Add a util to get the genesis hash for a network addrs Signed-off-by: Shreevatsa N --- packages/utils/src/genesis.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 packages/utils/src/genesis.ts diff --git a/packages/utils/src/genesis.ts b/packages/utils/src/genesis.ts new file mode 100644 index 00000000..1446a890 --- /dev/null +++ b/packages/utils/src/genesis.ts @@ -0,0 +1,28 @@ +import { connect, disconnect } from '@cord.network/config' +import { SDKErrors } from '.' + +/** + * Connects to chain and returnns 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 getGenesisHash( + 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(); + } +} From ac51535318b56a209e9002ed37bc588acad378b0 Mon Sep 17 00:00:00 2001 From: Shreevatsa N Date: Wed, 26 Jun 2024 16:34:49 +0530 Subject: [PATCH 2/3] get genesis with ApiPromise Signed-off-by: Shreevatsa N --- packages/utils/src/genesis.ts | 29 +++++++++++++++++++++++++---- packages/utils/src/index.ts | 1 + 2 files changed, 26 insertions(+), 4 deletions(-) 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' From 4c7d509802b3f56d80874c9671d70e2a68f504aa Mon Sep 17 00:00:00 2001 From: Shreevatsa N Date: Mon, 1 Jul 2024 10:59:29 +0530 Subject: [PATCH 3/3] Move genesisHash methods to config Signed-off-by: Shreevatsa N --- packages/config/src/Config.ts | 47 +++++++++++++++++++++++++++++++++ packages/config/src/index.ts | 2 +- packages/sdk/src/index.ts | 2 +- packages/utils/src/genesis.ts | 49 ----------------------------------- packages/utils/src/index.ts | 1 - 5 files changed, 49 insertions(+), 52 deletions(-) delete mode 100644 packages/utils/src/genesis.ts 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' diff --git a/packages/utils/src/genesis.ts b/packages/utils/src/genesis.ts deleted file mode 100644 index a222a8f2..00000000 --- a/packages/utils/src/genesis.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { connect, disconnect } from '@cord.network/config'; -import { SDKErrors } from '.'; -import { ApiPromise } from '@polkadot/api'; - -/** - * 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/utils/src/index.ts b/packages/utils/src/index.ts index 9f0dc34c..300da419 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -8,7 +8,6 @@ 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'