Skip to content

Commit

Permalink
Merge pull request #60 from cosmology-tech/refactor-tx-builder
Browse files Browse the repository at this point in the history
Refactor tx builder
  • Loading branch information
Zetazzz authored Nov 13, 2024
2 parents f44adf7 + f9a8614 commit 93326e3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 30 deletions.
8 changes: 5 additions & 3 deletions networks/cosmos/src/base/builder-context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { AuthInfo } from '@interchainjs/cosmos-types/cosmos/tx/v1beta1/tx';
import { ITxBuilderContext } from '@interchainjs/types';
import { BaseTxBuilderContext, ITxBuilderContext } from '@interchainjs/types';

/**
* Context for the transaction builder.
*/
export class BaseCosmosTxBuilderContext<Signer>
extends BaseTxBuilderContext<Signer>
implements ITxBuilderContext<Signer>
{
constructor(public signer: Signer, public authInfo?: AuthInfo) {}
constructor(public signer: Signer) {
super(signer);
}
}
4 changes: 3 additions & 1 deletion networks/cosmos/src/base/tx-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { calculateFee, toFee } from '../utils';
import { CosmosBaseSigner } from './base-signer';
import { BaseCosmosTxBuilderContext } from './builder-context';

export const STAGING_AUTH_INFO = 'staging_auth_info';

/**
* BaseCosmosSigBuilder is a helper class to build the signature from the document
*/
Expand Down Expand Up @@ -113,7 +115,7 @@ export abstract class BaseCosmosTxBuilder<SignDoc>

const { authInfo, encode: authEncode} = await this.buildAuthInfo([signerInfo], toFee(stdFee));

this.ctx.authInfo = authInfo;
this.ctx.setStagingData(STAGING_AUTH_INFO, authInfo);

return {
bodyBytes: txBodyEncode(),
Expand Down
6 changes: 4 additions & 2 deletions networks/cosmos/src/builder/amino-tx-builder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SignMode } from '@interchainjs/cosmos-types/cosmos/tx/signing/v1beta1/signing';
import { AuthInfo } from '@interchainjs/cosmos-types/cosmos/tx/v1beta1/tx';
import { TxRaw } from '@interchainjs/cosmos-types/cosmos/tx/v1beta1/tx';
import { SignDocResponse } from '@interchainjs/types';

import { type AminoSignerBase } from '../signers/amino';
import { BaseCosmosSigBuilder, BaseCosmosTxBuilder } from '../base';
import { BaseCosmosSigBuilder, BaseCosmosTxBuilder, STAGING_AUTH_INFO } from '../base';
import { BaseCosmosTxBuilderContext } from '../base/builder-context';
import { CosmosAminoDoc, CosmosSignArgs } from '../types';
import { encodeStdSignDoc, toAminoMsgs, toFee } from '../utils';
Expand Down Expand Up @@ -61,8 +62,9 @@ export class AminoTxBuilder extends BaseCosmosTxBuilder<CosmosAminoDoc> {

async syncSignedDoc(txRaw: TxRaw, signResp: SignDocResponse<CosmosAminoDoc>): Promise<TxRaw> {
const authFee = toFee(signResp.signDoc.fee);
const authInfo = this.ctx.getStagingData<AuthInfo>(STAGING_AUTH_INFO);

const { encode: authEncode } = await this.buildAuthInfo(this.ctx.authInfo.signerInfos, authFee);
const { encode: authEncode } = await this.buildAuthInfo(authInfo.signerInfos, authFee);
const authInfoBytes = authEncode();

return {
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './hdpath';
export * from './signer';
export * from './telescope';
export * from './wallet';
export * from './tx-builder';
24 changes: 0 additions & 24 deletions packages/types/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,6 @@ export interface SignResponse<Tx, Doc, BroadcastResponse = { hash: string }>
broadcast: (options?: BroadcastOptions) => Promise<BroadcastResponse>;
}

/**
* ISigBuilder is an interface for building signature from document.
*/
export interface ISigBuilder<Doc = unknown, Sig = unknown> {
/**
* build signature from document.
*/
buildSignature(doc: Doc): Sig | Promise<Sig>;
}

/**
* ITxBuilder is an interface for building signed transaction document.
*/
export interface ITxBuilder<SignArgs = unknown, SignResp = unknown> {
buildSignedTxDoc(args: SignArgs): Promise<SignResp>;
}

/**
* ITxBuilderContext is a context object for building transaction document.
*/
export interface ITxBuilderContext<Signer = unknown> {
signer?: Signer;
}

/**
* IDocSigner is an interface for signing document.
* @template TDoc - document type
Expand Down
51 changes: 51 additions & 0 deletions packages/types/src/tx-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* ISigBuilder is an interface for building signature from document.
*/
export interface ISigBuilder<Doc = unknown, Sig = unknown> {
/**
* build signature from document.
*/
buildSignature(doc: Doc): Sig | Promise<Sig>;
}

/**
* ITxBuilder is an interface for building signed transaction document.
*/
export interface ITxBuilder<SignArgs = unknown, SignResp = unknown> {
buildSignedTxDoc(args: SignArgs): Promise<SignResp>;
}

/**
* ITxBuilderContext is a context object for building transaction document.
*/
export interface ITxBuilderContext<Signer = unknown> {
signer?: Signer;

/**
* set staging data.
* @param data - staging data
*/
setStagingData(key: string, data: unknown): void;

/**
* get staging data.
*/
getStagingData<TStaging>(key: string): TStaging;
}

/**
* BaseTxBuilderContext is a base class for ITxBuilderContext.
*/
export class BaseTxBuilderContext<Signer> implements ITxBuilderContext<Signer> {
private stagingData: Record<string, unknown> = {};

constructor(public signer?: Signer) {}

setStagingData(key: string, data: unknown): void {
this.stagingData[key] = data;
}

getStagingData<TStaging>(key: string): TStaging {
return this.stagingData[key] as TStaging;
}
}

0 comments on commit 93326e3

Please sign in to comment.