This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
/
LibcoreBridge.js
334 lines (290 loc) · 9.49 KB
/
LibcoreBridge.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// @flow
import invariant from 'invariant'
import { BigNumber } from 'bignumber.js'
import { map } from 'rxjs/operators'
import { patchAccount } from '@ledgerhq/live-common/lib/reconciliation'
import type { Account, TokenAccount, Transaction } from '@ledgerhq/live-common/lib/types'
import type { CurrencyBridge, AccountBridge } from '@ledgerhq/live-common/lib/bridge/types'
import { FeeNotLoaded, InvalidAddress, NotEnoughBalance } from '@ledgerhq/errors'
import { makeLRUCache } from '@ledgerhq/live-common/lib/cache'
import { fromOperationRaw, toAccountRaw, fromAccountRaw } from '@ledgerhq/live-common/lib/account'
import { apiForCurrency } from '@ledgerhq/live-common/lib/api/Ethereum'
import libcoreScanAccounts from 'commands/libcoreScanAccounts'
import libcoreSyncAccount from 'commands/libcoreSyncAccount'
import libcoreSignAndBroadcast from 'commands/libcoreSignAndBroadcast'
import libcoreGetFees from 'commands/libcoreGetFees'
import libcoreValidAddress from 'commands/libcoreValidAddress'
export const currencyBridge: CurrencyBridge = {
scanAccountsOnDevice(currency, devicePath) {
return libcoreScanAccounts
.send({
devicePath,
currencyId: currency.id,
})
.pipe(map(fromAccountRaw))
},
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TODO: code below is not yet split per family but is a merge of all coin families.
// how to make this scale? need to use live-common
const getTransactionAccount = (a, t): Account | TokenAccount => {
const { tokenAccountId } = t
return tokenAccountId ? (a.tokenAccounts || []).find(ta => ta.id === tokenAccountId) || a : a
}
const asLibcoreTransaction = t => {
const obj: Transaction = {
recipient: t.recipient,
amount: t.amount ? BigNumber(t.amount).toString() : '0',
}
if ('useAllAmount' in t) {
obj.useAllAmount = t.useAllAmount
}
if (t.tokenAccountId) {
obj.tokenAccountId = t.tokenAccountId
}
// bitcoin fields
if (t.feePerByte) {
obj.feePerByte = BigNumber(t.feePerByte).toString()
}
// ethereum fields
if (t.gasPrice) {
obj.gasPrice = BigNumber(t.gasPrice).toString()
}
if (t.gasLimit) {
obj.gasLimit = BigNumber(t.gasLimit).toString()
}
// xrp
if (t.tag) {
obj.tag = t.tag
}
if (t.fee) {
obj.fee = BigNumber(t.fee).toString()
}
return obj
}
const createTransaction = () => ({
recipient: '',
amount: undefined,
useAllAmount: false,
feePerByte: undefined,
gasPrice: undefined,
gasLimit: undefined,
tag: undefined,
fee: undefined,
tokenAccountId: undefined,
})
const feesLoaded = (a, t) => {
switch (a.currency.family) {
case 'bitcoin':
return !!t.feePerByte && BigNumber(t.feePerByte).gt(0)
case 'ethereum':
return !!t.gasPrice
case 'ripple':
return !!t.fee
default:
return false
}
}
const feesHashFunction = (a, t) =>
`${a.id}_${a.blockHeight || 0}_${(t.amount || '0').toString()}_${t.recipient}_${
t.feePerByte ? t.feePerByte.toString() : ''
}_${t.useAllAmount ? 'sendMax' : 'noSendMax'}_${t.gasLimit ? t.gasLimit.toString() : ''}_${
t.gasPrice ? t.gasPrice.toString() : ''
}_${t.fee ? t.fee.toString() : ''}`
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const startSync = (initialAccount, _observation) =>
libcoreSyncAccount
.send(toAccountRaw(initialAccount))
.pipe(map(raw => account => patchAccount(account, raw)))
const checkValidRecipient = makeLRUCache(
(account, recipient) => {
if (!recipient)
return Promise.reject(new InvalidAddress('', { currencyName: account.currency.name }))
return libcoreValidAddress
.send({
address: recipient,
currencyId: account.currency.id,
})
.toPromise()
},
(currency, recipient) => `${currency.id}_${recipient}`,
)
/*
const fetchTransactionNetworkInfo = async ({ currency }) => {
const feeItems = await getFeeItems(currency)
return { feeItems }
}
const getTransactionNetworkInfo = (account, transaction) => transaction.networkInfo
const applyTransactionNetworkInfo = (account, transaction, networkInfo) => ({
...transaction,
networkInfo,
feePerByte: transaction.feePerByte || networkInfo.feeItems.defaultFeePerByte,
})
*/
// TODO do we need this concept to live here? probably will have to be moved to live-common because is crypto dependent.
const fetchTransactionNetworkInfo = () => Promise.resolve({})
const getTransactionNetworkInfo = () => ({})
const applyTransactionNetworkInfo = (_account, transaction) => transaction
const editTransactionAmount = (account, t, amount) => ({
...t,
amount,
})
const getTransactionAmount = (a, t) => BigNumber(t.amount || '0')
const editTransactionRecipient = (account, t, recipient) => ({
...t,
recipient,
gasLimit: undefined, // need recalculation
})
const getTransactionRecipient = (a, t) => t.recipient
const editTransactionExtra = (a, t, field, value) => {
switch (field) {
case 'feePerByte':
invariant(
!value || BigNumber.isBigNumber(value),
"editTransactionExtra(a,t,'feePerByte',value): BigNumber value expected",
)
return { ...t, feePerByte: value }
case 'useAllAmount':
invariant(
typeof value === 'boolean',
"editTransactionExtra(a,t,'useAllAmount',value): boolean value expected",
)
return { ...t, useAllAmount: value }
case 'gasLimit':
invariant(
value && BigNumber.isBigNumber(value),
"editTransactionExtra(a,t,'gasLimit',value): BigNumber value expected",
)
return { ...t, gasLimit: value }
case 'gasPrice':
invariant(
!value || BigNumber.isBigNumber(value),
"editTransactionExtra(a,t,'gasPrice',value): BigNumber value expected",
)
return { ...t, gasPrice: value }
case 'fee':
invariant(
!value || BigNumber.isBigNumber(value),
"editTransactionExtra(a,t,'fee',value): BigNumber value expected",
)
return { ...t, fee: value }
case 'tag':
invariant(
!value || typeof value === 'number',
"editTransactionExtra(a,t,'tag',value): number value expected",
)
return { ...t, tag: value }
default:
return t
}
}
const getTransactionExtra = (a, t, field) => t[field] // could add more check in future
const editTokenAccountId = (a, t, tokenAccountId) => ({
...t,
tokenAccountId,
gasLimit: undefined, // need recalculation
})
const getTokenAccountId = (a, t) => t.tokenAccountId
const signAndBroadcast = (account, transaction, deviceId) =>
libcoreSignAndBroadcast
.send({
account: toAccountRaw({ ...account, operations: [] }),
transaction: asLibcoreTransaction(transaction),
deviceId,
})
.pipe(
map(e => {
switch (e.type) {
case 'broadcasted':
return {
type: 'broadcasted',
operation: fromOperationRaw(e.operation, account.id),
}
default:
return e
}
}),
)
const getFees = makeLRUCache(async (a, t) => {
await checkValidRecipient(a, t.recipient)
const fees = await libcoreGetFees
.send({
accountRaw: toAccountRaw({ ...a, transactions: [] }),
transaction: asLibcoreTransaction(t),
})
.toPromise()
if (
// FIXME workaround for LLC-246
a.currency.family === 'ethereum' &&
!t.tokenAccountId &&
!t.useAllAmount &&
t.amount &&
BigNumber(t.amount)
.plus(fees)
.isGreaterThan(a.balance)
) {
throw new NotEnoughBalance()
}
return fees
}, feesHashFunction)
const checkValidTransaction = async (a, t) =>
!feesLoaded(a, t)
? Promise.reject(new FeeNotLoaded())
: !t.amount
? Promise.resolve(null)
: getFees(a, t).then(() => null)
const getTotalSpent = async (a, t) => {
const tAccount = getTransactionAccount(a, t)
if (t.useAllAmount) {
return tAccount.balance
}
const amount = BigNumber(t.amount || '0')
if (amount.isZero()) {
return Promise.resolve(amount)
}
if (tAccount.type === 'TokenAccount') {
return Promise.resolve(amount)
}
return getFees(a, t).then(totalFees => amount.plus(totalFees || 0))
}
const getMaxAmount = async (a, t) => {
const tAccount = getTransactionAccount(a, t)
if (tAccount.type === 'TokenAccount') {
return Promise.resolve(tAccount.balance)
}
return getFees(a, t).then(totalFees => tAccount.balance.minus(totalFees || 0))
}
const estimateGasLimitForERC20 = makeLRUCache(
(currency, addr) => apiForCurrency(currency).estimateGasLimitForERC20(addr),
(currency, addr) => `${currency.id}_${addr}`,
)
const prepareTransaction = (a, t) => {
if (a.currency.family !== 'ethereum') return Promise.resolve(t)
const tAccount = getTransactionAccount(a, t)
const addr = tAccount.type === 'TokenAccount' ? tAccount.token.contractAddress : t.recipient
if (!addr || t.gasLimit) return Promise.resolve(t)
return estimateGasLimitForERC20(a.currency, addr)
.then(str => BigNumber(str), () => BigNumber(0x5208))
.then(gasLimit => (t.gasLimit && BigNumber(t.gasLimit).eq(gasLimit) ? t : { ...t, gasLimit }))
}
export const accountBridge: AccountBridge<Transaction> = {
startSync,
checkValidRecipient,
createTransaction,
fetchTransactionNetworkInfo,
getTransactionNetworkInfo,
applyTransactionNetworkInfo,
editTokenAccountId,
getTokenAccountId,
editTransactionAmount,
getTransactionAmount,
editTransactionRecipient,
getTransactionRecipient,
editTransactionExtra,
getTransactionExtra,
checkValidTransaction,
getTotalSpent,
getMaxAmount,
signAndBroadcast,
prepareTransaction,
}