hermez-node/common/l1tx.go
common/l1tx.goRaw
1package common
2
3import (
4 "math/big"
5
6 ethCommon "github.com/ethereum/go-ethereum/common"
7 "github.com/iden3/go-iden3-crypto/babyjub"
8)
9
10const (
11 fromBJJCompressedB = 256
12 fromEthAddrB = 160
13 f16B = 16
14 tokenIDB = 32
15 cidXB = 32
16)
17
18// L1Tx is a struct that represents a L1 tx
19type L1Tx struct {
20 // Stored in DB: mandatory fileds
21 TxID TxID `meddler:"tx_id"`
22 ToForgeL1TxsNum uint32 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
23 Position int `meddler:"position"`
24 UserOrigin bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
25 FromIdx Idx `meddler:"from_idx"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
26 FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
27 FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
28 ToIdx Idx `meddler:"to_idx"` // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
29 TokenID TokenID `meddler:"token_id"`
30 Amount *big.Int `meddler:"amount,bigint"`
31 LoadAmount *big.Int `meddler:"load_amount,bigint"`
32 EthBlockNum uint64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
33 Type TxType `meddler:"tx_type"`
34 BatchNum BatchNum `meddler:"-"`
35}
36
37// Tx returns a *Tx from the L1Tx
38func (tx *L1Tx) Tx() *Tx {
39 return &Tx{
40 TxID: tx.TxID,
41 FromIdx: tx.FromIdx,
42 ToIdx: tx.ToIdx,
43 Amount: tx.Amount,
44 Type: tx.Type,
45 }
46}
47
48// Bytes encodes a L1Tx into []byte
49func (tx *L1Tx) Bytes(nLevels int) []byte {
50 res := big.NewInt(0)
51 res = res.Add(res, big.NewInt(0).Or(big.NewInt(0), tx.ToIdx.BigInt()))
52 res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), big.NewInt(int64(tx.TokenID))), uint(nLevels)))
53 res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.Amount), uint(nLevels+tokenIDB)))
54 res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.LoadAmount), uint(nLevels+tokenIDB+f16B)))
55 res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.FromIdx.BigInt()), uint(nLevels+tokenIDB+2*f16B)))
56
57 fromBJJ := big.NewInt(0)
58 fromBJJ.SetString(tx.FromBJJ.String(), 16)
59 fromBJJCompressed := big.NewInt(0).Or(big.NewInt(0), fromBJJ)
60 res = res.Add(res, big.NewInt(0).Lsh(fromBJJCompressed, uint(2*nLevels+tokenIDB+2*f16B)))
61
62 fromEthAddr := big.NewInt(0).Or(big.NewInt(0), tx.FromEthAddr.Hash().Big())
63 res = res.Add(res, big.NewInt(0).Lsh(fromEthAddr, uint(fromBJJCompressedB+2*nLevels+tokenIDB+2*f16B)))
64
65 return res.Bytes()
66}
67
68// L1TxFromBytes decodes a L1Tx from []byte
69func L1TxFromBytes(l1TxEncoded []byte) (*L1Tx, error) {
70 l1Tx := &L1Tx{}
71 var idxB uint = cidXB
72
73 l1TxEncodedBI := big.NewInt(0)
74 l1TxEncodedBI.SetBytes(l1TxEncoded)
75
76 toIdx, err := IdxFromBigInt(extract(l1TxEncodedBI, 0, idxB))
77
78 if err != nil {
79 return nil, err
80 }
81
82 l1Tx.ToIdx = toIdx
83
84 l1Tx.TokenID = TokenID(extract(l1TxEncodedBI, idxB, tokenIDB).Uint64())
85 l1Tx.Amount = extract(l1TxEncodedBI, idxB+tokenIDB, f16B)
86 l1Tx.LoadAmount = extract(l1TxEncodedBI, idxB+tokenIDB+f16B, f16B)
87 fromIdx, err := IdxFromBigInt(extract(l1TxEncodedBI, idxB+tokenIDB+2*f16B, f16B))
88
89 if err != nil {
90 return nil, err
91 }
92
93 l1Tx.FromIdx = fromIdx
94
95 var pkComp babyjub.PublicKeyComp
96 copy(pkComp[:], extract(l1TxEncodedBI, 2*idxB+tokenIDB+2*f16B, fromBJJCompressedB).Bytes())
97 pk, err := pkComp.Decompress()
98
99 if err != nil {
100 return nil, err
101 }
102
103 l1Tx.FromBJJ = pk
104
105 l1Tx.FromEthAddr = ethCommon.BigToAddress(extract(l1TxEncodedBI, fromBJJCompressedB+2*idxB+tokenIDB+2*f16B, fromEthAddrB))
106
107 return l1Tx, nil
108}
109
110// extract masks and shifts a bigInt
111func extract(num *big.Int, origin uint, len uint) *big.Int {
112 mask := big.NewInt(0).Sub(big.NewInt(0).Lsh(big.NewInt(1), len), big.NewInt(1))
113 return big.NewInt(0).And(big.NewInt(0).Rsh(num, origin), mask)
114}