From 0b7e3efaf5298c0f1d134f510f3ca60be2be8245 Mon Sep 17 00:00:00 2001 From: Hyoung-yoon Kim Date: Wed, 3 Jan 2024 13:36:12 +0900 Subject: [PATCH] fix: fix NewSeed tx signinng process --- app/utils/vrf_key.go | 21 +++++++++++ proto/sedachain/randomness/v1/tx.proto | 4 +- x/randomness/keeper/abci.go | 17 +++------ x/randomness/types/tx.pb.go | 52 +++++++++++++------------- x/staking/keeper/msg_server.go | 6 +-- 5 files changed, 58 insertions(+), 42 deletions(-) diff --git a/app/utils/vrf_key.go b/app/utils/vrf_key.go index 041d89d1..14704082 100644 --- a/app/utils/vrf_key.go +++ b/app/utils/vrf_key.go @@ -124,6 +124,27 @@ func (v *VRFKey) SignTransaction( Address: account.GetAddress().String(), } + // For SIGN_MODE_DIRECT, calling SetSignatures calls setSignerInfos on + // TxBuilder under the hood, and SignerInfos is needed to generate the sign + // bytes. This is the reason for setting SetSignatures here, with a nil + // signature. + // + // Note: This line is not needed for SIGN_MODE_LEGACY_AMINO, but putting it + // also doesn't affect its generated sign bytes, so for code's simplicity + // sake, we put it here. + nilSig := txsigning.SignatureV2{ + PubKey: v.PubKey, + Data: &txsigning.SingleSignatureData{ + SignMode: signMode, + Signature: nil, + }, + Sequence: account.GetSequence(), + } + + if err := txBuilder.SetSignatures(nilSig); err != nil { + return sigV2, err + } + bytesToSign, err := authsigning.GetSignBytesAdapter( context.Background(), txConfig.SignModeHandler(), diff --git a/proto/sedachain/randomness/v1/tx.proto b/proto/sedachain/randomness/v1/tx.proto index 643789e2..7a317ca0 100644 --- a/proto/sedachain/randomness/v1/tx.proto +++ b/proto/sedachain/randomness/v1/tx.proto @@ -11,9 +11,9 @@ service Msg { } message MsgNewSeed { - option (cosmos.msg.v1.signer) = "proposer"; + option (cosmos.msg.v1.signer) = "prover"; - string proposer = 1; + string prover = 1; // address of VRF key used to produce proof string pi = 2; // VRF proof string beta = 3; // VRF hash } diff --git a/x/randomness/keeper/abci.go b/x/randomness/keeper/abci.go index 407ea6f5..386e5626 100644 --- a/x/randomness/keeper/abci.go +++ b/x/randomness/keeper/abci.go @@ -66,19 +66,19 @@ func (h *ProposalHandler) PrepareProposalHandler( } // generate and sign NewSeed tx - pubKey, err := keeper.GetValidatorVRFPubKey(ctx, sdk.ConsAddress(req.ProposerAddress).String()) + vrfPubKey, err := keeper.GetValidatorVRFPubKey(ctx, sdk.ConsAddress(req.ProposerAddress).String()) if err != nil { return nil, err } - account := authKeeper.GetAccount(ctx, sdk.AccAddress(pubKey.Address().Bytes())) - err = account.SetPubKey(pubKey) // checked later when signing tx with VRF key + account := authKeeper.GetAccount(ctx, sdk.AccAddress(vrfPubKey.Address().Bytes())) + err = account.SetPubKey(vrfPubKey) // checked later when signing tx with VRF key if err != nil { return nil, err } newSeedTx, newSeedTxBz, err := generateAndSignNewSeedTx(ctx, txConfig, vrfSigner, account, &types.MsgNewSeed{ - Proposer: sdk.AccAddress(req.ProposerAddress).String(), - Pi: hex.EncodeToString(pi), - Beta: hex.EncodeToString(beta), + Prover: account.GetAddress().String(), + Pi: hex.EncodeToString(pi), + Beta: hex.EncodeToString(beta), }) if err != nil { return nil, err @@ -148,11 +148,6 @@ func (h *ProposalHandler) ProcessProposalHandler( return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, err } - if msg.Proposer != string(sdk.AccAddress(req.ProposerAddress).String()) { - return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, - fmt.Errorf("the NewSeed transaction must be from the block proposer") - } - // get block proposer's validator public key pubKey, err := keeper.GetValidatorVRFPubKey(ctx, sdk.ConsAddress(req.ProposerAddress).String()) if err != nil { diff --git a/x/randomness/types/tx.pb.go b/x/randomness/types/tx.pb.go index 55cd39ed..9435f7b5 100644 --- a/x/randomness/types/tx.pb.go +++ b/x/randomness/types/tx.pb.go @@ -29,9 +29,9 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type MsgNewSeed struct { - Proposer string `protobuf:"bytes,1,opt,name=proposer,proto3" json:"proposer,omitempty"` - Pi string `protobuf:"bytes,2,opt,name=pi,proto3" json:"pi,omitempty"` - Beta string `protobuf:"bytes,3,opt,name=beta,proto3" json:"beta,omitempty"` + Prover string `protobuf:"bytes,1,opt,name=prover,proto3" json:"prover,omitempty"` + Pi string `protobuf:"bytes,2,opt,name=pi,proto3" json:"pi,omitempty"` + Beta string `protobuf:"bytes,3,opt,name=beta,proto3" json:"beta,omitempty"` } func (m *MsgNewSeed) Reset() { *m = MsgNewSeed{} } @@ -67,9 +67,9 @@ func (m *MsgNewSeed) XXX_DiscardUnknown() { var xxx_messageInfo_MsgNewSeed proto.InternalMessageInfo -func (m *MsgNewSeed) GetProposer() string { +func (m *MsgNewSeed) GetProver() string { if m != nil { - return m.Proposer + return m.Prover } return "" } @@ -132,24 +132,24 @@ func init() { func init() { proto.RegisterFile("sedachain/randomness/v1/tx.proto", fileDescriptor_9575b460ec9dfc32) } var fileDescriptor_9575b460ec9dfc32 = []byte{ - // 264 bytes of a gzipped FileDescriptorProto + // 262 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x28, 0x4e, 0x4d, 0x49, 0x4c, 0xce, 0x48, 0xcc, 0xcc, 0xd3, 0x2f, 0x4a, 0xcc, 0x4b, 0xc9, 0xcf, 0xcd, 0x4b, 0x2d, 0x2e, 0xd6, 0x2f, 0x33, 0xd4, 0x2f, 0xa9, 0xd0, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x87, 0xab, 0xd0, 0x43, 0xa8, 0xd0, 0x2b, 0x33, 0x94, 0x12, 0x4f, 0xce, 0x2f, 0xce, 0xcd, 0x2f, 0xd6, 0xcf, - 0x2d, 0x4e, 0x07, 0x69, 0xc8, 0x2d, 0x4e, 0x87, 0xe8, 0x50, 0x8a, 0xe6, 0xe2, 0xf2, 0x2d, 0x4e, - 0xf7,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9d, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0x19, 0x34, + 0x88, 0xac, 0x7e, 0x01, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -266,10 +266,10 @@ func (m *MsgNewSeed) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x12 } - if len(m.Proposer) > 0 { - i -= len(m.Proposer) - copy(dAtA[i:], m.Proposer) - i = encodeVarintTx(dAtA, i, uint64(len(m.Proposer))) + if len(m.Prover) > 0 { + i -= len(m.Prover) + copy(dAtA[i:], m.Prover) + i = encodeVarintTx(dAtA, i, uint64(len(m.Prover))) i-- dAtA[i] = 0xa } @@ -316,7 +316,7 @@ func (m *MsgNewSeed) Size() (n int) { } var l int _ = l - l = len(m.Proposer) + l = len(m.Prover) if l > 0 { n += 1 + l + sovTx(uint64(l)) } @@ -377,7 +377,7 @@ func (m *MsgNewSeed) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Proposer", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field Prover", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -405,7 +405,7 @@ func (m *MsgNewSeed) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Proposer = string(dAtA[iNdEx:postIndex]) + m.Prover = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex case 2: if wireType != 2 { diff --git a/x/staking/keeper/msg_server.go b/x/staking/keeper/msg_server.go index e4b5dc9a..f7ad3943 100644 --- a/x/staking/keeper/msg_server.go +++ b/x/staking/keeper/msg_server.go @@ -51,11 +51,11 @@ func (k msgServer) CreateValidatorWithVRF(ctx context.Context, msg *types.MsgCre k.accountKeeper.SetAccount(ctx, acc) // register VRF public key to validator consensus address - pubKey, ok := msg.Pubkey.GetCachedValue().(cryptotypes.PubKey) + consPubKey, ok := msg.Pubkey.GetCachedValue().(cryptotypes.PubKey) if !ok { - return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", pubKey) + return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", consPubKey) } - k.randomnessKeeper.SetValidatorVRFPubKey(ctx, sdk.GetConsAddress(pubKey).String(), vrfPubKey) + k.randomnessKeeper.SetValidatorVRFPubKey(ctx, sdk.GetConsAddress(consPubKey).String(), vrfPubKey) sdkMsg := new(stakingtypes.MsgCreateValidator) sdkMsg.Description = msg.Description