Browse Source

Merge pull request #300 from hermeznetwork/feature/l1coord-update

Update L1 coordinator Txs
feature/sql-semaphore1
Eduard S 4 years ago
committed by GitHub
parent
commit
4d830fdc89
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 9 deletions
  1. +9
    -7
      common/l1tx.go
  2. +1
    -2
      common/l1tx_test.go

+ 9
- 7
common/l1tx.go

@ -285,14 +285,18 @@ 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 crypto.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) != L1CoordinatorTxBytesLen { if len(b) != L1CoordinatorTxBytesLen {
return nil, fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b)) return nil, fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b))
} }
bytesMessage1 := []byte("\x19Ethereum Signed Message:\n120")
bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
bytesMessage := []byte("I authorize this babyjubjub key for hermez rollup account creation")
tx := &L1Tx{ tx := &L1Tx{
UserOrigin: false, UserOrigin: false,
@ -320,18 +324,16 @@ func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, hermezAddress ethCommo
// 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) chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
hermezAddressBytes := ethCommon.LeftPadBytes(hermezAddress.Bytes(), 32)
var data []byte var data []byte
data = append(data, bytesMessage1...)
data = append(data, bytesMessage2...)
data = append(data, bytesMessage...)
data = append(data, pkCompB...) data = append(data, pkCompB...)
data = append(data, chainIDBytes[:]...) data = append(data, chainIDBytes[:]...)
data = append(data, hermezAddressBytes...)
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 := crypto.Keccak256(data)
hash := signHash(data)
pubKeyBytes, err := crypto.Ecrecover(hash, signature) pubKeyBytes, err := crypto.Ecrecover(hash, signature)
if err != nil { if err != nil {
return nil, err return nil, err

+ 1
- 2
common/l1tx_test.go

@ -138,7 +138,6 @@ func TestL1TxByteParsersCompatibility(t *testing.T) {
func TestL1CoordinatorTxByteParsers(t *testing.T) { func TestL1CoordinatorTxByteParsers(t *testing.T) {
hermezAddress := ethCommon.HexToAddress("0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe") hermezAddress := ethCommon.HexToAddress("0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe")
hermezAddressBytes := ethCommon.LeftPadBytes(hermezAddress.Bytes(), 32)
chainID := big.NewInt(1337) chainID := big.NewInt(1337)
chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2) chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
@ -170,7 +169,7 @@ func TestL1CoordinatorTxByteParsers(t *testing.T) {
data = append(data, bytesMessage2...) data = append(data, bytesMessage2...)
data = append(data, babyjubB[:]...) data = append(data, babyjubB[:]...)
data = append(data, chainIDBytes...) data = append(data, chainIDBytes...)
data = append(data, hermezAddressBytes...)
data = append(data, hermezAddress.Bytes()...)
hash := crypto.Keccak256Hash(data) hash := crypto.Keccak256Hash(data)
signature, err := crypto.Sign(hash.Bytes(), privateKey) signature, err := crypto.Sign(hash.Bytes(), privateKey)
require.Nil(t, err) require.Nil(t, err)

Loading…
Cancel
Save