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.

132 lines
4.3 KiB

4 years ago
4 years ago
  1. package common
  2. import (
  3. "math/big"
  4. ethCommon "github.com/ethereum/go-ethereum/common"
  5. "github.com/iden3/go-iden3-crypto/babyjub"
  6. )
  7. const (
  8. fromBJJCompressedB = 256
  9. fromEthAddrB = 160
  10. f16B = 16
  11. tokenIDB = 32
  12. cidXB = 32
  13. )
  14. // L1Tx is a struct that represents a L1 tx
  15. type L1Tx struct {
  16. // Stored in DB: mandatory fileds
  17. TxID TxID
  18. ToForgeL1TxsNum uint32 // toForgeL1TxsNum in which the tx was forged / will be forged
  19. Position int
  20. UserOrigin bool // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
  21. FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  22. FromEthAddr ethCommon.Address
  23. FromBJJ *babyjub.PublicKey
  24. ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
  25. TokenID TokenID
  26. Amount *big.Int
  27. LoadAmount *big.Int
  28. EthBlockNum int64 // Ethereum Block Number in which this L1Tx was added to the queue
  29. Type TxType
  30. BatchNum BatchNum
  31. }
  32. // Tx returns a *Tx from the L1Tx
  33. func (tx *L1Tx) Tx() *Tx {
  34. f := new(big.Float).SetInt(tx.Amount)
  35. amountFloat, _ := f.Float64()
  36. genericTx := &Tx{
  37. IsL1: true,
  38. TxID: tx.TxID,
  39. Type: tx.Type,
  40. Position: tx.Position,
  41. FromIdx: tx.FromIdx,
  42. ToIdx: tx.ToIdx,
  43. Amount: tx.Amount,
  44. AmountFloat: amountFloat,
  45. TokenID: tx.TokenID,
  46. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  47. UserOrigin: tx.UserOrigin,
  48. FromEthAddr: tx.FromEthAddr,
  49. FromBJJ: tx.FromBJJ,
  50. LoadAmount: tx.LoadAmount,
  51. EthBlockNum: tx.EthBlockNum,
  52. }
  53. if tx.LoadAmount != nil {
  54. lf := new(big.Float).SetInt(tx.LoadAmount)
  55. loadAmountFloat, _ := lf.Float64()
  56. genericTx.LoadAmountFloat = loadAmountFloat
  57. }
  58. return genericTx
  59. }
  60. // Bytes encodes a L1Tx into []byte
  61. func (tx *L1Tx) Bytes(nLevels int) []byte {
  62. res := big.NewInt(0)
  63. res = res.Add(res, big.NewInt(0).Or(big.NewInt(0), tx.ToIdx.BigInt()))
  64. res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), big.NewInt(int64(tx.TokenID))), uint(nLevels)))
  65. res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.Amount), uint(nLevels+tokenIDB)))
  66. res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.LoadAmount), uint(nLevels+tokenIDB+f16B)))
  67. res = res.Add(res, big.NewInt(0).Lsh(big.NewInt(0).Or(big.NewInt(0), tx.FromIdx.BigInt()), uint(nLevels+tokenIDB+2*f16B)))
  68. fromBJJ := big.NewInt(0)
  69. fromBJJ.SetString(tx.FromBJJ.String(), 16)
  70. fromBJJCompressed := big.NewInt(0).Or(big.NewInt(0), fromBJJ)
  71. res = res.Add(res, big.NewInt(0).Lsh(fromBJJCompressed, uint(2*nLevels+tokenIDB+2*f16B)))
  72. fromEthAddr := big.NewInt(0).Or(big.NewInt(0), tx.FromEthAddr.Hash().Big())
  73. res = res.Add(res, big.NewInt(0).Lsh(fromEthAddr, uint(fromBJJCompressedB+2*nLevels+tokenIDB+2*f16B)))
  74. return res.Bytes()
  75. }
  76. // L1TxFromBytes decodes a L1Tx from []byte
  77. func L1TxFromBytes(l1TxEncoded []byte) (*L1Tx, error) {
  78. l1Tx := &L1Tx{}
  79. var idxB uint = cidXB
  80. l1TxEncodedBI := big.NewInt(0)
  81. l1TxEncodedBI.SetBytes(l1TxEncoded)
  82. toIdx, err := IdxFromBigInt(extract(l1TxEncodedBI, 0, idxB))
  83. if err != nil {
  84. return nil, err
  85. }
  86. l1Tx.ToIdx = toIdx
  87. l1Tx.TokenID = TokenID(extract(l1TxEncodedBI, idxB, tokenIDB).Uint64())
  88. l1Tx.Amount = extract(l1TxEncodedBI, idxB+tokenIDB, f16B)
  89. l1Tx.LoadAmount = extract(l1TxEncodedBI, idxB+tokenIDB+f16B, f16B)
  90. fromIdx, err := IdxFromBigInt(extract(l1TxEncodedBI, idxB+tokenIDB+2*f16B, f16B))
  91. if err != nil {
  92. return nil, err
  93. }
  94. l1Tx.FromIdx = fromIdx
  95. var pkComp babyjub.PublicKeyComp
  96. copy(pkComp[:], extract(l1TxEncodedBI, 2*idxB+tokenIDB+2*f16B, fromBJJCompressedB).Bytes())
  97. pk, err := pkComp.Decompress()
  98. if err != nil {
  99. return nil, err
  100. }
  101. l1Tx.FromBJJ = pk
  102. l1Tx.FromEthAddr = ethCommon.BigToAddress(extract(l1TxEncodedBI, fromBJJCompressedB+2*idxB+tokenIDB+2*f16B, fromEthAddrB))
  103. return l1Tx, nil
  104. }
  105. // extract masks and shifts a bigInt
  106. func extract(num *big.Int, origin uint, len uint) *big.Int {
  107. mask := big.NewInt(0).Sub(big.NewInt(0).Lsh(big.NewInt(1), len), big.NewInt(1))
  108. return big.NewInt(0).And(big.NewInt(0).Rsh(num, origin), mask)
  109. }