Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: Add a util to get the genesis hash for a network address #228

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions packages/config/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -130,3 +131,49 @@ export async function disconnect(): Promise<boolean> {
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<string> {
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<string> {
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();
}
}
2 changes: 1 addition & 1 deletion packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down