-
Notifications
You must be signed in to change notification settings - Fork 96
/
remoteFetcher.js
258 lines (243 loc) · 8.27 KB
/
remoteFetcher.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// @flow
import type {
AddressUtxoRequest, AddressUtxoResponse,
TxBodiesRequest, TxBodiesResponse,
UtxoSumRequest, UtxoSumResponse,
HistoryRequest, HistoryResponse,
BestBlockRequest, BestBlockResponse,
SignedRequest, SignedResponse,
SignedRequestInternal,
RemoteTransaction,
} from './types';
import type {
FilterUsedRequest, FilterUsedResponse,
} from '../../../common/lib/state-fetch/currencySpecificTypes';
import type { IFetcher } from './IFetcher';
import axios from 'axios';
import {
Logger,
stringifyError
} from '../../../../utils/logging';
import {
GetTxsBodiesForUTXOsApiError,
GetUtxosForAddressesApiError,
GetUtxosSumsForAddressesApiError,
GetTxHistoryForAddressesApiError,
GetBestBlockError,
SendTransactionApiError,
CheckAddressesInUseApiError,
InvalidWitnessError,
RollbackApiError,
} from '../../../common/errors';
import type { ConfigType } from '../../../../../config/config-types';
import { fromWords, decode } from 'bech32';
declare var CONFIG: ConfigType;
const backendUrl = CONFIG.network.backendUrl;
/**
* Makes calls to Yoroi backend service
* https://github.com/Emurgo/yoroi-graphql-migration-backend
*/
export class RemoteFetcher implements IFetcher {
getLastLaunchVersion: () => string;
getCurrentLocale: () => string;
getPlatform: () => string;
constructor(
getLastLaunchVersion: () => string,
getCurrentLocale: () => string,
getPlatform: () => string,
) {
this.getLastLaunchVersion = getLastLaunchVersion;
this.getCurrentLocale = getCurrentLocale;
this.getPlatform = getPlatform;
}
getUTXOsForAddresses: AddressUtxoRequest => Promise<AddressUtxoResponse> = (body) => (
axios(
`${backendUrl}/api/txs/utxoForAddresses`,
{
method: 'post',
data: {
addresses: body.addresses
},
headers: {
'yoroi-version': this.getLastLaunchVersion(),
'yoroi-locale': this.getCurrentLocale()
}
}
).then(response => response.data)
.catch((error) => {
Logger.error(`${nameof(RemoteFetcher)}::${nameof(this.getUTXOsForAddresses)} error: ` + stringifyError(error));
throw new GetUtxosForAddressesApiError();
})
)
getTxsBodiesForUTXOs: TxBodiesRequest => Promise<TxBodiesResponse> = (body) => (
axios(
`${backendUrl}/api/txs/txBodies`,
{
method: 'post',
data: {
txsHashes: body.txsHashes
},
headers: {
'yoroi-version': this.getLastLaunchVersion(),
'yoroi-locale': this.getCurrentLocale()
}
}
).then(response => response.data)
.catch((error) => {
Logger.error(`${nameof(RemoteFetcher)}::${nameof(this.getTxsBodiesForUTXOs)} error: ` + stringifyError(error));
throw new GetTxsBodiesForUTXOsApiError();
})
)
getUTXOsSumsForAddresses: UtxoSumRequest => Promise<UtxoSumResponse> = (body) => (
axios(
`${backendUrl}/api/txs/utxoSumForAddresses`,
{
method: 'post',
data: {
addresses: body.addresses
},
headers: {
'yoroi-version': this.getLastLaunchVersion(),
'yoroi-locale': this.getCurrentLocale()
}
}
).then(response => response.data)
.catch((error) => {
Logger.error(`${nameof(RemoteFetcher)}::${nameof(this.getUTXOsSumsForAddresses)} error: ` + stringifyError(error));
throw new GetUtxosSumsForAddressesApiError();
})
)
getTransactionsHistoryForAddresses: HistoryRequest => Promise<HistoryResponse> = (body) => (
axios(
`${backendUrl}/api/v2/txs/history`,
{
method: 'post',
data: body,
headers: {
'yoroi-version': this.getLastLaunchVersion(),
'yoroi-locale': this.getCurrentLocale()
}
}
).then(response => {
return response.data.map((resp: RemoteTransaction) => {
if (resp.type === 'shelley') {
// unfortunately the backend returns Shelley addresses as bech32
// this is a bad idea, and so we manually change them to raw payload
for (const input of resp.inputs) {
try {
const payload = fromWords(decode(input.address, 1000).words);
// $FlowExpectedError[cannot-write]
input.address = Buffer.from(payload).toString('hex');
} catch (_e) { /* expected not to work for base58 addresses */ }
}
for (const output of resp.outputs) {
try {
const payload = fromWords(decode(output.address, 1000).words);
// $FlowExpectedError[cannot-write]
output.address = Buffer.from(payload).toString('hex');
} catch (_e) { /* expected not to work for base58 addresses */ }
}
// this was caused by a typo in the backend
if (resp.withdrawals == null) {
resp.withdrawals = [];
}
for (const withdrawal of resp.withdrawals) {
try {
const payload = fromWords(decode(withdrawal.address, 1000).words);
// $FlowExpectedError[cannot-write]
withdrawal.address = Buffer.from(payload).toString('hex');
} catch (_e) { /* expected not to work for base58 addresses */ }
}
}
for (const input of resp.inputs) {
// backend stores inputs as numbers but outputs as strings
// we solve this mismatch locally
// $FlowExpectedError[cannot-write]
input.amount = input.amount.toString();
}
if (resp.height != null) {
return resp;
}
// $FlowExpectedError[prop-missing] remove if we rename the field in the backend-service
const height = resp.block_num;
// $FlowExpectedError[prop-missing] remove if we rename the field in the backend-service
delete resp.block_num;
return {
...resp,
height,
};
});
})
.catch((error) => {
Logger.error(`${nameof(RemoteFetcher)}::${nameof(this.getTransactionsHistoryForAddresses)} error: ` + stringifyError(error));
if (
error?.response === 'REFERENCE_BLOCK_MISMATCH' ||
error?.response === 'REFERENCE_TX_NOT_FOUND' ||
error?.response === 'REFERENCE_BEST_BLOCK_MISMATCH'
) {
throw new RollbackApiError();
}
throw new GetTxHistoryForAddressesApiError();
})
)
getBestBlock: BestBlockRequest => Promise<BestBlockResponse> = (_body) => (
axios(
`${backendUrl}/api/v2/bestblock`,
{
method: 'get',
headers: {
'yoroi-version': this.getLastLaunchVersion(),
'yoroi-locale': this.getCurrentLocale()
}
}
).then(response => response.data)
.catch((error) => {
Logger.error(`${nameof(RemoteFetcher)}::${nameof(this.getBestBlock)} error: ` + stringifyError(error));
throw new GetBestBlockError();
})
)
sendTx: SignedRequest => Promise<SignedResponse> = (body) => {
const signedTx64 = Buffer.from(body.encodedTx).toString('base64');
return axios(
`${backendUrl}/api/txs/signed`,
{
method: 'post',
data: ({
signedTx: signedTx64
}: SignedRequestInternal),
headers: {
'yoroi-version': this.getLastLaunchVersion(),
'yoroi-locale': this.getCurrentLocale()
}
}
).then(() => ({
txId: body.id
}))
.catch((error) => {
Logger.error(`${nameof(RemoteFetcher)}::${nameof(this.sendTx)} error: ` + stringifyError(error));
if (error.request.response.includes('Invalid witness')) {
throw new InvalidWitnessError();
}
throw new SendTransactionApiError();
});
}
checkAddressesInUse: FilterUsedRequest => Promise<FilterUsedResponse> = (body) => (
axios(
`${backendUrl}/api/v2/addresses/filterUsed`,
{
method: 'post',
data: {
addresses: body.addresses
},
headers: {
'yoroi-version': this.getLastLaunchVersion(),
'yoroi-locale': this.getCurrentLocale()
}
}
).then(response => response.data)
.catch((error) => {
Logger.error(`${nameof(RemoteFetcher)}::${nameof(this.checkAddressesInUse)} error: ` + stringifyError(error));
throw new CheckAddressesInUseApiError();
})
)
}