diff --git a/networks/cosmos/src/base/builder-context.ts b/networks/cosmos/src/base/builder-context.ts index ce739acc..a1cb5325 100644 --- a/networks/cosmos/src/base/builder-context.ts +++ b/networks/cosmos/src/base/builder-context.ts @@ -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 +extends BaseTxBuilderContext implements ITxBuilderContext { - constructor(public signer: Signer, public authInfo?: AuthInfo) {} + constructor(public signer: Signer) { + super(signer); + } } diff --git a/networks/cosmos/src/base/tx-builder.ts b/networks/cosmos/src/base/tx-builder.ts index 3f758352..a305e49d 100644 --- a/networks/cosmos/src/base/tx-builder.ts +++ b/networks/cosmos/src/base/tx-builder.ts @@ -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 */ @@ -113,7 +115,7 @@ export abstract class BaseCosmosTxBuilder const { authInfo, encode: authEncode} = await this.buildAuthInfo([signerInfo], toFee(stdFee)); - this.ctx.authInfo = authInfo; + this.ctx.setStagingData(STAGING_AUTH_INFO, authInfo); return { bodyBytes: txBodyEncode(), diff --git a/networks/cosmos/src/builder/amino-tx-builder.ts b/networks/cosmos/src/builder/amino-tx-builder.ts index b3776a46..2e53aed5 100644 --- a/networks/cosmos/src/builder/amino-tx-builder.ts +++ b/networks/cosmos/src/builder/amino-tx-builder.ts @@ -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'; @@ -61,8 +62,9 @@ export class AminoTxBuilder extends BaseCosmosTxBuilder { async syncSignedDoc(txRaw: TxRaw, signResp: SignDocResponse): Promise { const authFee = toFee(signResp.signDoc.fee); + const authInfo = this.ctx.getStagingData(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 { diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 7d4501b4..f31cee32 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -5,3 +5,4 @@ export * from './hdpath'; export * from './signer'; export * from './telescope'; export * from './wallet'; +export * from './tx-builder'; diff --git a/packages/types/src/signer.ts b/packages/types/src/signer.ts index b7ccc0ed..2b0f495d 100644 --- a/packages/types/src/signer.ts +++ b/packages/types/src/signer.ts @@ -107,30 +107,6 @@ export interface SignResponse broadcast: (options?: BroadcastOptions) => Promise; } -/** - * ISigBuilder is an interface for building signature from document. - */ -export interface ISigBuilder { - /** - * build signature from document. - */ - buildSignature(doc: Doc): Sig | Promise; -} - -/** - * ITxBuilder is an interface for building signed transaction document. - */ -export interface ITxBuilder { - buildSignedTxDoc(args: SignArgs): Promise; -} - -/** - * ITxBuilderContext is a context object for building transaction document. - */ -export interface ITxBuilderContext { - signer?: Signer; -} - /** * IDocSigner is an interface for signing document. * @template TDoc - document type diff --git a/packages/types/src/tx-builder.ts b/packages/types/src/tx-builder.ts new file mode 100644 index 00000000..0b3b0c53 --- /dev/null +++ b/packages/types/src/tx-builder.ts @@ -0,0 +1,51 @@ +/** + * ISigBuilder is an interface for building signature from document. + */ +export interface ISigBuilder { + /** + * build signature from document. + */ + buildSignature(doc: Doc): Sig | Promise; +} + +/** + * ITxBuilder is an interface for building signed transaction document. + */ +export interface ITxBuilder { + buildSignedTxDoc(args: SignArgs): Promise; +} + +/** + * ITxBuilderContext is a context object for building transaction document. + */ +export interface ITxBuilderContext { + signer?: Signer; + + /** + * set staging data. + * @param data - staging data + */ + setStagingData(key: string, data: unknown): void; + + /** + * get staging data. + */ + getStagingData(key: string): TStaging; +} + +/** + * BaseTxBuilderContext is a base class for ITxBuilderContext. + */ +export class BaseTxBuilderContext implements ITxBuilderContext { + private stagingData: Record = {}; + + constructor(public signer?: Signer) {} + + setStagingData(key: string, data: unknown): void { + this.stagingData[key] = data; + } + + getStagingData(key: string): TStaging { + return this.stagingData[key] as TStaging; + } +} \ No newline at end of file