-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement the eth_sendRawTransaction
JSON-RPC endpoint
#26
Merged
m-Peter
merged 2 commits into
onflow:main
from
m-Peter:eth-send-raw-transaction-endpoint
Jan 31, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,18 +7,21 @@ import ( | |
"encoding/hex" | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/common/math" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/eth/filters" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
"github.com/ethereum/go-ethereum/rpc" | ||
"github.com/onflow/cadence" | ||
"github.com/onflow/flow-evm-gateway/storage" | ||
"github.com/onflow/flow-go-sdk" | ||
"github.com/onflow/flow-go-sdk/access" | ||
|
||
sdkCrypto "github.com/onflow/flow-go-sdk/crypto" | ||
) | ||
|
||
const EthNamespace = "eth" | ||
|
@@ -33,6 +36,9 @@ var ( | |
//go:embed cadence/scripts/bridged_account_call.cdc | ||
var bridgedAccountCall []byte | ||
|
||
//go:embed cadence/transactions/evm_run.cdc | ||
var evmRunTx []byte | ||
|
||
func SupportedAPIs(blockChainAPI *BlockChainAPI) []rpc.API { | ||
return []rpc.API{ | ||
{ | ||
|
@@ -101,7 +107,69 @@ func (api *BlockChainAPI) SendRawTransaction( | |
ctx context.Context, | ||
input hexutil.Bytes, | ||
) (common.Hash, error) { | ||
return crypto.Keccak256Hash([]byte("hello world")), nil | ||
gethTx := &types.Transaction{} | ||
encodedLen := uint(len(input)) | ||
err := gethTx.DecodeRLP( | ||
rlp.NewStream( | ||
bytes.NewReader(input), | ||
uint64(encodedLen), | ||
), | ||
) | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
|
||
block, err := api.FlowClient.GetLatestBlock(context.Background(), true) | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
|
||
privateKey, err := sdkCrypto.DecodePrivateKeyHex(sdkCrypto.ECDSA_P256, strings.Replace("2619878f0e2ff438d17835c2a4561cb87b4d24d72d12ec34569acd0dd4af7c21", "0x", "", 1)) | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
|
||
account, err := api.FlowClient.GetAccount(context.Background(), flow.HexToAddress("0xf8d6e0586b0a20c7")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This as well |
||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
accountKey := account.Keys[0] | ||
signer, err := sdkCrypto.NewInMemorySigner(privateKey, accountKey.HashAlgo) | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
|
||
tx := flow.NewTransaction(). | ||
SetScript(evmRunTx). | ||
SetProposalKey(account.Address, accountKey.Index, accountKey.SequenceNumber). | ||
SetReferenceBlockID(block.ID). | ||
SetPayer(account.Address). | ||
AddAuthorizer(account.Address) | ||
|
||
decodedTx, err := hex.DecodeString(input.String()[2:]) | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
cdcBytes := make([]cadence.Value, 0) | ||
for _, bt := range decodedTx { | ||
cdcBytes = append(cdcBytes, cadence.UInt8(bt)) | ||
} | ||
encodedTx := cadence.NewArray( | ||
cdcBytes, | ||
).WithType(cadence.NewVariableSizedArrayType(cadence.TheUInt8Type)) | ||
tx.AddArgument(encodedTx) | ||
|
||
err = tx.SignEnvelope(account.Address, accountKey.Index, signer) | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
|
||
err = api.FlowClient.SendTransaction(ctx, *tx) | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
|
||
return gethTx.Hash(), nil | ||
} | ||
|
||
// eth_createAccessList | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// TODO(m-Peter): Use proper address for each network | ||
import EVM from 0xf8d6e0586b0a20c7 | ||
|
||
transaction(encodedTx: [UInt8]) { | ||
let bridgedAccount: &EVM.BridgedAccount | ||
|
||
prepare(signer: auth(Storage) &Account) { | ||
self.bridgedAccount = signer.storage.borrow<&EVM.BridgedAccount>( | ||
from: /storage/evm | ||
) ?? panic("Could not borrow reference to the bridged account!") | ||
} | ||
|
||
execute { | ||
EVM.run(tx: encodedTx, coinbase: self.bridgedAccount.address()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you get this from ENV configuration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am planning to add a
flow.json
config file, to hold the account and private key that will be used for the gateway. Though I didn't get to it in this PR, to avoid cluttering things. Should we add it here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Just add a todo then maybe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing 👍 I also want to add the networks in there, where the indexer will connect to. And we'll see what other config makes sense to have in there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comments in 4ad8061