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.

278 lines
9.1 KiB

4 years ago
4 years ago
4 years ago
  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/big"
  6. "time"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/iden3/go-iden3-crypto/babyjub"
  9. "github.com/iden3/go-iden3-crypto/poseidon"
  10. )
  11. // PoolL2Tx is a struct that represents a L2Tx sent by an account to the coordinator hat is waiting to be forged
  12. type PoolL2Tx struct {
  13. // Stored in DB: mandatory fileds
  14. // TxID (12 bytes) for L2Tx is:
  15. // bytes: | 1 | 6 | 5 |
  16. // values: | type | FromIdx | Nonce |
  17. TxID TxID `meddler:"tx_id"`
  18. FromIdx Idx `meddler:"from_idx"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  19. ToIdx *Idx `meddler:"to_idx"` // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
  20. ToEthAddr *ethCommon.Address `meddler:"to_eth_addr"`
  21. ToBJJ *babyjub.PublicKey `meddler:"to_bjj"` // TODO: stop using json, use scanner/valuer
  22. TokenID TokenID `meddler:"token_id"`
  23. Amount *big.Int `meddler:"amount,bigint"` // TODO: change to float16
  24. AmountFloat float64 `meddler:"amount_f"` // TODO: change to float16
  25. USD *float64 `meddler:"value_usd"` // TODO: change to float16
  26. Fee FeeSelector `meddler:"fee"`
  27. Nonce Nonce `meddler:"nonce"` // effective 40 bits used
  28. State PoolL2TxState `meddler:"state"`
  29. Signature *babyjub.Signature `meddler:"signature"` // tx signature
  30. Timestamp time.Time `meddler:"timestamp,utctime"` // time when added to the tx pool
  31. // Stored in DB: optional fileds, may be uninitialized
  32. BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. Presence indicates "forged" state.
  33. RqFromIdx *Idx `meddler:"rq_from_idx"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  34. RqToIdx *Idx `meddler:"rq_to_idx"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  35. RqToEthAddr *ethCommon.Address `meddler:"rq_to_eth_addr"`
  36. RqToBJJ *babyjub.PublicKey `meddler:"rq_to_bjj"` // TODO: stop using json, use scanner/valuer
  37. RqTokenID *TokenID `meddler:"rq_token_id"`
  38. RqAmount *big.Int `meddler:"rq_amount,bigintnull"` // TODO: change to float16
  39. RqFee *FeeSelector `meddler:"rq_fee"`
  40. RqNonce *uint64 `meddler:"rq_nonce"` // effective 48 bits used
  41. AbsoluteFee *float64 `meddler:"fee_usd"`
  42. AbsoluteFeeUpdate *time.Time `meddler:"usd_update,utctime"`
  43. Type TxType `meddler:"tx_type"`
  44. // Extra metadata, may be uninitialized
  45. RqTxCompressedData []byte `meddler:"-"` // 253 bits, optional for atomic txs
  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 == nil || *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 := big.NewInt(0)
  179. if tx.ToEthAddr != nil {
  180. toEthAddr = EthAddrToBigInt(*tx.ToEthAddr)
  181. }
  182. rqToEthAddr := big.NewInt(0)
  183. if tx.RqToEthAddr != nil {
  184. rqToEthAddr = EthAddrToBigInt(*tx.RqToEthAddr)
  185. }
  186. toBJJAy := tx.ToBJJ.Y
  187. rqTxCompressedDataV2, err := tx.TxCompressedDataV2()
  188. if err != nil {
  189. return nil, err
  190. }
  191. return poseidon.Hash([]*big.Int{toCompressedData, toEthAddr, toBJJAy, rqTxCompressedDataV2, rqToEthAddr, tx.RqToBJJ.Y})
  192. }
  193. // VerifySignature returns true if the signature verification is correct for the given PublicKey
  194. func (tx *PoolL2Tx) VerifySignature(pk *babyjub.PublicKey) bool {
  195. h, err := tx.HashToSign()
  196. if err != nil {
  197. return false
  198. }
  199. return pk.VerifyPoseidon(h, tx.Signature)
  200. }
  201. // L2Tx returns a *L2Tx from the PoolL2Tx
  202. func (tx *PoolL2Tx) L2Tx() (*L2Tx, error) {
  203. if tx.ToIdx == nil || tx.BatchNum == nil {
  204. return nil, errors.New("PoolL2Tx must have ToIdx != nil and BatchNum != nil in order to be able to transform to Tx")
  205. }
  206. return &L2Tx{
  207. TxID: tx.TxID,
  208. BatchNum: *tx.BatchNum,
  209. FromIdx: tx.FromIdx,
  210. ToIdx: *tx.ToIdx,
  211. Amount: tx.Amount,
  212. Fee: tx.Fee,
  213. Nonce: tx.Nonce,
  214. Type: tx.Type,
  215. }, nil
  216. }
  217. // Tx returns a *Tx from the PoolL2Tx
  218. func (tx *PoolL2Tx) Tx() (*Tx, error) {
  219. if tx.ToIdx == nil {
  220. return nil, errors.New("PoolL2Tx must have ToIdx != nil in order to be able to transform to Tx")
  221. }
  222. fromIdx := new(Idx)
  223. *fromIdx = tx.FromIdx
  224. return &Tx{
  225. TxID: tx.TxID,
  226. FromIdx: fromIdx,
  227. ToIdx: tx.ToIdx,
  228. Amount: tx.Amount,
  229. Nonce: &tx.Nonce,
  230. Fee: &tx.Fee,
  231. Type: tx.Type,
  232. }, nil
  233. }
  234. // PoolL2TxsToL2Txs returns an array of []*L2Tx from an array of []*PoolL2Tx
  235. func PoolL2TxsToL2Txs(txs []*PoolL2Tx) ([]*L2Tx, error) {
  236. var r []*L2Tx
  237. for _, poolTx := range txs {
  238. tx, err := poolTx.L2Tx()
  239. if err != nil {
  240. return nil, err
  241. }
  242. r = append(r, tx)
  243. }
  244. return r, nil
  245. }
  246. // PoolL2TxState is a struct that represents the status of a L2 transaction
  247. type PoolL2TxState string
  248. const (
  249. // PoolL2TxStatePending represents a valid L2Tx that hasn't started the forging process
  250. PoolL2TxStatePending PoolL2TxState = "pend"
  251. // PoolL2TxStateForging represents a valid L2Tx that has started the forging process
  252. PoolL2TxStateForging PoolL2TxState = "fing"
  253. // PoolL2TxStateForged represents a L2Tx that has already been forged
  254. PoolL2TxStateForged PoolL2TxState = "fged"
  255. // PoolL2TxStateInvalid represents a L2Tx that has been invalidated
  256. PoolL2TxStateInvalid PoolL2TxState = "invl"
  257. )