Update ethclient & L1CoordinatorTx

This commit is contained in:
laisolizq
2020-11-20 13:45:22 +01:00
parent bf88eb60b8
commit f775af8890
10 changed files with 754 additions and 43 deletions

View File

@@ -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
}

View File

@@ -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)
}