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.

259 lines
8.6 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 (12 bytes) for L2Tx is:
  14. // bytes: | 1 | 6 | 5 |
  15. // values: | type | FromIdx | Nonce |
  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"`
  41. AbsoluteFeeUpdate *time.Time `meddler:"usd_update,utctime"`
  42. Type TxType `meddler:"tx_type"`
  43. // Extra metadata, may be uninitialized
  44. RqTxCompressedData []byte `meddler:"-"` // 253 bits, optional for atomic txs
  45. TokenSymbol string `meddler:"token_symbol"`
  46. }
  47. // NewPoolL2Tx returns the given L2Tx with the TxId & Type parameters calculated
  48. // from the L2Tx values
  49. func NewPoolL2Tx(poolL2Tx *PoolL2Tx) (*PoolL2Tx, error) {
  50. // calculate TxType
  51. var txType TxType
  52. if poolL2Tx.ToIdx == Idx(0) {
  53. txType = TxTypeTransfer
  54. } else if poolL2Tx.ToIdx == Idx(1) {
  55. txType = TxTypeExit
  56. } else if poolL2Tx.ToIdx >= IdxUserThreshold {
  57. txType = TxTypeTransfer
  58. } else {
  59. return poolL2Tx, fmt.Errorf("Can not determine type of PoolL2Tx, invalid ToIdx value: %d", poolL2Tx.ToIdx)
  60. }
  61. // if TxType!=poolL2Tx.TxType return error
  62. if poolL2Tx.Type != "" && poolL2Tx.Type != txType {
  63. return poolL2Tx, fmt.Errorf("PoolL2Tx.Type: %s, should be: %s", poolL2Tx.Type, txType)
  64. }
  65. poolL2Tx.Type = txType
  66. var txid [TxIDLen]byte
  67. txid[0] = TxIDPrefixL2Tx
  68. fromIdxBytes, err := poolL2Tx.FromIdx.Bytes()
  69. if err != nil {
  70. return poolL2Tx, err
  71. }
  72. copy(txid[1:7], fromIdxBytes[:])
  73. nonceBytes, err := poolL2Tx.Nonce.Bytes()
  74. if err != nil {
  75. return poolL2Tx, err
  76. }
  77. copy(txid[7:12], nonceBytes[:])
  78. poolL2Tx.TxID = TxID(txid)
  79. return poolL2Tx, nil
  80. }
  81. // TxCompressedData spec:
  82. // [ 1 bits ] toBJJSign // 1 byte
  83. // [ 8 bits ] userFee // 1 byte
  84. // [ 40 bits ] nonce // 5 bytes
  85. // [ 32 bits ] tokenID // 4 bytes
  86. // [ 16 bits ] amountFloat16 // 2 bytes
  87. // [ 48 bits ] toIdx // 6 bytes
  88. // [ 48 bits ] fromIdx // 6 bytes
  89. // [ 16 bits ] chainId // 2 bytes
  90. // [ 32 bits ] signatureConstant // 4 bytes
  91. // Total bits compressed data: 241 bits // 31 bytes in *big.Int representation
  92. func (tx *PoolL2Tx) TxCompressedData() (*big.Int, error) {
  93. // sigconstant
  94. sc, ok := new(big.Int).SetString("3322668559", 10)
  95. if !ok {
  96. return nil, fmt.Errorf("error parsing SignatureConstant")
  97. }
  98. amountFloat16, err := NewFloat16(tx.Amount)
  99. if err != nil {
  100. return nil, err
  101. }
  102. var b [31]byte
  103. toBJJSign := byte(0)
  104. if babyjub.PointCoordSign(tx.ToBJJ.X) {
  105. toBJJSign = byte(1)
  106. }
  107. b[0] = toBJJSign
  108. b[1] = byte(tx.Fee)
  109. nonceBytes, err := tx.Nonce.Bytes()
  110. if err != nil {
  111. return nil, err
  112. }
  113. copy(b[2:7], nonceBytes[:])
  114. copy(b[7:11], tx.TokenID.Bytes())
  115. copy(b[11:13], amountFloat16.Bytes())
  116. toIdxBytes, err := tx.ToIdx.Bytes()
  117. if err != nil {
  118. return nil, err
  119. }
  120. copy(b[13:19], toIdxBytes[:])
  121. fromIdxBytes, err := tx.FromIdx.Bytes()
  122. if err != nil {
  123. return nil, err
  124. }
  125. copy(b[19:25], fromIdxBytes[:])
  126. copy(b[25:27], []byte{0, 1, 0, 0}) // TODO check js implementation (unexpected behaviour from test vector generated from js)
  127. copy(b[27:31], sc.Bytes())
  128. bi := new(big.Int).SetBytes(b[:])
  129. return bi, nil
  130. }
  131. // TxCompressedDataV2 spec:
  132. // [ 1 bits ] toBJJSign // 1 byte
  133. // [ 8 bits ] userFee // 1 byte
  134. // [ 40 bits ] nonce // 5 bytes
  135. // [ 32 bits ] tokenID // 4 bytes
  136. // [ 16 bits ] amountFloat16 // 2 bytes
  137. // [ 48 bits ] toIdx // 6 bytes
  138. // [ 48 bits ] fromIdx // 6 bytes
  139. // Total bits compressed data: 193 bits // 25 bytes in *big.Int representation
  140. func (tx *PoolL2Tx) TxCompressedDataV2() (*big.Int, error) {
  141. amountFloat16, err := NewFloat16(tx.Amount)
  142. if err != nil {
  143. return nil, err
  144. }
  145. var b [25]byte
  146. toBJJSign := byte(0)
  147. if babyjub.PointCoordSign(tx.ToBJJ.X) {
  148. toBJJSign = byte(1)
  149. }
  150. b[0] = toBJJSign
  151. b[1] = byte(tx.Fee)
  152. nonceBytes, err := tx.Nonce.Bytes()
  153. if err != nil {
  154. return nil, err
  155. }
  156. copy(b[2:7], nonceBytes[:])
  157. copy(b[7:11], tx.TokenID.Bytes())
  158. copy(b[11:13], amountFloat16.Bytes())
  159. toIdxBytes, err := tx.ToIdx.Bytes()
  160. if err != nil {
  161. return nil, err
  162. }
  163. copy(b[13:19], toIdxBytes[:])
  164. fromIdxBytes, err := tx.FromIdx.Bytes()
  165. if err != nil {
  166. return nil, err
  167. }
  168. copy(b[19:25], fromIdxBytes[:])
  169. bi := new(big.Int).SetBytes(b[:])
  170. return bi, nil
  171. }
  172. // HashToSign returns the computed Poseidon hash from the *PoolL2Tx that will be signed by the sender.
  173. func (tx *PoolL2Tx) HashToSign() (*big.Int, error) {
  174. toCompressedData, err := tx.TxCompressedData()
  175. if err != nil {
  176. return nil, err
  177. }
  178. toEthAddr := EthAddrToBigInt(tx.ToEthAddr)
  179. toBJJAy := tx.ToBJJ.Y
  180. rqTxCompressedDataV2, err := tx.TxCompressedDataV2()
  181. if err != nil {
  182. return nil, err
  183. }
  184. return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBJJAy, rqTxCompressedDataV2, EthAddrToBigInt(tx.RqToEthAddr), tx.RqToBJJ.Y})
  185. }
  186. // VerifySignature returns true if the signature verification is correct for the given PublicKey
  187. func (tx *PoolL2Tx) VerifySignature(pk *babyjub.PublicKey) bool {
  188. h, err := tx.HashToSign()
  189. if err != nil {
  190. return false
  191. }
  192. return pk.VerifyPoseidon(h, tx.Signature)
  193. }
  194. // L2Tx returns a *L2Tx from the PoolL2Tx
  195. func (tx *PoolL2Tx) L2Tx() *L2Tx {
  196. return &L2Tx{
  197. TxID: tx.TxID,
  198. BatchNum: tx.BatchNum,
  199. FromIdx: tx.FromIdx,
  200. ToIdx: tx.ToIdx,
  201. Amount: tx.Amount,
  202. Fee: tx.Fee,
  203. Nonce: tx.Nonce,
  204. Type: tx.Type,
  205. }
  206. }
  207. // Tx returns a *Tx from the PoolL2Tx
  208. func (tx *PoolL2Tx) Tx() *Tx {
  209. return &Tx{
  210. TxID: tx.TxID,
  211. FromIdx: tx.FromIdx,
  212. ToIdx: tx.ToIdx,
  213. Amount: tx.Amount,
  214. Nonce: &tx.Nonce,
  215. Fee: &tx.Fee,
  216. Type: tx.Type,
  217. }
  218. }
  219. // PoolL2TxsToL2Txs returns an array of []*L2Tx from an array of []*PoolL2Tx
  220. func PoolL2TxsToL2Txs(txs []*PoolL2Tx) []*L2Tx {
  221. var r []*L2Tx
  222. for _, tx := range txs {
  223. r = append(r, tx.L2Tx())
  224. }
  225. return r
  226. }
  227. // PoolL2TxState is a struct that represents the status of a L2 transaction
  228. type PoolL2TxState string
  229. const (
  230. // PoolL2TxStatePending represents a valid L2Tx that hasn't started the forging process
  231. PoolL2TxStatePending PoolL2TxState = "pend"
  232. // PoolL2TxStateForging represents a valid L2Tx that has started the forging process
  233. PoolL2TxStateForging PoolL2TxState = "fing"
  234. // PoolL2TxStateForged represents a L2Tx that has already been forged
  235. PoolL2TxStateForged PoolL2TxState = "fged"
  236. // PoolL2TxStateInvalid represents a L2Tx that has been invalidated
  237. PoolL2TxStateInvalid PoolL2TxState = "invl"
  238. )