diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index e4400cb2c..984462ab9 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/rand" "fmt" + "math/big" "testing" "github.com/stretchr/testify/assert" @@ -48,7 +49,7 @@ func TestSigning(t *testing.T) { msg := []byte(("Flipity flobity floo")) sig, err := pk.Sign(msg) require.NoError(t, err) - ethSig, err := sig.GetEthSignature("floob") + ethSig, err := sig.GetEthSignature(big.NewInt(12342)) require.NoError(t, err) parity := ethSig.RecoveryIndex() require.True(t, parity == 0 || parity == 1) diff --git a/crypto/signature.go b/crypto/signature.go index afc85d27a..6ef310179 100644 --- a/crypto/signature.go +++ b/crypto/signature.go @@ -117,25 +117,16 @@ func (sig *Signature) String() string { return hex.EncodeUpperToString(sig.Signature) } -func GetEthChainID(chainID string) *big.Int { - b := new(big.Int) - id, ok := b.SetString(chainID, 10) - if ok { - return id - } - return b.SetBytes([]byte(chainID)) -} - -func GetEthSignatureRecoveryID(chainID string, parity *big.Int) *big.Int { +func GetEthSignatureRecoveryID(chainID *big.Int, parity *big.Int) *big.Int { // https://github.com/ethereum/EIPs/blob/b3bbee93dc8a775af6a6b2525c9ac5f70a7e5710/EIPS/eip-155.md v := new(big.Int) - v.Mul(GetEthChainID(chainID), big2) + v.Mul(chainID, big2) v.Add(v, parity) v.Add(v, ethereumRecoveryIDOffset) return v } -func (sig *Signature) GetEthSignature(chainID string) (*EIP155Signature, error) { +func (sig *Signature) GetEthSignature(chainID *big.Int) (*EIP155Signature, error) { if sig.CurveType != CurveTypeSecp256k1 { return nil, fmt.Errorf("can only GetEthSignature for %v keys, but got %v", CurveTypeSecp256k1, sig.CurveType) diff --git a/crypto/signature_test.go b/crypto/signature_test.go deleted file mode 100644 index 4e6dae626..000000000 --- a/crypto/signature_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package crypto - -import ( - "fmt" - "math/big" - "testing" -) - -func TestGetEthChainID(t *testing.T) { - chainIDString := "BurrowChain_FAB3C1-AB0FD1" - chainID := GetEthChainID(chainIDString) - b := new(big.Int).SetBytes([]byte(chainIDString)) - fmt.Println(b) - fmt.Println(chainID) - fmt.Printf("%X", chainID.Bytes()) -} diff --git a/encoding/ethereum.go b/encoding/ethereum.go new file mode 100644 index 000000000..0b511ff8c --- /dev/null +++ b/encoding/ethereum.go @@ -0,0 +1,28 @@ +package encoding + +import ( + "math/big" + "strings" + + "github.com/hyperledger/burrow/encoding/web3hex" +) + +// Convert Burrow's ChainID to a *big.Int so it can be used as a nonce for Ethereum signing. +// For compatibility with Ethereum tooling this function first tries to interpret the ChainID as an integer encoded +// either as an eth-style 0x-prefixed hex string or a base 10 integer, falling back to interpreting the string's +// raw bytes as a big-endian integer +func GetEthChainID(chainID string) *big.Int { + if strings.HasPrefix(chainID, "0x") { + d := new(web3hex.Decoder) + b := d.BigInt(chainID) + if d.Err() == nil { + return b + } + } + b := new(big.Int) + id, ok := b.SetString(chainID, 10) + if ok { + return id + } + return b.SetBytes([]byte(chainID)) +} diff --git a/encoding/ethereum_test.go b/encoding/ethereum_test.go new file mode 100644 index 000000000..2d28a5cf8 --- /dev/null +++ b/encoding/ethereum_test.go @@ -0,0 +1,17 @@ +package encoding + +import ( + "math/big" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetEthChainID(t *testing.T) { + assert.Equal(t, big.NewInt(1234), GetEthChainID("1234")) + assert.Equal(t, big.NewInt(1234), GetEthChainID("0x4d2")) + chainID, ok := new(big.Int).SetString("28980219985052679991929851741845949978287371722649499714751652210", 10) + require.True(t, ok) + assert.Equal(t, chainID, GetEthChainID("FrogsEatApplesOnlyWhenClear")) +} diff --git a/encoding/rlp/rlp_test.go b/encoding/rlp/rlp_test.go index 715849061..6564ab948 100644 --- a/encoding/rlp/rlp_test.go +++ b/encoding/rlp/rlp_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hyperledger/burrow/crypto" + "github.com/hyperledger/burrow/encoding" "github.com/test-go/testify/require" ) @@ -285,7 +286,7 @@ func TestEthRawTx(t *testing.T) { To: to[:], Amount: big.NewInt(232), Data: []byte{1, 3, 4}, - ChainID: crypto.GetEthChainID("flgoo"), + ChainID: encoding.GetEthChainID("flgoo"), V: big.NewInt(272), R: bigly, S: bigly, diff --git a/encoding/web3hex/decoder.go b/encoding/web3hex/decoder.go new file mode 100644 index 000000000..1bce7c496 --- /dev/null +++ b/encoding/web3hex/decoder.go @@ -0,0 +1,76 @@ +package web3hex + +import ( + "fmt" + "math/big" + "strings" + + "github.com/hyperledger/burrow/crypto" + "github.com/tmthrgd/go-hex" +) + +type Decoder struct { + error + must bool +} + +func (d *Decoder) Must() *Decoder { + return &Decoder{must: true} +} + +func (d *Decoder) Err() error { + return d.error +} + +func (d *Decoder) pushErr(err error) { + if d.must { + panic(err) + } + if d.error == nil { + d.error = err + } +} + +func (d *Decoder) Bytes(hs string) []byte { + hexString := strings.TrimPrefix(hs, "0x") + // Ethereum returns odd-length hexString strings when it removes leading zeros + if len(hexString)%2 == 1 { + hexString = "0" + hexString + } + bs, err := hex.DecodeString(hexString) + if err != nil { + d.pushErr(fmt.Errorf("could not decode bytes from '%s': %w", hs, err)) + } + return bs +} + +func (d *Decoder) Address(hs string) crypto.Address { + if hs == "" { + return crypto.Address{} + } + address, err := crypto.AddressFromBytes(d.Bytes(hs)) + if err != nil { + d.pushErr(fmt.Errorf("could not decode address from '%s': %w", hs, err)) + } + return address +} + +func (d *Decoder) BigInt(hs string) *big.Int { + return new(big.Int).SetBytes(d.Bytes(hs)) +} + +func (d *Decoder) Uint64(hs string) uint64 { + bi := d.BigInt(hs) + if !bi.IsUint64() { + d.pushErr(fmt.Errorf("%v is not uint64", bi)) + } + return bi.Uint64() +} + +func (d *Decoder) Int64(hs string) int64 { + bi := d.BigInt(hs) + if !bi.IsInt64() { + d.pushErr(fmt.Errorf("%v is not int64", bi)) + } + return bi.Int64() +} diff --git a/encoding/web3hex/decoder_test.go b/encoding/web3hex/decoder_test.go new file mode 100644 index 000000000..548de91e3 --- /dev/null +++ b/encoding/web3hex/decoder_test.go @@ -0,0 +1,17 @@ +package web3hex + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestDecoder_Bytes(t *testing.T) { + d := new(Decoder) + assert.Equal(t, []byte{}, d.Bytes("")) + assert.Equal(t, []byte{1}, d.Bytes("0x1")) + assert.Equal(t, []byte{1}, d.Bytes("0x01")) + assert.Equal(t, []byte{1, 0xff}, d.Bytes("0x1ff")) + require.NoError(t, d.Err()) +} diff --git a/encoding/web3hex/encoder.go b/encoding/web3hex/encoder.go new file mode 100644 index 000000000..1a479fcd2 --- /dev/null +++ b/encoding/web3hex/encoder.go @@ -0,0 +1,54 @@ +package web3hex + +import ( + bin "encoding/binary" + "math/big" + "strings" + + "github.com/hyperledger/burrow/crypto" + "github.com/tmthrgd/go-hex" +) + +type encoder struct { +} + +var Encoder = new(encoder) + +func (e *encoder) Bytes(bs []byte) string { + return "0x" + hex.EncodeToString(bs) +} + +func (e *encoder) BytesTrim(bs []byte) string { + if len(bs) == 0 { + return "" + } + str := hex.EncodeToString(bs) + // Ethereum expects leading zeros to be removed from RLP encodings (SMH) + str = strings.TrimLeft(str, "0") + if len(str) == 0 { + // Special case for zero + return "0x0" + } + return "0x" + str +} + +func (e *encoder) BigInt(x *big.Int) string { + return e.BytesTrim(x.Bytes()) +} + +func (e *encoder) Uint64OmitEmpty(x uint64) string { + if x == 0 { + return "" + } + return e.Uint64(x) +} + +func (e *encoder) Uint64(x uint64) string { + bs := make([]byte, 8) + bin.BigEndian.PutUint64(bs, x) + return e.BytesTrim(bs) +} + +func (e *encoder) Address(address crypto.Address) string { + return e.BytesTrim(address.Bytes()) +} diff --git a/encoding/web3hex/encoder_test.go b/encoding/web3hex/encoder_test.go new file mode 100644 index 000000000..c9aa054f7 --- /dev/null +++ b/encoding/web3hex/encoder_test.go @@ -0,0 +1,15 @@ +package web3hex + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEncoder_BytesTrim(t *testing.T) { + assert.Equal(t, "", Encoder.BytesTrim(nil)) + assert.Equal(t, "", Encoder.BytesTrim([]byte{})) + assert.Equal(t, "0x0", Encoder.BytesTrim([]byte{0})) + assert.Equal(t, "0x1", Encoder.BytesTrim([]byte{1})) + assert.Equal(t, "0x1ff", Encoder.BytesTrim([]byte{1, 255})) +} diff --git a/execution/evm/contract.go b/execution/evm/contract.go index c7b8b9cd1..201926e04 100644 --- a/execution/evm/contract.go +++ b/execution/evm/contract.go @@ -11,6 +11,7 @@ import ( "github.com/hyperledger/burrow/acm" . "github.com/hyperledger/burrow/binary" "github.com/hyperledger/burrow/crypto" + "github.com/hyperledger/burrow/encoding" "github.com/hyperledger/burrow/execution/engine" "github.com/hyperledger/burrow/execution/errors" "github.com/hyperledger/burrow/execution/evm/abi" @@ -488,7 +489,7 @@ func (c *Contract) execute(st engine.State, params engine.CallParams) ([]byte, e c.debugf(" => %v\n", *params.Gas) case CHAINID: // 0x46 - id := crypto.GetEthChainID(st.Blockchain.ChainID()) + id := encoding.GetEthChainID(st.Blockchain.ChainID()) stack.PushBigInt(id) c.debugf(" => %X\n", id) diff --git a/rpc/web3/eth_service.go b/rpc/web3/eth_service.go index 75362739e..16a2c222a 100644 --- a/rpc/web3/eth_service.go +++ b/rpc/web3/eth_service.go @@ -7,7 +7,9 @@ import ( "math/big" "strconv" + "github.com/hyperledger/burrow/encoding" "github.com/hyperledger/burrow/encoding/rlp" + "github.com/hyperledger/burrow/encoding/web3hex" "github.com/hyperledger/burrow/acm/acmstate" "github.com/hyperledger/burrow/acm/balance" @@ -74,7 +76,7 @@ func NewEthService( keyStore: keyStore, config: tmConfig.DefaultConfig(), // Ethereum expects ChainID to be an integer value - chainID: crypto.GetEthChainID(blockchain.ChainID()), + chainID: encoding.GetEthChainID(blockchain.ChainID()), logger: logger, } } @@ -97,9 +99,9 @@ func (srv *EthService) Web3ClientVersion() (*Web3ClientVersionResult, error) { // Web3Sha3 returns Keccak-256 (not the standardized SHA3-256) of the given data func (srv *EthService) Web3Sha3(req *Web3Sha3Params) (*Web3Sha3Result, error) { - d := new(HexDecoder) + d := new(web3hex.Decoder) return &Web3Sha3Result{ - HashedData: HexEncoder.Bytes(crypto.Keccak256(d.Bytes(req.Data))), + HashedData: web3hex.Encoder.Bytes(crypto.Keccak256(d.Bytes(req.Data))), }, d.Err() } @@ -112,7 +114,7 @@ func (srv *EthService) NetListening() (*NetListeningResult, error) { // NetPeerCount returns the number of connected peers func (srv *EthService) NetPeerCount() (*NetPeerCountResult, error) { - s := HexEncoder.Uint64(uint64(srv.nodeView.Peers().Size())) + s := web3hex.Encoder.Uint64(uint64(srv.nodeView.Peers().Size())) return &NetPeerCountResult{ s, }, nil @@ -122,7 +124,7 @@ func (srv *EthService) NetPeerCount() (*NetPeerCountResult, error) { // this is typically a small int (where 1 == Ethereum mainnet) func (srv *EthService) NetVersion() (*NetVersionResult, error) { return &NetVersionResult{ - ChainID: HexEncoder.BigInt(srv.chainID), + ChainID: web3hex.Encoder.BigInt(srv.chainID), }, nil } @@ -136,20 +138,20 @@ func (srv *EthService) EthProtocolVersion() (*EthProtocolVersionResult, error) { // EthChainId returns the chainID func (srv *EthService) EthChainId() (*EthChainIdResult, error) { return &EthChainIdResult{ - ChainId: HexEncoder.BigInt(srv.chainID), + ChainId: web3hex.Encoder.BigInt(srv.chainID), }, nil } // EthBlockNumber returns the latest height func (srv *EthService) EthBlockNumber() (*EthBlockNumberResult, error) { return &EthBlockNumberResult{ - BlockNumber: HexEncoder.Uint64(srv.blockchain.LastBlockHeight()), + BlockNumber: web3hex.Encoder.Uint64(srv.blockchain.LastBlockHeight()), }, nil } // EthCall executes a new message call immediately without creating a transaction func (srv *EthService) EthCall(req *EthCallParams) (*EthCallResult, error) { - d := new(HexDecoder) + d := new(web3hex.Decoder) from := d.Address(req.Transaction.From) to := d.Address(req.Transaction.To) @@ -167,7 +169,7 @@ func (srv *EthService) EthCall(req *EthCallParams) (*EthCallResult, error) { var result string if r := txe.GetResult(); r != nil { - result = HexEncoder.Bytes(r.GetReturn()) + result = web3hex.Encoder.Bytes(r.GetReturn()) } return &EthCallResult{ @@ -177,7 +179,7 @@ func (srv *EthService) EthCall(req *EthCallParams) (*EthCallResult, error) { // EthGetBalance returns an accounts balance, or an error if it does not exist func (srv *EthService) EthGetBalance(req *EthGetBalanceParams) (*EthGetBalanceResult, error) { - d := new(HexDecoder) + d := new(web3hex.Decoder) addr := d.Address(req.Address) if d.Err() != nil { return nil, d.Err() @@ -192,7 +194,7 @@ func (srv *EthService) EthGetBalance(req *EthGetBalanceParams) (*EthGetBalanceRe } return &EthGetBalanceResult{ - GetBalanceResult: HexEncoder.Bytes(balance.NativeToWei(acc.Balance).Bytes()), + GetBalanceResult: web3hex.Encoder.Bytes(balance.NativeToWei(acc.Balance).Bytes()), }, nil } @@ -243,7 +245,7 @@ func (srv *EthService) EthGetBlockTransactionCountByHash(req *EthGetBlockTransac } return &EthGetBlockTransactionCountByHashResult{ - BlockTransactionCountByHash: HexEncoder.Uint64(uint64(numTxs)), + BlockTransactionCountByHash: web3hex.Encoder.Uint64(uint64(numTxs)), }, nil } @@ -260,13 +262,13 @@ func (srv *EthService) EthGetBlockTransactionCountByNumber(req *EthGetBlockTrans } return &EthGetBlockTransactionCountByNumberResult{ - BlockTransactionCountByHash: HexEncoder.Uint64(uint64(numTxs)), + BlockTransactionCountByHash: web3hex.Encoder.Uint64(uint64(numTxs)), }, nil } // EthGetCode returns the EVM bytecode at an address func (srv *EthService) EthGetCode(req *EthGetCodeParams) (*EthGetCodeResult, error) { - d := new(HexDecoder) + d := new(web3hex.Decoder) addr := d.Address(req.Address) if d.Err() != nil { return nil, d.Err() @@ -280,7 +282,7 @@ func (srv *EthService) EthGetCode(req *EthGetCodeParams) (*EthGetCodeResult, err } return &EthGetCodeResult{ - Bytes: HexEncoder.Bytes(acc.EVMCode), + Bytes: web3hex.Encoder.Bytes(acc.EVMCode), }, nil } @@ -305,7 +307,7 @@ func (srv *EthService) EthGetTransactionByBlockHashAndIndex(req *EthGetTransacti return nil, err } - d := new(HexDecoder) + d := new(web3hex.Decoder) index := d.Uint64(req.Index) @@ -343,7 +345,7 @@ func (srv *EthService) EthGetTransactionByBlockNumberAndIndex(req *EthGetTransac if err != nil { return nil, err } - d := new(HexDecoder) + d := new(web3hex.Decoder) index := d.Uint64(req.Index) if d.Err() != nil { return nil, d.Err() @@ -366,7 +368,7 @@ func (srv *EthService) EthGetTransactionByBlockNumberAndIndex(req *EthGetTransac // EthGetTransactionByHash finds a tx by the given hash func (srv *EthService) EthGetTransactionByHash(req *EthGetTransactionByHashParams) (*EthGetTransactionByHashResult, error) { - d := new(HexDecoder) + d := new(web3hex.Decoder) hash := d.Bytes(req.TransactionHash) if d.Err() != nil { @@ -394,7 +396,7 @@ func (srv *EthService) EthGetTransactionByHash(req *EthGetTransactionByHashParam // EthGetTransactionCount returns the number of transactions sent from an address func (srv *EthService) EthGetTransactionCount(req *EthGetTransactionCountParams) (*EthGetTransactionCountResult, error) { - d := new(HexDecoder) + d := new(web3hex.Decoder) addr := d.Address(req.Address) if d.Err() != nil { return nil, d.Err() @@ -408,7 +410,7 @@ func (srv *EthService) EthGetTransactionCount(req *EthGetTransactionCountParams) // TODO: sequence may not always be accurate, is there a better way? return &EthGetTransactionCountResult{ - NonceOrNull: HexEncoder.Uint64(acc.GetSequence()), + NonceOrNull: web3hex.Encoder.Uint64(acc.GetSequence()), }, nil } @@ -430,7 +432,7 @@ func getHashAndCallTxFromExecution(txe *exec.TxExecution) ([]byte, *payload.Call // EthGetTransactionReceipt returns the receipt of a previously committed tx func (srv *EthService) EthGetTransactionReceipt(req *EthGetTransactionReceiptParams) (*EthGetTransactionReceiptResult, error) { - d := new(HexDecoder) + d := new(web3hex.Decoder) data := d.Bytes(req.TransactionHash) if d.Err() != nil { @@ -454,20 +456,20 @@ func (srv *EthService) EthGetTransactionReceipt(req *EthGetTransactionReceiptPar return nil, err } - status := HexEncoder.Uint64(1) + status := web3hex.Encoder.Uint64(1) if err := txe.Exception.AsError(); err != nil { - status = HexEncoder.Uint64(0) + status = web3hex.Encoder.Uint64(0) } result := &EthGetTransactionReceiptResult{ Receipt: Receipt{ Status: status, - TransactionIndex: HexEncoder.Uint64(txe.GetIndex()), - BlockNumber: HexEncoder.Uint64(uint64(block.Height)), - BlockHash: HexEncoder.Bytes(block.Hash()), - From: HexEncoder.Bytes(tx.GetInput().Address.Bytes()), - GasUsed: HexEncoder.Uint64(txe.Result.GetGasUsed()), - TransactionHash: HexEncoder.Bytes(hash), + TransactionIndex: web3hex.Encoder.Uint64(txe.GetIndex()), + BlockNumber: web3hex.Encoder.Uint64(uint64(block.Height)), + BlockHash: web3hex.Encoder.Bytes(block.Hash()), + From: web3hex.Encoder.Bytes(tx.GetInput().Address.Bytes()), + GasUsed: web3hex.Encoder.Uint64(txe.Result.GetGasUsed()), + TransactionHash: web3hex.Encoder.Bytes(hash), CumulativeGasUsed: hexZero, LogsBloom: hexZero, Logs: []Logs{}, @@ -475,10 +477,10 @@ func (srv *EthService) EthGetTransactionReceipt(req *EthGetTransactionReceiptPar } if txe.Receipt != nil { - result.Receipt.ContractAddress = HexEncoder.Bytes(txe.Receipt.ContractAddress.Bytes()) + result.Receipt.ContractAddress = web3hex.Encoder.Bytes(txe.Receipt.ContractAddress.Bytes()) result.Receipt.To = pending } else if tx.Address != nil { - result.Receipt.To = HexEncoder.Bytes(tx.Address.Bytes()) + result.Receipt.To = web3hex.Encoder.Bytes(tx.Address.Bytes()) } return result, nil @@ -563,7 +565,7 @@ func (srv *EthService) EthGetRawTransactionByBlockNumberAndIndex(req *EthGetRawT } func (srv *EthService) EthSendRawTransaction(req *EthSendRawTransactionParams) (*EthSendRawTransactionResult, error) { - d := new(HexDecoder) + d := new(web3hex.Decoder) data := d.Bytes(req.SignedTransactionData) @@ -627,7 +629,7 @@ func (srv *EthService) EthSendRawTransaction(req *EthSendRawTransactionParams) ( } return &EthSendRawTransactionResult{ - TransactionHash: HexEncoder.Bytes(txe.GetTxHash().Bytes()), + TransactionHash: web3hex.Encoder.Bytes(txe.GetTxHash().Bytes()), }, nil } @@ -636,7 +638,7 @@ func (srv *EthService) EthSyncing() (*EthSyncingResult, error) { // TODO: remaining sync fields return &EthSyncingResult{ Syncing: SyncStatus{ - CurrentBlock: HexEncoder.Uint64(srv.blockchain.LastBlockHeight()), + CurrentBlock: web3hex.Encoder.Uint64(srv.blockchain.LastBlockHeight()), }, }, nil } @@ -658,12 +660,12 @@ func (srv *EthService) getBlockHeaderAtHeight(height uint64) (*types.Header, err } func hexKeccak(data []byte) string { - return HexEncoder.Bytes(crypto.Keccak256(data)) + return web3hex.Encoder.Bytes(crypto.Keccak256(data)) } func hexKeccakAddress(data []byte) string { addr := crypto.Keccak256(data) - return HexEncoder.Bytes(addr[len(addr)-20:]) + return web3hex.Encoder.Bytes(addr[len(addr)-20:]) } func (srv *EthService) getBlockInfoAtHeight(height uint64, includeTxs bool) (Block, error) { @@ -678,8 +680,8 @@ func (srv *EthService) getBlockInfoAtHeight(height uint64, includeTxs bool) (Blo ParentHash: hexKeccak(doc.AppHash.Bytes()), ReceiptsRoot: hexKeccak(doc.AppHash.Bytes()), StateRoot: hexKeccak(doc.AppHash.Bytes()), - Miner: HexEncoder.Bytes(doc.Validators[0].Address.Bytes()), - Timestamp: HexEncoder.Uint64(uint64(doc.GenesisTime.Unix())), + Miner: web3hex.Encoder.Bytes(doc.Validators[0].Address.Bytes()), + Timestamp: web3hex.Encoder.Uint64(uint64(doc.GenesisTime.Unix())), Number: hexZero, Size: hexZero, ExtraData: hexZero, @@ -725,17 +727,17 @@ func (srv *EthService) getBlockInfoAtHeight(height uint64, includeTxs bool) (Blo StateRoot: hexKeccak(block.Hash().Bytes()), ReceiptsRoot: hexKeccak(block.Hash().Bytes()), Nonce: hexZeroNonce, - Size: HexEncoder.Uint64(uint64(numTxs)), - Number: HexEncoder.Uint64(uint64(block.Height)), - Miner: HexEncoder.Bytes(block.ProposerAddress.Bytes()), + Size: web3hex.Encoder.Uint64(uint64(numTxs)), + Number: web3hex.Encoder.Uint64(uint64(block.Height)), + Miner: web3hex.Encoder.Bytes(block.ProposerAddress.Bytes()), Sha3Uncles: hexZero, LogsBloom: hexZero, ExtraData: hexZero, Difficulty: hexZero, TotalDifficulty: hexZero, GasUsed: hexZero, - GasLimit: HexEncoder.Uint64(maxGasLimit), - Timestamp: HexEncoder.Uint64(uint64(block.Time.Unix())), + GasLimit: web3hex.Encoder.Uint64(maxGasLimit), + Timestamp: web3hex.Encoder.Uint64(uint64(block.Time.Unix())), Transactions: transactions, Uncles: []string{}, }, nil @@ -747,24 +749,24 @@ func getTransaction(block *types.Header, hash []byte, tx *payload.CallTx) Transa V: hexZero, R: hexZero, S: hexZero, - From: HexEncoder.Bytes(tx.Input.Address.Bytes()), - Value: HexEncoder.Uint64(tx.Input.Amount), - Nonce: HexEncoder.Uint64(tx.Input.Sequence), - Gas: HexEncoder.Uint64(tx.GasLimit), - GasPrice: HexEncoder.Uint64(tx.GasPrice), - Data: HexEncoder.Bytes(tx.Data), + From: web3hex.Encoder.Bytes(tx.Input.Address.Bytes()), + Value: web3hex.Encoder.Uint64(tx.Input.Amount), + Nonce: web3hex.Encoder.Uint64(tx.Input.Sequence), + Gas: web3hex.Encoder.Uint64(tx.GasLimit), + GasPrice: web3hex.Encoder.Uint64(tx.GasPrice), + Data: web3hex.Encoder.Bytes(tx.Data), } if block != nil { // may be pending transaction.BlockHash = hexKeccak(block.Hash().Bytes()) - transaction.Hash = HexEncoder.Bytes(hash) - transaction.BlockNumber = HexEncoder.Uint64(uint64(block.Height)) + transaction.Hash = web3hex.Encoder.Bytes(hash) + transaction.BlockNumber = web3hex.Encoder.Uint64(uint64(block.Height)) transaction.TransactionIndex = hexZero } if tx.Address != nil { - transaction.To = HexEncoder.Bytes(tx.Address.Bytes()) + transaction.To = web3hex.Encoder.Bytes(tx.Address.Bytes()) } else { transaction.To = pending } @@ -785,7 +787,7 @@ func (srv *EthService) getHeightByWord(height string) (uint64, bool) { } func getHeightByNumber(height string) (uint64, error) { - d := new(HexDecoder) + d := new(web3hex.Decoder) return d.Uint64(height), d.Err() } @@ -809,7 +811,7 @@ func (srv *EthService) EthSendTransaction(req *EthSendTransactionParams) (*EthSe } var err error - d := new(HexDecoder) + d := new(web3hex.Decoder) if from := req.Transaction.From; from != "" { tx.Input.Address = d.Address(from) if d.Err() != nil { @@ -868,7 +870,7 @@ func (srv *EthService) EthSendTransaction(req *EthSendTransactionParams) (*EthSe } return &EthSendTransactionResult{ - TransactionHash: HexEncoder.Bytes(txe.GetTxHash().Bytes()), + TransactionHash: web3hex.Encoder.Bytes(txe.GetTxHash().Bytes()), }, nil } @@ -893,7 +895,7 @@ func (srv *EthService) EthAccounts() (*EthAccountsResult, error) { continue } // TODO: only return accounts that exist in current chain - addrs = append(addrs, HexEncoder.Bytes(key.Address.Bytes())) + addrs = append(addrs, web3hex.Encoder.Bytes(key.Address.Bytes())) } return &EthAccountsResult{ @@ -903,7 +905,7 @@ func (srv *EthService) EthAccounts() (*EthAccountsResult, error) { // EthSign: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign func (srv *EthService) EthSign(req *EthSignParams) (*EthSignResult, error) { - d := new(HexDecoder) + d := new(web3hex.Decoder) to := d.Address(req.Address) signer, err := keys.AddressableSigner(srv.keyClient, to) if err != nil { @@ -925,7 +927,7 @@ func (srv *EthService) EthSign(req *EthSignParams) (*EthSignResult, error) { } return &EthSignResult{ - Signature: HexEncoder.Bytes(sig.RawBytes()), + Signature: web3hex.Encoder.Bytes(sig.RawBytes()), }, nil } diff --git a/rpc/web3/eth_service_test.go b/rpc/web3/eth_service_test.go index e3881f9cb..594ead86b 100644 --- a/rpc/web3/eth_service_test.go +++ b/rpc/web3/eth_service_test.go @@ -10,6 +10,8 @@ import ( "github.com/hyperledger/burrow/acm/balance" "github.com/hyperledger/burrow/crypto" + "github.com/hyperledger/burrow/encoding" + "github.com/hyperledger/burrow/encoding/web3hex" "github.com/hyperledger/burrow/execution/evm/abi" "github.com/hyperledger/burrow/integration" "github.com/hyperledger/burrow/keys" @@ -22,7 +24,7 @@ import ( "github.com/stretchr/testify/require" ) -var d = new(web3.HexDecoder).Must() +var d = new(web3hex.Decoder).Must() // Check we can force set a decimal ChainID const chainID = "15321" @@ -93,7 +95,7 @@ func TestWeb3Service(t *testing.T) { t.Run("NetVersion", func(t *testing.T) { result, err := eth.NetVersion() require.NoError(t, err) - require.Equal(t, web3.HexEncoder.BigInt(crypto.GetEthChainID(genesisDoc.GetChainID())), result.ChainID) + require.Equal(t, web3hex.Encoder.BigInt(encoding.GetEthChainID(genesisDoc.GetChainID())), result.ChainID) }) t.Run("EthProtocolVersion", func(t *testing.T) { @@ -106,7 +108,7 @@ func TestWeb3Service(t *testing.T) { result, err := eth.EthChainId() require.NoError(t, err) doc := config.GenesisDoc - require.Equal(t, web3.HexEncoder.BigInt(crypto.GetEthChainID(doc.GetChainID())), result.ChainId) + require.Equal(t, web3hex.Encoder.BigInt(encoding.GetEthChainID(doc.GetChainID())), result.ChainId) }) }) @@ -139,7 +141,7 @@ func TestWeb3Service(t *testing.T) { bs, err := rawTx.Marshal() require.NoError(t, err) - raw := web3.HexEncoder.BytesTrim(bs) + raw := web3hex.Encoder.BytesTrim(bs) _, err = eth.EthSendRawTransaction(&web3.EthSendRawTransactionParams{ SignedTransactionData: raw, @@ -149,7 +151,7 @@ func TestWeb3Service(t *testing.T) { t.Run("EthGetBalance", func(t *testing.T) { result, err := eth.EthGetBalance(&web3.EthGetBalanceParams{ - Address: web3.HexEncoder.BytesTrim(receivee.Bytes()), + Address: web3hex.Encoder.BytesTrim(receivee.Bytes()), BlockNumber: "latest", }) require.NoError(t, err) @@ -163,7 +165,7 @@ func TestWeb3Service(t *testing.T) { Address: genesisAccounts[1].GetAddress().String(), }) require.NoError(t, err) - require.Equal(t, web3.HexEncoder.Uint64(1), result.NonceOrNull) + require.Equal(t, web3hex.Encoder.Uint64(1), result.NonceOrNull) }) // create contract on chain @@ -179,9 +181,9 @@ func TestWeb3Service(t *testing.T) { go func() { tx := &web3.EthSendTransactionParams{ Transaction: web3.Transaction{ - From: web3.HexEncoder.BytesTrim(genesisAccounts[3].GetAddress().Bytes()), - Gas: web3.HexEncoder.Uint64(uint64(40 + idx)), // make tx unique in mempool - Data: web3.HexEncoder.BytesTrim(rpc.Bytecode_HelloWorld), + From: web3hex.Encoder.BytesTrim(genesisAccounts[3].GetAddress().Bytes()), + Gas: web3hex.Encoder.Uint64(uint64(40 + idx)), // make tx unique in mempool + Data: web3hex.Encoder.BytesTrim(rpc.Bytecode_HelloWorld), }, } result, err := eth.EthSendTransaction(tx) @@ -202,9 +204,9 @@ func TestWeb3Service(t *testing.T) { t.Run("EthGetTransactionReceipt", func(t *testing.T) { sendResult, err := eth.EthSendTransaction(&web3.EthSendTransactionParams{ Transaction: web3.Transaction{ - From: web3.HexEncoder.BytesTrim(genesisAccounts[3].GetAddress().Bytes()), - Gas: web3.HexEncoder.Uint64(40), - Data: web3.HexEncoder.BytesTrim(rpc.Bytecode_HelloWorld), + From: web3hex.Encoder.BytesTrim(genesisAccounts[3].GetAddress().Bytes()), + Gas: web3hex.Encoder.Uint64(40), + Data: web3hex.Encoder.BytesTrim(rpc.Bytecode_HelloWorld), }, }) require.NoError(t, err) @@ -226,9 +228,9 @@ func TestWeb3Service(t *testing.T) { result, err := eth.EthCall(&web3.EthCallParams{ Transaction: web3.Transaction{ - From: web3.HexEncoder.BytesTrim(genesisAccounts[1].GetAddress().Bytes()), + From: web3hex.Encoder.BytesTrim(genesisAccounts[1].GetAddress().Bytes()), To: contractAddress, - Data: web3.HexEncoder.BytesTrim(packed), + Data: web3hex.Encoder.BytesTrim(packed), }, }) require.NoError(t, err) @@ -246,7 +248,7 @@ func TestWeb3Service(t *testing.T) { Address: contractAddress, }) require.NoError(t, err) - require.Equal(t, web3.HexEncoder.BytesTrim(rpc.DeployedBytecode_HelloWorld), strings.ToLower(result.Bytes)) + require.Equal(t, web3hex.Encoder.BytesTrim(rpc.DeployedBytecode_HelloWorld), strings.ToLower(result.Bytes)) }) }) @@ -262,7 +264,7 @@ func TestWeb3Service(t *testing.T) { require.Len(t, result.Addresses, len(genesisAccounts)-1) for _, acc := range genesisAccounts { if acc.PrivateKey().CurveType == crypto.CurveTypeSecp256k1 { - require.Contains(t, result.Addresses, web3.HexEncoder.BytesTrim(acc.GetAddress().Bytes())) + require.Contains(t, result.Addresses, web3hex.Encoder.BytesTrim(acc.GetAddress().Bytes())) } } }) @@ -277,7 +279,7 @@ func TestWeb3Service(t *testing.T) { }) t.Run("EthGetBlock", func(t *testing.T) { - numberResult, err := eth.EthGetBlockByNumber(&web3.EthGetBlockByNumberParams{BlockNumber: web3.HexEncoder.Uint64(1)}) + numberResult, err := eth.EthGetBlockByNumber(&web3.EthGetBlockByNumberParams{BlockNumber: web3hex.Encoder.Uint64(1)}) require.NoError(t, err) hashResult, err := eth.EthGetBlockByHash(&web3.EthGetBlockByHashParams{BlockHash: numberResult.GetBlockByNumberResult.Hash}) require.NoError(t, err) diff --git a/rpc/web3/ethclient/client.go b/rpc/web3/ethclient/client.go index 99365c263..cdeab8f29 100644 --- a/rpc/web3/ethclient/client.go +++ b/rpc/web3/ethclient/client.go @@ -6,6 +6,7 @@ import ( "time" "github.com/hyperledger/burrow/crypto" + "github.com/hyperledger/burrow/encoding/web3hex" "github.com/hyperledger/burrow/rpc" "github.com/hyperledger/burrow/rpc/rpcevents" "github.com/hyperledger/burrow/rpc/web3" @@ -58,7 +59,7 @@ func (c *EthClient) SendRawTransaction(txHex string) (string, error) { func (c *EthClient) GetTransactionCount(address crypto.Address) (string, error) { var count string - err := c.Call(EthGetTransactionCountMethod, []string{web3.HexEncoder.Address(address), "latest"}, &count) + err := c.Call(EthGetTransactionCountMethod, []string{web3hex.Encoder.Address(address), "latest"}, &count) if err != nil { return "", err } @@ -125,7 +126,7 @@ func (c *EthClient) BlockNumber() (uint64, error) { if err != nil { return 0, err } - d := new(web3.HexDecoder) + d := new(web3hex.Decoder) return d.Uint64(*latestBlock), d.Err() } @@ -192,7 +193,7 @@ func logBound(bound *rpcevents.Bound) string { case rpcevents.Bound_LATEST: return "latest" case rpcevents.Bound_ABSOLUTE: - return web3.HexEncoder.Uint64(bound.Index) + return web3hex.Encoder.Uint64(bound.Index) default: return "" } diff --git a/rpc/web3/ethclient/client_test.go b/rpc/web3/ethclient/client_test.go index b155e2b4e..1086c5804 100644 --- a/rpc/web3/ethclient/client_test.go +++ b/rpc/web3/ethclient/client_test.go @@ -8,9 +8,9 @@ import ( "testing" "github.com/hyperledger/burrow/crypto" + "github.com/hyperledger/burrow/encoding/web3hex" "github.com/hyperledger/burrow/execution/solidity" "github.com/hyperledger/burrow/rpc/rpcevents" - "github.com/hyperledger/burrow/rpc/web3" "github.com/hyperledger/burrow/tests/web3/web3test" "github.com/stretchr/testify/assert" @@ -28,11 +28,11 @@ func TestEthAccounts(t *testing.T) { func TestEthSendTransaction(t *testing.T) { pk := web3test.GetPrivateKey(t) - d := new(web3.HexDecoder) + d := new(web3hex.Decoder) param := &EthSendTransactionParam{ - From: web3.HexEncoder.Address(pk.GetAddress()), - Gas: web3.HexEncoder.Uint64(999999), - Data: web3.HexEncoder.BytesTrim(solidity.Bytecode_EventEmitter), + From: web3hex.Encoder.Address(pk.GetAddress()), + Gas: web3hex.Encoder.Uint64(999999), + Data: web3hex.Encoder.BytesTrim(solidity.Bytecode_EventEmitter), } txHash, err := client.SendTransaction(param) require.NoError(t, err) @@ -82,7 +82,7 @@ func TestEthClient_GetLogs(t *testing.T) { func TestEthClient_GetBlockByNumber(t *testing.T) { block, err := client.GetBlockByNumber("latest") require.NoError(t, err) - d := new(web3.HexDecoder) + d := new(web3hex.Decoder) assert.Greater(t, d.Uint64(block.Number), uint64(0)) require.NoError(t, d.Err()) } diff --git a/rpc/web3/ethclient/transact_client.go b/rpc/web3/ethclient/transact_client.go index 64bcaa8a4..6258468bc 100644 --- a/rpc/web3/ethclient/transact_client.go +++ b/rpc/web3/ethclient/transact_client.go @@ -6,8 +6,8 @@ import ( "github.com/hyperledger/burrow/acm" "github.com/hyperledger/burrow/crypto" + "github.com/hyperledger/burrow/encoding/web3hex" "github.com/hyperledger/burrow/execution/exec" - "github.com/hyperledger/burrow/rpc/web3" "github.com/hyperledger/burrow/txs" "github.com/hyperledger/burrow/txs/payload" "google.golang.org/grpc" @@ -78,7 +78,7 @@ func (cli *TransactClient) CallTxSync(ctx context.Context, tx *payload.CallTx, return nil, err } - d := new(web3.HexDecoder) + d := new(web3hex.Decoder) header := &exec.TxHeader{ TxType: payload.TypeCall, @@ -102,21 +102,21 @@ func (cli *TransactClient) CallTxSync(ctx context.Context, tx *payload.CallTx, func (cli *TransactClient) SendTransaction(tx *payload.CallTx) (string, error) { var to string if tx.Address != nil { - to = web3.HexEncoder.Address(*tx.Address) + to = web3hex.Encoder.Address(*tx.Address) } var nonce string if tx.Input.Sequence != 0 { - nonce = web3.HexEncoder.Uint64OmitEmpty(tx.Input.Sequence) + nonce = web3hex.Encoder.Uint64OmitEmpty(tx.Input.Sequence) } param := &EthSendTransactionParam{ - From: web3.HexEncoder.Address(tx.Input.Address), + From: web3hex.Encoder.Address(tx.Input.Address), To: to, - Gas: web3.HexEncoder.Uint64OmitEmpty(tx.GasLimit), - GasPrice: web3.HexEncoder.Uint64OmitEmpty(tx.GasPrice), - Value: web3.HexEncoder.Uint64OmitEmpty(tx.Input.Amount), - Data: web3.HexEncoder.BytesTrim(tx.Data), + Gas: web3hex.Encoder.Uint64OmitEmpty(tx.GasLimit), + GasPrice: web3hex.Encoder.Uint64OmitEmpty(tx.GasPrice), + Value: web3hex.Encoder.Uint64OmitEmpty(tx.Input.Amount), + Data: web3hex.Encoder.BytesTrim(tx.Data), Nonce: nonce, } @@ -147,7 +147,7 @@ func (cli *TransactClient) SendRawTransaction(tx *payload.CallTx, signer acm.Add return "", fmt.Errorf("could not marshal Ethereum raw transaction: %w", err) } - return cli.client.SendRawTransaction(web3.HexEncoder.BytesTrim(bs)) + return cli.client.SendRawTransaction(web3hex.Encoder.BytesTrim(bs)) } func (cli *TransactClient) GetChainID() (string, error) { @@ -166,7 +166,7 @@ func (cli *TransactClient) GetGasPrice() (uint64, error) { if err != nil { return 0, fmt.Errorf("could not get gas price: %w", err) } - d := new(web3.HexDecoder) + d := new(web3hex.Decoder) return d.Uint64(gasPrice), d.Err() } @@ -175,7 +175,7 @@ func (cli *TransactClient) GetTransactionCount(address crypto.Address) (uint64, if err != nil { return 0, fmt.Errorf("could not get transaction acount for address %s: %w", address, err) } - d := new(web3.HexDecoder) + d := new(web3hex.Decoder) return d.Uint64(count), d.Err() } diff --git a/rpc/web3/ethclient/types.go b/rpc/web3/ethclient/types.go index 295adec0e..c7ede5d4b 100644 --- a/rpc/web3/ethclient/types.go +++ b/rpc/web3/ethclient/types.go @@ -3,8 +3,8 @@ package ethclient import ( "github.com/hyperledger/burrow/binary" "github.com/hyperledger/burrow/crypto" + "github.com/hyperledger/burrow/encoding/web3hex" "github.com/hyperledger/burrow/rpc/rpcevents" - "github.com/hyperledger/burrow/rpc/web3" ) // These types partially duplicate some of those web3/types.go, the should probably be unified at some point but @@ -55,11 +55,11 @@ type Filter struct { func (f *Filter) EthFilter() *EthFilter { topics := make([]string, len(f.Topics)) for i, t := range f.Topics { - topics[i] = web3.HexEncoder.BytesTrim(t[:]) + topics[i] = web3hex.Encoder.BytesTrim(t[:]) } addresses := make([]string, len(f.Addresses)) for i, a := range f.Addresses { - addresses[i] = web3.HexEncoder.Address(a) + addresses[i] = web3hex.Encoder.Address(a) } return &EthFilter{ FromBlock: logBound(f.GetStart()), diff --git a/rpc/web3/hex.go b/rpc/web3/hex.go deleted file mode 100644 index 24bbe6792..000000000 --- a/rpc/web3/hex.go +++ /dev/null @@ -1,121 +0,0 @@ -package web3 - -import ( - bin "encoding/binary" - "fmt" - "math/big" - "strings" - - "github.com/hyperledger/burrow/crypto" - "github.com/tmthrgd/go-hex" -) - -type HexDecoder struct { - error - must bool -} - -func (d *HexDecoder) Must() *HexDecoder { - return &HexDecoder{must: true} -} - -func (d *HexDecoder) Err() error { - return d.error -} - -func (d *HexDecoder) pushErr(err error) { - if d.must { - panic(err) - } - if d.error == nil { - d.error = err - } -} - -func (d *HexDecoder) Bytes(hs string) []byte { - hexString := strings.TrimPrefix(hs, "0x") - // Ethereum returns odd-length hexString strings when it removes leading zeros - if len(hexString)%2 == 1 { - hexString = "0" + hexString - } - bs, err := hex.DecodeString(hexString) - if err != nil { - d.pushErr(fmt.Errorf("could not decode bytes from '%s': %w", hs, err)) - } - return bs -} - -func (d *HexDecoder) Address(hs string) crypto.Address { - if hs == "" { - return crypto.Address{} - } - address, err := crypto.AddressFromBytes(d.Bytes(hs)) - if err != nil { - d.pushErr(fmt.Errorf("could not decode address from '%s': %w", hs, err)) - } - return address -} - -func (d *HexDecoder) BigInt(hs string) *big.Int { - return new(big.Int).SetBytes(d.Bytes(hs)) -} - -func (d *HexDecoder) Uint64(hs string) uint64 { - bi := d.BigInt(hs) - if !bi.IsUint64() { - d.pushErr(fmt.Errorf("%v is not uint64", bi)) - } - return bi.Uint64() -} - -func (d *HexDecoder) Int64(hs string) int64 { - bi := d.BigInt(hs) - if !bi.IsInt64() { - d.pushErr(fmt.Errorf("%v is not int64", bi)) - } - return bi.Int64() -} - -type hexEncoder struct { -} - -var HexEncoder = new(hexEncoder) - -func (e *hexEncoder) Bytes(bs []byte) string { - return "0x" + hex.EncodeToString(bs) -} - -func (e *hexEncoder) BytesTrim(bs []byte) string { - if len(bs) == 0 { - return "" - } - str := hex.EncodeToString(bs) - // Ethereum expects leading zeros to be removed from RLP encodings (SMH) - str = strings.TrimLeft(str, "0") - if len(str) == 0 { - // Special case for zero - return "0x0" - } - return "0x" + str -} - -func (e *hexEncoder) BigInt(x *big.Int) string { - return e.BytesTrim(x.Bytes()) -} - -func (e *hexEncoder) Uint64OmitEmpty(x uint64) string { - if x == 0 { - return "" - } - return e.Uint64(x) -} - -func (e *hexEncoder) Uint64(x uint64) string { - bs := make([]byte, 8) - bin.BigEndian.PutUint64(bs, x) - return e.BytesTrim(bs) -} - -func (e *hexEncoder) Address(address crypto.Address) string { - return e.BytesTrim(address.Bytes()) -} diff --git a/rpc/web3/hex_test.go b/rpc/web3/hex_test.go deleted file mode 100644 index 9dcb32a6f..000000000 --- a/rpc/web3/hex_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package web3 - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestHexEncoder_BytesTrim(t *testing.T) { - assert.Equal(t, "", HexEncoder.BytesTrim(nil)) - assert.Equal(t, "", HexEncoder.BytesTrim([]byte{})) - assert.Equal(t, "0x0", HexEncoder.BytesTrim([]byte{0})) - assert.Equal(t, "0x1", HexEncoder.BytesTrim([]byte{1})) - assert.Equal(t, "0x1ff", HexEncoder.BytesTrim([]byte{1, 255})) -} - -func TestHexDecoder_Bytes(t *testing.T) { - d := new(HexDecoder) - assert.Equal(t, []byte{}, d.Bytes("")) - assert.Equal(t, []byte{1}, d.Bytes("0x1")) - assert.Equal(t, []byte{1}, d.Bytes("0x01")) - assert.Equal(t, []byte{1, 0xff}, d.Bytes("0x1ff")) - require.NoError(t, d.Err()) -} diff --git a/txs/ethereum_tx.go b/txs/ethereum_tx.go index 79ca23918..b697da426 100644 --- a/txs/ethereum_tx.go +++ b/txs/ethereum_tx.go @@ -6,6 +6,7 @@ import ( "github.com/btcsuite/btcd/btcec" "github.com/hyperledger/burrow/crypto" + "github.com/hyperledger/burrow/encoding" "github.com/hyperledger/burrow/encoding/rlp" "github.com/tmthrgd/go-hex" ) @@ -44,7 +45,7 @@ func EthRawTxFromEnvelope(txEnv *Envelope) (*EthRawTx, error) { if len(txEnv.Signatories) > 1 { return nil, fmt.Errorf("can only form EthRawTx from Envelope with a zero or one signatories") } - sig, err := txEnv.Signatories[0].Signature.GetEthSignature(txEnv.Tx.ChainID) + sig, err := txEnv.Signatories[0].Signature.GetEthSignature(encoding.GetEthChainID(txEnv.Tx.ChainID)) if err != nil { return nil, err } diff --git a/txs/tx.go b/txs/tx.go index 8d03fac98..b2eec4107 100644 --- a/txs/tx.go +++ b/txs/tx.go @@ -98,7 +98,7 @@ func (tx *Tx) RLPRawTx() (*EthRawTx, error) { To: to, Amount: balance.NativeToWei(payload.Input.Amount), Data: payload.Data.Bytes(), - chainID: crypto.GetEthChainID(tx.ChainID), + chainID: encoding.GetEthChainID(tx.ChainID), }, nil default: return nil, fmt.Errorf("tx type %v not supported for rlp encoding", tx.Payload.Type()) diff --git a/vent/chain/ethereum/ethereum.go b/vent/chain/ethereum/ethereum.go index 89182611b..b0a983502 100644 --- a/vent/chain/ethereum/ethereum.go +++ b/vent/chain/ethereum/ethereum.go @@ -8,19 +8,17 @@ import ( "strconv" "time" - "github.com/hyperledger/burrow/event" - "github.com/hyperledger/burrow/event/query" - "github.com/hyperledger/burrow/logging" - "github.com/hyperledger/burrow/rpc/web3" - "github.com/hyperledger/burrow/vent/chain" - - "github.com/hyperledger/burrow/rpc/web3/ethclient" - "github.com/hyperledger/burrow/binary" "github.com/hyperledger/burrow/crypto" + "github.com/hyperledger/burrow/encoding/web3hex" + "github.com/hyperledger/burrow/event" + "github.com/hyperledger/burrow/event/query" "github.com/hyperledger/burrow/execution/errors" "github.com/hyperledger/burrow/execution/exec" + "github.com/hyperledger/burrow/logging" "github.com/hyperledger/burrow/rpc/rpcevents" + "github.com/hyperledger/burrow/rpc/web3/ethclient" + "github.com/hyperledger/burrow/vent/chain" "github.com/hyperledger/burrow/vent/types" "google.golang.org/grpc/connectivity" ) @@ -131,11 +129,11 @@ func (b *Block) GetTxs() []chain.Transaction { } func (b *Block) GetMetadata(columns types.SQLColumnNames) (map[string]interface{}, error) { - block, err := b.client.GetBlockByNumber(web3.HexEncoder.Uint64(b.Height)) + block, err := b.client.GetBlockByNumber(web3hex.Encoder.Uint64(b.Height)) if err != nil { return nil, err } - d := new(web3.HexDecoder) + d := new(web3hex.Decoder) blockHeader, err := json.Marshal(block) if err != nil { return nil, fmt.Errorf("could not serialise block header: %w", err) @@ -223,7 +221,7 @@ type Event struct { var _ chain.Event = (*Event)(nil) func newEvent(log *ethclient.EthLog) (*Event, error) { - d := new(web3.HexDecoder) + d := new(web3hex.Decoder) topics := make([]binary.Word256, len(log.Topics)) for i, t := range log.Topics { topics[i] = binary.LeftPadWord256(d.Bytes(t))