diff --git a/oracle/reactor.go b/oracle/reactor.go index 50b1a123847..91392f1970a 100644 --- a/oracle/reactor.go +++ b/oracle/reactor.go @@ -22,6 +22,7 @@ import ( "github.com/cometbft/cometbft/libs/log" "github.com/cometbft/cometbft/oracle/service/runner" oracletypes "github.com/cometbft/cometbft/oracle/service/types" + "github.com/cometbft/cometbft/oracle/service/utils" "github.com/cometbft/cometbft/p2p" oracleproto "github.com/cometbft/cometbft/proto/tendermint/oracle" "github.com/cometbft/cometbft/types" @@ -132,8 +133,7 @@ func (oracleR *Reactor) Receive(e p2p.Envelope) { switch msg := e.Message.(type) { case *oracleproto.GossipedVotes: // get account and sign type of oracle votes - accountType := []byte{msg.Signature[0]} - signType := []byte{msg.Signature[1]} + accountType, signType := utils.GetAccountSignTypeFromSignature(msg.Signature) var pubKey crypto.PubKey // get pubkey based on sign type diff --git a/oracle/service/runner/runner.go b/oracle/service/runner/runner.go index 75e5462cb8c..3b6598a6c45 100644 --- a/oracle/service/runner/runner.go +++ b/oracle/service/runner/runner.go @@ -10,6 +10,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/cometbft/cometbft/oracle/service/types" + "github.com/cometbft/cometbft/oracle/service/utils" abcitypes "github.com/cometbft/cometbft/abci/types" cs "github.com/cometbft/cometbft/consensus" @@ -71,23 +72,9 @@ func ProcessSignVoteQueue(oracleInfo *types.OracleInfo, consensusState *cs.State } // set sigPrefix based on account type and sign type - sigPrefix := []byte{} - if oracleInfo.Config.EnableSubAccountSigning { - sigPrefix = append(sigPrefix, types.SubAccountSigPrefix...) - } else { - sigPrefix = append(sigPrefix, types.MainAccountSigPrefix...) - } - - signType := oracleInfo.PubKey.Type() - switch signType { - case "ed25519": - sigPrefix = append(sigPrefix, types.Ed25519SignType...) - case "sr25519": - sigPrefix = append(sigPrefix, types.Sr25519SignType...) - case "secp256k1": - sigPrefix = append(sigPrefix, types.Secp256k1SignType...) - default: - log.Errorf("processSignVoteQueue: unsupported sign type: %v", signType) + sigPrefix, err := utils.FormSignaturePrefix(oracleInfo.Config.EnableSubAccountSigning, oracleInfo.PubKey.Type()) + if err != nil { + log.Errorf("processSignVoteQueue: unable to form sig prefix: %v", err) return } diff --git a/oracle/service/utils/utils.go b/oracle/service/utils/utils.go new file mode 100644 index 00000000000..8635700262f --- /dev/null +++ b/oracle/service/utils/utils.go @@ -0,0 +1,38 @@ +package utils + +import ( + "fmt" + + "github.com/cometbft/cometbft/oracle/service/types" +) + +// signature prefix for oracle votes is as such: +// index 0: accountType (if votes are signed by main val or oracle delegate) +// index 1: signType (type of key used: ed25519/sr25519/secp256k1) + +func GetAccountSignTypeFromSignature(signature []byte) (accountType []byte, signType []byte) { + return []byte{signature[0]}, []byte{signature[1]} +} + +func FormSignaturePrefix(isSubAccount bool, signType string) ([]byte, error) { + sigPrefix := []byte{} + + if isSubAccount { + sigPrefix = append(sigPrefix, types.SubAccountSigPrefix...) + } else { + sigPrefix = append(sigPrefix, types.MainAccountSigPrefix...) + } + + switch signType { + case "ed25519": + sigPrefix = append(sigPrefix, types.Ed25519SignType...) + case "sr25519": + sigPrefix = append(sigPrefix, types.Sr25519SignType...) + case "secp256k1": + sigPrefix = append(sigPrefix, types.Secp256k1SignType...) + default: + return nil, fmt.Errorf("FormSignaturePrefix: unsupported sign type: %v", signType) + } + + return sigPrefix, nil +}