This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 174
/
LibcoreBitcoinAccountBridge.js
177 lines (154 loc) · 4.5 KB
/
LibcoreBitcoinAccountBridge.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
// @flow
import invariant from "invariant";
import { BigNumber } from "bignumber.js";
import { FeeNotLoaded, InvalidAddress } from "@ledgerhq/errors";
import type { AccountBridge } from "./types";
import { getFeeItems } from "../api/FeesBitcoin";
import type { FeeItems } from "../api/FeesBitcoin";
import { syncAccount } from "../libcore/syncAccount";
import { isValidRecipient } from "../libcore/isValidRecipient";
import { getFeesForTransaction } from "../libcore/getFeesForTransaction";
import libcoreSignAndBroadcast from "../libcore/signAndBroadcast";
import { makeLRUCache } from "../cache";
export type Transaction = {
amount: BigNumber | string,
recipient: string,
feePerByte: ?(BigNumber | string),
networkInfo: ?{ feeItems: FeeItems },
useAllAmount?: boolean
};
const asLibcoreTransaction = ({
amount,
recipient,
feePerByte,
useAllAmount
}) => ({
amount,
recipient,
feePerByte,
useAllAmount
});
const startSync = (initialAccount, _observation) => syncAccount(initialAccount);
const checkValidRecipient = makeLRUCache(
(account, recipient) => {
if (!recipient)
return Promise.reject(
new InvalidAddress("", { currencyName: account.currency.name })
);
return isValidRecipient({ currency: account.currency, recipient });
},
(currency, recipient) => `${currency.id}_${recipient}`
);
const createTransaction = () => ({
amount: BigNumber(0),
recipient: "",
feePerByte: undefined,
networkInfo: null,
useAllAmount: false
});
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
});
const editTransactionAmount = (account, t, amount) => ({
...t,
amount
});
const getTransactionAmount = (a, t) => BigNumber(t.amount);
const editTransactionRecipient = (account, t, recipient) => ({
...t,
recipient
});
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 };
default:
return t;
}
};
const getTransactionExtra = (a, t, field) => {
switch (field) {
case "feePerByte":
return t.feePerByte;
case "useAllAmount":
return t.useAllAmount;
default:
return undefined;
}
};
const signAndBroadcast = (account, transaction, deviceId) =>
libcoreSignAndBroadcast({
account,
transaction: asLibcoreTransaction(transaction),
deviceId
});
const getFees = makeLRUCache(
async (a, t) => {
await checkValidRecipient(a, t.recipient);
return getFeesForTransaction({
account: a,
transaction: asLibcoreTransaction(t)
});
},
(a, t) =>
`${a.id}_${a.blockHeight || 0}_${t.amount.toString()}_${t.recipient}_${
t.feePerByte ? t.feePerByte.toString() : ""
}`
);
const checkValidTransaction = async (a, t) =>
!t.feePerByte
? Promise.reject(new FeeNotLoaded())
: !t.amount
? Promise.resolve(null)
: getFees(a, t).then(() => null);
const getTotalSpent = async (a, t) =>
t.useAllAmount
? a.balance
: !t.amount || BigNumber(t.amount).isZero()
? Promise.resolve(BigNumber(0))
: getFees(a, t).then(totalFees =>
BigNumber(t.amount || "0").plus(totalFees || 0)
);
const getMaxAmount = async (a, t) =>
getFees(a, t).then(totalFees => a.balance.minus(totalFees || 0));
const prepareTransaction = (account, transaction) =>
Promise.resolve(transaction);
const bridge: AccountBridge<Transaction> = {
startSync,
checkValidRecipient,
createTransaction,
fetchTransactionNetworkInfo,
getTransactionNetworkInfo,
applyTransactionNetworkInfo,
editTransactionAmount,
getTransactionAmount,
editTransactionRecipient,
getTransactionRecipient,
editTransactionExtra,
getTransactionExtra,
checkValidTransaction,
getTotalSpent,
getMaxAmount,
prepareTransaction,
signAndBroadcast
};
export default bridge;