Skip to content

Commit

Permalink
npm run format
Browse files Browse the repository at this point in the history
  • Loading branch information
ankurdotb committed Sep 26, 2023
1 parent c146ed7 commit ff28ca4
Show file tree
Hide file tree
Showing 18 changed files with 3,928 additions and 3,456 deletions.
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"useTabs": true,
"tabWidth": 4,
"printWidth": 120,
"proseWrap": "always",
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
}
20 changes: 18 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"build": "rm -rf build && npm run build:types && npm run build:esm && npm run build:cjs",
"build:types": "tsc -p tsconfig.types.json",
"build:esm": "tsc -p tsconfig.esm.json",
"build:cjs": "echo 'Experimental backwards compatibility! Use v2 major version or `@cjs` npm tag for deep CommonJS exports.' && tsc -p tsconfig.cjs.json"
"build:cjs": "echo 'Experimental backwards compatibility! Use v2 major version or `@cjs` npm tag for deep CommonJS exports.' && tsc -p tsconfig.cjs.json",
"format": "prettier --write 'src/**/*.{js,ts,cjs,mjs}' 'tests/**/*.{js,ts,cjs,mjs}'"
},
"repository": "https://github.com/cheqd/sdk.git",
"keywords": [
Expand Down Expand Up @@ -84,6 +85,7 @@
"cross-env": "^7.0.3",
"jest": "^29.7.0",
"long": "^4.0.0",
"prettier": "^3.0.3",
"semantic-release": "^22.0.5",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
Expand Down
188 changes: 91 additions & 97 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,146 +1,140 @@
import {
OfflineSigner,
Registry
} from '@cosmjs/proto-signing'
import {
DIDModule,
MinimalImportableDIDModule,
DidExtension
} from './modules/did.js';
import {
MinimalImportableResourceModule,
ResourceModule,
ResourceExtension
} from './modules/resource.js';
import { OfflineSigner, Registry } from '@cosmjs/proto-signing';
import { DIDModule, MinimalImportableDIDModule, DidExtension } from './modules/did.js';
import { MinimalImportableResourceModule, ResourceModule, ResourceExtension } from './modules/resource.js';
import {
AbstractCheqdSDKModule,
applyMixins,
instantiateCheqdSDKModule,
instantiateCheqdSDKModuleRegistryTypes,
instantiateCheqdSDKModuleQuerierExtensionSetup
instantiateCheqdSDKModuleQuerierExtensionSetup,
} from './modules/_.js';
import { createDefaultCheqdRegistry } from './registry.js'
import { CheqdSigningStargateClient } from './signer.js'
import {
CheqdNetwork,
IContext,
IModuleMethodMap
} from './types.js';
import {
GasPrice,
QueryClient
} from '@cosmjs/stargate'
import { CheqdQuerier } from './querier.js'
import { Tendermint34Client } from '@cosmjs/tendermint-rpc'
import { createDefaultCheqdRegistry } from './registry.js';
import { CheqdSigningStargateClient } from './signer.js';
import { CheqdNetwork, IContext, IModuleMethodMap } from './types.js';
import { GasPrice, QueryClient } from '@cosmjs/stargate';
import { CheqdQuerier } from './querier.js';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';

export interface ICheqdSDKOptions {
modules: AbstractCheqdSDKModule[]
querierExtensions?: Record<string, any>[]
rpcUrl: string
network?: CheqdNetwork
gasPrice?: GasPrice
authorizedMethods?: string[]
readonly wallet: OfflineSigner
modules: AbstractCheqdSDKModule[];
querierExtensions?: Record<string, any>[];
rpcUrl: string;
network?: CheqdNetwork;
gasPrice?: GasPrice;
authorizedMethods?: string[];
readonly wallet: OfflineSigner;
}

export type DefaultCheqdSDKModules = MinimalImportableDIDModule & MinimalImportableResourceModule
export type DefaultCheqdSDKModules = MinimalImportableDIDModule & MinimalImportableResourceModule;

export interface CheqdSDK extends DefaultCheqdSDKModules {}

export class CheqdSDK {
methods: IModuleMethodMap
signer: CheqdSigningStargateClient
querier: CheqdQuerier & DidExtension & ResourceExtension
options: ICheqdSDKOptions
private protectedMethods: string[] = ['constructor', 'build', 'loadModules', 'loadRegistry']
methods: IModuleMethodMap;
signer: CheqdSigningStargateClient;
querier: CheqdQuerier & DidExtension & ResourceExtension;
options: ICheqdSDKOptions;
private protectedMethods: string[] = ['constructor', 'build', 'loadModules', 'loadRegistry'];

constructor(options: ICheqdSDKOptions) {
if (!options?.wallet) {
throw new Error('No wallet provided')
throw new Error('No wallet provided');
}

this.options = {
authorizedMethods: [],
network: CheqdNetwork.Testnet,
...options
}
...options,
};

this.methods = {}
this.signer = new CheqdSigningStargateClient(undefined, this.options.wallet, {})
this.querier = <any> new QueryClient({} as unknown as Tendermint34Client)
this.methods = {};
this.signer = new CheqdSigningStargateClient(undefined, this.options.wallet, {});
this.querier = <any>new QueryClient({} as unknown as Tendermint34Client);
}

async execute<P = any, R = any>(method: string, ...params: P[]): Promise<R> {
if (!Object.keys(this.methods).includes(method)) {
throw new Error(`Method ${method} is not authorized`)
throw new Error(`Method ${method} is not authorized`);
}
return await this.methods[method](...params, { sdk: this } as IContext)
return await this.methods[method](...params, { sdk: this } as IContext);
}

private async loadModules(modules: AbstractCheqdSDKModule[]): Promise<CheqdSDK> {
this.options.modules = this.options.modules.map((module: any) => instantiateCheqdSDKModule(module, this.signer, this.querier, { sdk: this } as IContext) as unknown as AbstractCheqdSDKModule)

const methods = applyMixins(this, modules)
this.methods = { ...this.methods, ...filterUnauthorizedMethods(methods, this.options.authorizedMethods || [], this.protectedMethods) }
this.options.modules = this.options.modules.map(
(module: any) =>
instantiateCheqdSDKModule(module, this.signer, this.querier, {
sdk: this,
} as IContext) as unknown as AbstractCheqdSDKModule
);

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: any[]) => {
return await this.execute(method, ...params)
}
return await this.execute(method, ...params);
};
}

return this
return this;
}

private loadRegistry(): Registry {
const registryTypes = this.options.modules.map((module: any) => instantiateCheqdSDKModuleRegistryTypes(module)).reduce((acc, types) => {
return [...acc, ...types]
})
return createDefaultCheqdRegistry(registryTypes)
}
private loadRegistry(): Registry {
const registryTypes = this.options.modules
.map((module: any) => instantiateCheqdSDKModuleRegistryTypes(module))
.reduce((acc, types) => {
return [...acc, ...types];
});
return createDefaultCheqdRegistry(registryTypes);
}

private async loadQuerierExtensions(): Promise<CheqdQuerier & DidExtension & ResourceExtension> {
const querierExtensions = this.options.modules.map((module: any) => instantiateCheqdSDKModuleQuerierExtensionSetup(module))
const querier = await CheqdQuerier.connectWithExtensions(this.options.rpcUrl, ...querierExtensions)
return <CheqdQuerier & DidExtension & ResourceExtension>querier
const querierExtensions = this.options.modules.map((module: any) =>
instantiateCheqdSDKModuleQuerierExtensionSetup(module)
);
const querier = await CheqdQuerier.connectWithExtensions(this.options.rpcUrl, ...querierExtensions);
return <CheqdQuerier & DidExtension & ResourceExtension>querier;
}

async build(): Promise<CheqdSDK> {
const registry = this.loadRegistry()

this.querier = await this.loadQuerierExtensions()
this.signer = await CheqdSigningStargateClient.connectWithSigner(
this.options.rpcUrl,
this.options.wallet,
{
registry,
gasPrice: this.options?.gasPrice,
}
)

return await this.loadModules(this.options.modules)
const registry = this.loadRegistry();

this.querier = await this.loadQuerierExtensions();
this.signer = await CheqdSigningStargateClient.connectWithSigner(this.options.rpcUrl, this.options.wallet, {
registry,
gasPrice: this.options?.gasPrice,
});

return await this.loadModules(this.options.modules);
}
}

export function filterUnauthorizedMethods(methods: IModuleMethodMap, authorizedMethods: string[], protectedMethods: string[]): IModuleMethodMap {
let _methods = Object.keys(methods)
export function filterUnauthorizedMethods(
methods: IModuleMethodMap,
authorizedMethods: string[],
protectedMethods: string[]
): IModuleMethodMap {
let _methods = Object.keys(methods);
if (authorizedMethods.length === 0)
return _methods
.filter(method => !protectedMethods.includes(method))
.reduce((acc, method) => ({ ...acc, [method]: methods[method] }), {})
.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] }), {})
.filter((method) => authorizedMethods.includes(method) && !protectedMethods.includes(method))
.reduce((acc, method) => ({ ...acc, [method]: methods[method] }), {});
}

export async function createCheqdSDK(options: ICheqdSDKOptions): Promise<CheqdSDK> {
return await (new CheqdSDK(options)).build()
return await new CheqdSDK(options).build();
}

export { DIDModule, ResourceModule }
export { AbstractCheqdSDKModule, applyMixins } from './modules/_.js'
export { DIDModule, ResourceModule };
export { AbstractCheqdSDKModule, applyMixins } from './modules/_.js';
export {
DidExtension,
MinimalImportableDIDModule,
Expand All @@ -163,7 +157,7 @@ export {
isMsgCreateDidDocEncodeObject,
isMsgUpdateDidDocEncodeObject,
isMsgDeactivateDidDocEncodeObject,
} from './modules/did.js'
} from './modules/did.js';
export {
ResourceExtension,
MinimalImportableResourceModule,
Expand All @@ -173,14 +167,14 @@ export {
typeUrlMsgCreateResourceResponse,
setupResourceExtension,
isMsgCreateResourceEncodeObject,
} from './modules/resource.js'
export * from './signer.js'
export * from './querier.js'
export * from './registry.js'
export * from './types.js'
} from './modules/resource.js';
export * from './signer.js';
export * from './querier.js';
export * from './registry.js';
export * from './types.js';
export {
TImportableEd25519Key,
createKeyPairRaw,
createKeyPairRaw,
createKeyPairBase64,
createKeyPairHex,
createVerificationKeys,
Expand All @@ -189,8 +183,8 @@ export {
createSignInputsFromImportableEd25519Key,
validateSpecCompliantPayload,
isEqualKeyValuePair,
createCosmosPayerWallet,
createCosmosPayerWallet,
getCosmosAccount,
checkBalance,
toMultibaseRaw
} from './utils.js'
toMultibaseRaw,
} from './utils.js';
Loading

0 comments on commit ff28ca4

Please sign in to comment.