hermez-node/api/dbtoapistructs.go
api/dbtoapistructs.goRaw
1package api
2
3import (
4 "encoding/base64"
5 "encoding/hex"
6 "errors"
7 "math/big"
8 "strconv"
9 "strings"
10 "time"
11
12 ethCommon "github.com/ethereum/go-ethereum/common"
13 "github.com/hermeznetwork/hermez-node/common"
14 "github.com/hermeznetwork/hermez-node/db"
15 "github.com/hermeznetwork/hermez-node/db/historydb"
16 "github.com/hermeznetwork/hermez-node/eth"
17 "github.com/iden3/go-iden3-crypto/babyjub"
18)
19
20const exitIdx = "hez:EXIT:1"
21
22type errorMsg struct {
23 Message string
24}
25
26func bjjToString(bjj *babyjub.PublicKey) string {
27 pkComp := [32]byte(bjj.Compress())
28 sum := pkComp[0]
29 for i := 1; i < len(pkComp); i++ {
30 sum += pkComp[i]
31 }
32 bjjSum := append(pkComp[:], sum)
33 return "hez:" + base64.RawURLEncoding.EncodeToString(bjjSum)
34}
35
36func ethAddrToHez(addr ethCommon.Address) string {
37 return "hez:" + addr.String()
38}
39
40func idxToHez(idx common.Idx, tokenSymbol string) string {
41 if idx == 1 {
42 return exitIdx
43 }
44 return "hez:" + tokenSymbol + ":" + strconv.Itoa(int(idx))
45}
46
47// Exit
48
49type exitsAPI struct {
50 Exits []exitAPI `json:"exits"`
51 Pagination *db.Pagination `json:"pagination"`
52}
53
54func (e *exitsAPI) GetPagination() *db.Pagination {
55 if e.Exits[0].ItemID < e.Exits[len(e.Exits)-1].ItemID {
56 e.Pagination.FirstReturnedItem = e.Exits[0].ItemID
57 e.Pagination.LastReturnedItem = e.Exits[len(e.Exits)-1].ItemID
58 } else {
59 e.Pagination.LastReturnedItem = e.Exits[0].ItemID
60 e.Pagination.FirstReturnedItem = e.Exits[len(e.Exits)-1].ItemID
61 }
62 return e.Pagination
63}
64func (e *exitsAPI) Len() int { return len(e.Exits) }
65
66type merkleProofAPI struct {
67 Root string
68 Siblings []string
69 OldKey string
70 OldValue string
71 IsOld0 bool
72 Key string
73 Value string
74 Fnc int
75}
76
77type exitAPI struct {
78 ItemID int `json:"itemId"`
79 BatchNum common.BatchNum `json:"batchNum"`
80 AccountIdx string `json:"accountIndex"`
81 MerkleProof merkleProofAPI `json:"merkleProof"`
82 Balance string `json:"balance"`
83 InstantWithdrawn *int64 `json:"instantWithdrawn"`
84 DelayedWithdrawRequest *int64 `json:"delayedWithdrawRequest"`
85 DelayedWithdrawn *int64 `json:"delayedWithdrawn"`
86 Token historydb.TokenWithUSD `json:"token"`
87}
88
89func historyExitsToAPI(dbExits []historydb.HistoryExit) []exitAPI {
90 apiExits := []exitAPI{}
91 for i := 0; i < len(dbExits); i++ {
92 exit := exitAPI{
93 ItemID: dbExits[i].ItemID,
94 BatchNum: dbExits[i].BatchNum,
95 AccountIdx: idxToHez(dbExits[i].AccountIdx, dbExits[i].TokenSymbol),
96 MerkleProof: merkleProofAPI{
97 Root: dbExits[i].MerkleProof.Root.String(),
98 OldKey: dbExits[i].MerkleProof.OldKey.String(),
99 OldValue: dbExits[i].MerkleProof.OldValue.String(),
100 IsOld0: dbExits[i].MerkleProof.IsOld0,
101 Key: dbExits[i].MerkleProof.Key.String(),
102 Value: dbExits[i].MerkleProof.Value.String(),
103 Fnc: dbExits[i].MerkleProof.Fnc,
104 },
105 Balance: dbExits[i].Balance.String(),
106 InstantWithdrawn: dbExits[i].InstantWithdrawn,
107 DelayedWithdrawRequest: dbExits[i].DelayedWithdrawRequest,
108 DelayedWithdrawn: dbExits[i].DelayedWithdrawn,
109 Token: historydb.TokenWithUSD{
110 TokenID: dbExits[i].TokenID,
111 EthBlockNum: dbExits[i].TokenEthBlockNum,
112 EthAddr: dbExits[i].TokenEthAddr,
113 Name: dbExits[i].TokenName,
114 Symbol: dbExits[i].TokenSymbol,
115 Decimals: dbExits[i].TokenDecimals,
116 USD: dbExits[i].TokenUSD,
117 USDUpdate: dbExits[i].TokenUSDUpdate,
118 },
119 }
120 siblings := []string{}
121 for j := 0; j < len(dbExits[i].MerkleProof.Siblings); j++ {
122 siblings = append(siblings, dbExits[i].MerkleProof.Siblings[j].String())
123 }
124 exit.MerkleProof.Siblings = siblings
125 apiExits = append(apiExits, exit)
126 }
127 return apiExits
128}
129
130// Config
131
132type rollupConstants struct {
133 PublicConstants eth.RollupPublicConstants `json:"publicConstants"`
134 MaxFeeIdxCoordinator int `json:"maxFeeIdxCoordinator"`
135 ReservedIdx int `json:"reservedIdx"`
136 ExitIdx int `json:"exitIdx"`
137 LimitLoadAmount *big.Int `json:"limitLoadAmount"`
138 LimitL2TransferAmount *big.Int `json:"limitL2TransferAmount"`
139 LimitTokens int `json:"limitTokens"`
140 L1CoordinatorTotalBytes int `json:"l1CoordinatorTotalBytes"`
141 L1UserTotalBytes int `json:"l1UserTotalBytes"`
142 MaxL1UserTx int `json:"maxL1UserTx"`
143 MaxL1Tx int `json:"maxL1Tx"`
144 InputSHAConstantBytes int `json:"inputSHAConstantBytes"`
145 NumBuckets int `json:"numBuckets"`
146 MaxWithdrawalDelay int `json:"maxWithdrawalDelay"`
147 ExchangeMultiplier int `json:"exchangeMultiplier"`
148}
149
150type configAPI struct {
151 RollupConstants rollupConstants `json:"hermez"`
152 AuctionConstants eth.AuctionConstants `json:"auction"`
153 WDelayerConstants eth.WDelayerConstants `json:"withdrawalDelayer"`
154}
155
156// AccountCreationAuth
157
158type accountCreationAuthAPI struct {
159 EthAddr string `json:"hezEthereumAddress" binding:"required"`
160 BJJ string `json:"bjj" binding:"required"`
161 Signature string `json:"signature" binding:"required"`
162 Timestamp time.Time `json:"timestamp"`
163}
164
165func accountCreationAuthToAPI(dbAuth *common.AccountCreationAuth) *accountCreationAuthAPI {
166 return &accountCreationAuthAPI{
167 EthAddr: ethAddrToHez(dbAuth.EthAddr),
168 BJJ: bjjToString(dbAuth.BJJ),
169 Signature: "0x" + hex.EncodeToString(dbAuth.Signature),
170 Timestamp: dbAuth.Timestamp,
171 }
172}
173
174func accountCreationAuthAPIToCommon(apiAuth *accountCreationAuthAPI) (*common.AccountCreationAuth, error) {
175 ethAddr, err := hezStringToEthAddr(apiAuth.EthAddr, "hezEthereumAddress")
176 if err != nil {
177 return nil, err
178 }
179 bjj, err := hezStringToBJJ(apiAuth.BJJ, "bjj")
180 if err != nil {
181 return nil, err
182 }
183 without0x := strings.TrimPrefix(apiAuth.Signature, "0x")
184 s, err := hex.DecodeString(without0x)
185 if err != nil {
186 return nil, err
187 }
188 auth := &common.AccountCreationAuth{
189 EthAddr: *ethAddr,
190 BJJ: bjj,
191 Signature: s,
192 Timestamp: apiAuth.Timestamp,
193 }
194 if !auth.VerifySignature() {
195 return nil, errors.New("invalid signature")
196 }
197 return auth, nil
198}