Skip to content

Commit

Permalink
Implements a new signEthTransaction precompile (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruteri authored Oct 27, 2023
1 parent 6abc2b6 commit 33c2ea2
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/types/suave_structs.go

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

1 change: 1 addition & 0 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ var PrecompiledContractsSuave = map[common.Address]SuavePrecompiledContract{
fetchBidsAddress: newFetchBids(),
extractHintAddress: &extractHint{},

signEthTransactionAddress: &signEthTransaction{},
simulateBundleAddress: &simulateBundle{},
buildEthBlockAddress: &buildEthBlock{},
submitEthBlockBidToRelayAddress: &submitEthBlockBidToRelay{},
Expand Down
4 changes: 4 additions & 0 deletions core/vm/contracts_suave.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ func (b *suaveRuntime) confidentialStoreStore(bidId types.BidId, key string, dat
return (&confStoreStore{}).runImpl(b.suaveContext, bidId, key, data)
}

func (b *suaveRuntime) signEthTransaction(txn []byte, chainId string, signingKey string) ([]byte, error) {
return (&signEthTransaction{}).runImpl(txn, chainId, signingKey)
}

func (b *suaveRuntime) extractHint(bundleData []byte) ([]byte, error) {
return (&extractHint{}).runImpl(b.suaveContext, bundleData)
}
Expand Down
48 changes: 48 additions & 0 deletions core/vm/contracts_suave_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
)

var (
signEthTransactionAddress = common.HexToAddress("0x40100001")
simulateBundleAddress = common.HexToAddress("0x42100000")
extractHintAddress = common.HexToAddress("0x42100037")
buildEthBlockAddress = common.HexToAddress("0x42100001")
Expand All @@ -43,6 +44,53 @@ var (
fillMevShareBundleAddress = common.HexToAddress("0x43200001")
)

type signEthTransaction struct{}

func (c *signEthTransaction) RequiredGas(input []byte) uint64 {
// Should be proportional to bundle gas limit
return 1000
}

func (c *signEthTransaction) Run(input []byte) ([]byte, error) {
return nil, errors.New("not available in this context")
}

func (c *signEthTransaction) RunConfidential(suaveContext *SuaveContext, input []byte) ([]byte, error) {
return nil, errors.New("not available in this context")
}

func (c *signEthTransaction) runImpl(txn []byte, chainId string, signingKey string) ([]byte, error) {
key, err := crypto.HexToECDSA(signingKey)
if err != nil {
return nil, fmt.Errorf("key not formatted properly: %w", err)
}

chainIdInt, err := hexutil.DecodeBig(chainId)
if err != nil {
return nil, fmt.Errorf("chainId not formatted properly: %w", err)
}

var tx types.Transaction
err = tx.UnmarshalBinary(txn)
if err != nil {
return nil, fmt.Errorf("txn not formatted properly: %w", err)
}

signer := types.LatestSignerForChainID(chainIdInt)

signedTx, err := types.SignTx(&tx, signer, key)
if err != nil {
return nil, fmt.Errorf("could not sign: %w", err)
}

signedBytes, err := signedTx.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("could not encode signed transaction: %w", err)
}

return signedBytes, nil
}

type simulateBundle struct {
}

Expand Down
45 changes: 44 additions & 1 deletion core/vm/contracts_suave_runtime_adapter.go

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

17 changes: 17 additions & 0 deletions core/vm/contracts_suave_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vm
import (
"context"
"math/big"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -123,6 +124,10 @@ func TestSuavePrecompileStub(t *testing.T) {
"precompile buildEthBlock (0000000000000000000000000000000042100001) not allowed on 00000000000000000000000000000000",
}

expectedVariableErrors := []*regexp.Regexp{
regexp.MustCompile("key not formatted properly: invalid hex character.*in private key"),
}

for name, addr := range artifacts.SuaveMethods {
abiMethod, ok := artifacts.SuaveAbi.Methods[name]
if !ok {
Expand All @@ -140,6 +145,18 @@ func TestSuavePrecompileStub(t *testing.T) {
for _, expectedError := range expectedErrors {
if strings.Contains(err.Error(), expectedError) {
found = true
break
}
}

if found {
continue
}

for _, expectedErrRe := range expectedVariableErrors {
if expectedErrRe.Match([]byte(err.Error())) {
found = true
break
}
}
if !found {
Expand Down
3 changes: 3 additions & 0 deletions core/vm/suave.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ func (p *SuavePrecompiledContractWrapper) Run(input []byte) ([]byte, error) {
case extractHintAddress:
ret, err = stub.extractHint(input)

case signEthTransactionAddress:
ret, err = stub.signEthTransaction(input)

case simulateBundleAddress:
ret, err = stub.simulateBundle(input)

Expand Down
17 changes: 15 additions & 2 deletions suave/artifacts/Suave.sol/Suave.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "SIGN_ETH_TRANSACTION",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "SIMULATE_BUNDLE",
Expand Down Expand Up @@ -187,9 +200,9 @@
}
],
"deployedBytecode": {
"object": "0x73000000000000000000000000000000000000000030146080604052600436106100d95760003560e01c8063b61b127d11610096578063c91e11df11610070578063c91e11df1461015d578063d91525db14610168578063f0608b1c14610173578063f6ab3de51461017e57600080fd5b8063b61b127d1461013c578063b7817da014610147578063bc50c0051461015257600080fd5b806301c19530146100de578063040e51831461010557806369094cbc146101105780637320cb171461011b578063751afe2c1461012657806394804c6914610131575b600080fd5b6100e9634320000181565b6040516001600160a01b03909116815260200160405180910390f35b6100e9634210000381565b6100e9634201000181565b6100e9634203000081565b6100e9634210003781565b6100e9634210000181565b6100e9634210000081565b6100e9634202000081565b6100e9634210000281565b6100e9634203000181565b6100e9634201000081565b6100e9634300000181565b6100e963420200018156fea164736f6c6343000813000a"
"object": "0x73000000000000000000000000000000000000000030146080604052600436106100f45760003560e01c8063b61b127d11610096578063c91e11df11610070578063c91e11df14610183578063d91525db1461018e578063f0608b1c14610199578063f6ab3de5146101a457600080fd5b8063b61b127d14610162578063b7817da01461016d578063bc50c0051461017857600080fd5b80637320cb17116100d25780637320cb1714610136578063744795b914610141578063751afe2c1461014c57806394804c691461015757600080fd5b806301c19530146100f9578063040e51831461012057806369094cbc1461012b575b600080fd5b610104634320000181565b6040516001600160a01b03909116815260200160405180910390f35b610104634210000381565b610104634201000181565b610104634203000081565b610104634010000181565b610104634210003781565b610104634210000181565b610104634210000081565b610104634202000081565b610104634210000281565b610104634203000181565b610104634201000081565b610104634300000181565b61010463420200018156fea164736f6c6343000813000a"
},
"bytecode": {
"object": "0x61019661003a600b82828239805160001a60731461002d57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600436106100d95760003560e01c8063b61b127d11610096578063c91e11df11610070578063c91e11df1461015d578063d91525db14610168578063f0608b1c14610173578063f6ab3de51461017e57600080fd5b8063b61b127d1461013c578063b7817da014610147578063bc50c0051461015257600080fd5b806301c19530146100de578063040e51831461010557806369094cbc146101105780637320cb171461011b578063751afe2c1461012657806394804c6914610131575b600080fd5b6100e9634320000181565b6040516001600160a01b03909116815260200160405180910390f35b6100e9634210000381565b6100e9634201000181565b6100e9634203000081565b6100e9634210003781565b6100e9634210000181565b6100e9634210000081565b6100e9634202000081565b6100e9634210000281565b6100e9634203000181565b6100e9634201000081565b6100e9634300000181565b6100e963420200018156fea164736f6c6343000813000a"
"object": "0x6101bc61003a600b82828239805160001a60731461002d57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600436106100f45760003560e01c8063b61b127d11610096578063c91e11df11610070578063c91e11df14610183578063d91525db1461018e578063f0608b1c14610199578063f6ab3de5146101a457600080fd5b8063b61b127d14610162578063b7817da01461016d578063bc50c0051461017857600080fd5b80637320cb17116100d25780637320cb1714610136578063744795b914610141578063751afe2c1461014c57806394804c691461015757600080fd5b806301c19530146100f9578063040e51831461012057806369094cbc1461012b575b600080fd5b610104634320000181565b6040516001600160a01b03909116815260200160405180910390f35b610104634210000381565b610104634201000181565b610104634203000081565b610104634010000181565b610104634210003781565b610104634210000181565b610104634210000081565b610104634202000081565b610104634210000281565b610104634203000181565b610104634201000081565b610104634300000181565b61010463420200018156fea164736f6c6343000813000a"
}
}
Loading

0 comments on commit 33c2ea2

Please sign in to comment.