Skip to content

Commit

Permalink
Merge pull request #43 from cosmology-tech/unify-doc-auth
Browse files Browse the repository at this point in the history
Unify doc auth
  • Loading branch information
Zetazzz authored Sep 25, 2024
2 parents 228946e + 3bc52b7 commit 4c39c2b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
16 changes: 10 additions & 6 deletions networks/cosmos/src/base/base-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
BroadcastOptions,
HttpEndpoint,
IAccount,
IDocSigner,
IKey,
isDocAuth,
ISigBuilder,
Expand Down Expand Up @@ -39,9 +40,12 @@ import { BaseCosmosTxBuilder } from './tx-builder';
/**
* Base class for Cosmos Doc Signer.
* It provides the basic methods for signing a document.
* @template SignDoc - The type of the document to be signed.
* @template TDoc - The type of the document to be signed.
* * @template TArgs The type of the args.
*/
export abstract class CosmosDocSigner<SignDoc> extends BaseSigner {
export abstract class CosmosDocSigner<TDoc, TArgs = unknown> extends BaseSigner
implements IDocSigner<TDoc, TArgs, SignDocResponse<TDoc>>
{
constructor(auth: Auth, config: SignerConfig) {
super(auth, config);

Expand All @@ -51,18 +55,18 @@ export abstract class CosmosDocSigner<SignDoc> extends BaseSigner {
/**
* signature builder
*/
txBuilder: ISigBuilder<SignDoc, IKey>;
txBuilder: ISigBuilder<TDoc, IKey>;

/**
* abstract method to get the signature builder
*/
abstract getTxBuilder(): ISigBuilder<SignDoc, IKey>;
abstract getTxBuilder(): ISigBuilder<TDoc, IKey>;

/**
* Sign a document.
*/
async signDoc(doc: SignDoc): Promise<SignDocResponse<SignDoc>> {
if (isDocAuth<SignDoc>(this.auth)) {
async signDoc(doc: TDoc): Promise<SignDocResponse<TDoc>> {
if (isDocAuth<TDoc>(this.auth)) {
return await this.auth.signDoc(doc);
} else {
const sig = await this.txBuilder.buildSignature(doc);
Expand Down
8 changes: 4 additions & 4 deletions networks/cosmos/src/types/docAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { CosmosAminoDoc, CosmosDirectDoc, ICosmosGeneralOfflineSigner } from './
/**
* Base class for Doc Auth.
*/
export abstract class BaseDocAuth<Signer, Doc> implements DocAuth<Doc> {
export abstract class BaseDocAuth<TSigner, TDoc, TArgs = unknown, TAddr = string> implements DocAuth<TDoc, TArgs, TAddr> {
constructor(
public readonly algo: string,
public readonly address: string,
public readonly address: TAddr,
public readonly pubkey: Uint8Array,
public readonly offlineSigner: Signer
public readonly offlineSigner: TSigner
) {
this.algo = algo;
this.address = address;
Expand All @@ -29,7 +29,7 @@ export abstract class BaseDocAuth<Signer, Doc> implements DocAuth<Doc> {
getPublicKey(): IKey {
return Key.from(this.pubkey);
}
abstract signDoc(doc: Doc): Promise<SignDocResponse<Doc>>;
abstract signDoc(doc: TDoc, args?: TArgs): Promise<SignDocResponse<TDoc>>;
}

/**
Expand Down
12 changes: 7 additions & 5 deletions packages/types/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,21 @@ export function isByteAuth<Sig>(auth: Auth): auth is ByteAuth<Sig> {
* DocAuth is an interface that represents the authentication method of an account that can sign a document using OfflineSigners.
* DocAuth is actually a wrapper for offline signers.
* DocAuth is usually used by signers built from offline signers.
* @template Doc The type of the document.
* @template TDoc The type of the doc.
* @template TArgs The type of the args.
* @template TAddr The type of the address.
*/
export interface DocAuth<Doc> extends Auth {
address: string;
signDoc(doc: Doc): SignDocResponse<Doc> | Promise<SignDocResponse<Doc>>;
export interface DocAuth<TDoc, TArgs = unknown, TAddr = string> extends Auth {
address: TAddr;
signDoc(doc: TDoc, args?: TArgs): SignDocResponse<TDoc> | Promise<SignDocResponse<TDoc>>;
}

/**
* Check if the authentication object is a DocAuth.
* @param auth The object to check
* @returns Whether the object is a DocAuth.
*/
export function isDocAuth<Doc>(auth: Auth): auth is DocAuth<Doc> {
export function isDocAuth<TDoc, TArgs = unknown, TAddr = string>(auth: Auth): auth is DocAuth<TDoc, TArgs, TAddr> {
return 'signDoc' in auth;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/types/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ export interface ITxBuilderContext<Signer = unknown> {
signer?: Signer;
}

/**
* IDocSigner is an interface for signing document.
* @template TDoc - document type
* @template TArgs - arguments type
*/
export interface IDocSigner<TDoc, TArgs = unknown, TResp = SignDocResponse<TDoc>> {
/**
* sign document.
* @param doc - document to be signed
* @param args - arguments for signing
*/
signDoc(doc: TDoc, args?: TArgs): Promise<TResp>;
}

/**
* UniSigner is a generic interface for signing and broadcasting transactions.
* It is used to abstract the signing and broadcasting process for different chains.
Expand Down

0 comments on commit 4c39c2b

Please sign in to comment.