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.

287 lines
8.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package common
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math/big"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/crypto"
  8. "github.com/iden3/go-iden3-crypto/babyjub"
  9. )
  10. const (
  11. // L1TxBytesLen is the length of the byte array that represents the L1Tx
  12. L1TxBytesLen = 72
  13. // L1CoordinatorTxBytesLen is the length of the byte array that represents the L1CoordinatorTx
  14. L1CoordinatorTxBytesLen = 101
  15. )
  16. // L1Tx is a struct that represents a L1 tx
  17. type L1Tx struct {
  18. // Stored in DB: mandatory fileds
  19. // TxID (12 bytes) for L1Tx is:
  20. // bytes: | 1 | 8 | 2 | 1 |
  21. // values: | type | ToForgeL1TxsNum | Position | 0 (padding) |
  22. // where type:
  23. // - L1UserTx: 0
  24. // - L1CoordinatorTx: 1
  25. TxID TxID
  26. ToForgeL1TxsNum *int64 // toForgeL1TxsNum in which the tx was forged / will be forged
  27. Position int
  28. 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
  29. FromIdx *Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  30. FromEthAddr ethCommon.Address
  31. FromBJJ *babyjub.PublicKey
  32. ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
  33. TokenID TokenID
  34. Amount *big.Int
  35. LoadAmount *big.Int
  36. EthBlockNum int64 // Ethereum Block Number in which this L1Tx was added to the queue
  37. Type TxType
  38. BatchNum *BatchNum
  39. USD *float64
  40. LoadAmountUSD *float64
  41. }
  42. // NewL1Tx returns the given L1Tx with the TxId & Type parameters calculated
  43. // from the L1Tx values
  44. func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) {
  45. // calculate TxType
  46. var txType TxType
  47. if l1Tx.FromIdx == nil {
  48. if l1Tx.ToIdx == Idx(0) {
  49. txType = TxTypeCreateAccountDeposit
  50. } else if l1Tx.ToIdx >= IdxUserThreshold {
  51. txType = TxTypeCreateAccountDepositTransfer
  52. } else {
  53. return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx)
  54. }
  55. } else if *l1Tx.FromIdx >= IdxUserThreshold {
  56. if l1Tx.ToIdx == Idx(0) {
  57. txType = TxTypeDeposit
  58. } else if l1Tx.ToIdx == Idx(1) {
  59. txType = TxTypeExit
  60. } else if l1Tx.ToIdx >= IdxUserThreshold {
  61. if l1Tx.LoadAmount.Int64() == int64(0) {
  62. txType = TxTypeForceTransfer
  63. } else {
  64. txType = TxTypeDepositTransfer
  65. }
  66. } else {
  67. return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx)
  68. }
  69. } else {
  70. return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid FromIdx value: %d", l1Tx.FromIdx)
  71. }
  72. if l1Tx.Type != "" && l1Tx.Type != txType {
  73. return l1Tx, fmt.Errorf("L1Tx.Type: %s, should be: %s", l1Tx.Type, txType)
  74. }
  75. l1Tx.Type = txType
  76. txID, err := l1Tx.CalcTxID()
  77. if err != nil {
  78. return nil, err
  79. }
  80. l1Tx.TxID = *txID
  81. return l1Tx, nil
  82. }
  83. // CalcTxID calculates the TxId of the L1Tx
  84. func (tx *L1Tx) CalcTxID() (*TxID, error) {
  85. var txID TxID
  86. if tx.UserOrigin {
  87. if tx.ToForgeL1TxsNum == nil {
  88. return nil, fmt.Errorf("L1Tx.UserOrigin == true && L1Tx.ToForgeL1TxsNum == nil")
  89. }
  90. txID[0] = TxIDPrefixL1UserTx
  91. var toForgeL1TxsNumBytes [8]byte
  92. binary.BigEndian.PutUint64(toForgeL1TxsNumBytes[:], uint64(*tx.ToForgeL1TxsNum))
  93. copy(txID[1:9], toForgeL1TxsNumBytes[:])
  94. } else {
  95. if tx.BatchNum == nil {
  96. return nil, fmt.Errorf("L1Tx.UserOrigin == false && L1Tx.BatchNum == nil")
  97. }
  98. txID[0] = TxIDPrefixL1CoordTx
  99. var batchNumBytes [8]byte
  100. binary.BigEndian.PutUint64(batchNumBytes[:], uint64(*tx.BatchNum))
  101. copy(txID[1:9], batchNumBytes[:])
  102. }
  103. var positionBytes [2]byte
  104. binary.BigEndian.PutUint16(positionBytes[:], uint16(tx.Position))
  105. copy(txID[9:11], positionBytes[:])
  106. return &txID, nil
  107. }
  108. // Tx returns a *Tx from the L1Tx
  109. func (tx *L1Tx) Tx() *Tx {
  110. f := new(big.Float).SetInt(tx.Amount)
  111. amountFloat, _ := f.Float64()
  112. userOrigin := new(bool)
  113. *userOrigin = tx.UserOrigin
  114. fromEthAddr := new(ethCommon.Address)
  115. *fromEthAddr = tx.FromEthAddr
  116. toIdx := new(Idx)
  117. *toIdx = tx.ToIdx
  118. genericTx := &Tx{
  119. IsL1: true,
  120. TxID: tx.TxID,
  121. Type: tx.Type,
  122. Position: tx.Position,
  123. FromIdx: tx.FromIdx,
  124. ToIdx: toIdx,
  125. Amount: tx.Amount,
  126. AmountFloat: amountFloat,
  127. TokenID: tx.TokenID,
  128. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  129. UserOrigin: userOrigin,
  130. FromEthAddr: fromEthAddr,
  131. FromBJJ: tx.FromBJJ,
  132. LoadAmount: tx.LoadAmount,
  133. EthBlockNum: tx.EthBlockNum,
  134. USD: tx.USD,
  135. LoadAmountUSD: tx.LoadAmountUSD,
  136. }
  137. if tx.LoadAmount != nil {
  138. lf := new(big.Float).SetInt(tx.LoadAmount)
  139. loadAmountFloat, _ := lf.Float64()
  140. genericTx.LoadAmountFloat = &loadAmountFloat
  141. }
  142. return genericTx
  143. }
  144. // Bytes encodes a L1Tx into []byte
  145. func (tx *L1Tx) Bytes() ([]byte, error) {
  146. var b [L1TxBytesLen]byte
  147. copy(b[0:20], tx.FromEthAddr.Bytes())
  148. pkComp := tx.FromBJJ.Compress()
  149. copy(b[20:52], pkComp[:])
  150. fromIdxBytes, err := tx.FromIdx.Bytes()
  151. if err != nil {
  152. return nil, err
  153. }
  154. copy(b[52:58], fromIdxBytes[:])
  155. loadAmountFloat16, err := NewFloat16(tx.LoadAmount)
  156. if err != nil {
  157. return nil, err
  158. }
  159. copy(b[58:60], loadAmountFloat16.Bytes())
  160. amountFloat16, err := NewFloat16(tx.Amount)
  161. if err != nil {
  162. return nil, err
  163. }
  164. copy(b[60:62], amountFloat16.Bytes())
  165. copy(b[62:66], tx.TokenID.Bytes())
  166. toIdxBytes, err := tx.ToIdx.Bytes()
  167. if err != nil {
  168. return nil, err
  169. }
  170. copy(b[66:72], toIdxBytes[:])
  171. return b[:], nil
  172. }
  173. // BytesCoordinatorTx encodes a L1CoordinatorTx into []byte
  174. func (tx *L1Tx) BytesCoordinatorTx(compressedSignatureBytes []byte) ([]byte, error) {
  175. var b [L1CoordinatorTxBytesLen]byte
  176. v := compressedSignatureBytes[64]
  177. s := compressedSignatureBytes[32:64]
  178. r := compressedSignatureBytes[0:32]
  179. b[0] = v
  180. copy(b[1:33], s)
  181. copy(b[33:65], r)
  182. pkComp := tx.FromBJJ.Compress()
  183. copy(b[65:97], pkComp[:])
  184. copy(b[97:101], tx.TokenID.Bytes())
  185. return b[:], nil
  186. }
  187. // L1TxFromBytes decodes a L1Tx from []byte
  188. func L1TxFromBytes(b []byte) (*L1Tx, error) {
  189. if len(b) != L1TxBytesLen {
  190. return nil, fmt.Errorf("Can not parse L1Tx bytes, expected length %d, current: %d", 68, len(b))
  191. }
  192. tx := &L1Tx{}
  193. var err error
  194. tx.FromEthAddr = ethCommon.BytesToAddress(b[0:20])
  195. pkCompB := b[20:52]
  196. var pkComp babyjub.PublicKeyComp
  197. copy(pkComp[:], pkCompB)
  198. tx.FromBJJ, err = pkComp.Decompress()
  199. if err != nil {
  200. return nil, err
  201. }
  202. fromIdx, err := IdxFromBytes(b[52:58])
  203. if err != nil {
  204. return nil, err
  205. }
  206. if fromIdx != 0 {
  207. tx.FromIdx = new(Idx)
  208. *tx.FromIdx = fromIdx
  209. }
  210. tx.LoadAmount = Float16FromBytes(b[58:60]).BigInt()
  211. tx.Amount = Float16FromBytes(b[60:62]).BigInt()
  212. tx.TokenID, err = TokenIDFromBytes(b[62:66])
  213. if err != nil {
  214. return nil, err
  215. }
  216. tx.ToIdx, err = IdxFromBytes(b[66:72])
  217. if err != nil {
  218. return nil, err
  219. }
  220. return tx, nil
  221. }
  222. // L1TxFromCoordinatorBytes decodes a L1Tx from []byte
  223. func L1TxFromCoordinatorBytes(b []byte) (*L1Tx, error) {
  224. if len(b) != L1CoordinatorTxBytesLen {
  225. return nil, fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b))
  226. }
  227. bytesMessage1 := []byte("\x19Ethereum Signed Message:\n98")
  228. bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
  229. tx := &L1Tx{}
  230. var err error
  231. // Ethereum adds 27 to v
  232. v := b[0] - byte(27) //nolint:gomnd
  233. s := b[1:33]
  234. r := b[33:65]
  235. pkCompB := b[65:97]
  236. var pkComp babyjub.PublicKeyComp
  237. copy(pkComp[:], pkCompB)
  238. tx.FromBJJ, err = pkComp.Decompress()
  239. if err != nil {
  240. return nil, err
  241. }
  242. tx.TokenID, err = TokenIDFromBytes(b[97:101])
  243. if err != nil {
  244. return nil, err
  245. }
  246. var data []byte
  247. data = append(data, bytesMessage1...)
  248. data = append(data, bytesMessage2...)
  249. data = append(data, pkCompB...)
  250. var signature []byte
  251. signature = append(signature, r[:]...)
  252. signature = append(signature, s[:]...)
  253. signature = append(signature, v)
  254. hash := crypto.Keccak256(data)
  255. pubKeyBytes, err := crypto.Ecrecover(hash, signature)
  256. if err != nil {
  257. return nil, err
  258. }
  259. pubKey, err := crypto.UnmarshalPubkey(pubKeyBytes)
  260. if err != nil {
  261. return nil, err
  262. }
  263. tx.FromEthAddr = crypto.PubkeyToAddress(*pubKey)
  264. return tx, nil
  265. }