Skip to content

Commit

Permalink
abstract out getter and creation of sig prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
yan-soon committed Jul 17, 2024
1 parent c4faa49 commit 0da46e4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
4 changes: 2 additions & 2 deletions oracle/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
21 changes: 4 additions & 17 deletions oracle/service/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
38 changes: 38 additions & 0 deletions oracle/service/utils/utils.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 0da46e4

Please sign in to comment.