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.

293 lines
8.2 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. pkCompL := tx.FromBJJ.Compress()
  149. pkCompB := SwapEndianness(pkCompL[:])
  150. copy(b[20:52], pkCompB[:])
  151. fromIdxBytes, err := tx.FromIdx.Bytes()
  152. if err != nil {
  153. return nil, err
  154. }
  155. copy(b[52:58], fromIdxBytes[:])
  156. loadAmountFloat16, err := NewFloat16(tx.LoadAmount)
  157. if err != nil {
  158. return nil, err
  159. }
  160. copy(b[58:60], loadAmountFloat16.Bytes())
  161. amountFloat16, err := NewFloat16(tx.Amount)
  162. if err != nil {
  163. return nil, err
  164. }
  165. copy(b[60:62], amountFloat16.Bytes())
  166. copy(b[62:66], tx.TokenID.Bytes())
  167. toIdxBytes, err := tx.ToIdx.Bytes()
  168. if err != nil {
  169. return nil, err
  170. }
  171. copy(b[66:72], toIdxBytes[:])
  172. return b[:], nil
  173. }
  174. // BytesCoordinatorTx encodes a L1CoordinatorTx into []byte
  175. func (tx *L1Tx) BytesCoordinatorTx(compressedSignatureBytes []byte) ([]byte, error) {
  176. var b [L1CoordinatorTxBytesLen]byte
  177. v := compressedSignatureBytes[64]
  178. s := compressedSignatureBytes[32:64]
  179. r := compressedSignatureBytes[0:32]
  180. b[0] = v
  181. copy(b[1:33], s)
  182. copy(b[33:65], r)
  183. pkCompL := tx.FromBJJ.Compress()
  184. pkCompB := SwapEndianness(pkCompL[:])
  185. copy(b[65:97], pkCompB[:])
  186. copy(b[97:101], tx.TokenID.Bytes())
  187. return b[:], nil
  188. }
  189. // L1TxFromBytes decodes a L1Tx from []byte
  190. func L1TxFromBytes(b []byte) (*L1Tx, error) {
  191. if len(b) != L1TxBytesLen {
  192. return nil, fmt.Errorf("Can not parse L1Tx bytes, expected length %d, current: %d", 68, len(b))
  193. }
  194. tx := &L1Tx{}
  195. var err error
  196. tx.FromEthAddr = ethCommon.BytesToAddress(b[0:20])
  197. pkCompB := b[20:52]
  198. pkCompL := SwapEndianness(pkCompB)
  199. var pkComp babyjub.PublicKeyComp
  200. copy(pkComp[:], pkCompL)
  201. tx.FromBJJ, err = pkComp.Decompress()
  202. if err != nil {
  203. return nil, err
  204. }
  205. fromIdx, err := IdxFromBytes(b[52:58])
  206. if err != nil {
  207. return nil, err
  208. }
  209. if fromIdx != 0 {
  210. tx.FromIdx = new(Idx)
  211. *tx.FromIdx = fromIdx
  212. }
  213. tx.LoadAmount = Float16FromBytes(b[58:60]).BigInt()
  214. tx.Amount = Float16FromBytes(b[60:62]).BigInt()
  215. tx.TokenID, err = TokenIDFromBytes(b[62:66])
  216. if err != nil {
  217. return nil, err
  218. }
  219. tx.ToIdx, err = IdxFromBytes(b[66:72])
  220. if err != nil {
  221. return nil, err
  222. }
  223. return tx, nil
  224. }
  225. // L1TxFromCoordinatorBytes decodes a L1Tx from []byte
  226. func L1TxFromCoordinatorBytes(b []byte) (*L1Tx, error) {
  227. if len(b) != L1CoordinatorTxBytesLen {
  228. return nil, fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b))
  229. }
  230. bytesMessage1 := []byte("\x19Ethereum Signed Message:\n98")
  231. bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
  232. tx := &L1Tx{}
  233. var err error
  234. // Ethereum adds 27 to v
  235. v := b[0] - byte(27) //nolint:gomnd
  236. s := b[1:33]
  237. r := b[33:65]
  238. pkCompB := b[65:97]
  239. pkCompL := SwapEndianness(pkCompB)
  240. var pkComp babyjub.PublicKeyComp
  241. copy(pkComp[:], pkCompL)
  242. tx.FromBJJ, err = pkComp.Decompress()
  243. if err != nil {
  244. return nil, err
  245. }
  246. tx.TokenID, err = TokenIDFromBytes(b[97:101])
  247. if err != nil {
  248. return nil, err
  249. }
  250. var data []byte
  251. data = append(data, bytesMessage1...)
  252. data = append(data, bytesMessage2...)
  253. data = append(data, pkCompB...)
  254. var signature []byte
  255. signature = append(signature, r[:]...)
  256. signature = append(signature, s[:]...)
  257. signature = append(signature, v)
  258. hash := crypto.Keccak256(data)
  259. pubKeyBytes, err := crypto.Ecrecover(hash, signature)
  260. if err != nil {
  261. return nil, err
  262. }
  263. pubKey, err := crypto.UnmarshalPubkey(pubKeyBytes)
  264. if err != nil {
  265. return nil, err
  266. }
  267. tx.FromEthAddr = crypto.PubkeyToAddress(*pubKey)
  268. return tx, nil
  269. }