Skip to content

Commit

Permalink
fix: fix NewSeed tx signinng process
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Jan 4, 2024
1 parent 4caf1c0 commit 0b7e3ef
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 42 deletions.
21 changes: 21 additions & 0 deletions app/utils/vrf_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions proto/sedachain/randomness/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 6 additions & 11 deletions x/randomness/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
52 changes: 26 additions & 26 deletions x/randomness/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0b7e3ef

Please sign in to comment.