mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Update L1CoordinatorTxFromBytes to EIP712
This commit is contained in:
@@ -368,19 +368,12 @@ func L1UserTxFromBytes(b []byte) (*L1Tx, error) {
|
|||||||
return tx, nil
|
return tx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func signHash(data []byte) []byte {
|
|
||||||
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
|
|
||||||
return ethCrypto.Keccak256([]byte(msg))
|
|
||||||
}
|
|
||||||
|
|
||||||
// L1CoordinatorTxFromBytes decodes a L1Tx from []byte
|
// L1CoordinatorTxFromBytes decodes a L1Tx from []byte
|
||||||
func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, hermezAddress ethCommon.Address) (*L1Tx, error) {
|
func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, hermezAddress ethCommon.Address) (*L1Tx, error) {
|
||||||
if len(b) != RollupConstL1CoordinatorTotalBytes {
|
if len(b) != RollupConstL1CoordinatorTotalBytes {
|
||||||
return nil, tracerr.Wrap(fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b)))
|
return nil, tracerr.Wrap(fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b)))
|
||||||
}
|
}
|
||||||
|
|
||||||
bytesMessage := []byte("I authorize this babyjubjub key for hermez rollup account creation")
|
|
||||||
|
|
||||||
tx := &L1Tx{
|
tx := &L1Tx{
|
||||||
UserOrigin: false,
|
UserOrigin: false,
|
||||||
}
|
}
|
||||||
@@ -401,18 +394,20 @@ func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, hermezAddress ethCommo
|
|||||||
// L1CoordinatorTX ETH
|
// L1CoordinatorTX ETH
|
||||||
// Ethereum adds 27 to v
|
// Ethereum adds 27 to v
|
||||||
v = b[0] - byte(27) //nolint:gomnd
|
v = b[0] - byte(27) //nolint:gomnd
|
||||||
chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
|
|
||||||
var data []byte
|
|
||||||
data = append(data, bytesMessage...)
|
|
||||||
data = append(data, pkCompB...)
|
|
||||||
data = append(data, chainIDBytes[:]...)
|
|
||||||
data = append(data, hermezAddress.Bytes()...)
|
|
||||||
var signature []byte
|
var signature []byte
|
||||||
signature = append(signature, r[:]...)
|
signature = append(signature, r[:]...)
|
||||||
signature = append(signature, s[:]...)
|
signature = append(signature, s[:]...)
|
||||||
signature = append(signature, v)
|
signature = append(signature, v)
|
||||||
hash := signHash(data)
|
|
||||||
pubKeyBytes, err := ethCrypto.Ecrecover(hash, signature)
|
accCreationAuth := AccountCreationAuth{
|
||||||
|
BJJ: tx.FromBJJ,
|
||||||
|
}
|
||||||
|
h, err := accCreationAuth.HashToSign(uint16(chainID.Uint64()), hermezAddress)
|
||||||
|
if err != nil {
|
||||||
|
return nil, tracerr.Wrap(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKeyBytes, err := ethCrypto.Ecrecover(h, signature)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, tracerr.Wrap(err)
|
return nil, tracerr.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -227,7 +227,6 @@ func TestL1TxByteParsersCompatibility(t *testing.T) {
|
|||||||
func TestL1CoordinatorTxByteParsers(t *testing.T) {
|
func TestL1CoordinatorTxByteParsers(t *testing.T) {
|
||||||
hermezAddress := ethCommon.HexToAddress("0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe")
|
hermezAddress := ethCommon.HexToAddress("0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe")
|
||||||
chainID := big.NewInt(1337)
|
chainID := big.NewInt(1337)
|
||||||
chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
|
|
||||||
|
|
||||||
privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
|
privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -245,18 +244,16 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
|
|||||||
pkCompL := []byte("56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
|
pkCompL := []byte("56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
|
||||||
err = pkComp.UnmarshalText(pkCompL)
|
err = pkComp.UnmarshalText(pkCompL)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
bytesMessage1 := []byte("\x19Ethereum Signed Message:\n120")
|
|
||||||
bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
|
|
||||||
|
|
||||||
babyjubB := SwapEndianness(pkComp[:])
|
accCreationAuth := AccountCreationAuth{
|
||||||
var data []byte
|
EthAddr: fromEthAddr,
|
||||||
data = append(data, bytesMessage1...)
|
BJJ: pkComp,
|
||||||
data = append(data, bytesMessage2...)
|
}
|
||||||
data = append(data, babyjubB[:]...)
|
|
||||||
data = append(data, chainIDBytes...)
|
h, err := accCreationAuth.HashToSign(uint16(chainID.Uint64()), hermezAddress)
|
||||||
data = append(data, hermezAddress.Bytes()...)
|
require.NoError(t, err)
|
||||||
hash := crypto.Keccak256Hash(data)
|
|
||||||
signature, err := crypto.Sign(hash.Bytes(), privateKey)
|
signature, err := crypto.Sign(h, privateKey)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
// Ethereum adds 27 to v
|
// Ethereum adds 27 to v
|
||||||
v := int(signature[64])
|
v := int(signature[64])
|
||||||
|
|||||||
Reference in New Issue
Block a user