You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
8.1 KiB

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