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.

217 lines
7.4 KiB

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