-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapi.js
219 lines (172 loc) · 7.64 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const axios = require('axios');
const { BlockWithTransactions, TransactionResponse, TransactionReceipt } = require('@ethersproject/abstract-provider');
module.exports = class Api {
apiRoot;
firebaseUserId;
currentUser;
currentWorkspace;
auth;
apiToken;
constructor(apiRoot) {
this.apiRoot = apiRoot;
this.currentWorkspace = {};
this.currentUser = {};
}
get isLoggedIn() {
return !!this.currentUser.email;
}
get hasWorkspace() {
return !!this.currentWorkspace;
}
get currentWorkspace() {
return this.currentWorkspace;
}
get isUsingApiToken() {
return !!this.apiToken;
}
get currentUser() {
return { email: this.currentUser.email };
}
async setApiToken(apiToken) {
try {
this.apiToken = apiToken;
axios.defaults.headers.common['authorization'] = `Bearer ${this.apiToken}`;
return await this.fetchUser();
} catch(error) {
throw error;
}
}
async fetchUser() {
if (!this.isUsingApiToken)
throw new Error('You need to authenticate first.');
this.currentUser = (await axios.get(`${this.apiRoot}/api/users/me`)).data;
if (!this.currentUser.workspaces.length)
throw new Error(`You need to create a new workspace on ${this.webappRoot} before using the plugin.`);
if (this.currentUser.currentWorkspace)
this.currentWorkspace = this.currentUser.currentWorkspace;
else {
await this.setWorkspace(this.currentUser.workspaces[0].name);
await axios.post(`${this.apiRoot}/api/users/me/setCurrentWorkspace`, { data: { workspace: this.currentUser.workspaces[0].name }});
}
return this.currentWorkspace;
}
async login(email, password) {
try {
if (this.apiToken)
throw new Error('Authenticating with API token.');
const { data: { user } } = await axios.post(`${this.apiRoot}/api/users/signin`, { email, password });
if (user)
await this.setApiToken(user.apiToken);
else
throw new Error(`Couldn't login with the specified email/password.`);
} catch(error) {
console.log(error);
throw new Error(`Couldn't login with the specified email/password.`);
}
}
async setWorkspace(workspace) {
if (workspace && this.currentUser) {
let foundWorkspace = false;
for (let i = 0; i < this.currentUser.workspaces.length; i++) {
const loopedWorkspace = this.currentUser.workspaces[i];
if (loopedWorkspace.name == workspace) {
this.currentWorkspace = loopedWorkspace;
foundWorkspace = true;
break;
}
}
if (!foundWorkspace)
throw new Error(`Couldn't find workspace ${workspace}. Make sure you're logged in with the correct account.`);
if (!this.isUsingApiToken)
throw new Error('[setWorkspace] You need to be authenticated to set a workspace.');
}
return this.currentWorkspace;
}
async resetWorkspace(workspaceName) {
if (!workspaceName)
throw new Error('[resetWorkspace] Missing workspace name.');
if (!this.isUsingApiToken)
throw new Error('[resetWorkspace] You need to be authenticated to reset a workspace.');
return await axios.post(`${this.apiRoot}/api/workspaces/reset`, { data: { workspace: workspaceName }});
}
async syncBlockRange(from, to) {
if (from === undefined || from === null || to === undefined || to === null)
throw new Error('[syncBlockRange] Missing block range.');
if (!this.isUsingApiToken)
throw new Error('[syncBlockRange] You need to be authenticated to synchronize a block range.');
if (!this.currentWorkspace)
throw new Error('[syncBlockRange] A workspace needs to be set to synchronize a block range.');
return await axios.post(`${this.apiRoot}/api/blocks/syncRange`, { data: { workspace: this.currentWorkspace.name, from: from, to: to }});
}
async syncBlock(block, serverSync = false) {
if (!block)
throw new Error('[syncBlock] Missing block');
if (!this.isUsingApiToken)
throw new Error('[syncBlock] You need to be authenticated to synchronize transactions.');
if (!this.currentWorkspace)
throw new Error('[syncBlock] A workspace needs to be set to synchronize blocks.')
return await axios.post(`${this.apiRoot}/api/blocks?serverSync=${serverSync}`, { data: { block: block, workspace: this.currentWorkspace.name }});
}
async syncTransaction(block, transaction, transactionReceipt) {
if (!block || !transaction || !transactionReceipt)
throw new Error('[syncTransaction] Missing parameter?');
if (!this.isUsingApiToken)
throw new Error('[syncTransaction] You need to be authenticated to synchronize transactions.');
if (!this.currentWorkspace)
throw new Error('[syncTransaction] The workspace needs to be set to synchronize transactions.');
return await axios.post(`${this.apiRoot}/api/transactions`, {
data: {
block: block,
transaction: transaction,
transactionReceipt: transactionReceipt,
workspace: this.currentWorkspace.name
}
});
}
async syncContractData(name, address, abi, hashedBytecode) {
if (!name || !address)
throw new Error('[syncContractData] Missing parameter.');
if (!this.isUsingApiToken)
throw new Error('[syncContractData] You need to be authenticated to synchronize contract data.');
if (!this.currentWorkspace)
throw new Error('[syncContractData] The workspace needs to be set to synchronize contract data.');
return await axios.post(`${this.apiRoot}/api/contracts/${address}`, {
data: {
name: name,
address: address,
abi: abi,
hashedBytecode: hashedBytecode,
workspace: this.currentWorkspace.name
}
});
}
async syncContractAst(address, ast) {
if (!address || !ast)
throw new Error('[syncContractAst] Missing parameter.');
if (!this.isUsingApiToken)
throw new Error('[syncContractAst] You need to be authenticated to synchronize contract data.');
if (!this.currentWorkspace)
throw new Error('[syncContractAst] The workspace needs to be set to synchronize contract data.');
return await axios.post(`${this.apiRoot}/api/contracts/${address}`, {
data: {
ast: ast,
workspace: this.currentWorkspace.name
}
});
}
async syncTrace(transactionHash, trace) {
if (!transactionHash || !trace)
throw new Error('[syncTrace] Missing parameter.');
if (!this.isUsingApiToken)
throw new Error('[syncTrace] You need to be authenticated to synchronize transactions trace.');
if (!this.currentWorkspace)
throw new Error('[syncTrace] The workspace needs to be set to synchronize tranactions trace.');
return await axios.post(`${this.apiRoot}/api/transactions/${transactionHash}/trace`, {
data: {
txHash: transactionHash,
steps: trace,
workspace: this.currentWorkspace.name
}
});
}
}