This repository has been archived by the owner on Mar 24, 2020. It is now read-only.
forked from jackson-jackson/vanbtc.ca
-
Notifications
You must be signed in to change notification settings - Fork 19
/
bcoin.js
103 lines (97 loc) · 3.42 KB
/
bcoin.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
(function() {
var bcoin, config, db, twilio;
bcoin = require('bcoin');
config = require('./config');
db = require('./redis');
twilio = require('twilio');
module.exports = {
init: function(app) {
var chain, pool, users;
users = {};
chain = new bcoin.chain({
db: 'leveldb',
name: 'spvchain',
location: __dirname + '/db/spvchain',
spv: true
});
pool = new bcoin.pool({
chain: chain,
spv: true,
size: 1,
maxPeers: 1,
seeds: ['dctrl.ca']
});
return pool.open().then(function() {
db.keysAsync("user:*").then(function(keys) {
return Promise.all(keys.map(function(key) {
return db.hgetallAsync(key).then(function(user) {
if (user.address) {
pool.watchAddress(user.address);
return users[user.address] = {
email: user.email,
currency: user.currency,
symbol: user.symbol,
phone: user.phone
};
}
});
}));
});
pool.connect().then(function () { pool.startSync(); });
pool.on('error', function(err) {
return console.log(err);
});
return pool.on('tx', function(tx) {
var address, client, i, len, output, ref, results, value;
ref = tx.outputs;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
output = ref[i];
value = (output.value / 100000000).toFixed(8);
address = output.getAddress().toBase58();
if (Object.keys(users).includes(address)) {
app.render('payment', {
value: value,
address: address
}, function(err, html) {
var content, from_email, helper, mail, request, sg, subject, to_email;
helper = require('sendgrid').mail;
from_email = new helper.Email('info@coinos.io');
to_email = new helper.Email(users[address].email);
subject = 'Received Payment';
content = new helper.Content('text/html', html);
mail = new helper.Mail(from_email, subject, to_email, content);
sg = require('sendgrid')(config.sendgrid_token);
request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
return sg.API(request, function(error, response) {
console.log(response.statusCode);
console.log(response.body);
return console.log(response.headers);
});
});
if (users[address].phone) {
client = new twilio.RestClient(config.twilio_sid, config.twilio_token);
results.push(client.messages.create({
to: user.phone,
from: config.twilio_number,
body: "You received a payment of " + value + " BTC"
}, function(err, message) {
return console.log(message.sid);
}));
} else {
results.push(void 0);
}
} else {
results.push(void 0);
}
}
return results;
});
});
}
};
}).call(this);