hermez-node/common/pooll2tx.go
common/pooll2tx.goRaw
1package common
2
3import (
4 "fmt"
5 "math/big"
6 "time"
7
8 ethCommon "github.com/ethereum/go-ethereum/common"
9 "github.com/iden3/go-iden3-crypto/babyjub"
10 "github.com/iden3/go-iden3-crypto/poseidon"
11)
12
13// PoolL2Tx is a struct that represents a L2Tx sent by an account to the coordinator hat is waiting to be forged
14type PoolL2Tx struct {
15 // Stored in DB: mandatory fileds
16 TxID TxID `meddler:"tx_id"`
17 FromIdx Idx `meddler:"from_idx"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
18 ToIdx Idx `meddler:"to_idx"` // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
19 ToEthAddr ethCommon.Address `meddler:"to_eth_addr"`
20 ToBJJ *babyjub.PublicKey `meddler:"to_bjj"` // TODO: stop using json, use scanner/valuer
21 TokenID TokenID `meddler:"token_id"`
22 Amount *big.Int `meddler:"amount,bigint"` // TODO: change to float16
23 AmountFloat float64 `meddler:"amount_f"` // TODO: change to float16
24 USD float64 `meddler:"value_usd"` // TODO: change to float16
25 Fee FeeSelector `meddler:"fee"`
26 Nonce Nonce `meddler:"nonce"` // effective 40 bits used
27 State PoolL2TxState `meddler:"state"`
28 Signature *babyjub.Signature `meddler:"signature"` // tx signature
29 Timestamp time.Time `meddler:"timestamp,utctime"` // time when added to the tx pool
30 // Stored in DB: optional fileds, may be uninitialized
31 BatchNum BatchNum `meddler:"batch_num,zeroisnull"` // batchNum in which this tx was forged. Presence indicates "forged" state.
32 RqFromIdx Idx `meddler:"rq_from_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
33 RqToIdx Idx `meddler:"rq_to_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
34 RqToEthAddr ethCommon.Address `meddler:"rq_to_eth_addr"`
35 RqToBJJ *babyjub.PublicKey `meddler:"rq_to_bjj"` // TODO: stop using json, use scanner/valuer
36 RqTokenID TokenID `meddler:"rq_token_id,zeroisnull"`
37 RqAmount *big.Int `meddler:"rq_amount,bigintnull"` // TODO: change to float16
38 RqFee FeeSelector `meddler:"rq_fee,zeroisnull"`
39 RqNonce uint64 `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used
40 AbsoluteFee float64 `meddler:"fee_usd,zeroisnull"`
41 AbsoluteFeeUpdate time.Time `meddler:"usd_update,utctimez"`
42 Type TxType `meddler:"tx_type"`
43 // Extra metadata, may be uninitialized
44 RqTxCompressedData []byte `meddler:"-"` // 253 bits, optional for atomic txs
45}
46
47// TxCompressedData spec:
48// [ 1 bits ] toBJJSign // 1 byte
49// [ 8 bits ] userFee // 1 byte
50// [ 40 bits ] nonce // 5 bytes
51// [ 32 bits ] tokenID // 4 bytes
52// [ 16 bits ] amountFloat16 // 2 bytes
53// [ 48 bits ] toIdx // 6 bytes
54// [ 48 bits ] fromIdx // 6 bytes
55// [ 16 bits ] chainId // 2 bytes
56// [ 32 bits ] signatureConstant // 4 bytes
57// Total bits compressed data: 241 bits // 31 bytes in *big.Int representation
58func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
59 // sigconstant
60 sc, ok := new(big.Int).SetString("3322668559", 10)
61 if !ok {
62 return nil, fmt.Errorf("error parsing SignatureConstant")
63 }
64
65 amountFloat16, err := NewFloat16(tx.Amount)
66 if err != nil {
67 return nil, err
68 }
69 var b [31]byte
70 toBJJSign := byte(0)
71 if babyjub.PointCoordSign(tx.ToBJJ.X) {
72 toBJJSign = byte(1)
73 }
74 b[0] = toBJJSign
75 b[1] = byte(tx.Fee)
76 nonceBytes, err := tx.Nonce.Bytes()
77 if err != nil {
78 return nil, err
79 }
80 copy(b[2:7], nonceBytes[:])
81 copy(b[7:11], tx.TokenID.Bytes())
82 copy(b[11:13], amountFloat16.Bytes())
83 toIdxBytes, err := tx.ToIdx.Bytes()
84 if err != nil {
85 return nil, err
86 }
87 copy(b[13:19], toIdxBytes[:])
88 fromIdxBytes, err := tx.FromIdx.Bytes()
89 if err != nil {
90 return nil, err
91 }
92 copy(b[19:25], fromIdxBytes[:])
93 copy(b[25:27], []byte{0, 1, 0, 0}) // TODO check js implementation (unexpected behaviour from test vector generated from js)
94 copy(b[27:31], sc.Bytes())
95
96 bi := new(big.Int).SetBytes(b[:])
97 return bi, nil
98}
99
100// TxCompressedDataV2 spec:
101// [ 1 bits ] toBJJSign // 1 byte
102// [ 8 bits ] userFee // 1 byte
103// [ 40 bits ] nonce // 5 bytes
104// [ 32 bits ] tokenID // 4 bytes
105// [ 16 bits ] amountFloat16 // 2 bytes
106// [ 48 bits ] toIdx // 6 bytes
107// [ 48 bits ] fromIdx // 6 bytes
108// Total bits compressed data: 193 bits // 25 bytes in *big.Int representation
109func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
110 amountFloat16, err := NewFloat16(tx.Amount)
111 if err != nil {
112 return nil, err
113 }
114 var b [25]byte
115 toBJJSign := byte(0)
116 if babyjub.PointCoordSign(tx.ToBJJ.X) {
117 toBJJSign = byte(1)
118 }
119 b[0] = toBJJSign
120 b[1] = byte(tx.Fee)
121 nonceBytes, err := tx.Nonce.Bytes()
122 if err != nil {
123 return nil, err
124 }
125 copy(b[2:7], nonceBytes[:])
126 copy(b[7:11], tx.TokenID.Bytes())
127 copy(b[11:13], amountFloat16.Bytes())
128 toIdxBytes, err := tx.ToIdx.Bytes()
129 if err != nil {
130 return nil, err
131 }
132 copy(b[13:19], toIdxBytes[:])
133 fromIdxBytes, err := tx.FromIdx.Bytes()
134 if err != nil {
135 return nil, err
136 }
137 copy(b[19:25], fromIdxBytes[:])
138
139 bi := new(big.Int).SetBytes(b[:])
140 return bi, nil
141}
142
143// HashToSign returns the computed Poseidon hash from the *PoolL2Tx that will be signed by the sender.
144func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
145 toCompressedData, err := tx.TxCompressedData()
146 if err != nil {
147 return nil, err
148 }
149 toEthAddr := EthAddrToBigInt(tx.ToEthAddr)
150 toBJJAy := tx.ToBJJ.Y
151 rqTxCompressedDataV2, err := tx.TxCompressedDataV2()
152 if err != nil {
153 return nil, err
154 }
155
156 return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBJJAy, rqTxCompressedDataV2, EthAddrToBigInt(tx.RqToEthAddr), tx.RqToBJJ.Y})
157}
158
159// VerifySignature returns true if the signature verification is correct for the given PublicKey
160func (tx *PoolL2Tx) VerifySignature(pk *babyjub.PublicKey) bool {
161 h, err := tx.HashToSign()
162 if err != nil {
163 return false
164 }
165 return pk.VerifyPoseidon(h, tx.Signature)
166}
167
168// L2Tx returns a *L2Tx from the PoolL2Tx
169func (tx *PoolL2Tx) L2Tx() *L2Tx {
170 return &L2Tx{
171 TxID: tx.TxID,
172 BatchNum: tx.BatchNum,
173 FromIdx: tx.FromIdx,
174 ToIdx: tx.ToIdx,
175 Amount: tx.Amount,
176 Fee: tx.Fee,
177 Nonce: tx.Nonce,
178 Type: tx.Type,
179 }
180}
181
182// Tx returns a *Tx from the PoolL2Tx
183func (tx *PoolL2Tx) Tx() *Tx {
184 return &Tx{
185 TxID: tx.TxID,
186 FromIdx: tx.FromIdx,
187 ToIdx: tx.ToIdx,
188 Amount: tx.Amount,
189 Nonce: tx.Nonce,
190 Fee: tx.Fee,
191 Type: tx.Type,
192 }
193}
194
195// PoolL2TxsToL2Txs returns an array of []*L2Tx from an array of []*PoolL2Tx
196func PoolL2TxsToL2Txs(txs []*PoolL2Tx) []*L2Tx {
197 var r []*L2Tx
198 for _, tx := range txs {
199 r = append(r, tx.L2Tx())
200 }
201 return r
202}
203
204// PoolL2TxState is a struct that represents the status of a L2 transaction
205type PoolL2TxState string
206
207const (
208 // PoolL2TxStatePending represents a valid L2Tx that hasn't started the forging process
209 PoolL2TxStatePending PoolL2TxState = "pend"
210 // PoolL2TxStateForging represents a valid L2Tx that has started the forging process
211 PoolL2TxStateForging PoolL2TxState = "fing"
212 // PoolL2TxStateForged represents a L2Tx that has already been forged
213 PoolL2TxStateForged PoolL2TxState = "fged"
214 // PoolL2TxStateInvalid represents a L2Tx that has been invalidated
215 PoolL2TxStateInvalid PoolL2TxState = "invl"
216)