UNPKG

@cheqd/sdk

Version:

A TypeScript SDK built with CosmJS to interact with the cheqd network ledger

109 lines 7.16 kB
import { DIDModule } from './modules/did.js'; import { ResourceModule } from './modules/resource.js'; import { FeemarketModule, defaultGasPriceTiers, } from './modules/feemarket.js'; import { applyMixins, instantiateCheqdSDKModule, instantiateCheqdSDKModuleRegistryTypes, instantiateCheqdSDKModuleQuerierExtensionSetup, } from './modules/_.js'; import { createDefaultCheqdRegistry } from './registry.js'; import { CheqdSigningStargateClient } from './signer.js'; import { CheqdNetwork } from './types.js'; import { QueryClient } from '@cosmjs/stargate'; import { CheqdQuerier } from './querier.js'; import { FeeabstractionModule, } from './modules/feeabstraction.js'; export class CheqdSDK { methods; signer; querier; options; protectedMethods = ['constructor', 'build', 'loadModules', 'loadRegistry']; constructor(options) { if (!options?.wallet) { throw new Error('No wallet provided'); } this.options = { authorizedMethods: [], network: CheqdNetwork.Testnet, ...options, }; this.methods = {}; this.signer = new CheqdSigningStargateClient(undefined, this.options.wallet, {}); this.querier = new QueryClient({}); } async execute(method, ...params) { if (!Object.keys(this.methods).includes(method)) { throw new Error(`Method ${method} is not authorized`); } return await this.methods[method](...params, { sdk: this }); } async loadModules(modules) { this.options.modules = this.options.modules.map((module) => instantiateCheqdSDKModule(module, this.signer, this.querier, { sdk: this, })); const methods = applyMixins(this, modules); this.methods = { ...this.methods, ...filterUnauthorizedMethods(methods, this.options.authorizedMethods || [], this.protectedMethods), }; for (const method of Object.keys(this.methods)) { // @ts-ignore this[method] = async (...params) => { return await this.execute(method, ...params); }; } return this; } loadRegistry() { const registryTypes = this.options.modules .map((module) => instantiateCheqdSDKModuleRegistryTypes(module)) .reduce((acc, types) => { return [...acc, ...types]; }); return createDefaultCheqdRegistry(registryTypes); } async loadQuerierExtensions() { const querierExtensions = this.options.modules.map((module) => instantiateCheqdSDKModuleQuerierExtensionSetup(module)); const querier = await CheqdQuerier.connectWithExtensions(this.options.rpcUrl, ...querierExtensions); return querier; } async build() { const registry = this.loadRegistry(); this.querier = await this.loadQuerierExtensions(); // ensure feemarket module is loaded, if not already if (!this.options.modules.find((module) => module instanceof FeemarketModule)) { this.options.modules.push(FeemarketModule); } const sdk = await this.loadModules(this.options.modules); // define gas price this.options.gasPrice = this.options.gasPrice || (await this.generateSafeGasPriceWithExponentialBackoff(DIDModule.baseMinimalDenom, defaultGasPriceTiers.Low, undefined, { sdk })); this.signer = await CheqdSigningStargateClient.connectWithSigner(this.options.rpcUrl, this.options.wallet, { registry, gasPrice: this.options.gasPrice, }); return sdk; } } export function filterUnauthorizedMethods(methods, authorizedMethods, protectedMethods) { let _methods = Object.keys(methods); if (authorizedMethods.length === 0) return _methods .filter((method) => !protectedMethods.includes(method)) .reduce((acc, method) => ({ ...acc, [method]: methods[method] }), {}); return _methods .filter((method) => authorizedMethods.includes(method) && !protectedMethods.includes(method)) .reduce((acc, method) => ({ ...acc, [method]: methods[method] }), {}); } export async function createCheqdSDK(options) { return await new CheqdSDK(options).build(); } export { DIDModule, ResourceModule, FeemarketModule, FeeabstractionModule }; export { AbstractCheqdSDKModule, applyMixins } from './modules/_.js'; export { MsgCreateDidDocResponseEncodeObject, MsgUpdateDidDocEncodeObject, MsgUpdateDidDocResponseEncodeObject, MsgDeactivateDidDocEncodeObject, contexts, defaultDidExtensionKey, protobufLiterals as protobufLiteralsDid, typeUrlMsgCreateDidDoc, typeUrlMsgCreateDidDocResponse, typeUrlMsgUpdateDidDoc, typeUrlMsgUpdateDidDocResponse, typeUrlMsgDeactivateDidDoc, typeUrlMsgDeactivateDidDocResponse, setupDidExtension, isMsgCreateDidDocEncodeObject, isMsgUpdateDidDocEncodeObject, isMsgDeactivateDidDocEncodeObject, } from './modules/did.js'; export { defaultResourceExtensionKey, protobufLiterals as protobufLiteralsResource, typeUrlMsgCreateResource, typeUrlMsgCreateResourceResponse, setupResourceExtension, isMsgCreateResourceEncodeObject, } from './modules/resource.js'; export { defaultFeemarketExtensionKey, defaultGasPriceTiers, protobufLiterals as protobufLiteralsFeemarket, typeUrlGasPriceResponse, typeUrlGasPricesResponse, typeUrlParamsResponse, setupFeemarketExtension, isGasPriceEncodeObject, isGasPricesEncodeObject, isParamsEncodeObject, } from './modules/feemarket.js'; export { defaultExtensionKey, protobufLiterals as protobufLiteralsFeeabstraction, typeUrlMsgAddHostZone, typeUrlMsgAddHostZoneResponse, typeUrlMsgFundFeeAbsModuleAccount, typeUrlMsgFundFeeAbsModuleAccountResponse, typeUrlMsgRemoveHostZone, typeUrlMsgRemoveHostZoneResponse, typeUrlMsgUpdateHostZone, typeUrlMsgUpdateHostZoneResponse, typeUrlMsgSendQueryIbcDenomTWAP, typeUrlMsgSendQueryIbcDenomTWAPResponse, typeUrlMsgSwapCrossChain, typeUrlMsgSwapCrossChainResponse, typeUrlMsgUpdateParams, typeUrlMsgUpdateParamsResponse, setupFeeabstractionExtension, isMsgAddHostZoneEncodeObject, isMsgAddHostZoneResponseEncodeObject, isMsgFundFeeAbsModuleAccountEncodeObject, isMsgFundFeeAbsModuleAccountResponseEncodeObject, isMsgRemoveHostZoneEncodeObject, isMsgRemoveHostZoneResponseEncodeObject, isMsgUpdateHostZoneEncodeObject, isMsgUpdateHostZoneResponseEncodeObject, isMsgSendQueryIbcDenomTWAPEncodeObject, isMsgSendQueryIbcDenomTWAPResponseEncodeObject, isMsgSwapCrossChainEncodeObject, isMsgSwapCrossChainResponseEncodeObject, isMsgUpdateParamsEncodeObject, isMsgUpdateParamsResponseEncodeObject, } from './modules/feeabstraction.js'; export * from './signer.js'; export * from './querier.js'; export * from './registry.js'; export * from './types.js'; export { TImportableEd25519Key, createKeyPairRaw, createKeyPairBase64, createKeyPairHex, createVerificationKeys, createDidVerificationMethod, createDidPayload, createSignInputsFromImportableEd25519Key, validateSpecCompliantPayload, isEqualKeyValuePair, createCosmosPayerWallet, getCosmosAccount, checkBalance, toMultibaseRaw, } from './utils.js'; //# sourceMappingURL=index.js.map