Skip to content

Commit 5a3f05b

Browse files
authored
Cherry-pick signature v2 (#1155)
* Cherry-pick signature v2 * Retract v5.0.0 * Make CI pick the job * Remove master branch trigger from CI
1 parent 2197882 commit 5a3f05b

File tree

4 files changed

+64
-61
lines changed

4 files changed

+64
-61
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: ci
22
on:
33
push:
4-
branches: [ master ]
4+
branches: [ release-v5 ]
55
pull_request:
6-
branches: [ master ]
6+
branches: [ release-v5 ]
77
workflow_dispatch:
88

99
concurrency:

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,5 @@ require (
254254
pgregory.net/rapid v1.1.0 // indirect
255255
sigs.k8s.io/yaml v1.4.0 // indirect
256256
)
257+
258+
retract v5.0.0

integration-tests/modules/auth_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
sdkmath "cosmossdk.io/math"
1111
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
1212
clienttx "github.com/cosmos/cosmos-sdk/client/tx"
13+
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
1314
sdk "github.com/cosmos/cosmos-sdk/types"
1415
cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors"
1516
"github.com/cosmos/cosmos-sdk/types/tx/signing"
@@ -246,6 +247,7 @@ func TestGasEstimation(t *testing.T) {
246247
ctx, chain := integrationtests.NewCoreumTestingContext(t)
247248

248249
singlesigAddress := chain.GenAccount()
250+
singlesigUnimportedAddress := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
249251

250252
multisigPublicKey1, _, err := chain.GenMultisigAccount(3, 2)
251253
require.NoError(t, err)
@@ -261,6 +263,7 @@ func TestGasEstimation(t *testing.T) {
261263

262264
// For accounts to exist on chain we need to fund them at least with min amount (1ucore).
263265
chain.FundAccountWithOptions(ctx, t, singlesigAddress, integration.BalancesOptions{Amount: sdkmath.NewInt(1)})
266+
chain.FundAccountWithOptions(ctx, t, singlesigUnimportedAddress, integration.BalancesOptions{Amount: sdkmath.NewInt(1)})
264267
chain.FundAccountWithOptions(ctx, t, multisigAddress1, integration.BalancesOptions{Amount: sdkmath.NewInt(1)})
265268
chain.FundAccountWithOptions(ctx, t, multisigAddress2, integration.BalancesOptions{Amount: sdkmath.NewInt(1)})
266269

@@ -287,6 +290,38 @@ func TestGasEstimation(t *testing.T) {
287290
return dgc.FixedGas + deterministicgas.BankSendPerCoinGas
288291
},
289292
},
293+
{
294+
name: "singlesig_bank_send_unsigned",
295+
fromAddress: singlesigAddress,
296+
msgs: []sdk.Msg{
297+
&banktypes.MsgSend{
298+
FromAddress: singlesigAddress.String(),
299+
ToAddress: singlesigAddress.String(),
300+
Amount: sdk.NewCoins(chain.NewCoin(sdkmath.NewInt(1))),
301+
},
302+
},
303+
simulateUnsigned: true,
304+
// single signature no extra bytes.
305+
expectedGas: func(txBytes []byte) uint64 {
306+
return dgc.FixedGas + deterministicgas.BankSendPerCoinGas
307+
},
308+
},
309+
{
310+
name: "singlesig_no_pub_key_bank_send_unsigned",
311+
fromAddress: singlesigUnimportedAddress,
312+
msgs: []sdk.Msg{
313+
&banktypes.MsgSend{
314+
FromAddress: singlesigUnimportedAddress.String(),
315+
ToAddress: singlesigUnimportedAddress.String(),
316+
Amount: sdk.NewCoins(chain.NewCoin(sdkmath.NewInt(1))),
317+
},
318+
},
319+
simulateUnsigned: true,
320+
// single signature no extra bytes.
321+
expectedGas: func(txBytes []byte) uint64 {
322+
return dgc.FixedGas + deterministicgas.BankSendPerCoinGas
323+
},
324+
},
290325
{
291326
name: "multisig_2_3_bank_send",
292327
fromAddress: multisigAddress1,
@@ -331,7 +366,7 @@ func TestGasEstimation(t *testing.T) {
331366
},
332367
simulateUnsigned: true,
333368
expectedGas: func(txBytes []byte) uint64 {
334-
signatureCostDifference := uint64(1610)
369+
signatureCostDifference := uint64(1830)
335370
bytesCost := uint64(len(txBytes)) * authParams.Params.TxSizeCostPerByte
336371
expectedGas := dgc.FixedGas + deterministicgas.BankSendPerCoinGas + bytesCost - signatureCostDifference
337372
return expectedGas

pkg/client/tx.go

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ import (
1818
"github.com/cosmos/cosmos-sdk/client/flags"
1919
"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
2020
"github.com/cosmos/cosmos-sdk/client/tx"
21-
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
2221
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
2322
"github.com/cosmos/cosmos-sdk/crypto/types"
23+
multisigtypes "github.com/cosmos/cosmos-sdk/crypto/types/multisig"
2424
sdk "github.com/cosmos/cosmos-sdk/types"
2525
cosmoserrors "github.com/cosmos/cosmos-sdk/types/errors"
2626
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
2727
"github.com/cosmos/cosmos-sdk/types/tx/signing"
28-
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
2928
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
30-
"github.com/cosmos/gogoproto/proto"
3129
"github.com/pkg/errors"
3230

3331
"github.com/CoreumFoundation/coreum-tools/pkg/retry"
@@ -169,8 +167,10 @@ func BuildTxForSimulation(
169167
txf = txf.WithGasAdjustment(clientCtx.GasAdjustment())
170168
}
171169

170+
var signatureData signing.SignatureData = &signing.SingleSignatureData{
171+
SignMode: txf.SignMode(),
172+
}
172173
var pubKey types.PubKey
173-
var pubKeyAny *codectypes.Any
174174
if !clientCtx.GetUnsignedSimulation() {
175175
keyInfo, err := txf.Keybase().KeyByAddress(clientCtx.FromAddress())
176176
if err != nil {
@@ -180,71 +180,37 @@ func BuildTxForSimulation(
180180
if err != nil {
181181
return nil, errors.WithStack(err)
182182
}
183-
pubKeyAny = keyInfo.PubKey
184-
}
185-
186-
msgsAny := make([]*codectypes.Any, 0, len(msgs))
187-
for _, msg := range msgs {
188-
msgAny, err := codectypes.NewAnyWithValue(msg)
189-
if err != nil {
190-
return nil, errors.WithStack(err)
191-
}
192-
msgsAny = append(msgsAny, msgAny)
193-
}
194-
195-
txBodyBytes, err := proto.Marshal(&sdktx.TxBody{
196-
Messages: msgsAny,
197-
Memo: txf.Memo(),
198-
})
199-
if err != nil {
200-
return nil, errors.WithStack(err)
201-
}
202183

203-
var signatureData signing.SignatureData
204-
if multisigPubKey, ok := pubKey.(*multisig.LegacyAminoPubKey); ok {
205-
multiSignatureData := make([]signing.SignatureData, 0, multisigPubKey.Threshold)
206-
for range multisigPubKey.Threshold {
207-
multiSignatureData = append(multiSignatureData, &signing.SingleSignatureData{
208-
SignMode: txf.SignMode(),
209-
})
210-
}
211-
signatureData = &signing.MultiSignatureData{
212-
Signatures: multiSignatureData,
184+
if multisigPubKey, ok := pubKey.(*multisig.LegacyAminoPubKey); ok {
185+
multiSignatureData := make([]signing.SignatureData, 0, multisigPubKey.Threshold)
186+
for range multisigPubKey.Threshold {
187+
multiSignatureData = append(multiSignatureData, &signing.SingleSignatureData{
188+
SignMode: txf.SignMode(),
189+
})
190+
}
191+
signatureData = &signing.MultiSignatureData{
192+
Signatures: multiSignatureData,
193+
}
213194
}
214195
} else {
215-
signatureData = &signing.SingleSignatureData{
216-
SignMode: txf.SignMode(),
217-
}
196+
// For unsigned simulation, signatureData doesn't matter. It should just not be nil
197+
signatureData = multisigtypes.NewMultisig(4)
218198
}
219199

220-
modeInfo, signature := authtx.SignatureDataToModeInfoAndSig(signatureData)
221-
222-
simAuthInfoBytes, err := proto.Marshal(&sdktx.AuthInfo{
223-
SignerInfos: []*sdktx.SignerInfo{
224-
{
225-
PublicKey: pubKeyAny,
226-
ModeInfo: modeInfo,
227-
Sequence: txf.Sequence(),
228-
},
229-
},
230-
Fee: &sdktx.Fee{},
231-
})
200+
signature := signing.SignatureV2{
201+
PubKey: pubKey,
202+
Data: signatureData,
203+
Sequence: txf.Sequence(),
204+
}
205+
txBuilder, err := txf.BuildUnsignedTx(msgs...)
232206
if err != nil {
233207
return nil, errors.WithStack(err)
234208
}
235-
236-
txBytes, err := proto.Marshal(&sdktx.TxRaw{
237-
BodyBytes: txBodyBytes,
238-
AuthInfoBytes: simAuthInfoBytes,
239-
Signatures: [][]byte{
240-
signature,
241-
},
242-
})
243-
if err != nil {
209+
if err = txBuilder.SetSignatures(signature); err != nil {
244210
return nil, errors.WithStack(err)
245211
}
246212

247-
return txBytes, nil
213+
return clientCtx.TxConfig().TxEncoder()(txBuilder.GetTx())
248214
}
249215

250216
// BroadcastRawTx broadcast the txBytes using the clientCtx and set BroadcastMode.

0 commit comments

Comments
 (0)