generated from cheqd/.github
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Build esm and cjs using workspaces
- Loading branch information
1 parent
2dba14b
commit 630dfe0
Showing
46 changed files
with
16,199 additions
and
11,854 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{ | ||
"name": "@cheqd/sdk-cjs", | ||
"private": true, | ||
"version": "2.6.0", | ||
"description": "A TypeScript SDK built with CosmJS to interact with cheqd network ledger", | ||
"license": "Apache-2.0", | ||
"author": "Cheqd Foundation Limited (https://github.com/cheqd)", | ||
"source": "src/index.ts", | ||
"main": "build/index.js", | ||
"types": "build/types/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./build/types/index.d.ts", | ||
"require": "./build/index.js", | ||
"default": "./build/index.js" | ||
}, | ||
"./*": { | ||
"types": "./build/types/*.d.ts", | ||
"require": "./build/*.js", | ||
"default": "./build/*.js" | ||
} | ||
}, | ||
"scripts": { | ||
"test": "jest --colors --passWithNoTests --maxWorkers 1 --maxConcurrency 1", | ||
"test:watch": "jest --colors --passWithNoTests --maxWorkers 1 --maxConcurrency 1 --watch", | ||
"build": "npm run build:types && npm run build:cjs", | ||
"build:types": "tsc -p tsconfig.types.json", | ||
"build:cjs": "tsc -p tsconfig.json", | ||
"format": "prettier --write '**/*.{js,ts,cjs,mjs,json}'" | ||
}, | ||
"repository": "https://github.com/cheqd/sdk.git", | ||
"keywords": [ | ||
"cheqd", | ||
"sdk", | ||
"ssi", | ||
"did", | ||
"vc", | ||
"resources" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/cheqd/sdk/issues" | ||
}, | ||
"homepage": "https://github.com/cheqd/sdk#readme", | ||
"files": [ | ||
"build/**/*", | ||
"LICENSE", | ||
"package.json", | ||
"README.md" | ||
], | ||
"dependencies": { | ||
"@cheqd/ts-proto": "^2.4.0", | ||
"@cosmjs/amino": "~0.30.1", | ||
"@cosmjs/crypto": "~0.30.1", | ||
"@cosmjs/encoding": "~0.30.1", | ||
"@cosmjs/math": "~0.30.1", | ||
"@cosmjs/proto-signing": "~0.30.1", | ||
"@cosmjs/stargate": "~0.30.1", | ||
"@cosmjs/tendermint-rpc": "~0.30.1", | ||
"@cosmjs/utils": "~0.30.1", | ||
"@stablelib/ed25519": "^1.0.3", | ||
"@types/secp256k1": "^4.0.6", | ||
"cosmjs-types": "^0.7.2", | ||
"did-jwt": "^8.0.4", | ||
"did-resolver": "^4.1.0", | ||
"file-type": "^16.5.4", | ||
"long": "^4.0.0", | ||
"multiformats": "^9.9.0", | ||
"secp256k1": "^5.0.0", | ||
"uuid": "^10.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.19.47", | ||
"@types/uuid": "^10.0.0", | ||
"uint8arrays": "^3.1.1" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"engines": { | ||
"node": ">=18" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
import { OfflineSigner, Registry } from '@cosmjs/proto-signing'; | ||
import { DIDModule, MinimalImportableDIDModule, DidExtension } from './modules/did'; | ||
import { MinimalImportableResourceModule, ResourceModule, ResourceExtension } from './modules/resource'; | ||
import { | ||
AbstractCheqdSDKModule, | ||
applyMixins, | ||
instantiateCheqdSDKModule, | ||
instantiateCheqdSDKModuleRegistryTypes, | ||
instantiateCheqdSDKModuleQuerierExtensionSetup, | ||
} from './modules/_'; | ||
import { createDefaultCheqdRegistry } from './registry'; | ||
import { CheqdSigningStargateClient } from './signer'; | ||
import { CheqdNetwork, IContext, IModuleMethodMap } from './types'; | ||
import { GasPrice, QueryClient } from '@cosmjs/stargate'; | ||
import { CheqdQuerier } from './querier'; | ||
import { Tendermint37Client } from '@cosmjs/tendermint-rpc'; | ||
import { FeemarketExtension, FeemarketModule } from './modules/feemarket'; | ||
|
||
export interface ICheqdSDKOptions { | ||
modules: AbstractCheqdSDKModule[]; | ||
querierExtensions?: Record<string, any>[]; | ||
rpcUrl: string; | ||
network?: CheqdNetwork; | ||
gasPrice?: GasPrice; | ||
authorizedMethods?: string[]; | ||
readonly wallet: OfflineSigner; | ||
} | ||
|
||
export type DefaultCheqdSDKModules = MinimalImportableDIDModule & MinimalImportableResourceModule; | ||
|
||
export interface CheqdSDK extends DefaultCheqdSDKModules {} | ||
|
||
export class CheqdSDK { | ||
methods: IModuleMethodMap; | ||
signer: CheqdSigningStargateClient; | ||
querier: CheqdQuerier & DidExtension & ResourceExtension & FeemarketExtension; | ||
options: ICheqdSDKOptions; | ||
private protectedMethods: string[] = ['constructor', 'build', 'loadModules', 'loadRegistry']; | ||
|
||
constructor(options: ICheqdSDKOptions) { | ||
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 = <any>new QueryClient({} as unknown as Tendermint37Client); | ||
} | ||
|
||
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`); | ||
} | ||
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), | ||
}; | ||
|
||
for (const method of Object.keys(this.methods)) { | ||
// @ts-ignore | ||
this[method] = async (...params: any[]) => { | ||
return await this.execute(method, ...params); | ||
}; | ||
} | ||
|
||
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 async loadQuerierExtensions(): Promise< | ||
CheqdQuerier & DidExtension & ResourceExtension & FeemarketExtension | ||
> { | ||
const querierExtensions = this.options.modules.map((module) => | ||
instantiateCheqdSDKModuleQuerierExtensionSetup(module) | ||
); | ||
const querier = await CheqdQuerier.connectWithExtensions(this.options.rpcUrl, ...querierExtensions); | ||
return <CheqdQuerier & DidExtension & ResourceExtension & FeemarketExtension>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); | ||
} | ||
} | ||
|
||
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] }), {}); | ||
|
||
return _methods | ||
.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(); | ||
} | ||
|
||
export { DIDModule, ResourceModule, FeemarketModule }; | ||
export { AbstractCheqdSDKModule, applyMixins } from './modules/_'; | ||
export { | ||
DidExtension, | ||
MinimalImportableDIDModule, | ||
MsgCreateDidDocEncodeObject, | ||
MsgCreateDidDocResponseEncodeObject, | ||
MsgUpdateDidDocEncodeObject, | ||
MsgUpdateDidDocResponseEncodeObject, | ||
MsgDeactivateDidDocEncodeObject, | ||
MsgDeactivateDidDocResponseEncodeObject, | ||
contexts, | ||
defaultDidExtensionKey, | ||
protobufLiterals as protobufLiteralsDid, | ||
typeUrlMsgCreateDidDoc, | ||
typeUrlMsgCreateDidDocResponse, | ||
typeUrlMsgUpdateDidDoc, | ||
typeUrlMsgUpdateDidDocResponse, | ||
typeUrlMsgDeactivateDidDoc, | ||
typeUrlMsgDeactivateDidDocResponse, | ||
setupDidExtension, | ||
isMsgCreateDidDocEncodeObject, | ||
isMsgUpdateDidDocEncodeObject, | ||
isMsgDeactivateDidDocEncodeObject, | ||
} from './modules/did'; | ||
export { | ||
ResourceExtension, | ||
MinimalImportableResourceModule, | ||
defaultResourceExtensionKey, | ||
protobufLiterals as protobufLiteralsResource, | ||
typeUrlMsgCreateResource, | ||
typeUrlMsgCreateResourceResponse, | ||
setupResourceExtension, | ||
isMsgCreateResourceEncodeObject, | ||
} from './modules/resource'; | ||
export { | ||
FeemarketExtension, | ||
MinimalImportableFeemarketModule, | ||
defaultFeemarketExtensionKey, | ||
protobufLiterals as protobufLiteralsFeemarket, | ||
typeUrlGasPriceResponse, | ||
typeUrlGasPricesResponse, | ||
typeUrlParamsResponse, | ||
setupFeemarketExtension, | ||
isGasPriceEncodeObject, | ||
isGasPricesEncodeObject, | ||
isParamsEncodeObject, | ||
} from './modules/feemarket'; | ||
export * from './signer'; | ||
export * from './querier'; | ||
export * from './registry'; | ||
export * from './types'; | ||
export { | ||
TImportableEd25519Key, | ||
createKeyPairRaw, | ||
createKeyPairBase64, | ||
createKeyPairHex, | ||
createVerificationKeys, | ||
createDidVerificationMethod, | ||
createDidPayload, | ||
createSignInputsFromImportableEd25519Key, | ||
validateSpecCompliantPayload, | ||
isEqualKeyValuePair, | ||
createCosmosPayerWallet, | ||
getCosmosAccount, | ||
checkBalance, | ||
toMultibaseRaw, | ||
} from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { GeneratedType } from '@cosmjs/proto-signing'; | ||
import { QueryClient } from '@cosmjs/stargate'; | ||
import { CheqdSigningStargateClient } from '../signer'; | ||
import { IModuleMethodMap, QueryExtensionSetup } from '../types'; | ||
import { CheqdQuerier } from '../querier'; | ||
|
||
export abstract class AbstractCheqdSDKModule { | ||
_signer: CheqdSigningStargateClient; | ||
methods: IModuleMethodMap = {}; | ||
querier: CheqdQuerier; | ||
readonly _protectedMethods: string[] = ['constructor', 'getRegistryTypes', 'getQuerierExtensionSetup']; | ||
static readonly registryTypes: Iterable<[string, GeneratedType]> = []; | ||
static readonly querierExtensionSetup: QueryExtensionSetup<any> = (base: QueryClient) => ({}); | ||
|
||
constructor(signer: CheqdSigningStargateClient, querier: CheqdQuerier) { | ||
if (!signer) { | ||
throw new Error('signer is required'); | ||
} | ||
if (!querier) { | ||
throw new Error('querier is required'); | ||
} | ||
this._signer = signer; | ||
this.querier = querier; | ||
} | ||
|
||
abstract getRegistryTypes(): Iterable<[string, GeneratedType]>; | ||
} | ||
|
||
type ProtectedMethods<T extends AbstractCheqdSDKModule, K extends keyof T> = T[K] extends string[] | ||
? T[K][number] | ||
: T[K]; | ||
|
||
export type MinimalImportableCheqdSDKModule<T extends AbstractCheqdSDKModule> = Omit< | ||
T, | ||
| '_signer' | ||
| '_protectedMethods' | ||
| 'registryTypes' | ||
| 'querierExtensionSetup' | ||
| 'getRegistryTypes' | ||
| 'getQuerierExtensionSetup' | ||
>; | ||
|
||
export function instantiateCheqdSDKModule<T extends new (...args: any[]) => T>( | ||
module: T, | ||
...args: ConstructorParameters<T> | ||
): T { | ||
return new module(...args); | ||
} | ||
|
||
export function instantiateCheqdSDKModuleRegistryTypes(module: any): Iterable<[string, GeneratedType]> { | ||
return module.registryTypes ?? []; | ||
} | ||
|
||
export function instantiateCheqdSDKModuleQuerierExtensionSetup(module: any): QueryExtensionSetup<any> { | ||
return module.querierExtensionSetup ?? {}; | ||
} | ||
|
||
export function applyMixins(derivedCtor: any, constructors: any[]): IModuleMethodMap { | ||
let methods: IModuleMethodMap = {}; | ||
|
||
constructors.forEach((baseCtor) => { | ||
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => { | ||
const property = baseCtor.prototype[name]; | ||
if ( | ||
typeof property !== 'function' || | ||
derivedCtor.hasOwnProperty(name) || | ||
derivedCtor?.protectedMethods.includes(name) || | ||
baseCtor.prototype?._protectedMethods?.includes(name) | ||
) | ||
return; | ||
|
||
methods = { ...methods, [name]: property }; | ||
}); | ||
}); | ||
|
||
return methods; | ||
} |
Oops, something went wrong.