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.

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