-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbtip.js
65 lines (58 loc) · 2.29 KB
/
btip.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
var config = require('./config');
var logger = require('./lib/logger');
var Netmask = require('netmask').Netmask
var bitaddress = require('bitcoin-address');
var express = require('express');
var bitcoin = require('bitcoin');
var git = require('./lib/github');
var app = express();
var block = new Netmask(config.netmask);
var client = new bitcoin.Client({
host: config.host,
port: config.port,
user: config.username,
pass: config.password
});
app.use(express.bodyParser());
client.getBalance('*', 6, function (err, balance) {
if (err) return console.log(err);
app.post('/api', function (req, res) {
if (block.contains(req.connection.remoteAddress)) {
if (balance > 0) {
var gitjson = JSON.parse(req.body.payload);
if (gitjson.ref == 'refs/heads/' + config.branch) {
logger.log('detected push to master');
var regExp = /\(btip:([^)]+)\)/;
var pullnumber = git.getPullNumber(gitjson.head_commit.message)
var tipaddr = regExp.exec(gitjson.head_commit.message);
var tipTo = tipaddr[1].trim();
if (bitaddress.validate(tipTo)) {
if (balance >= config.tip) {
logger.log('info', 'Sending ' + config.tip + 'BTC to ' + tipTo);
client.walletPassphrase(config.walletPassphrase, '1');
client.sendToAddress(tipTo, config.tip);
git.addComment('Sent ' + config.tip + 'BTC to ' + tipTo + '. Thanks for committing!', pullnumber)
}
else {
logger.log('error', 'Failed send to: ' + tipTo + ' - Insufficient balance');
git.addComment('Failed send to: ' + tipTo + ' due to insufficient balance. Repo admin has been notified', pullnumber)
}
}
else {
logger.log('error', 'Failed send to: ' + tipTo + ' - Invalid address');
git.addComment('Failed send to: ' + tipTo + ' Address is invalid. Repo admin has been notified', pullnumber)
}
}
}
else
logger.log('error', 'No balance available');
}
else {
logger.log('warn', 'Logged unauthorized request from IP: ' + req.connection.remoteAddress);
res.send('IP Not authorized');
}
res.end('done');
});
});
app.listen(3090);
logger.log('info', 'btip Listening on port 3090');