-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
30 lines (24 loc) · 1.07 KB
/
util.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
const request = require('superagent')
, iferr = require('iferr')
, qs = require('querystring')
, movedec = require('move-decimal-point')
, { PaymentRequest, PaymentDetails } = require('bcoin').bip70
const getRate = (currency, cb) => process.env.DUMMYRATE ? cb(null, 1000)
: request('https://apiv2.bitcoinaverage.com/indices/local/ticker/BTC'+currency)
.end(iferr(cb, resp => cb(null, +resp.body.last)))
const toSatoshis = btcs => movedec(btcs, 8)
const toBTC = (amount, currency, cb) => getRate(currency, iferr(cb,
rate => cb(null, (amount/rate).toFixed(8))))
const formatURL = (addr, amount, opt) => `bitcoin:${ addr }?${ qs.stringify(Object.assign({}, opt, { amount })) }`
const makePaymentReq = (addr, amount, opt) =>
PaymentRequest.fromOptions({
version: 1
, paymentDetails: PaymentDetails.fromOptions({
network: 'main'
, time: Date.now()/1000 | 0
, expires: opt.expires
, memo: opt.memo
, outputs: [ { address: addr, value: +toSatoshis(amount) } ]
})
}).toRaw()
module.exports = { toBTC, formatURL, makePaymentReq }