Browse Source

Update ethclient & L1CoordinatorTx

feature/sql-semaphore1
laisolizq 3 years ago
parent
commit
f775af8890
10 changed files with 754 additions and 43 deletions
  1. +33
    -24
      common/l1tx.go
  2. +12
    -5
      common/l1tx_test.go
  3. +1
    -1
      eth/README.md
  4. +4
    -1
      eth/client_test.go
  5. +2
    -2
      eth/contracts/README.md
  6. +1
    -1
      eth/contracts/auction/HermezAuctionProtocol.go
  7. +687
    -2
      eth/contracts/hermez/Hermez.go
  8. +1
    -1
      eth/contracts/withdrawdelayer/WithdrawalDelayer.go
  9. +10
    -5
      eth/rollup.go
  10. +3
    -1
      eth/rollup_test.go

+ 33
- 24
common/l1tx.go

@ -249,23 +249,21 @@ func L1UserTxFromBytes(b []byte) (*L1Tx, error) {
}
// L1CoordinatorTxFromBytes decodes a L1Tx from []byte
func L1CoordinatorTxFromBytes(b []byte) (*L1Tx, error) {
func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, hermezAddress ethCommon.Address) (*L1Tx, error) {
if len(b) != L1CoordinatorTxBytesLen {
return nil, fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b))
}
bytesMessage1 := []byte("\x19Ethereum Signed Message:\n98")
bytesMessage1 := []byte("\x19Ethereum Signed Message:\n120")
bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
tx := &L1Tx{
UserOrigin: false,
}
var err error
// Ethereum adds 27 to v
v := b[0] - byte(27) //nolint:gomnd
v := b[0]
s := b[1:33]
r := b[33:65]
pkCompB := b[65:97]
pkCompL := SwapEndianness(pkCompB)
var pkComp babyjub.PublicKeyComp
@ -278,26 +276,37 @@ func L1CoordinatorTxFromBytes(b []byte) (*L1Tx, error) {
if err != nil {
return nil, err
}
var data []byte
data = append(data, bytesMessage1...)
data = append(data, bytesMessage2...)
data = append(data, pkCompB...)
var signature []byte
signature = append(signature, r[:]...)
signature = append(signature, s[:]...)
signature = append(signature, v)
hash := crypto.Keccak256(data)
pubKeyBytes, err := crypto.Ecrecover(hash, signature)
if err != nil {
return nil, err
}
pubKey, err := crypto.UnmarshalPubkey(pubKeyBytes)
if err != nil {
return nil, err
}
tx.FromEthAddr = crypto.PubkeyToAddress(*pubKey)
tx.Amount = big.NewInt(0)
tx.LoadAmount = big.NewInt(0)
if int(v) > 0 {
// L1CoordinatorTX ETH
// Ethereum adds 27 to v
v = b[0] - byte(27) //nolint:gomnd
chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
hermezAddressBytes := ethCommon.LeftPadBytes(hermezAddress.Bytes(), 32)
var data []byte
data = append(data, bytesMessage1...)
data = append(data, bytesMessage2...)
data = append(data, pkCompB...)
data = append(data, chainIDBytes[:]...)
data = append(data, hermezAddressBytes...)
var signature []byte
signature = append(signature, r[:]...)
signature = append(signature, s[:]...)
signature = append(signature, v)
hash := crypto.Keccak256(data)
pubKeyBytes, err := crypto.Ecrecover(hash, signature)
if err != nil {
return nil, err
}
pubKey, err := crypto.UnmarshalPubkey(pubKeyBytes)
if err != nil {
return nil, err
}
tx.FromEthAddr = crypto.PubkeyToAddress(*pubKey)
} else {
// L1Coordinator Babyjub
tx.FromEthAddr = RollupConstEthAddressInternalOnly
}
return tx, nil
}

+ 12
- 5
common/l1tx_test.go

@ -121,6 +121,11 @@ func TestL1TxByteParsersCompatibility(t *testing.T) {
}
func TestL1CoordinatorTxByteParsers(t *testing.T) {
hermezAddress := ethCommon.HexToAddress("0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe")
hermezAddressBytes := ethCommon.LeftPadBytes(hermezAddress.Bytes(), 32)
chainID := big.NewInt(1337)
chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
require.Nil(t, err)
@ -139,7 +144,7 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
require.Nil(t, err)
pk, err := pkComp.Decompress()
require.Nil(t, err)
bytesMessage1 := []byte("\x19Ethereum Signed Message:\n98")
bytesMessage1 := []byte("\x19Ethereum Signed Message:\n120")
bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
babyjub := pk.Compress()
@ -148,6 +153,8 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
data = append(data, bytesMessage1...)
data = append(data, bytesMessage2...)
data = append(data, babyjubB[:]...)
data = append(data, chainIDBytes...)
data = append(data, hermezAddressBytes...)
hash := crypto.Keccak256Hash(data)
signature, err := crypto.Sign(hash.Bytes(), privateKey)
require.Nil(t, err)
@ -165,7 +172,7 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
require.Nil(t, err)
l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1)
l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1, chainID, hermezAddress)
require.Nil(t, err)
assert.Equal(t, l1Tx, l1txDecoded)
bytesCoordinatorL12, err := l1txDecoded.BytesCoordinatorTx(signature)
@ -173,11 +180,11 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
assert.Equal(t, bytesCoordinatorL1, bytesCoordinatorL12)
// expect error if length!=68
_, err = L1CoordinatorTxFromBytes(bytesCoordinatorL1[:66])
_, err = L1CoordinatorTxFromBytes(bytesCoordinatorL1[:66], chainID, hermezAddress)
require.NotNil(t, err)
_, err = L1CoordinatorTxFromBytes([]byte{})
_, err = L1CoordinatorTxFromBytes([]byte{}, chainID, hermezAddress)
require.NotNil(t, err)
_, err = L1CoordinatorTxFromBytes(nil)
_, err = L1CoordinatorTxFromBytes(nil, chainID, hermezAddress)
require.NotNil(t, err)
}

+ 1
- 1
eth/README.md

@ -8,7 +8,7 @@ The first step is to clone the github repository where the contracts are located
While the prepared deployment is not found to master, branch in repository must be changed:
`git checkout feature/ethclient-test-deployment-ganache` (tested with commit `f62c768bd4817921872666b3644403a119e28248`)
`git checkout feature/newDeploymentScript-ethclient` (tested with commit `af4c93916d6cd93d866c121cc63b6a6794f649b2`)
Now, install the dependencies:

+ 4
- 1
eth/client_test.go

@ -3,12 +3,15 @@ package eth
import (
"testing"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
)
func TestClientInterface(t *testing.T) {
ethClient, err := ethclient.Dial(ethClientDialURL)
require.Nil(t, err)
var c ClientInterface
client, _ := NewClient(nil, nil, nil, &ClientConfig{})
client, _ := NewClient(ethClient, nil, nil, &ClientConfig{})
c = client
require.NotNil(t, c)
}

+ 2
- 2
eth/contracts/README.md

@ -10,8 +10,8 @@ abigen --abi=HEZ.abi --bin=HEZ.bin --pkg=HEZ --out=HEZ.go
```
You must compile the contracts to get the `.bin` and `.abi` files. The contracts used are in the repo: https://github.com/hermeznetwork/contracts
Branch: `RemoveERC777`
Specifically they have been processed in the commit with hash: `2a1cfccfba6770c1077ecea983d2c743dc4a1e93`
Branch: `feature/newDeploymentScript`
Specifically they have been processed in the commit with hash: `4489f8e7fe4dd17cf22f1e96741b09bdf81946d8`
Versions:
```

+ 1
- 1
eth/contracts/auction/HermezAuctionProtocol.go
File diff suppressed because it is too large
View File


+ 687
- 2
eth/contracts/hermez/Hermez.go
File diff suppressed because it is too large
View File


+ 1
- 1
eth/contracts/withdrawdelayer/WithdrawalDelayer.go
File diff suppressed because it is too large
View File


+ 10
- 5
eth/rollup.go

@ -190,6 +190,7 @@ type RollupInterface interface {
// RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
type RollupClient struct {
client *EthereumClient
chainID *big.Int
address ethCommon.Address
tokenHEZCfg TokenConfig
hermez *Hermez.Hermez
@ -211,8 +212,13 @@ func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZ
if err != nil {
return nil, err
}
chainID, err := client.client.ChainID(context.Background())
if err != nil {
return nil, err
}
return &RollupClient{
client: client,
chainID: chainID,
address: address,
tokenHEZCfg: tokenHEZCfg,
hermez: hermez,
@ -290,8 +296,7 @@ func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address, feeAddToke
}
tokenName := c.tokenHEZCfg.Name
tokenAddr := c.tokenHEZCfg.Address
chainid, _ := c.client.Client().ChainID(context.Background())
digest, _ := createPermitDigest(tokenAddr, owner, spender, chainid, feeAddToken, nonce, deadline, tokenName)
digest, _ := createPermitDigest(tokenAddr, owner, spender, c.chainID, feeAddToken, nonce, deadline, tokenName)
signature, _ := c.client.ks.SignHash(*c.client.account, digest)
permit := createPermit(owner, spender, feeAddToken, deadline, digest, signature)
@ -387,8 +392,7 @@ func (c *RollupClient) RollupL1UserTxERC20Permit(fromBJJ *babyjub.PublicKey, fro
}
tokenName := c.tokenHEZCfg.Name
tokenAddr := c.tokenHEZCfg.Address
chainid, _ := c.client.Client().ChainID(context.Background())
digest, _ := createPermitDigest(tokenAddr, owner, spender, chainid, amount, nonce, deadline, tokenName)
digest, _ := createPermitDigest(tokenAddr, owner, spender, c.chainID, amount, nonce, deadline, tokenName)
signature, _ := c.client.ks.SignHash(*c.client.account, digest)
permit := createPermit(owner, spender, amount, deadline, digest, signature)
return c.hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(loadAmountF),
@ -601,6 +605,7 @@ func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupFo
return nil, nil, err
}
txData := tx.Data()
method, err := c.contractAbi.MethodById(txData[:4])
if err != nil {
return nil, nil, err
@ -641,7 +646,7 @@ func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupFo
signature = append(signature, r[:]...)
signature = append(signature, s[:]...)
signature = append(signature, v)
l1Tx, err := common.L1CoordinatorTxFromBytes(bytesL1Coordinator)
l1Tx, err := common.L1CoordinatorTxFromBytes(bytesL1Coordinator, c.chainID, c.address)
if err != nil {
return nil, nil, err
}

+ 3
- 1
eth/rollup_test.go

@ -1,6 +1,7 @@
package eth
import (
"context"
"crypto/ecdsa"
"encoding/binary"
"encoding/hex"
@ -86,6 +87,7 @@ func TestRollupAddToken(t *testing.T) {
}
func TestRollupForgeBatch(t *testing.T) {
chainid, _ := auctionClient.client.Client().ChainID(context.Background())
// Register Coordinator
forgerAddress := governanceAddressConst
_, err := auctionClient.AuctionSetCoordinator(forgerAddress, URL)
@ -125,7 +127,7 @@ func TestRollupForgeBatch(t *testing.T) {
signature = append(signature, r[:]...)
signature = append(signature, s[:]...)
signature = append(signature, v)
l1Tx, err := common.L1CoordinatorTxFromBytes(bytesL1Coordinator)
l1Tx, err := common.L1CoordinatorTxFromBytes(bytesL1Coordinator, chainid, rollupClient.address)
require.Nil(t, err)
args.L1CoordinatorTxs = append(args.L1CoordinatorTxs, *l1Tx)
args.L1CoordinatorTxsAuths = append(args.L1CoordinatorTxsAuths, signature)

Loading…
Cancel
Save