Update TxID to use eth Keccak256

- Update TxID to use eth Keccak256
- Added more tests to L2Tx TxID calculation to check compatibility with
js version from https://github.com/hermeznetwork/hermezjs/pull/57
This commit is contained in:
arnaucube
2021-02-01 15:24:57 +01:00
parent f0886b3d2a
commit c1cd37913f
8 changed files with 111 additions and 45 deletions

View File

@@ -17,14 +17,17 @@ import (
const (
// NLeafElems is the number of elements for a leaf
NLeafElems = 4
// maxNonceValue is the maximum value that the Account.Nonce can have (40 bits: maxNonceValue=2**40-1)
// maxNonceValue is the maximum value that the Account.Nonce can have
// (40 bits: maxNonceValue=2**40-1)
maxNonceValue = 0xffffffffff
// maxBalanceBytes is the maximum bytes that can use the Account.Balance *big.Int
// maxBalanceBytes is the maximum bytes that can use the
// Account.Balance *big.Int
maxBalanceBytes = 24
// IdxBytesLen idx bytes
IdxBytesLen = 6
// maxIdxValue is the maximum value that Idx can have (48 bits: maxIdxValue=2**48-1)
// maxIdxValue is the maximum value that Idx can have (48 bits:
// maxIdxValue=2**48-1)
maxIdxValue = 0xffffffffffff
// UserThreshold determines the threshold from the User Idxs can be
@@ -85,7 +88,8 @@ func IdxFromBigInt(b *big.Int) (Idx, error) {
return Idx(uint64(b.Int64())), nil
}
// Nonce represents the nonce value in a uint64, which has the method Bytes that returns a byte array of length 5 (40 bits).
// Nonce represents the nonce value in a uint64, which has the method Bytes
// that returns a byte array of length 5 (40 bits).
type Nonce uint64
// Bytes returns a byte array of length 5 representing the Nonce
@@ -113,7 +117,9 @@ func NonceFromBytes(b [5]byte) Nonce {
return Nonce(nonce)
}
// Account is a struct that gives information of the holdings of an address and a specific token. Is the data structure that generates the Value stored in the leaf of the MerkleTree
// Account is a struct that gives information of the holdings of an address and
// a specific token. Is the data structure that generates the Value stored in
// the leaf of the MerkleTree
type Account struct {
Idx Idx `meddler:"idx"`
TokenID TokenID `meddler:"token_id"`

View File

@@ -51,7 +51,7 @@ func TestAccountCreationAuthSignVerify(t *testing.T) {
func TestKeccak256JSComp(t *testing.T) {
// check keccak256 compatible with js version
h := ethCrypto.Keccak256Hash([]byte("test")).Bytes()
h := ethCrypto.Keccak256([]byte("test"))
assert.Equal(t, "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658",
hex.EncodeToString(h))
}

View File

@@ -1,13 +1,12 @@
package common
import (
"crypto/sha256"
"encoding/binary"
"fmt"
"math/big"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
)
@@ -137,14 +136,9 @@ func (tx *L1Tx) SetID() error {
b = append(b, positionBytes[:]...)
// calculate hash
h := sha256.New()
_, err := h.Write(b[:])
if err != nil {
return tracerr.Wrap(err)
}
r := h.Sum(nil)
h := ethCrypto.Keccak256Hash(b).Bytes()
copy(tx.TxID[1:], r)
copy(tx.TxID[1:], h)
return nil
}
@@ -369,7 +363,7 @@ func L1UserTxFromBytes(b []byte) (*L1Tx, error) {
func signHash(data []byte) []byte {
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
return crypto.Keccak256([]byte(msg))
return ethCrypto.Keccak256([]byte(msg))
}
// L1CoordinatorTxFromBytes decodes a L1Tx from []byte
@@ -411,15 +405,15 @@ func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, hermezAddress ethCommo
signature = append(signature, s[:]...)
signature = append(signature, v)
hash := signHash(data)
pubKeyBytes, err := crypto.Ecrecover(hash, signature)
pubKeyBytes, err := ethCrypto.Ecrecover(hash, signature)
if err != nil {
return nil, tracerr.Wrap(err)
}
pubKey, err := crypto.UnmarshalPubkey(pubKeyBytes)
pubKey, err := ethCrypto.UnmarshalPubkey(pubKeyBytes)
if err != nil {
return nil, tracerr.Wrap(err)
}
tx.FromEthAddr = crypto.PubkeyToAddress(*pubKey)
tx.FromEthAddr = ethCrypto.PubkeyToAddress(*pubKey)
} else {
// L1Coordinator Babyjub
tx.FromEthAddr = RollupConstEthAddressInternalOnly

View File

@@ -29,7 +29,7 @@ func TestNewL1UserTx(t *testing.T) {
}
l1Tx, err := NewL1Tx(l1Tx)
assert.NoError(t, err)
assert.Equal(t, "0x00eb5e95e1ce5e9f6c4ed402d415e8d0bdd7664769cfd2064d28da04a2c76be432", l1Tx.TxID.String())
assert.Equal(t, "0x00a6cbae3b8661fb75b0919ca6605a02cfb04d9c6dd16870fa0fcdf01befa32768", l1Tx.TxID.String())
}
func TestNewL1CoordinatorTx(t *testing.T) {
@@ -46,7 +46,7 @@ func TestNewL1CoordinatorTx(t *testing.T) {
}
l1Tx, err := NewL1Tx(l1Tx)
assert.NoError(t, err)
assert.Equal(t, "0x01b8ae2bf60cb8f7c2315a27f13c8863fa2370d15ccc2e68490e197030ba22b97e", l1Tx.TxID.String())
assert.Equal(t, "0x01274482d73df4dab34a1b6740adfca347a462513aa14e82f27b12f818d1b68c84", l1Tx.TxID.String())
}
func TestL1TxCompressedData(t *testing.T) {

View File

@@ -1,10 +1,10 @@
package common
import (
"crypto/sha256"
"fmt"
"math/big"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/hermeznetwork/tracerr"
)
@@ -104,15 +104,10 @@ func (tx L2Tx) CalculateTxID() ([TxIDLen]byte, error) {
b = append(b, byte(tx.Fee))
// calculate hash
h := sha256.New()
_, err = h.Write(b)
if err != nil {
return txID, tracerr.Wrap(err)
}
r := h.Sum(nil)
h := ethCrypto.Keccak256Hash(b).Bytes()
txID[0] = TxIDPrefixL2Tx
copy(txID[1:], r)
copy(txID[1:], h)
return txID, nil
}

View File

@@ -19,7 +19,78 @@ func TestNewL2Tx(t *testing.T) {
}
l2Tx, err := NewL2Tx(l2Tx)
assert.NoError(t, err)
assert.Equal(t, "0x024f67ec893467419cdfacfc9152e55dc7ce16088952bf2b3442732fd046bd031c", l2Tx.TxID.String())
assert.Equal(t, "0x02fb52b5d0b9ef2626c11701bb751b2720c76d59946b9a48146ac153bb6e63bf6a", l2Tx.TxID.String())
l2Tx = &L2Tx{
FromIdx: 87654,
ToIdx: 300,
TokenID: 5,
Amount: big.NewInt(4),
Nonce: 1,
}
l2Tx, err = NewL2Tx(l2Tx)
assert.NoError(t, err)
assert.Equal(t, "0x0276114a8f666fa1ff7dbf34b4a9da577808dc501e3b2760d01fe3ef5473f5737f", l2Tx.TxID.String())
l2Tx = &L2Tx{
FromIdx: 87654,
ToIdx: 300,
TokenID: 5,
Amount: big.NewInt(4),
Fee: 126,
Nonce: 3,
}
l2Tx, err = NewL2Tx(l2Tx)
assert.NoError(t, err)
assert.Equal(t, "0x025afb63126d3067f61f633d13e5a51da0551af3a4567a9af2db5321ed04214ff4", l2Tx.TxID.String())
l2Tx = &L2Tx{
FromIdx: 87654,
ToIdx: 300,
TokenID: 5,
Amount: big.NewInt(4),
Nonce: 1003,
Fee: 144,
}
l2Tx, err = NewL2Tx(l2Tx)
assert.NoError(t, err)
assert.Equal(t, "0x02cf390157041c3b1b59f0aaed4da464f0d0d48f1d026e46fd89c7fe1e5aed7fcf", l2Tx.TxID.String())
l2Tx = &L2Tx{
FromIdx: 1,
ToIdx: 1,
TokenID: 1,
Amount: big.NewInt(1),
Nonce: 1,
Fee: 1,
}
l2Tx, err = NewL2Tx(l2Tx)
assert.NoError(t, err)
assert.Equal(t, "0x020ec18eaae67fcd545998841a9c4be09ee3083e12db6ae5e5213a2ecaaa52d5cf", l2Tx.TxID.String())
l2Tx = &L2Tx{
FromIdx: 999,
ToIdx: 999,
TokenID: 999,
Amount: big.NewInt(999),
Nonce: 999,
Fee: 255,
}
l2Tx, err = NewL2Tx(l2Tx)
assert.NoError(t, err)
assert.Equal(t, "0x02f036223e79fac776de107f50822552cc964ee9fc4caa304613285f6976bcc940", l2Tx.TxID.String())
l2Tx = &L2Tx{
FromIdx: 4444,
ToIdx: 300,
TokenID: 0,
Amount: big.NewInt(3400000000),
Nonce: 2,
Fee: 25,
}
l2Tx, err = NewL2Tx(l2Tx)
assert.NoError(t, err)
assert.Equal(t, "0x029c8aef9ef24531e4cf84e78cbab1018ba1626a5a10afb6b7c356be1b5c28e92c", l2Tx.TxID.String())
}
func TestL2TxByteParsers(t *testing.T) {

View File

@@ -21,7 +21,7 @@ func TestNewPoolL2Tx(t *testing.T) {
}
poolL2Tx, err := NewPoolL2Tx(poolL2Tx)
assert.NoError(t, err)
assert.Equal(t, "0x024f67ec893467419cdfacfc9152e55dc7ce16088952bf2b3442732fd046bd031c", poolL2Tx.TxID.String())
assert.Equal(t, "0x02fb52b5d0b9ef2626c11701bb751b2720c76d59946b9a48146ac153bb6e63bf6a", poolL2Tx.TxID.String())
}
func TestTxCompressedData(t *testing.T) {