Skip to content

Commit

Permalink
Extend the keygen and sign precompiles to handle multiple crypto sign…
Browse files Browse the repository at this point in the history
…atures
  • Loading branch information
ferranbt committed Feb 18, 2024
1 parent 83a7be3 commit 73e1f91
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 109 deletions.
10 changes: 9 additions & 1 deletion core/types/suave_structs.go

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

34 changes: 28 additions & 6 deletions core/vm/contracts_suave.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/suave/consolelog"
suave "github.com/ethereum/go-ethereum/suave/core"
"github.com/flashbots/go-boost-utils/bls"
)

var (
Expand Down Expand Up @@ -131,18 +132,39 @@ func (b *suaveRuntime) fetchDataRecords(targetBlock uint64, namespace string) ([
return records, nil
}

func (s *suaveRuntime) signMessage(digest []byte, signingKey string) ([]byte, error) {
key, err := crypto.HexToECDSA(signingKey)
func (s *suaveRuntime) signMessage(digest []byte, cryptoType types.CryptoSignature, signingKey string) ([]byte, error) {
if !strings.HasPrefix(signingKey, "0x") {
// we need to prefix with 0x if not present because the 'hexutil.Decode' fails to decode if there is no '0x' prefix
signingKey = "0x" + signingKey
}
signingKeyBuf, err := hexutil.Decode(signingKey)
if err != nil {
return nil, fmt.Errorf("key not formatted properly: %w", err)
}

signature, err := crypto.Sign(digest, key)
if err != nil {
return nil, fmt.Errorf("Failed to sign message: %v", err)
if cryptoType == types.CryptoSignature_SECP256 {
key, err := crypto.ToECDSA(signingKeyBuf)
if err != nil {
return nil, fmt.Errorf("key not formatted properly: %w", err)
}

signature, err := crypto.Sign(digest, key)
if err != nil {
return nil, fmt.Errorf("failed to sign message: %v", err)
}
return signature, nil

} else if cryptoType == types.CryptoSignature_BLS {
suaveEthBlockSigningKey, err := bls.SecretKeyFromBytes(signingKeyBuf)
if err != nil {
fmt.Println("_B!", err)
return nil, fmt.Errorf("failed to sign message: %v", err)
}
signature := bls.Sign(suaveEthBlockSigningKey, digest).Bytes()
return signature[:], nil
}

return signature, nil
return nil, fmt.Errorf("unsupported crypto type")
}

func mustParseAbi(data string) abi.ABI {
Expand Down
19 changes: 14 additions & 5 deletions core/vm/contracts_suave_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,22 @@ func (b *suaveRuntime) buildEthBlock(blockArgs types.BuildBlockArgs, dataID type
return bidBytes, envelopeBytes, nil
}

func (b *suaveRuntime) privateKeyGen() (string, error) {
sk, err := crypto.GenerateKey()
if err != nil {
return "", fmt.Errorf("could not generate new a private key: %w", err)
func (b *suaveRuntime) privateKeyGen(cryptoType types.CryptoSignature) (string, error) {
if cryptoType == types.CryptoSignature_SECP256 {
sk, err := crypto.GenerateKey()
if err != nil {
return "", fmt.Errorf("could not generate new a private key: %w", err)
}
return hex.EncodeToString(crypto.FromECDSA(sk)), nil
} else if cryptoType == types.CryptoSignature_BLS {
sk, err := bls.GenerateRandomSecretKey()
if err != nil {
return "", fmt.Errorf("could not generate new a private key: %w", err)
}
return hex.EncodeToString(sk.Marshal()), nil
}

return hex.EncodeToString(crypto.FromECDSA(sk)), nil
return "", fmt.Errorf("unsupported crypto type %v", cryptoType)
}

func (b *suaveRuntime) submitEthBlockToRelay(relayUrl string, builderDataRecordJson []byte) ([]byte, error) {
Expand Down
28 changes: 21 additions & 7 deletions core/vm/contracts_suave_runtime_adapter.go

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

4 changes: 2 additions & 2 deletions core/vm/contracts_suave_runtime_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (m *mockRuntime) signEthTransaction(txn []byte, chainId string, signingKey
return []byte{0x1}, nil
}

func (m *mockRuntime) signMessage(digest []byte, signingKey string) ([]byte, error) {
func (m *mockRuntime) signMessage(digest []byte, crypto types.CryptoSignature, signingKey string) ([]byte, error) {
return []byte{0x1}, nil
}

Expand Down Expand Up @@ -83,7 +83,7 @@ func (m *mockRuntime) simulateTransaction(session string, txn []byte) (types.Sim
return types.SimulateTransactionResult{}, nil
}

func (m *mockRuntime) privateKeyGen() (string, error) {
func (m *mockRuntime) privateKeyGen(crypto types.CryptoSignature) (string, error) {
return "", nil
}

Expand Down
Loading

0 comments on commit 73e1f91

Please sign in to comment.