arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/pooll2tx.go
common/pooll2tx.goRaw
  1package common
  2
  3import (
  4	"encoding/binary"
  5	"errors"
  6	"fmt"
  7	"math/big"
  8	"time"
  9
 10	ethCommon "github.com/ethereum/go-ethereum/common"
 11	"github.com/hermeznetwork/tracerr"
 12	"github.com/iden3/go-iden3-crypto/babyjub"
 13	"github.com/iden3/go-iden3-crypto/poseidon"
 14)
 15
 16// EmptyBJJComp contains the 32 byte array of a empty BabyJubJub PublicKey
 17// Compressed. It is a valid point in the BabyJubJub curve, so does not give
 18// errors when being decompressed.
 19var EmptyBJJComp = babyjub.PublicKeyComp([32]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 20	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
 21
 22// PoolL2Tx is a struct that represents a L2Tx sent by an account to the
 23// coordinator that is waiting to be forged
 24type PoolL2Tx struct {
 25	// Stored in DB: mandatory fileds
 26
 27	// TxID (12 bytes) for L2Tx is:
 28	// bytes:  |  1   |    6    |   5   |
 29	// values: | type | FromIdx | Nonce |
 30	TxID    TxID `meddler:"tx_id"`
 31	FromIdx Idx  `meddler:"from_idx"`
 32	ToIdx   Idx  `meddler:"to_idx,zeroisnull"`
 33	// AuxToIdx is only used internally at the StateDB to avoid repeated
 34	// computation when processing transactions (from Synchronizer,
 35	// TxSelector, BatchBuilder)
 36	AuxToIdx  Idx                   `meddler:"-"`
 37	ToEthAddr ethCommon.Address     `meddler:"to_eth_addr,zeroisnull"`
 38	ToBJJ     babyjub.PublicKeyComp `meddler:"to_bjj,zeroisnull"`
 39	TokenID   TokenID               `meddler:"token_id"`
 40	Amount    *big.Int              `meddler:"amount,bigint"`
 41	Fee       FeeSelector           `meddler:"fee"`
 42	Nonce     Nonce                 `meddler:"nonce"` // effective 40 bits used
 43	State     PoolL2TxState         `meddler:"state"`
 44	// Info contains information about the status & State of the
 45	// transaction. As for example, if the Tx has not been selected in the
 46	// last batch due not enough Balance at the Sender account, this reason
 47	// would appear at this parameter.
 48	Info      string                `meddler:"info,zeroisnull"`
 49	Signature babyjub.SignatureComp `meddler:"signature"`         // tx signature
 50	Timestamp time.Time             `meddler:"timestamp,utctime"` // time when added to the tx pool
 51	// Stored in DB: optional fileds, may be uninitialized
 52	RqFromIdx         Idx                   `meddler:"rq_from_idx,zeroisnull"`
 53	RqToIdx           Idx                   `meddler:"rq_to_idx,zeroisnull"`
 54	RqToEthAddr       ethCommon.Address     `meddler:"rq_to_eth_addr,zeroisnull"`
 55	RqToBJJ           babyjub.PublicKeyComp `meddler:"rq_to_bjj,zeroisnull"`
 56	RqTokenID         TokenID               `meddler:"rq_token_id,zeroisnull"`
 57	RqAmount          *big.Int              `meddler:"rq_amount,bigintnull"`
 58	RqFee             FeeSelector           `meddler:"rq_fee,zeroisnull"`
 59	RqNonce           Nonce                 `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used
 60	AbsoluteFee       float64               `meddler:"fee_usd,zeroisnull"`
 61	AbsoluteFeeUpdate time.Time             `meddler:"usd_update,utctimez"`
 62	Type              TxType                `meddler:"tx_type"`
 63	// Extra metadata, may be uninitialized
 64	RqTxCompressedData []byte `meddler:"-"` // 253 bits, optional for atomic txs
 65}
 66
 67// NewPoolL2Tx returns the given L2Tx with the TxId & Type parameters calculated
 68// from the L2Tx values
 69func NewPoolL2Tx(tx *PoolL2Tx) (*PoolL2Tx, error) {
 70	txTypeOld := tx.Type
 71	if err := tx.SetType(); err != nil {
 72		return nil, tracerr.Wrap(err)
 73	}
 74	// If original Type doesn't match the correct one, return error
 75	if txTypeOld != "" && txTypeOld != tx.Type {
 76		return nil, tracerr.Wrap(fmt.Errorf("L2Tx.Type: %s, should be: %s",
 77			txTypeOld, tx.Type))
 78	}
 79
 80	txIDOld := tx.TxID
 81	if err := tx.SetID(); err != nil {
 82		return nil, tracerr.Wrap(err)
 83	}
 84	// If original TxID doesn't match the correct one, return error
 85	if txIDOld != (TxID{}) && txIDOld != tx.TxID {
 86		return tx, tracerr.Wrap(fmt.Errorf("PoolL2Tx.TxID: %s, should be: %s",
 87			txIDOld.String(), tx.TxID.String()))
 88	}
 89
 90	return tx, nil
 91}
 92
 93// SetType sets the type of the transaction
 94func (tx *PoolL2Tx) SetType() error {
 95	if tx.ToIdx >= IdxUserThreshold {
 96		tx.Type = TxTypeTransfer
 97	} else if tx.ToIdx == 1 {
 98		tx.Type = TxTypeExit
 99	} else if tx.ToIdx == 0 {
100		if tx.ToBJJ != EmptyBJJComp && tx.ToEthAddr == FFAddr {
101			tx.Type = TxTypeTransferToBJJ
102		} else if tx.ToEthAddr != FFAddr && tx.ToEthAddr != EmptyAddr {
103			tx.Type = TxTypeTransferToEthAddr
104		} else {
105			return tracerr.Wrap(errors.New("malformed transaction"))
106		}
107	} else {
108		return tracerr.Wrap(errors.New("malformed transaction"))
109	}
110	return nil
111}
112
113// SetID sets the ID of the transaction
114func (tx *PoolL2Tx) SetID() error {
115	txID, err := tx.L2Tx().CalculateTxID()
116	if err != nil {
117		return tracerr.Wrap(err)
118	}
119	tx.TxID = txID
120	return nil
121}
122
123// TxCompressedData spec:
124// [ 1 bits  ] toBJJSign // 1 byte
125// [ 8 bits  ] userFee // 1 byte
126// [ 40 bits ] nonce // 5 bytes
127// [ 32 bits ] tokenID // 4 bytes
128// [ 48 bits ] toIdx // 6 bytes
129// [ 48 bits ] fromIdx // 6 bytes
130// [ 16 bits ] chainId // 2 bytes
131// [ 32 bits ] signatureConstant // 4 bytes
132// Total bits compressed data:  225 bits // 29 bytes in *big.Int representation
133func (tx *PoolL2Tx) TxCompressedData(chainID uint16) (*big.Int, error) {
134	var b [29]byte
135
136	toBJJSign := byte(0)
137	pkSign, _ := babyjub.UnpackSignY(tx.ToBJJ)
138	if pkSign {
139		toBJJSign = byte(1)
140	}
141
142	b[0] = toBJJSign
143	b[1] = byte(tx.Fee)
144	nonceBytes, err := tx.Nonce.Bytes()
145	if err != nil {
146		return nil, tracerr.Wrap(err)
147	}
148	copy(b[2:7], nonceBytes[:])
149	copy(b[7:11], tx.TokenID.Bytes())
150	toIdxBytes, err := tx.ToIdx.Bytes()
151	if err != nil {
152		return nil, tracerr.Wrap(err)
153	}
154	copy(b[11:17], toIdxBytes[:])
155	fromIdxBytes, err := tx.FromIdx.Bytes()
156	if err != nil {
157		return nil, tracerr.Wrap(err)
158	}
159	copy(b[17:23], fromIdxBytes[:])
160	binary.BigEndian.PutUint16(b[23:25], chainID)
161	copy(b[25:29], SignatureConstantBytes[:])
162
163	bi := new(big.Int).SetBytes(b[:])
164	return bi, nil
165}
166
167// TxCompressedDataEmpty calculates the TxCompressedData of an empty
168// transaction
169func TxCompressedDataEmpty(chainID uint16) *big.Int {
170	var b [29]byte
171	binary.BigEndian.PutUint16(b[23:25], chainID)
172	copy(b[25:29], SignatureConstantBytes[:])
173	bi := new(big.Int).SetBytes(b[:])
174	return bi
175}
176
177// TxCompressedDataV2 spec:
178// [ 1 bits  ] toBJJSign // 1 byte
179// [ 8 bits  ] userFee // 1 byte
180// [ 40 bits ] nonce // 5 bytes
181// [ 32 bits ] tokenID // 4 bytes
182// [ 40 bits ] amountFloat40 // 5 bytes
183// [ 48 bits ] toIdx // 6 bytes
184// [ 48 bits ] fromIdx // 6 bytes
185// Total bits compressed data:  217 bits // 28 bytes in *big.Int representation
186func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
187	if tx.Amount == nil {
188		tx.Amount = big.NewInt(0)
189	}
190	amountFloat40, err := NewFloat40(tx.Amount)
191	if err != nil {
192		return nil, tracerr.Wrap(err)
193	}
194	amountFloat40Bytes, err := amountFloat40.Bytes()
195	if err != nil {
196		return nil, tracerr.Wrap(err)
197	}
198
199	var b [28]byte
200	toBJJSign := byte(0)
201	if tx.ToBJJ != EmptyBJJComp {
202		sign, _ := babyjub.UnpackSignY(tx.ToBJJ)
203		if sign {
204			toBJJSign = byte(1)
205		}
206	}
207	b[0] = toBJJSign
208	b[1] = byte(tx.Fee)
209	nonceBytes, err := tx.Nonce.Bytes()
210	if err != nil {
211		return nil, tracerr.Wrap(err)
212	}
213	copy(b[2:7], nonceBytes[:])
214	copy(b[7:11], tx.TokenID.Bytes())
215	copy(b[11:16], amountFloat40Bytes)
216	toIdxBytes, err := tx.ToIdx.Bytes()
217	if err != nil {
218		return nil, tracerr.Wrap(err)
219	}
220	copy(b[16:22], toIdxBytes[:])
221	fromIdxBytes, err := tx.FromIdx.Bytes()
222	if err != nil {
223		return nil, tracerr.Wrap(err)
224	}
225	copy(b[22:28], fromIdxBytes[:])
226
227	bi := new(big.Int).SetBytes(b[:])
228	return bi, nil
229}
230
231// RqTxCompressedDataV2 is like the TxCompressedDataV2 but using the 'Rq'
232// parameters. In a future iteration of the hermez-node, the 'Rq' parameters
233// can be inside a struct, which contains the 'Rq' transaction grouped inside,
234// so then computing the 'RqTxCompressedDataV2' would be just calling
235// 'tx.Rq.TxCompressedDataV2()'.
236// RqTxCompressedDataV2 spec:
237// [ 1 bits  ] rqToBJJSign // 1 byte
238// [ 8 bits  ] rqUserFee // 1 byte
239// [ 40 bits ] rqNonce // 5 bytes
240// [ 32 bits ] rqTokenID // 4 bytes
241// [ 40 bits ] rqAmountFloat40 // 5 bytes
242// [ 48 bits ] rqToIdx // 6 bytes
243// [ 48 bits ] rqFromIdx // 6 bytes
244// Total bits compressed data:  217 bits // 28 bytes in *big.Int representation
245func (tx *PoolL2Tx) RqTxCompressedDataV2() (*big.Int, error) {
246	if tx.RqAmount == nil {
247		tx.RqAmount = big.NewInt(0)
248	}
249	amountFloat40, err := NewFloat40(tx.RqAmount)
250	if err != nil {
251		return nil, tracerr.Wrap(err)
252	}
253	amountFloat40Bytes, err := amountFloat40.Bytes()
254	if err != nil {
255		return nil, tracerr.Wrap(err)
256	}
257
258	var b [28]byte
259	rqToBJJSign := byte(0)
260	if tx.RqToBJJ != EmptyBJJComp {
261		sign, _ := babyjub.UnpackSignY(tx.RqToBJJ)
262		if sign {
263			rqToBJJSign = byte(1)
264		}
265	}
266	b[0] = rqToBJJSign
267	b[1] = byte(tx.RqFee)
268	nonceBytes, err := tx.RqNonce.Bytes()
269	if err != nil {
270		return nil, tracerr.Wrap(err)
271	}
272	copy(b[2:7], nonceBytes[:])
273	copy(b[7:11], tx.RqTokenID.Bytes())
274	copy(b[11:16], amountFloat40Bytes)
275	toIdxBytes, err := tx.RqToIdx.Bytes()
276	if err != nil {
277		return nil, tracerr.Wrap(err)
278	}
279	copy(b[16:22], toIdxBytes[:])
280	fromIdxBytes, err := tx.RqFromIdx.Bytes()
281	if err != nil {
282		return nil, tracerr.Wrap(err)
283	}
284	copy(b[22:28], fromIdxBytes[:])
285
286	bi := new(big.Int).SetBytes(b[:])
287	return bi, nil
288}
289
290// HashToSign returns the computed Poseidon hash from the *PoolL2Tx that will
291// be signed by the sender.
292func (tx *PoolL2Tx) HashToSign(chainID uint16) (*big.Int, error) {
293	toCompressedData, err := tx.TxCompressedData(chainID)
294	if err != nil {
295		return nil, tracerr.Wrap(err)
296	}
297
298	// e1: [5 bytes AmountFloat40 | 20 bytes ToEthAddr]
299	var e1B [25]byte
300	amountFloat40, err := NewFloat40(tx.Amount)
301	if err != nil {
302		return nil, tracerr.Wrap(err)
303	}
304	amountFloat40Bytes, err := amountFloat40.Bytes()
305	if err != nil {
306		return nil, tracerr.Wrap(err)
307	}
308	copy(e1B[0:5], amountFloat40Bytes)
309	toEthAddr := EthAddrToBigInt(tx.ToEthAddr)
310	copy(e1B[5:25], toEthAddr.Bytes())
311	e1 := new(big.Int).SetBytes(e1B[:])
312
313	rqToEthAddr := EthAddrToBigInt(tx.RqToEthAddr)
314
315	_, toBJJY := babyjub.UnpackSignY(tx.ToBJJ)
316
317	rqTxCompressedDataV2, err := tx.RqTxCompressedDataV2()
318	if err != nil {
319		return nil, tracerr.Wrap(err)
320	}
321
322	_, rqToBJJY := babyjub.UnpackSignY(tx.RqToBJJ)
323
324	return poseidon.Hash([]*big.Int{toCompressedData, e1, toBJJY, rqTxCompressedDataV2,
325		rqToEthAddr, rqToBJJY})
326}
327
328// VerifySignature returns true if the signature verification is correct for the given PublicKeyComp
329func (tx *PoolL2Tx) VerifySignature(chainID uint16, pkComp babyjub.PublicKeyComp) bool {
330	h, err := tx.HashToSign(chainID)
331	if err != nil {
332		return false
333	}
334	s, err := tx.Signature.Decompress()
335	if err != nil {
336		return false
337	}
338	pk, err := pkComp.Decompress()
339	if err != nil {
340		return false
341	}
342	return pk.VerifyPoseidon(h, s)
343}
344
345// L2Tx returns a *L2Tx from the PoolL2Tx
346func (tx PoolL2Tx) L2Tx() L2Tx {
347	var toIdx Idx
348	if tx.ToIdx == Idx(0) {
349		toIdx = tx.AuxToIdx
350	} else {
351		toIdx = tx.ToIdx
352	}
353	return L2Tx{
354		TxID:    tx.TxID,
355		FromIdx: tx.FromIdx,
356		ToIdx:   toIdx,
357		TokenID: tx.TokenID,
358		Amount:  tx.Amount,
359		Fee:     tx.Fee,
360		Nonce:   tx.Nonce,
361		Type:    tx.Type,
362	}
363}
364
365// Tx returns a *Tx from the PoolL2Tx
366func (tx PoolL2Tx) Tx() Tx {
367	return Tx{
368		TxID:    tx.TxID,
369		FromIdx: tx.FromIdx,
370		ToIdx:   tx.ToIdx,
371		Amount:  tx.Amount,
372		TokenID: tx.TokenID,
373		Nonce:   &tx.Nonce,
374		Fee:     &tx.Fee,
375		Type:    tx.Type,
376	}
377}
378
379// PoolL2TxsToL2Txs returns an array of []L2Tx from an array of []PoolL2Tx
380func PoolL2TxsToL2Txs(txs []PoolL2Tx) ([]L2Tx, error) {
381	l2Txs := make([]L2Tx, len(txs))
382	for i, poolTx := range txs {
383		l2Txs[i] = poolTx.L2Tx()
384	}
385	return l2Txs, nil
386}
387
388// TxIDsFromPoolL2Txs returns an array of TxID from the []PoolL2Tx
389func TxIDsFromPoolL2Txs(txs []PoolL2Tx) []TxID {
390	txIDs := make([]TxID, len(txs))
391	for i, tx := range txs {
392		txIDs[i] = tx.TxID
393	}
394	return txIDs
395}
396
397// PoolL2TxState is a string that represents the status of a L2 transaction
398type PoolL2TxState string
399
400const (
401	// PoolL2TxStatePending represents a valid L2Tx that hasn't started the
402	// forging process
403	PoolL2TxStatePending PoolL2TxState = "pend"
404	// PoolL2TxStateForging represents a valid L2Tx that has started the
405	// forging process
406	PoolL2TxStateForging PoolL2TxState = "fing"
407	// PoolL2TxStateForged represents a L2Tx that has already been forged
408	PoolL2TxStateForged PoolL2TxState = "fged"
409	// PoolL2TxStateInvalid represents a L2Tx that has been invalidated
410	PoolL2TxStateInvalid PoolL2TxState = "invl"
411)