From b712a0ac1c1fed126a9dfaff1647798825717847 Mon Sep 17 00:00:00 2001 From: arnabghose997 Date: Thu, 2 Jun 2022 15:01:37 +0530 Subject: [PATCH 1/9] add granter field in StdFee --- packages/amino/src/signdoc.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/amino/src/signdoc.ts b/packages/amino/src/signdoc.ts index 54bf86b0..5f9eb168 100644 --- a/packages/amino/src/signdoc.ts +++ b/packages/amino/src/signdoc.ts @@ -12,6 +12,7 @@ export interface AminoMsg { export interface StdFee { readonly amount: readonly Coin[]; readonly gas: string; + readonly granter?: string; } /** From 5a40cdd3bb88d3cb5bf7f2d0b3e03c0378d2ae04 Mon Sep 17 00:00:00 2001 From: arnabghose997 Date: Thu, 2 Jun 2022 15:05:08 +0530 Subject: [PATCH 2/9] add feePayer param for assignment of granter field in AuthInfo --- packages/proto-signing/src/signing.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/proto-signing/src/signing.ts b/packages/proto-signing/src/signing.ts index 73dec600..a7322d4c 100644 --- a/packages/proto-signing/src/signing.ts +++ b/packages/proto-signing/src/signing.ts @@ -34,6 +34,7 @@ export function makeAuthInfoBytes( signers: ReadonlyArray<{ readonly pubkey: Any; readonly sequence: number }>, feeAmount: readonly Coin[], gasLimit: number, + feePayer: string, signMode = SignMode.SIGN_MODE_DIRECT, ): Uint8Array { const authInfo = { @@ -41,7 +42,8 @@ export function makeAuthInfoBytes( fee: { amount: [...feeAmount], gasLimit: Long.fromNumber(gasLimit), - }, + granter: feePayer + } }; return AuthInfo.encode(AuthInfo.fromPartial(authInfo)).finish(); } From 45ee5fe15aa7f20872e078900afbd21c87cfead9 Mon Sep 17 00:00:00 2001 From: arnabghose997 Date: Thu, 2 Jun 2022 15:11:03 +0530 Subject: [PATCH 3/9] reciprocated makeAuthInfoBytes function changes --- .../src/cosmwasmclient.searchtx.spec.ts | 3 ++- packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts | 3 ++- .../cosmwasm-stargate/src/signingcosmwasmclient.ts | 5 ++++- packages/cosmwasm-stargate/src/testutils.spec.ts | 2 ++ .../proto-signing/src/directsecp256k1hdwallet.spec.ts | 3 ++- .../proto-signing/src/directsecp256k1wallet.spec.ts | 3 ++- packages/stargate/src/signingstargateclient.ts | 5 ++++- packages/stargate/src/stargateclient.searchtx.spec.ts | 3 ++- packages/stargate/src/stargateclient.spec.ts | 11 +++++++---- packages/stargate/src/testutils.spec.ts | 2 ++ 10 files changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts index f65e2f48..c316ebc3 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts @@ -79,7 +79,8 @@ async function sendTokens( }, ]; const gasLimit = 200000; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit); + const feePayer = ""; + const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feePayer); const chainId = await client.getChainId(); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts index 669602dd..d25bffc0 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts @@ -203,7 +203,8 @@ describe("CosmWasmClient", () => { }; const txBodyBytes = registry.encode(txBody); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit); + const feePayer = "" + const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit, feePayer); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signed, signature } = await wallet.signDirect(alice.address0, signDoc); const txRaw = TxRaw.fromPartial({ diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index 2065437c..92152524 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -582,10 +582,12 @@ export class SigningCosmWasmClient extends CosmWasmClient { const signedTxBodyBytes = this.registry.encode(signedTxBody); const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber(); const signedSequence = Int53.fromString(signed.sequence).toNumber(); + const signedFeePayer = "" const signedAuthInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence: signedSequence }], signed.fee.amount, signedGasLimit, + signedFeePayer, signMode, ); return TxRaw.fromPartial({ @@ -619,7 +621,8 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const txBodyBytes = this.registry.encode(txBody); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit); + const feePayer = fee.granter == undefined ? "" : fee.granter + const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit, feePayer); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signature, signed } = await this.signer.signDirect(signerAddress, signDoc); return TxRaw.fromPartial({ diff --git a/packages/cosmwasm-stargate/src/testutils.spec.ts b/packages/cosmwasm-stargate/src/testutils.spec.ts index c4166443..05da4884 100644 --- a/packages/cosmwasm-stargate/src/testutils.spec.ts +++ b/packages/cosmwasm-stargate/src/testutils.spec.ts @@ -225,6 +225,7 @@ export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet { })); const modifiedFeeAmount = coins(3000, "ucosm"); const modifiedGasLimit = 333333; + const modifiedFeePayer = ""; const modifiedSignDoc = { ...signDoc, bodyBytes: Uint8Array.from(TxBody.encode(modifiedTxBody).finish()), @@ -232,6 +233,7 @@ export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet { signers, modifiedFeeAmount, modifiedGasLimit, + modifiedFeePayer, SignMode.SIGN_MODE_DIRECT, ), }; diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts index 0b9122f3..0aef01c1 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts @@ -261,10 +261,11 @@ describe("DirectSecp256k1HdWallet", () => { }; const fee = coins(2000, "ucosm"); const gasLimit = 200000; + const feePayer = "" const chainId = "simd-testing"; const signDoc = makeSignDoc( fromHex(bodyBytes), - makeAuthInfoBytes([{ pubkey, sequence }], fee, gasLimit), + makeAuthInfoBytes([{ pubkey, sequence }], fee, gasLimit, feePayer), chainId, accountNumber, ); diff --git a/packages/proto-signing/src/directsecp256k1wallet.spec.ts b/packages/proto-signing/src/directsecp256k1wallet.spec.ts index ab42c726..fe388eb5 100644 --- a/packages/proto-signing/src/directsecp256k1wallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1wallet.spec.ts @@ -44,9 +44,10 @@ describe("DirectSecp256k1Wallet", () => { const fee = coins(2000, "ucosm"); const gasLimit = 200000; const chainId = "simd-testing"; + const feePayer = ""; const signDoc = makeSignDoc( fromHex(bodyBytes), - makeAuthInfoBytes([{ pubkey, sequence }], fee, gasLimit), + makeAuthInfoBytes([{ pubkey, sequence }], fee, gasLimit, feePayer), chainId, accountNumber, ); diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index 9dec3293..4afece8b 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -362,10 +362,12 @@ export class SigningStargateClient extends StargateClient { const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject); const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber(); const signedSequence = Int53.fromString(signed.sequence).toNumber(); + const signedFeePayer = signed.fee.granter == undefined ? "" : signed.fee.granter const signedAuthInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence: signedSequence }], signed.fee.amount, signedGasLimit, + signedFeePayer, signMode, ); return TxRaw.fromPartial({ @@ -399,7 +401,8 @@ export class SigningStargateClient extends StargateClient { }; const txBodyBytes = this.registry.encode(txBodyEncodeObject); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit); + const feePayer = fee.granter == undefined ? "" : fee.granter + const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit, feePayer); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signature, signed } = await this.signer.signDirect(signerAddress, signDoc); return TxRaw.fromPartial({ diff --git a/packages/stargate/src/stargateclient.searchtx.spec.ts b/packages/stargate/src/stargateclient.searchtx.spec.ts index 02f0b1b4..465bb5af 100644 --- a/packages/stargate/src/stargateclient.searchtx.spec.ts +++ b/packages/stargate/src/stargateclient.searchtx.spec.ts @@ -74,7 +74,8 @@ async function sendTokens( }, ]; const gasLimit = 200000; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit); + const feePayer = ""; + const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feePayer); const chainId = await client.getChainId(); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); diff --git a/packages/stargate/src/stargateclient.spec.ts b/packages/stargate/src/stargateclient.spec.ts index 1b937656..3cc62456 100644 --- a/packages/stargate/src/stargateclient.spec.ts +++ b/packages/stargate/src/stargateclient.spec.ts @@ -364,7 +364,8 @@ describe("StargateClient", () => { const { accountNumber, sequence } = (await client.getSequence(address))!; const feeAmount = coins(2000, "ucosm"); const gasLimit = 200000; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit); + const feePayer = ""; + const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feePayer); const chainId = await client.getChainId(); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); @@ -421,7 +422,8 @@ describe("StargateClient", () => { const { accountNumber, sequence } = (await client.getSequence(address))!; const feeAmount = coins(2000, "ucosm"); const gasLimit = 200000; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, sequence); + const feePayer = ""; + const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feePayer, sequence); const chainId = await client.getChainId(); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); @@ -482,9 +484,10 @@ describe("StargateClient", () => { const chainId = await client.getChainId(); const feeAmount = coins(2000, "ucosm"); const gasLimit = 200000; + const feePayer = ""; const { accountNumber: accountNumber1, sequence: sequence1 } = (await client.getSequence(address))!; - const authInfoBytes1 = makeAuthInfoBytes([{ pubkey, sequence: sequence1 }], feeAmount, gasLimit); + const authInfoBytes1 = makeAuthInfoBytes([{ pubkey, sequence: sequence1 }], feeAmount, gasLimit, feePayer); const signDoc1 = makeSignDoc(txBodyBytes, authInfoBytes1, chainId, accountNumber1); const { signature: signature1 } = await wallet.signDirect(address, signDoc1); const txRaw1 = TxRaw.fromPartial({ @@ -498,7 +501,7 @@ describe("StargateClient", () => { assertIsDeliverTxSuccess(txResult); const { accountNumber: accountNumber2, sequence: sequence2 } = (await client.getSequence(address))!; - const authInfoBytes2 = makeAuthInfoBytes([{ pubkey, sequence: sequence2 }], feeAmount, gasLimit); + const authInfoBytes2 = makeAuthInfoBytes([{ pubkey, sequence: sequence2 }], feeAmount, gasLimit, feePayer); const signDoc2 = makeSignDoc(txBodyBytes, authInfoBytes2, chainId, accountNumber2); const { signature: signature2 } = await wallet.signDirect(address, signDoc2); const txRaw2 = TxRaw.fromPartial({ diff --git a/packages/stargate/src/testutils.spec.ts b/packages/stargate/src/testutils.spec.ts index 47f33fdc..b5ddb640 100644 --- a/packages/stargate/src/testutils.spec.ts +++ b/packages/stargate/src/testutils.spec.ts @@ -234,6 +234,7 @@ export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet { })); const modifiedFeeAmount = coins(3000, "ucosm"); const modifiedGasLimit = 333333; + const modifiedFeePayer = ""; const modifiedSignDoc = { ...signDoc, bodyBytes: Uint8Array.from(TxBody.encode(modifiedTxBody).finish()), @@ -241,6 +242,7 @@ export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet { signers, modifiedFeeAmount, modifiedGasLimit, + modifiedFeePayer, SignMode.SIGN_MODE_DIRECT, ), }; From 4e7490ee5f9a6f494e34fb91ec5987a25dd0f1cc Mon Sep 17 00:00:00 2001 From: arnabghose997 Date: Thu, 2 Jun 2022 15:33:09 +0530 Subject: [PATCH 4/9] lint fix --- .../src/cosmwasmclient.spec.ts | 2 +- .../src/signingcosmwasmclient.ts | 4 ++-- .../src/directsecp256k1hdwallet.spec.ts | 2 +- packages/proto-signing/src/signing.ts | 4 ++-- .../stargate/src/signingstargateclient.ts | 4 ++-- packages/stargate/src/stargateclient.spec.ts | 22 ++++++++++++++++--- 6 files changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts index d25bffc0..f8a446d1 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts @@ -203,7 +203,7 @@ describe("CosmWasmClient", () => { }; const txBodyBytes = registry.encode(txBody); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const feePayer = "" + const feePayer = ""; const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit, feePayer); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signed, signature } = await wallet.signDirect(alice.address0, signDoc); diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index 92152524..ef0fde37 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -582,7 +582,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { const signedTxBodyBytes = this.registry.encode(signedTxBody); const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber(); const signedSequence = Int53.fromString(signed.sequence).toNumber(); - const signedFeePayer = "" + const signedFeePayer = ""; const signedAuthInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence: signedSequence }], signed.fee.amount, @@ -621,7 +621,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const txBodyBytes = this.registry.encode(txBody); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const feePayer = fee.granter == undefined ? "" : fee.granter + const feePayer = fee.granter == undefined ? "" : fee.granter; const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit, feePayer); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signature, signed } = await this.signer.signDirect(signerAddress, signDoc); diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts index 0aef01c1..03a2f3e3 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts @@ -261,7 +261,7 @@ describe("DirectSecp256k1HdWallet", () => { }; const fee = coins(2000, "ucosm"); const gasLimit = 200000; - const feePayer = "" + const feePayer = ""; const chainId = "simd-testing"; const signDoc = makeSignDoc( fromHex(bodyBytes), diff --git a/packages/proto-signing/src/signing.ts b/packages/proto-signing/src/signing.ts index a7322d4c..26f9fddf 100644 --- a/packages/proto-signing/src/signing.ts +++ b/packages/proto-signing/src/signing.ts @@ -42,8 +42,8 @@ export function makeAuthInfoBytes( fee: { amount: [...feeAmount], gasLimit: Long.fromNumber(gasLimit), - granter: feePayer - } + granter: feePayer, + }, }; return AuthInfo.encode(AuthInfo.fromPartial(authInfo)).finish(); } diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index 4afece8b..a3ad6e7d 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -362,7 +362,7 @@ export class SigningStargateClient extends StargateClient { const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject); const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber(); const signedSequence = Int53.fromString(signed.sequence).toNumber(); - const signedFeePayer = signed.fee.granter == undefined ? "" : signed.fee.granter + const signedFeePayer = signed.fee.granter == undefined ? "" : signed.fee.granter; const signedAuthInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence: signedSequence }], signed.fee.amount, @@ -401,7 +401,7 @@ export class SigningStargateClient extends StargateClient { }; const txBodyBytes = this.registry.encode(txBodyEncodeObject); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const feePayer = fee.granter == undefined ? "" : fee.granter + const feePayer = fee.granter == undefined ? "" : fee.granter; const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit, feePayer); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signature, signed } = await this.signer.signDirect(signerAddress, signDoc); diff --git a/packages/stargate/src/stargateclient.spec.ts b/packages/stargate/src/stargateclient.spec.ts index 3cc62456..241a8543 100644 --- a/packages/stargate/src/stargateclient.spec.ts +++ b/packages/stargate/src/stargateclient.spec.ts @@ -423,7 +423,13 @@ describe("StargateClient", () => { const feeAmount = coins(2000, "ucosm"); const gasLimit = 200000; const feePayer = ""; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feePayer, sequence); + const authInfoBytes = makeAuthInfoBytes( + [{ pubkey, sequence }], + feeAmount, + gasLimit, + feePayer, + sequence, + ); const chainId = await client.getChainId(); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); @@ -487,7 +493,12 @@ describe("StargateClient", () => { const feePayer = ""; const { accountNumber: accountNumber1, sequence: sequence1 } = (await client.getSequence(address))!; - const authInfoBytes1 = makeAuthInfoBytes([{ pubkey, sequence: sequence1 }], feeAmount, gasLimit, feePayer); + const authInfoBytes1 = makeAuthInfoBytes( + [{ pubkey, sequence: sequence1 }], + feeAmount, + gasLimit, + feePayer, + ); const signDoc1 = makeSignDoc(txBodyBytes, authInfoBytes1, chainId, accountNumber1); const { signature: signature1 } = await wallet.signDirect(address, signDoc1); const txRaw1 = TxRaw.fromPartial({ @@ -501,7 +512,12 @@ describe("StargateClient", () => { assertIsDeliverTxSuccess(txResult); const { accountNumber: accountNumber2, sequence: sequence2 } = (await client.getSequence(address))!; - const authInfoBytes2 = makeAuthInfoBytes([{ pubkey, sequence: sequence2 }], feeAmount, gasLimit, feePayer); + const authInfoBytes2 = makeAuthInfoBytes( + [{ pubkey, sequence: sequence2 }], + feeAmount, + gasLimit, + feePayer, + ); const signDoc2 = makeSignDoc(txBodyBytes, authInfoBytes2, chainId, accountNumber2); const { signature: signature2 } = await wallet.signDirect(address, signDoc2); const txRaw2 = TxRaw.fromPartial({ From 3d5863be8fa5645e0497bbc3df1207a5dee35fc4 Mon Sep 17 00:00:00 2001 From: arnabghose997 Date: Tue, 14 Jun 2022 18:27:34 +0530 Subject: [PATCH 5/9] feePayer field is made optional --- packages/proto-signing/src/signing.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/proto-signing/src/signing.ts b/packages/proto-signing/src/signing.ts index 26f9fddf..303c1844 100644 --- a/packages/proto-signing/src/signing.ts +++ b/packages/proto-signing/src/signing.ts @@ -34,7 +34,7 @@ export function makeAuthInfoBytes( signers: ReadonlyArray<{ readonly pubkey: Any; readonly sequence: number }>, feeAmount: readonly Coin[], gasLimit: number, - feePayer: string, + feePayer: string | undefined, signMode = SignMode.SIGN_MODE_DIRECT, ): Uint8Array { const authInfo = { From d843da4a602c23e394dea819ae3711501399f93b Mon Sep 17 00:00:00 2001 From: arnabghose997 Date: Wed, 15 Jun 2022 18:31:51 +0530 Subject: [PATCH 6/9] granter and payer are added in the StdFee interface --- packages/amino/src/signdoc.ts | 1 + .../src/cosmwasmclient.searchtx.spec.ts | 3 ++- .../cosmwasm-stargate/src/cosmwasmclient.spec.ts | 9 ++++++++- .../src/signingcosmwasmclient.ts | 15 ++++++++++++--- packages/cosmwasm-stargate/src/testutils.spec.ts | 2 ++ .../src/directsecp256k1hdwallet.spec.ts | 3 ++- .../src/directsecp256k1wallet.spec.ts | 3 ++- packages/proto-signing/src/signing.ts | 4 +++- packages/stargate/src/signingstargateclient.ts | 15 ++++++++++++--- .../stargate/src/stargateclient.searchtx.spec.ts | 3 ++- packages/stargate/src/stargateclient.spec.ts | 14 +++++++++++++- packages/stargate/src/testutils.spec.ts | 2 ++ 12 files changed, 61 insertions(+), 13 deletions(-) diff --git a/packages/amino/src/signdoc.ts b/packages/amino/src/signdoc.ts index 5f9eb168..6e20a321 100644 --- a/packages/amino/src/signdoc.ts +++ b/packages/amino/src/signdoc.ts @@ -13,6 +13,7 @@ export interface StdFee { readonly amount: readonly Coin[]; readonly gas: string; readonly granter?: string; + readonly payer?: string; } /** diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts index c316ebc3..506ddd44 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts @@ -79,8 +79,9 @@ async function sendTokens( }, ]; const gasLimit = 200000; + const feeGranter = ""; const feePayer = ""; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feePayer); + const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feeGranter, feePayer); const chainId = await client.getChainId(); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts index f8a446d1..ecc25adf 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts @@ -203,8 +203,15 @@ describe("CosmWasmClient", () => { }; const txBodyBytes = registry.encode(txBody); const gasLimit = Int53.fromString(fee.gas).toNumber(); + const feeGranter = ""; const feePayer = ""; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit, feePayer); + const authInfoBytes = makeAuthInfoBytes( + [{ pubkey, sequence }], + fee.amount, + gasLimit, + feeGranter, + feePayer, + ); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signed, signature } = await wallet.signDirect(alice.address0, signDoc); const txRaw = TxRaw.fromPartial({ diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index ef0fde37..5c8eac4a 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -582,11 +582,13 @@ export class SigningCosmWasmClient extends CosmWasmClient { const signedTxBodyBytes = this.registry.encode(signedTxBody); const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber(); const signedSequence = Int53.fromString(signed.sequence).toNumber(); - const signedFeePayer = ""; + const signedFeeGranter = signed.fee.granter == undefined ? "" : signed.fee.granter; + const signedFeePayer = signed.fee.payer == undefined ? "" : signed.fee.payer; const signedAuthInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence: signedSequence }], signed.fee.amount, signedGasLimit, + signedFeeGranter, signedFeePayer, signMode, ); @@ -621,8 +623,15 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const txBodyBytes = this.registry.encode(txBody); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const feePayer = fee.granter == undefined ? "" : fee.granter; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit, feePayer); + const feePayer = fee.payer == undefined ? "" : fee.payer; + const feeGranter = fee.granter == undefined ? "" : fee.granter; + const authInfoBytes = makeAuthInfoBytes( + [{ pubkey, sequence }], + fee.amount, + gasLimit, + feeGranter, + feePayer, + ); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signature, signed } = await this.signer.signDirect(signerAddress, signDoc); return TxRaw.fromPartial({ diff --git a/packages/cosmwasm-stargate/src/testutils.spec.ts b/packages/cosmwasm-stargate/src/testutils.spec.ts index 05da4884..b2e4a4e4 100644 --- a/packages/cosmwasm-stargate/src/testutils.spec.ts +++ b/packages/cosmwasm-stargate/src/testutils.spec.ts @@ -225,6 +225,7 @@ export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet { })); const modifiedFeeAmount = coins(3000, "ucosm"); const modifiedGasLimit = 333333; + const modifiedFeeGranter = ""; const modifiedFeePayer = ""; const modifiedSignDoc = { ...signDoc, @@ -233,6 +234,7 @@ export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet { signers, modifiedFeeAmount, modifiedGasLimit, + modifiedFeeGranter, modifiedFeePayer, SignMode.SIGN_MODE_DIRECT, ), diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts index 03a2f3e3..02c9aae2 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts @@ -261,11 +261,12 @@ describe("DirectSecp256k1HdWallet", () => { }; const fee = coins(2000, "ucosm"); const gasLimit = 200000; + const feeGranter = ""; const feePayer = ""; const chainId = "simd-testing"; const signDoc = makeSignDoc( fromHex(bodyBytes), - makeAuthInfoBytes([{ pubkey, sequence }], fee, gasLimit, feePayer), + makeAuthInfoBytes([{ pubkey, sequence }], fee, gasLimit, feeGranter, feePayer), chainId, accountNumber, ); diff --git a/packages/proto-signing/src/directsecp256k1wallet.spec.ts b/packages/proto-signing/src/directsecp256k1wallet.spec.ts index fe388eb5..3e071621 100644 --- a/packages/proto-signing/src/directsecp256k1wallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1wallet.spec.ts @@ -45,9 +45,10 @@ describe("DirectSecp256k1Wallet", () => { const gasLimit = 200000; const chainId = "simd-testing"; const feePayer = ""; + const feeGranter = ""; const signDoc = makeSignDoc( fromHex(bodyBytes), - makeAuthInfoBytes([{ pubkey, sequence }], fee, gasLimit, feePayer), + makeAuthInfoBytes([{ pubkey, sequence }], fee, gasLimit, feeGranter, feePayer), chainId, accountNumber, ); diff --git a/packages/proto-signing/src/signing.ts b/packages/proto-signing/src/signing.ts index 303c1844..24e69c3d 100644 --- a/packages/proto-signing/src/signing.ts +++ b/packages/proto-signing/src/signing.ts @@ -34,6 +34,7 @@ export function makeAuthInfoBytes( signers: ReadonlyArray<{ readonly pubkey: Any; readonly sequence: number }>, feeAmount: readonly Coin[], gasLimit: number, + feeGranter: string | undefined, feePayer: string | undefined, signMode = SignMode.SIGN_MODE_DIRECT, ): Uint8Array { @@ -42,7 +43,8 @@ export function makeAuthInfoBytes( fee: { amount: [...feeAmount], gasLimit: Long.fromNumber(gasLimit), - granter: feePayer, + granter: feeGranter, + payer: feePayer, }, }; return AuthInfo.encode(AuthInfo.fromPartial(authInfo)).finish(); diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index a3ad6e7d..b1ebf67f 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -362,11 +362,13 @@ export class SigningStargateClient extends StargateClient { const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject); const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber(); const signedSequence = Int53.fromString(signed.sequence).toNumber(); - const signedFeePayer = signed.fee.granter == undefined ? "" : signed.fee.granter; + const signedFeeGranter = signed.fee.granter == undefined ? "" : signed.fee.granter; + const signedFeePayer = signed.fee.payer == undefined ? "" : signed.fee.payer; const signedAuthInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence: signedSequence }], signed.fee.amount, signedGasLimit, + signedFeeGranter, signedFeePayer, signMode, ); @@ -401,8 +403,15 @@ export class SigningStargateClient extends StargateClient { }; const txBodyBytes = this.registry.encode(txBodyEncodeObject); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const feePayer = fee.granter == undefined ? "" : fee.granter; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit, feePayer); + const feeGranter = fee.granter == undefined ? "" : fee.granter; + const feePayer = fee.payer == undefined ? "" : fee.payer; + const authInfoBytes = makeAuthInfoBytes( + [{ pubkey, sequence }], + fee.amount, + gasLimit, + feeGranter, + feePayer, + ); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signature, signed } = await this.signer.signDirect(signerAddress, signDoc); return TxRaw.fromPartial({ diff --git a/packages/stargate/src/stargateclient.searchtx.spec.ts b/packages/stargate/src/stargateclient.searchtx.spec.ts index 465bb5af..a4c8bd64 100644 --- a/packages/stargate/src/stargateclient.searchtx.spec.ts +++ b/packages/stargate/src/stargateclient.searchtx.spec.ts @@ -74,8 +74,9 @@ async function sendTokens( }, ]; const gasLimit = 200000; + const feeGranter = ""; const feePayer = ""; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feePayer); + const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feeGranter, feePayer); const chainId = await client.getChainId(); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); diff --git a/packages/stargate/src/stargateclient.spec.ts b/packages/stargate/src/stargateclient.spec.ts index 241a8543..7227927a 100644 --- a/packages/stargate/src/stargateclient.spec.ts +++ b/packages/stargate/src/stargateclient.spec.ts @@ -364,8 +364,15 @@ describe("StargateClient", () => { const { accountNumber, sequence } = (await client.getSequence(address))!; const feeAmount = coins(2000, "ucosm"); const gasLimit = 200000; + const feeGranter = ""; const feePayer = ""; - const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feePayer); + const authInfoBytes = makeAuthInfoBytes( + [{ pubkey, sequence }], + feeAmount, + gasLimit, + feeGranter, + feePayer, + ); const chainId = await client.getChainId(); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); @@ -422,11 +429,13 @@ describe("StargateClient", () => { const { accountNumber, sequence } = (await client.getSequence(address))!; const feeAmount = coins(2000, "ucosm"); const gasLimit = 200000; + const feeGranter = ""; const feePayer = ""; const authInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence }], feeAmount, gasLimit, + feeGranter, feePayer, sequence, ); @@ -490,6 +499,7 @@ describe("StargateClient", () => { const chainId = await client.getChainId(); const feeAmount = coins(2000, "ucosm"); const gasLimit = 200000; + const feeGranter = ""; const feePayer = ""; const { accountNumber: accountNumber1, sequence: sequence1 } = (await client.getSequence(address))!; @@ -497,6 +507,7 @@ describe("StargateClient", () => { [{ pubkey, sequence: sequence1 }], feeAmount, gasLimit, + feeGranter, feePayer, ); const signDoc1 = makeSignDoc(txBodyBytes, authInfoBytes1, chainId, accountNumber1); @@ -516,6 +527,7 @@ describe("StargateClient", () => { [{ pubkey, sequence: sequence2 }], feeAmount, gasLimit, + feeGranter, feePayer, ); const signDoc2 = makeSignDoc(txBodyBytes, authInfoBytes2, chainId, accountNumber2); diff --git a/packages/stargate/src/testutils.spec.ts b/packages/stargate/src/testutils.spec.ts index b5ddb640..50710267 100644 --- a/packages/stargate/src/testutils.spec.ts +++ b/packages/stargate/src/testutils.spec.ts @@ -234,6 +234,7 @@ export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet { })); const modifiedFeeAmount = coins(3000, "ucosm"); const modifiedGasLimit = 333333; + const modifiedFeeGranter = ""; const modifiedFeePayer = ""; const modifiedSignDoc = { ...signDoc, @@ -242,6 +243,7 @@ export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet { signers, modifiedFeeAmount, modifiedGasLimit, + modifiedFeeGranter, modifiedFeePayer, SignMode.SIGN_MODE_DIRECT, ), From b1bba0911e5a9c4ac95fd17b9902443ae7ad9fc7 Mon Sep 17 00:00:00 2001 From: arnabghose997 Date: Sun, 3 Jul 2022 10:16:13 +0530 Subject: [PATCH 7/9] removed assignment of payer and granter to empty string --- .../src/cosmwasmclient.searchtx.spec.ts | 4 ++-- .../cosmwasm-stargate/src/cosmwasmclient.spec.ts | 4 ++-- .../cosmwasm-stargate/src/signingcosmwasmclient.ts | 12 ++++-------- packages/cosmwasm-stargate/src/testutils.spec.ts | 4 ++-- packages/stargate/src/signingstargateclient.ts | 12 ++++-------- .../stargate/src/stargateclient.searchtx.spec.ts | 4 ++-- packages/stargate/src/stargateclient.spec.ts | 8 ++++---- packages/stargate/src/testutils.spec.ts | 4 ++-- 8 files changed, 22 insertions(+), 30 deletions(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts index 506ddd44..18a8211d 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.searchtx.spec.ts @@ -79,8 +79,8 @@ async function sendTokens( }, ]; const gasLimit = 200000; - const feeGranter = ""; - const feePayer = ""; + const feeGranter = undefined; + const feePayer = undefined; const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feeGranter, feePayer); const chainId = await client.getChainId(); diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts index ecc25adf..25fec479 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts @@ -203,8 +203,8 @@ describe("CosmWasmClient", () => { }; const txBodyBytes = registry.encode(txBody); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const feeGranter = ""; - const feePayer = ""; + const feeGranter = undefined; + const feePayer = undefined; const authInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence }], fee.amount, diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index 5c8eac4a..9fe64565 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -582,14 +582,12 @@ export class SigningCosmWasmClient extends CosmWasmClient { const signedTxBodyBytes = this.registry.encode(signedTxBody); const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber(); const signedSequence = Int53.fromString(signed.sequence).toNumber(); - const signedFeeGranter = signed.fee.granter == undefined ? "" : signed.fee.granter; - const signedFeePayer = signed.fee.payer == undefined ? "" : signed.fee.payer; const signedAuthInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence: signedSequence }], signed.fee.amount, signedGasLimit, - signedFeeGranter, - signedFeePayer, + signed.fee.granter, + signed.fee.payer, signMode, ); return TxRaw.fromPartial({ @@ -623,14 +621,12 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const txBodyBytes = this.registry.encode(txBody); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const feePayer = fee.payer == undefined ? "" : fee.payer; - const feeGranter = fee.granter == undefined ? "" : fee.granter; const authInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence }], fee.amount, gasLimit, - feeGranter, - feePayer, + fee.granter, + fee.payer, ); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signature, signed } = await this.signer.signDirect(signerAddress, signDoc); diff --git a/packages/cosmwasm-stargate/src/testutils.spec.ts b/packages/cosmwasm-stargate/src/testutils.spec.ts index b2e4a4e4..e40b8b5b 100644 --- a/packages/cosmwasm-stargate/src/testutils.spec.ts +++ b/packages/cosmwasm-stargate/src/testutils.spec.ts @@ -225,8 +225,8 @@ export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet { })); const modifiedFeeAmount = coins(3000, "ucosm"); const modifiedGasLimit = 333333; - const modifiedFeeGranter = ""; - const modifiedFeePayer = ""; + const modifiedFeeGranter = undefined; + const modifiedFeePayer = undefined; const modifiedSignDoc = { ...signDoc, bodyBytes: Uint8Array.from(TxBody.encode(modifiedTxBody).finish()), diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index b1ebf67f..23641688 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -362,14 +362,12 @@ export class SigningStargateClient extends StargateClient { const signedTxBodyBytes = this.registry.encode(signedTxBodyEncodeObject); const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber(); const signedSequence = Int53.fromString(signed.sequence).toNumber(); - const signedFeeGranter = signed.fee.granter == undefined ? "" : signed.fee.granter; - const signedFeePayer = signed.fee.payer == undefined ? "" : signed.fee.payer; const signedAuthInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence: signedSequence }], signed.fee.amount, signedGasLimit, - signedFeeGranter, - signedFeePayer, + signed.fee.granter, + signed.fee.payer, signMode, ); return TxRaw.fromPartial({ @@ -403,14 +401,12 @@ export class SigningStargateClient extends StargateClient { }; const txBodyBytes = this.registry.encode(txBodyEncodeObject); const gasLimit = Int53.fromString(fee.gas).toNumber(); - const feeGranter = fee.granter == undefined ? "" : fee.granter; - const feePayer = fee.payer == undefined ? "" : fee.payer; const authInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence }], fee.amount, gasLimit, - feeGranter, - feePayer, + fee.granter, + fee.payer, ); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const { signature, signed } = await this.signer.signDirect(signerAddress, signDoc); diff --git a/packages/stargate/src/stargateclient.searchtx.spec.ts b/packages/stargate/src/stargateclient.searchtx.spec.ts index a4c8bd64..01d0e27a 100644 --- a/packages/stargate/src/stargateclient.searchtx.spec.ts +++ b/packages/stargate/src/stargateclient.searchtx.spec.ts @@ -74,8 +74,8 @@ async function sendTokens( }, ]; const gasLimit = 200000; - const feeGranter = ""; - const feePayer = ""; + const feeGranter = undefined; + const feePayer = undefined; const authInfoBytes = makeAuthInfoBytes([{ pubkey, sequence }], feeAmount, gasLimit, feeGranter, feePayer); const chainId = await client.getChainId(); diff --git a/packages/stargate/src/stargateclient.spec.ts b/packages/stargate/src/stargateclient.spec.ts index 7227927a..df4823e8 100644 --- a/packages/stargate/src/stargateclient.spec.ts +++ b/packages/stargate/src/stargateclient.spec.ts @@ -364,8 +364,8 @@ describe("StargateClient", () => { const { accountNumber, sequence } = (await client.getSequence(address))!; const feeAmount = coins(2000, "ucosm"); const gasLimit = 200000; - const feeGranter = ""; - const feePayer = ""; + const feeGranter = undefined; + const feePayer = undefined; const authInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence }], feeAmount, @@ -429,8 +429,8 @@ describe("StargateClient", () => { const { accountNumber, sequence } = (await client.getSequence(address))!; const feeAmount = coins(2000, "ucosm"); const gasLimit = 200000; - const feeGranter = ""; - const feePayer = ""; + const feeGranter = undefined; + const feePayer = undefined; const authInfoBytes = makeAuthInfoBytes( [{ pubkey, sequence }], feeAmount, diff --git a/packages/stargate/src/testutils.spec.ts b/packages/stargate/src/testutils.spec.ts index 50710267..fb59b743 100644 --- a/packages/stargate/src/testutils.spec.ts +++ b/packages/stargate/src/testutils.spec.ts @@ -234,8 +234,8 @@ export class ModifyingDirectSecp256k1HdWallet extends DirectSecp256k1HdWallet { })); const modifiedFeeAmount = coins(3000, "ucosm"); const modifiedGasLimit = 333333; - const modifiedFeeGranter = ""; - const modifiedFeePayer = ""; + const modifiedFeeGranter = undefined; + const modifiedFeePayer = undefined; const modifiedSignDoc = { ...signDoc, bodyBytes: Uint8Array.from(TxBody.encode(modifiedTxBody).finish()), From c959343cacd868ed60891cbd7c92a1a2f24448ba Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 7 Sep 2022 16:35:17 +0200 Subject: [PATCH 8/9] Test feegrant --- .../stargate/src/modules/feegrant/queries.ts | 40 ++++++++ packages/stargate/src/modules/index.ts | 1 + .../src/signingstargateclient.spec.ts | 93 ++++++++++++++++++- 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 packages/stargate/src/modules/feegrant/queries.ts diff --git a/packages/stargate/src/modules/feegrant/queries.ts b/packages/stargate/src/modules/feegrant/queries.ts new file mode 100644 index 00000000..f37634cf --- /dev/null +++ b/packages/stargate/src/modules/feegrant/queries.ts @@ -0,0 +1,40 @@ +import { + QueryAllowanceResponse, + QueryAllowancesResponse, + QueryClientImpl, +} from "cosmjs-types/cosmos/feegrant/v1beta1/query"; + +import { createPagination, createProtobufRpcClient, QueryClient } from "../../queryclient"; + +export interface FeegrantExtension { + readonly feegrant: { + readonly allowance: (granter: string, grantee: string) => Promise; + readonly allowances: (grantee: string, paginationKey?: Uint8Array) => Promise; + }; +} + +export function setupFeegrantExtension(base: QueryClient): FeegrantExtension { + // Use this service to get easy typed access to query methods + // This cannot be used for proof verification + const rpc = createProtobufRpcClient(base); + const queryService = new QueryClientImpl(rpc); + + return { + feegrant: { + allowance: async (granter: string, grantee: string) => { + const response = await queryService.Allowance({ + granter: granter, + grantee: grantee, + }); + return response; + }, + allowances: async (grantee: string, paginationKey?: Uint8Array) => { + const response = await queryService.Allowances({ + grantee: grantee, + pagination: createPagination(paginationKey), + }); + return response; + }, + }, + }; +} diff --git a/packages/stargate/src/modules/index.ts b/packages/stargate/src/modules/index.ts index e96cf863..0a31a5f7 100644 --- a/packages/stargate/src/modules/index.ts +++ b/packages/stargate/src/modules/index.ts @@ -39,6 +39,7 @@ export { } from "./evidence/aminomessages"; export { createFreegrantAminoConverters } from "./feegrant/aminomessages"; export { feegrantTypes } from "./feegrant/messages"; +export { FeegrantExtension, setupFeegrantExtension } from "./feegrant/queries"; export { AminoMsgDeposit, AminoMsgSubmitProposal, diff --git a/packages/stargate/src/signingstargateclient.spec.ts b/packages/stargate/src/signingstargateclient.spec.ts index 96789b34..a138e3e4 100644 --- a/packages/stargate/src/signingstargateclient.spec.ts +++ b/packages/stargate/src/signingstargateclient.spec.ts @@ -1,16 +1,33 @@ /* eslint-disable @typescript-eslint/naming-convention,no-bitwise */ import { Secp256k1HdWallet } from "@cosmjs/amino"; -import { coin, coins, decodeTxRaw, DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing"; +import { + coin, + coins, + decodeTxRaw, + DirectSecp256k1HdWallet, + makeCosmoshubPath, + Registry, +} from "@cosmjs/proto-signing"; +import { Tendermint34Client } from "@cosmjs/tendermint-rpc"; import { assert, sleep } from "@cosmjs/utils"; import { MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx"; import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin"; +import { BasicAllowance } from "cosmjs-types/cosmos/feegrant/v1beta1/feegrant"; +import { MsgGrantAllowance } from "cosmjs-types/cosmos/feegrant/v1beta1/tx"; import { DeepPartial, MsgDelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx"; import { AuthInfo, TxBody, TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx"; +import { Any } from "cosmjs-types/google/protobuf/any"; import Long from "long"; import protobuf from "protobufjs/minimal"; import { AminoTypes } from "./aminotypes"; -import { AminoMsgDelegate, MsgDelegateEncodeObject, MsgSendEncodeObject } from "./modules"; +import { + AminoMsgDelegate, + MsgDelegateEncodeObject, + MsgSendEncodeObject, + setupFeegrantExtension, +} from "./modules"; +import { QueryClient } from "./queryclient"; import { PrivateSigningStargateClient, SigningStargateClient } from "./signingstargateclient"; import { assertIsDeliverTxFailure, assertIsDeliverTxSuccess, isDeliverTxFailure } from "./stargateclient"; import { @@ -140,6 +157,78 @@ describe("SigningStargateClient", () => { const after = await client.getBalance(beneficiaryAddress, "ucosm"); expect(after).toEqual(amount[0]); }); + + it("works with feegrant granter", async () => { + pendingWithoutSimapp(); + const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic, { + hdPaths: [makeCosmoshubPath(0), makeCosmoshubPath(1)], + }); + const [{ address: signer }, { address: payer }] = await wallet.getAccounts(); + const client = await SigningStargateClient.connectWithSigner( + simapp.tendermintUrl, + wallet, + defaultSigningClientOptions, + ); + + const tmClient = await Tendermint34Client.connect(simapp.tendermintUrl); + const queryClient = QueryClient.withExtensions(tmClient, setupFeegrantExtension); + let allowanceExists: boolean; + try { + const _existingAllowance = await queryClient.feegrant.allowance(payer, signer); + allowanceExists = true; + } catch { + allowanceExists = false; + } + + if (!allowanceExists) { + // Create feegrant allowance + const allowance: Any = { + typeUrl: "/cosmos.feegrant.v1beta1.BasicAllowance", + value: Uint8Array.from( + BasicAllowance.encode({ + spendLimit: [ + { + denom: "ucosm", + amount: "1234567", + }, + ], + }).finish(), + ), + }; + const grantMsg = { + typeUrl: "/cosmos.feegrant.v1beta1.MsgGrantAllowance", + value: MsgGrantAllowance.fromPartial({ + granter: payer, + grantee: signer, + allowance: allowance, + }), + }; + const grantResult = await client.signAndBroadcast(payer, [grantMsg], "auto", "Create allowance"); + assertIsDeliverTxSuccess(grantResult); + } + + const balanceSigner1 = await client.getBalance(signer, "ucosm"); + const balancePayer1 = await client.getBalance(payer, "ucosm"); + + const sendAmount = coins(7890, "ucosm"); + const feeAmount = coins(4444, "ucosm"); + + // send + const result = await client.sendTokens(signer, makeRandomAddress(), sendAmount, { + amount: feeAmount, + gas: "120000", + granter: payer, + }); + assertIsDeliverTxSuccess(result); + + const balanceSigner2 = await client.getBalance(signer, "ucosm"); + const balancePayer2 = await client.getBalance(payer, "ucosm"); + + const diffSigner = Number(BigInt(balanceSigner1.amount) - BigInt(balanceSigner2.amount)); + const diffPayer = Number(BigInt(balancePayer1.amount) - BigInt(balancePayer2.amount)); + expect(diffSigner).toEqual(7890); // the send amount + expect(diffPayer).toEqual(4444); // the fee + }); }); describe("sendIbcTokens", () => { From cc2e36995ad10cdb6afdb5dcd6f1a8eb8c6f81a1 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 7 Sep 2022 17:50:57 +0200 Subject: [PATCH 9/9] Finalize feegrant code --- CHANGELOG.md | 2 ++ packages/amino/src/signdoc.ts | 2 ++ .../proto-signing/src/directsecp256k1hdwallet.spec.ts | 4 ++-- packages/proto-signing/src/directsecp256k1wallet.spec.ts | 4 ++-- packages/proto-signing/src/signing.ts | 8 ++++++++ packages/stargate/src/stargateclient.spec.ts | 4 ++-- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ead672e..737266f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,6 +71,8 @@ and this project adheres to - @cosmjs/stargate: `BankExtension.totalSupply` now takes a pagination key argument and returns the full `QueryTotalSupplyResponse` including the next pagination key ([#1095]). +- @cosmjs/proto-signing: `makeAuthInfoBytes` now expects a fee granter and fee + payer argument in position 4 and 5. [#1131]: https://github.com/cosmos/cosmjs/pull/1131 [#1168]: https://github.com/cosmos/cosmjs/pull/1168 diff --git a/packages/amino/src/signdoc.ts b/packages/amino/src/signdoc.ts index 6e20a321..2348353e 100644 --- a/packages/amino/src/signdoc.ts +++ b/packages/amino/src/signdoc.ts @@ -12,7 +12,9 @@ export interface AminoMsg { export interface StdFee { readonly amount: readonly Coin[]; readonly gas: string; + /** The granter address that is used for paying with feegrants */ readonly granter?: string; + /** The fee payer address. The payer must have signed the transaction. */ readonly payer?: string; } diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts index 02c9aae2..24757783 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts @@ -261,8 +261,8 @@ describe("DirectSecp256k1HdWallet", () => { }; const fee = coins(2000, "ucosm"); const gasLimit = 200000; - const feeGranter = ""; - const feePayer = ""; + const feeGranter = undefined; + const feePayer = undefined; const chainId = "simd-testing"; const signDoc = makeSignDoc( fromHex(bodyBytes), diff --git a/packages/proto-signing/src/directsecp256k1wallet.spec.ts b/packages/proto-signing/src/directsecp256k1wallet.spec.ts index 3e071621..58af6c3a 100644 --- a/packages/proto-signing/src/directsecp256k1wallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1wallet.spec.ts @@ -44,8 +44,8 @@ describe("DirectSecp256k1Wallet", () => { const fee = coins(2000, "ucosm"); const gasLimit = 200000; const chainId = "simd-testing"; - const feePayer = ""; - const feeGranter = ""; + const feePayer = undefined; + const feeGranter = undefined; const signDoc = makeSignDoc( fromHex(bodyBytes), makeAuthInfoBytes([{ pubkey, sequence }], fee, gasLimit, feeGranter, feePayer), diff --git a/packages/proto-signing/src/signing.ts b/packages/proto-signing/src/signing.ts index 24e69c3d..7ce7e476 100644 --- a/packages/proto-signing/src/signing.ts +++ b/packages/proto-signing/src/signing.ts @@ -1,4 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ +import { assert } from "@cosmjs/utils"; import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin"; import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing"; import { AuthInfo, SignDoc, SignerInfo } from "cosmjs-types/cosmos/tx/v1beta1/tx"; @@ -38,6 +39,13 @@ export function makeAuthInfoBytes( feePayer: string | undefined, signMode = SignMode.SIGN_MODE_DIRECT, ): Uint8Array { + // Required arguments 4 and 5 were added in CosmJS 0.29. Use runtime checks to help our non-TS users. + assert( + feeGranter === undefined || typeof feeGranter === "string", + "feeGranter must be undefined or string", + ); + assert(feePayer === undefined || typeof feePayer === "string", "feePayer must be undefined or string"); + const authInfo = { signerInfos: makeSignerInfos(signers, signMode), fee: { diff --git a/packages/stargate/src/stargateclient.spec.ts b/packages/stargate/src/stargateclient.spec.ts index df4823e8..641a31e8 100644 --- a/packages/stargate/src/stargateclient.spec.ts +++ b/packages/stargate/src/stargateclient.spec.ts @@ -499,8 +499,8 @@ describe("StargateClient", () => { const chainId = await client.getChainId(); const feeAmount = coins(2000, "ucosm"); const gasLimit = 200000; - const feeGranter = ""; - const feePayer = ""; + const feeGranter = undefined; + const feePayer = undefined; const { accountNumber: accountNumber1, sequence: sequence1 } = (await client.getSequence(address))!; const authInfoBytes1 = makeAuthInfoBytes(