blob: 8a0db17766a9942c217fbfaa1d69cda772b9029c (
plain) (
blame)
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
|
module.exports = async (req, res) => {
let { user } = req;
let { address, amount, feeRate } = req.body;
let tx, fee;
let recipient = await db.User.findOne({
attributes: ['username'],
where: { address }
});
if (recipient)
emit(user.username, "to", recipient);
try {
amount = parseInt(amount);
let partial = await bc.createRawTransaction([], {
[address]: (amount / SATS).toFixed(8)
});
let params = {
subtractFeeFromOutputs: amount === user.balance ? [0] : []
}
if (feeRate) params.feeRate = (feeRate / SATS).toFixed(8);
tx = await bc.fundRawTransaction(partial, params);
if (config.bitcoin.walletpass)
await bc.walletPassphrase(config.bitcoin.walletpass, 300);
let signed = (await bc.signRawTransactionWithWallet(tx.hex));
decoded = await bc.decodeRawTransaction(signed.hex);
feeRate = Math.round(tx.fee * SATS * 1000 / decoded.vsize);
res.send({ feeRate, tx });
} catch (e) {
l.error("bitcoin fee estimation error", e);
return res.status(500).send(e.message);
}
};
|