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.

340 lines
10 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
  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. // L1UserTxBytesLen is the length of the byte array that represents the L1Tx
  12. L1UserTxBytesLen = 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 `meddler:"id"`
  26. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  27. Position int `meddler:"position"`
  28. UserOrigin bool `meddler:"user_origin"` // 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 `meddler:"from_idx,zeroisnull"` // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  30. FromEthAddr ethCommon.Address `meddler:"from_eth_addr,zeroisnull"`
  31. FromBJJ *babyjub.PublicKey `meddler:"from_bjj,zeroisnull"`
  32. ToIdx Idx `meddler:"to_idx"` // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
  33. TokenID TokenID `meddler:"token_id"`
  34. Amount *big.Int `meddler:"amount,bigint"`
  35. LoadAmount *big.Int `meddler:"load_amount,bigint"`
  36. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  37. Type TxType `meddler:"type"`
  38. BatchNum *BatchNum `meddler:"batch_num"`
  39. }
  40. // NewL1Tx returns the given L1Tx with the TxId & Type parameters calculated
  41. // from the L1Tx values
  42. func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) {
  43. // calculate TxType
  44. var txType TxType
  45. if l1Tx.FromIdx == 0 {
  46. if l1Tx.ToIdx == Idx(0) {
  47. txType = TxTypeCreateAccountDeposit
  48. } else if l1Tx.ToIdx >= IdxUserThreshold {
  49. txType = TxTypeCreateAccountDepositTransfer
  50. } else {
  51. return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx)
  52. }
  53. } else if l1Tx.FromIdx >= IdxUserThreshold {
  54. if l1Tx.ToIdx == Idx(0) {
  55. txType = TxTypeDeposit
  56. } else if l1Tx.ToIdx == Idx(1) {
  57. txType = TxTypeForceExit
  58. } else if l1Tx.ToIdx >= IdxUserThreshold {
  59. if l1Tx.LoadAmount.Int64() == int64(0) {
  60. txType = TxTypeForceTransfer
  61. } else {
  62. txType = TxTypeDepositTransfer
  63. }
  64. } else {
  65. return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx)
  66. }
  67. } else {
  68. return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid FromIdx value: %d", l1Tx.FromIdx)
  69. }
  70. if l1Tx.Type != "" && l1Tx.Type != txType {
  71. return l1Tx, fmt.Errorf("L1Tx.Type: %s, should be: %s", l1Tx.Type, txType)
  72. }
  73. l1Tx.Type = txType
  74. txID, err := l1Tx.CalcTxID()
  75. if err != nil {
  76. return nil, err
  77. }
  78. l1Tx.TxID = *txID
  79. return l1Tx, nil
  80. }
  81. // CalcTxID calculates the TxId of the L1Tx
  82. func (tx *L1Tx) CalcTxID() (*TxID, error) {
  83. var txID TxID
  84. if tx.UserOrigin {
  85. if tx.ToForgeL1TxsNum == nil {
  86. return nil, fmt.Errorf("L1Tx.UserOrigin == true && L1Tx.ToForgeL1TxsNum == nil")
  87. }
  88. txID[0] = TxIDPrefixL1UserTx
  89. var toForgeL1TxsNumBytes [8]byte
  90. binary.BigEndian.PutUint64(toForgeL1TxsNumBytes[:], uint64(*tx.ToForgeL1TxsNum))
  91. copy(txID[1:9], toForgeL1TxsNumBytes[:])
  92. } else {
  93. if tx.BatchNum == nil {
  94. return nil, fmt.Errorf("L1Tx.UserOrigin == false && L1Tx.BatchNum == nil")
  95. }
  96. txID[0] = TxIDPrefixL1CoordTx
  97. var batchNumBytes [8]byte
  98. binary.BigEndian.PutUint64(batchNumBytes[:], uint64(*tx.BatchNum))
  99. copy(txID[1:9], batchNumBytes[:])
  100. }
  101. var positionBytes [2]byte
  102. binary.BigEndian.PutUint16(positionBytes[:], uint16(tx.Position))
  103. copy(txID[9:11], positionBytes[:])
  104. return &txID, nil
  105. }
  106. // Tx returns a *Tx from the L1Tx
  107. func (tx L1Tx) Tx() Tx {
  108. f := new(big.Float).SetInt(tx.Amount)
  109. amountFloat, _ := f.Float64()
  110. userOrigin := new(bool)
  111. *userOrigin = tx.UserOrigin
  112. genericTx := Tx{
  113. IsL1: true,
  114. TxID: tx.TxID,
  115. Type: tx.Type,
  116. Position: tx.Position,
  117. FromIdx: tx.FromIdx,
  118. ToIdx: tx.ToIdx,
  119. Amount: tx.Amount,
  120. AmountFloat: amountFloat,
  121. TokenID: tx.TokenID,
  122. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  123. UserOrigin: userOrigin,
  124. FromEthAddr: tx.FromEthAddr,
  125. FromBJJ: tx.FromBJJ,
  126. LoadAmount: tx.LoadAmount,
  127. EthBlockNum: tx.EthBlockNum,
  128. }
  129. if tx.LoadAmount != nil {
  130. lf := new(big.Float).SetInt(tx.LoadAmount)
  131. loadAmountFloat, _ := lf.Float64()
  132. genericTx.LoadAmountFloat = &loadAmountFloat
  133. }
  134. return genericTx
  135. }
  136. // TxCompressedData spec:
  137. // [ 1 bits ] empty (toBJJSign) // 1 byte
  138. // [ 8 bits ] empty (userFee) // 1 byte
  139. // [ 40 bits ] empty (nonce) // 5 bytes
  140. // [ 32 bits ] tokenID // 4 bytes
  141. // [ 16 bits ] amountFloat16 // 2 bytes
  142. // [ 48 bits ] toIdx // 6 bytes
  143. // [ 48 bits ] fromIdx // 6 bytes
  144. // [ 16 bits ] chainId // 2 bytes
  145. // [ 32 bits ] empty (signatureConstant) // 4 bytes
  146. // Total bits compressed data: 241 bits // 31 bytes in *big.Int representation
  147. func (tx L1Tx) TxCompressedData() (*big.Int, error) {
  148. amountFloat16, err := NewFloat16(tx.Amount)
  149. if err != nil {
  150. return nil, err
  151. }
  152. var b [31]byte
  153. // b[0:7] empty: no fee neither nonce
  154. copy(b[7:11], tx.TokenID.Bytes())
  155. copy(b[11:13], amountFloat16.Bytes())
  156. toIdxBytes, err := tx.ToIdx.Bytes()
  157. if err != nil {
  158. return nil, err
  159. }
  160. copy(b[13:19], toIdxBytes[:])
  161. fromIdxBytes, err := tx.FromIdx.Bytes()
  162. if err != nil {
  163. return nil, err
  164. }
  165. copy(b[19:25], fromIdxBytes[:])
  166. copy(b[25:27], []byte{0, 1}) // TODO this will be generated by the ChainID config parameter
  167. // b[27:] empty: no signature
  168. bi := new(big.Int).SetBytes(b[:])
  169. return bi, nil
  170. }
  171. // BytesGeneric returns the generic representation of a L1Tx. This method is
  172. // used to compute the []byte representation of a L1UserTx, and also to compute
  173. // the L1TxData for the ZKInputs (at the HashGlobalInputs), using this method
  174. // for L1CoordinatorTxs & L1UserTxs (for the ZKInputs case).
  175. func (tx *L1Tx) BytesGeneric() ([]byte, error) {
  176. var b [L1UserTxBytesLen]byte
  177. copy(b[0:20], tx.FromEthAddr.Bytes())
  178. if tx.FromBJJ != nil {
  179. pkCompL := tx.FromBJJ.Compress()
  180. pkCompB := SwapEndianness(pkCompL[:])
  181. copy(b[20:52], pkCompB[:])
  182. }
  183. fromIdxBytes, err := tx.FromIdx.Bytes()
  184. if err != nil {
  185. return nil, err
  186. }
  187. copy(b[52:58], fromIdxBytes[:])
  188. loadAmountFloat16, err := NewFloat16(tx.LoadAmount)
  189. if err != nil {
  190. return nil, err
  191. }
  192. copy(b[58:60], loadAmountFloat16.Bytes())
  193. amountFloat16, err := NewFloat16(tx.Amount)
  194. if err != nil {
  195. return nil, err
  196. }
  197. copy(b[60:62], amountFloat16.Bytes())
  198. copy(b[62:66], tx.TokenID.Bytes())
  199. toIdxBytes, err := tx.ToIdx.Bytes()
  200. if err != nil {
  201. return nil, err
  202. }
  203. copy(b[66:72], toIdxBytes[:])
  204. return b[:], nil
  205. }
  206. // BytesUser encodes a L1UserTx into []byte
  207. func (tx *L1Tx) BytesUser() ([]byte, error) {
  208. if !tx.UserOrigin {
  209. return nil, fmt.Errorf("Can not calculate BytesUser() for a L1CoordinatorTx")
  210. }
  211. return tx.BytesGeneric()
  212. }
  213. // BytesCoordinatorTx encodes a L1CoordinatorTx into []byte
  214. func (tx *L1Tx) BytesCoordinatorTx(compressedSignatureBytes []byte) ([]byte, error) {
  215. if tx.UserOrigin {
  216. return nil, fmt.Errorf("Can not calculate BytesCoordinatorTx() for a L1UserTx")
  217. }
  218. var b [L1CoordinatorTxBytesLen]byte
  219. v := compressedSignatureBytes[64]
  220. s := compressedSignatureBytes[32:64]
  221. r := compressedSignatureBytes[0:32]
  222. b[0] = v
  223. copy(b[1:33], s)
  224. copy(b[33:65], r)
  225. pkCompL := tx.FromBJJ.Compress()
  226. pkCompB := SwapEndianness(pkCompL[:])
  227. copy(b[65:97], pkCompB[:])
  228. copy(b[97:101], tx.TokenID.Bytes())
  229. return b[:], nil
  230. }
  231. // L1UserTxFromBytes decodes a L1Tx from []byte
  232. func L1UserTxFromBytes(b []byte) (*L1Tx, error) {
  233. if len(b) != L1UserTxBytesLen {
  234. return nil, fmt.Errorf("Can not parse L1Tx bytes, expected length %d, current: %d", 68, len(b))
  235. }
  236. tx := &L1Tx{
  237. UserOrigin: true,
  238. }
  239. var err error
  240. tx.FromEthAddr = ethCommon.BytesToAddress(b[0:20])
  241. pkCompB := b[20:52]
  242. pkCompL := SwapEndianness(pkCompB)
  243. var pkComp babyjub.PublicKeyComp
  244. copy(pkComp[:], pkCompL)
  245. tx.FromBJJ, err = pkComp.Decompress()
  246. if err != nil {
  247. return nil, err
  248. }
  249. fromIdx, err := IdxFromBytes(b[52:58])
  250. if err != nil {
  251. return nil, err
  252. }
  253. tx.FromIdx = fromIdx
  254. tx.LoadAmount = Float16FromBytes(b[58:60]).BigInt()
  255. tx.Amount = Float16FromBytes(b[60:62]).BigInt()
  256. tx.TokenID, err = TokenIDFromBytes(b[62:66])
  257. if err != nil {
  258. return nil, err
  259. }
  260. tx.ToIdx, err = IdxFromBytes(b[66:72])
  261. if err != nil {
  262. return nil, err
  263. }
  264. return tx, nil
  265. }
  266. // L1CoordinatorTxFromBytes decodes a L1Tx from []byte
  267. func L1CoordinatorTxFromBytes(b []byte) (*L1Tx, error) {
  268. if len(b) != L1CoordinatorTxBytesLen {
  269. return nil, fmt.Errorf("Can not parse L1CoordinatorTx bytes, expected length %d, current: %d", 101, len(b))
  270. }
  271. bytesMessage1 := []byte("\x19Ethereum Signed Message:\n98")
  272. bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
  273. tx := &L1Tx{
  274. UserOrigin: false,
  275. }
  276. var err error
  277. // Ethereum adds 27 to v
  278. v := b[0] - byte(27) //nolint:gomnd
  279. s := b[1:33]
  280. r := b[33:65]
  281. pkCompB := b[65:97]
  282. pkCompL := SwapEndianness(pkCompB)
  283. var pkComp babyjub.PublicKeyComp
  284. copy(pkComp[:], pkCompL)
  285. tx.FromBJJ, err = pkComp.Decompress()
  286. if err != nil {
  287. return nil, err
  288. }
  289. tx.TokenID, err = TokenIDFromBytes(b[97:101])
  290. if err != nil {
  291. return nil, err
  292. }
  293. var data []byte
  294. data = append(data, bytesMessage1...)
  295. data = append(data, bytesMessage2...)
  296. data = append(data, pkCompB...)
  297. var signature []byte
  298. signature = append(signature, r[:]...)
  299. signature = append(signature, s[:]...)
  300. signature = append(signature, v)
  301. hash := crypto.Keccak256(data)
  302. pubKeyBytes, err := crypto.Ecrecover(hash, signature)
  303. if err != nil {
  304. return nil, err
  305. }
  306. pubKey, err := crypto.UnmarshalPubkey(pubKeyBytes)
  307. if err != nil {
  308. return nil, err
  309. }
  310. tx.FromEthAddr = crypto.PubkeyToAddress(*pubKey)
  311. tx.Amount = big.NewInt(0)
  312. tx.LoadAmount = big.NewInt(0)
  313. return tx, nil
  314. }