|
| 1 | +import { OfflineSigner, Registry } from '@cosmjs/proto-signing'; |
| 2 | +import { DIDModule, MinimalImportableDIDModule, DidExtension } from './modules/did'; |
| 3 | +import { MinimalImportableResourceModule, ResourceModule, ResourceExtension } from './modules/resource'; |
| 4 | +import { |
| 5 | + AbstractCheqdSDKModule, |
| 6 | + applyMixins, |
| 7 | + instantiateCheqdSDKModule, |
| 8 | + instantiateCheqdSDKModuleRegistryTypes, |
| 9 | + instantiateCheqdSDKModuleQuerierExtensionSetup, |
| 10 | +} from './modules/_'; |
| 11 | +import { createDefaultCheqdRegistry } from './registry'; |
| 12 | +import { CheqdSigningStargateClient } from './signer'; |
| 13 | +import { CheqdNetwork, IContext, IModuleMethodMap } from './types'; |
| 14 | +import { GasPrice, QueryClient } from '@cosmjs/stargate'; |
| 15 | +import { CheqdQuerier } from './querier'; |
| 16 | +import { Tendermint37Client } from '@cosmjs/tendermint-rpc'; |
| 17 | +import { FeemarketExtension, FeemarketModule } from './modules/feemarket'; |
| 18 | + |
| 19 | +export interface ICheqdSDKOptions { |
| 20 | + modules: AbstractCheqdSDKModule[]; |
| 21 | + querierExtensions?: Record<string, any>[]; |
| 22 | + rpcUrl: string; |
| 23 | + network?: CheqdNetwork; |
| 24 | + gasPrice?: GasPrice; |
| 25 | + authorizedMethods?: string[]; |
| 26 | + readonly wallet: OfflineSigner; |
| 27 | +} |
| 28 | + |
| 29 | +export type DefaultCheqdSDKModules = MinimalImportableDIDModule & MinimalImportableResourceModule; |
| 30 | + |
| 31 | +export interface CheqdSDK extends DefaultCheqdSDKModules {} |
| 32 | + |
| 33 | +export class CheqdSDK { |
| 34 | + methods: IModuleMethodMap; |
| 35 | + signer: CheqdSigningStargateClient; |
| 36 | + querier: CheqdQuerier & DidExtension & ResourceExtension & FeemarketExtension; |
| 37 | + options: ICheqdSDKOptions; |
| 38 | + private protectedMethods: string[] = ['constructor', 'build', 'loadModules', 'loadRegistry']; |
| 39 | + |
| 40 | + constructor(options: ICheqdSDKOptions) { |
| 41 | + if (!options?.wallet) { |
| 42 | + throw new Error('No wallet provided'); |
| 43 | + } |
| 44 | + |
| 45 | + this.options = { |
| 46 | + authorizedMethods: [], |
| 47 | + network: CheqdNetwork.Testnet, |
| 48 | + ...options, |
| 49 | + }; |
| 50 | + |
| 51 | + this.methods = {}; |
| 52 | + this.signer = new CheqdSigningStargateClient(undefined, this.options.wallet, {}); |
| 53 | + this.querier = <any>new QueryClient({} as unknown as Tendermint37Client); |
| 54 | + } |
| 55 | + |
| 56 | + async execute<P = any, R = any>(method: string, ...params: P[]): Promise<R> { |
| 57 | + if (!Object.keys(this.methods).includes(method)) { |
| 58 | + throw new Error(`Method ${method} is not authorized`); |
| 59 | + } |
| 60 | + return await this.methods[method](...params, { sdk: this } as IContext); |
| 61 | + } |
| 62 | + |
| 63 | + private async loadModules(modules: AbstractCheqdSDKModule[]): Promise<CheqdSDK> { |
| 64 | + this.options.modules = this.options.modules.map( |
| 65 | + (module: any) => |
| 66 | + instantiateCheqdSDKModule(module, this.signer, this.querier, { |
| 67 | + sdk: this, |
| 68 | + } as IContext) as unknown as AbstractCheqdSDKModule |
| 69 | + ); |
| 70 | + |
| 71 | + const methods = applyMixins(this, modules); |
| 72 | + this.methods = { |
| 73 | + ...this.methods, |
| 74 | + ...filterUnauthorizedMethods(methods, this.options.authorizedMethods || [], this.protectedMethods), |
| 75 | + }; |
| 76 | + |
| 77 | + for (const method of Object.keys(this.methods)) { |
| 78 | + // @ts-ignore |
| 79 | + this[method] = async (...params: any[]) => { |
| 80 | + return await this.execute(method, ...params); |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + private loadRegistry(): Registry { |
| 88 | + const registryTypes = this.options.modules |
| 89 | + .map((module: any) => instantiateCheqdSDKModuleRegistryTypes(module)) |
| 90 | + .reduce((acc, types) => { |
| 91 | + return [...acc, ...types]; |
| 92 | + }); |
| 93 | + return createDefaultCheqdRegistry(registryTypes); |
| 94 | + } |
| 95 | + |
| 96 | + private async loadQuerierExtensions(): Promise< |
| 97 | + CheqdQuerier & DidExtension & ResourceExtension & FeemarketExtension |
| 98 | + > { |
| 99 | + const querierExtensions = this.options.modules.map((module) => |
| 100 | + instantiateCheqdSDKModuleQuerierExtensionSetup(module) |
| 101 | + ); |
| 102 | + const querier = await CheqdQuerier.connectWithExtensions(this.options.rpcUrl, ...querierExtensions); |
| 103 | + return <CheqdQuerier & DidExtension & ResourceExtension & FeemarketExtension>querier; |
| 104 | + } |
| 105 | + |
| 106 | + async build(): Promise<CheqdSDK> { |
| 107 | + const registry = this.loadRegistry(); |
| 108 | + |
| 109 | + this.querier = await this.loadQuerierExtensions(); |
| 110 | + this.signer = await CheqdSigningStargateClient.connectWithSigner(this.options.rpcUrl, this.options.wallet, { |
| 111 | + registry, |
| 112 | + gasPrice: this.options?.gasPrice, |
| 113 | + }); |
| 114 | + |
| 115 | + return await this.loadModules(this.options.modules); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +export function filterUnauthorizedMethods( |
| 120 | + methods: IModuleMethodMap, |
| 121 | + authorizedMethods: string[], |
| 122 | + protectedMethods: string[] |
| 123 | +): IModuleMethodMap { |
| 124 | + let _methods = Object.keys(methods); |
| 125 | + if (authorizedMethods.length === 0) |
| 126 | + return _methods |
| 127 | + .filter((method) => !protectedMethods.includes(method)) |
| 128 | + .reduce((acc, method) => ({ ...acc, [method]: methods[method] }), {}); |
| 129 | + |
| 130 | + return _methods |
| 131 | + .filter((method) => authorizedMethods.includes(method) && !protectedMethods.includes(method)) |
| 132 | + .reduce((acc, method) => ({ ...acc, [method]: methods[method] }), {}); |
| 133 | +} |
| 134 | + |
| 135 | +export async function createCheqdSDK(options: ICheqdSDKOptions): Promise<CheqdSDK> { |
| 136 | + return await new CheqdSDK(options).build(); |
| 137 | +} |
| 138 | + |
| 139 | +export { DIDModule, ResourceModule, FeemarketModule }; |
| 140 | +export { AbstractCheqdSDKModule, applyMixins } from './modules/_'; |
| 141 | +export { |
| 142 | + DidExtension, |
| 143 | + MinimalImportableDIDModule, |
| 144 | + MsgCreateDidDocEncodeObject, |
| 145 | + MsgCreateDidDocResponseEncodeObject, |
| 146 | + MsgUpdateDidDocEncodeObject, |
| 147 | + MsgUpdateDidDocResponseEncodeObject, |
| 148 | + MsgDeactivateDidDocEncodeObject, |
| 149 | + MsgDeactivateDidDocResponseEncodeObject, |
| 150 | + contexts, |
| 151 | + defaultDidExtensionKey, |
| 152 | + protobufLiterals as protobufLiteralsDid, |
| 153 | + typeUrlMsgCreateDidDoc, |
| 154 | + typeUrlMsgCreateDidDocResponse, |
| 155 | + typeUrlMsgUpdateDidDoc, |
| 156 | + typeUrlMsgUpdateDidDocResponse, |
| 157 | + typeUrlMsgDeactivateDidDoc, |
| 158 | + typeUrlMsgDeactivateDidDocResponse, |
| 159 | + setupDidExtension, |
| 160 | + isMsgCreateDidDocEncodeObject, |
| 161 | + isMsgUpdateDidDocEncodeObject, |
| 162 | + isMsgDeactivateDidDocEncodeObject, |
| 163 | +} from './modules/did'; |
| 164 | +export { |
| 165 | + ResourceExtension, |
| 166 | + MinimalImportableResourceModule, |
| 167 | + defaultResourceExtensionKey, |
| 168 | + protobufLiterals as protobufLiteralsResource, |
| 169 | + typeUrlMsgCreateResource, |
| 170 | + typeUrlMsgCreateResourceResponse, |
| 171 | + setupResourceExtension, |
| 172 | + isMsgCreateResourceEncodeObject, |
| 173 | +} from './modules/resource'; |
| 174 | +export { |
| 175 | + FeemarketExtension, |
| 176 | + MinimalImportableFeemarketModule, |
| 177 | + defaultFeemarketExtensionKey, |
| 178 | + protobufLiterals as protobufLiteralsFeemarket, |
| 179 | + typeUrlGasPriceResponse, |
| 180 | + typeUrlGasPricesResponse, |
| 181 | + typeUrlParamsResponse, |
| 182 | + setupFeemarketExtension, |
| 183 | + isGasPriceEncodeObject, |
| 184 | + isGasPricesEncodeObject, |
| 185 | + isParamsEncodeObject, |
| 186 | +} from './modules/feemarket'; |
| 187 | +export * from './signer'; |
| 188 | +export * from './querier'; |
| 189 | +export * from './registry'; |
| 190 | +export * from './types'; |
| 191 | +export { |
| 192 | + TImportableEd25519Key, |
| 193 | + createKeyPairRaw, |
| 194 | + createKeyPairBase64, |
| 195 | + createKeyPairHex, |
| 196 | + createVerificationKeys, |
| 197 | + createDidVerificationMethod, |
| 198 | + createDidPayload, |
| 199 | + createSignInputsFromImportableEd25519Key, |
| 200 | + validateSpecCompliantPayload, |
| 201 | + isEqualKeyValuePair, |
| 202 | + createCosmosPayerWallet, |
| 203 | + getCosmosAccount, |
| 204 | + checkBalance, |
| 205 | + toMultibaseRaw, |
| 206 | +} from './utils'; |
0 commit comments