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(sdk): Added base constructor manager class
- Loading branch information
1 parent
9c24549
commit 441bfa3
Showing
10 changed files
with
1,214 additions
and
28 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,12 +1,61 @@ | ||
import { DIDModule } from './modules/did'; | ||
import { ResourcesModule } from './modules/resources'; | ||
import { OfflineSigner } from '@cosmjs/proto-signing' | ||
import { DIDModule } from './modules/did' | ||
import { ResourcesModule } from './modules/resources' | ||
import { AbstractCheqdSDKModule, applyMixins, } from './modules/_' | ||
import { CheqdSigningStargateClient } from './signer' | ||
import { CheqdNetwork } from './types' | ||
|
||
class CheqdAPI { | ||
constructor() { | ||
// | ||
export interface ICheqdSDKOptions { | ||
modules: AbstractCheqdSDKModule[] | ||
authorizedMethods?: string[] | ||
network?: CheqdNetwork | ||
rpcUrl: string | ||
readonly wallet: OfflineSigner | ||
} | ||
|
||
export interface CheqdSDK extends DIDModule, ResourcesModule {} | ||
|
||
export class CheqdSDK { | ||
methods: string[] | ||
signer: CheqdSigningStargateClient | ||
options: ICheqdSDKOptions | ||
private protectedMethods: string[] = ['build', 'loadModules'] | ||
|
||
constructor(options: ICheqdSDKOptions) { | ||
if (!options?.wallet) { | ||
throw new Error('No wallet provided') | ||
} | ||
|
||
this.options = { | ||
authorizedMethods: [], | ||
network: CheqdNetwork.Testnet, | ||
...options | ||
} | ||
|
||
this.methods = this.options.authorizedMethods || [] | ||
this.signer = new CheqdSigningStargateClient(undefined, this.options.wallet, {}) | ||
} | ||
|
||
private loadModules(modules: AbstractCheqdSDKModule[]): CheqdSDK { | ||
const methods = applyMixins(this, modules) | ||
this.methods = this.methods.concat(methods) | ||
|
||
return this | ||
} | ||
|
||
async build() { | ||
this.signer = await CheqdSigningStargateClient.connectWithSigner( | ||
this.options.rpcUrl, | ||
this.options.wallet | ||
) | ||
this.options.modules = this.options.modules.map(module => module.constructor(this.signer)) || [] | ||
|
||
return this.loadModules(this.options.modules) | ||
} | ||
} | ||
|
||
export default CheqdAPI | ||
export async function createCheqdSDK(options: ICheqdSDKOptions): Promise<CheqdSDK> { | ||
return await (new CheqdSDK(options)).build() | ||
} | ||
|
||
export { DIDModule } | ||
export { DIDModule, ResourcesModule } |
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,51 @@ | ||
import { QueryClient } from "@cosmjs/stargate"; | ||
import { CheqdSigningStargateClient } from '../signer' | ||
import { setupDidExtension } from './did' | ||
|
||
export abstract class AbstractCheqdSDKModule { | ||
_signer: CheqdSigningStargateClient | ||
|
||
constructor(signer: CheqdSigningStargateClient) { | ||
if (!signer) { | ||
throw new Error("signer is required"); | ||
} | ||
this._signer = signer | ||
} | ||
} | ||
|
||
export function applyMixins(derivedCtor: any, constructors: any[]): string[] { | ||
let methods: string[] = [] | ||
|
||
constructors.forEach((baseCtor) => { | ||
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => { | ||
const property = Object.getOwnPropertyDescriptor(baseCtor.prototype, name) | ||
if (typeof property !== 'function' || name in derivedCtor.prototype || derivedCtor?.protectedMethods.includes(name)) return | ||
|
||
Object.defineProperty( | ||
derivedCtor.prototype, | ||
name, | ||
property || | ||
Object.create(null) | ||
); | ||
|
||
methods.push(name) | ||
}); | ||
}); | ||
|
||
return methods | ||
} | ||
|
||
export type CheqdExtension<K extends string, V = any> = { | ||
[P in K]: (Record<P, V> & Partial<Record<Exclude<K, P>, never>>) extends infer O | ||
? { [Q in keyof O]: O[Q] } | ||
: never | ||
}[K] | ||
|
||
export type CheqdExtensions = CheqdExtension<'did' | 'resources', any> | ||
|
||
export const setupCheqdExtensions = (base: QueryClient): CheqdExtensions => { | ||
return { | ||
...setupDidExtension(base), | ||
/** setupResourcesExtension(base) */ | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,34 @@ | ||
import { createProtobufRpcClient, DeliverTxResponse, QueryClient } from "@cosmjs/stargate" | ||
import { QueryClientImpl } from '@cheqd/ts-proto/cheqd/v1/query' | ||
import { CheqdExtension, AbstractCheqdSDKModule } from "./_" | ||
import { CheqdSigningStargateClient } from "../signer" | ||
|
||
export class DIDModule extends AbstractCheqdSDKModule { | ||
constructor(signer: CheqdSigningStargateClient){ | ||
super(signer) | ||
} | ||
|
||
async createDidTx(did: string, publicKey: string): Promise<string> { | ||
return '' | ||
} | ||
|
||
async updateDidTx(did: string, publicKey: string): Promise<string> { | ||
return '' | ||
} | ||
} | ||
|
||
export interface DidExtension extends CheqdExtension<string, {}> { | ||
did: {} | ||
} | ||
|
||
export const setupDidExtension = (base: QueryClient): DidExtension => { | ||
const rpc = createProtobufRpcClient(base) | ||
|
||
const queryService = new QueryClientImpl(rpc) | ||
|
||
export class DIDModule { | ||
constructor(){ | ||
// | ||
return { | ||
did: { | ||
//... | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { AbstractCheqdSDKModule } from "./_" | ||
import { CheqdSigningStargateClient } from "../signer" | ||
|
||
|
||
export class ResourcesModule { | ||
constructor() { | ||
// | ||
export class ResourcesModule extends AbstractCheqdSDKModule { | ||
constructor(signer: CheqdSigningStargateClient) { | ||
super(signer) | ||
} | ||
} |
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,71 @@ | ||
import { | ||
Registry, | ||
GeneratedType, | ||
EncodeObject | ||
} from '@cosmjs/proto-signing' | ||
|
||
import { | ||
defaultRegistryTypes | ||
} from '@cosmjs/stargate' | ||
|
||
import { | ||
MsgCreateDid, MsgCreateDidResponse, MsgUpdateDid, MsgUpdateDidResponse | ||
} from '@cheqd/ts-proto/cheqd/v1/tx' | ||
|
||
export const typeUrlMsgCreateDid = '/cheqdid.cheqdnode.cheqd.v1.MsgCreateDid' | ||
export const typeUrlMsgCreateDidResponse = '/cheqdid.cheqdnode.cheqd.v1.MsgCreateDidResponse' | ||
export const typeUrlMsgUpdateDid = '/cheqdid.cheqdnode.cheqd.v1.MsgUpdateDid' | ||
export const typeUrlMsgUpdateDidResponse = '/cheqdid.cheqdnode.cheqd.v1.MsgUpdateDidResponse' | ||
|
||
const defaultCheqdRegistryTypes: Iterable<[string, GeneratedType]> = [ | ||
...defaultRegistryTypes, | ||
|
||
/** Move them as registrations to each module's constructor. */ | ||
|
||
[typeUrlMsgCreateDid, MsgCreateDid], | ||
[typeUrlMsgCreateDidResponse, MsgCreateDidResponse], | ||
[typeUrlMsgUpdateDid, MsgUpdateDid], | ||
[typeUrlMsgUpdateDidResponse, MsgUpdateDidResponse], | ||
] | ||
|
||
export function createDefaultCheqdRegistry(): Registry { | ||
return new Registry(defaultCheqdRegistryTypes) | ||
} | ||
|
||
export const CheqdRegistry = new Registry(defaultCheqdRegistryTypes) | ||
|
||
export interface MsgCreateDidEncodeObject extends EncodeObject { | ||
readonly typeUrl: typeof typeUrlMsgCreateDid, | ||
readonly value: MsgCreateDid | ||
} | ||
|
||
export function isMsgCreateDidEncodeObject(obj: EncodeObject): obj is MsgCreateDidEncodeObject { | ||
return obj.typeUrl === typeUrlMsgCreateDid | ||
} | ||
|
||
export interface MsgCreateDidResponseEncodeObject extends EncodeObject { | ||
readonly typeUrl: typeof typeUrlMsgCreateDidResponse, | ||
readonly value: MsgCreateDidResponse | ||
} | ||
|
||
export function MsgCreateDidResponseEncodeObject(obj: EncodeObject): obj is MsgCreateDidResponseEncodeObject { | ||
return obj.typeUrl === typeUrlMsgCreateDidResponse | ||
} | ||
|
||
export interface MsgUpdateDidEncodeObject extends EncodeObject { | ||
readonly typeUrl: typeof typeUrlMsgUpdateDid, | ||
readonly value: MsgUpdateDid | ||
} | ||
|
||
export function MsgUpdateDidEncodeObject(obj: EncodeObject): obj is MsgUpdateDidEncodeObject { | ||
return obj.typeUrl === typeUrlMsgUpdateDid | ||
} | ||
|
||
export interface MsgUpdateDidResponseEncodeObject extends EncodeObject { | ||
readonly typeUrl: typeof typeUrlMsgUpdateDidResponse, | ||
readonly value: MsgUpdateDidResponse | ||
} | ||
|
||
export function MsgUpdateDidResponseEncodeObject(obj: EncodeObject): obj is MsgUpdateDidResponseEncodeObject { | ||
return obj.typeUrl === typeUrlMsgUpdateDidResponse | ||
} |
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,32 @@ | ||
import { CheqdExtensions, setupCheqdExtensions } from './modules/_' | ||
import { EncodeObject, OfflineSigner } from "@cosmjs/proto-signing"; | ||
import { DeliverTxResponse, HttpEndpoint, QueryClient, SigningStargateClient, SigningStargateClientOptions, StdFee } from "@cosmjs/stargate"; | ||
import { Tendermint34Client } from "@cosmjs/tendermint-rpc"; | ||
import { createDefaultCheqdRegistry } from "./registry"; | ||
|
||
|
||
export class CheqdSigningStargateClient extends SigningStargateClient { | ||
public readonly cheqdExtensions: CheqdExtensions | undefined | ||
|
||
public static async connectWithSigner(endpoint: string | HttpEndpoint, signer: OfflineSigner, options?: SigningStargateClientOptions | undefined): Promise<CheqdSigningStargateClient> { | ||
const tmClient = await Tendermint34Client.connect(endpoint) | ||
return new CheqdSigningStargateClient(tmClient, signer, { | ||
registry: createDefaultCheqdRegistry(), | ||
...options | ||
}); | ||
} | ||
|
||
constructor( | ||
tmClient: Tendermint34Client | undefined, | ||
signer: OfflineSigner, | ||
options: SigningStargateClientOptions = {} | ||
) { | ||
super(tmClient, signer, options) | ||
|
||
/** GRPC Connection */ | ||
|
||
/* if (tmClient) { | ||
this.cheqdExtensions = QueryClient.withExtensions(tmClient, setupCheqdExtensions) | ||
} */ | ||
} | ||
} |
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,4 @@ | ||
export enum CheqdNetwork { | ||
Mainnet = 'mainnet', | ||
Testnet = 'testnet', | ||
} |