commit
af8e68d943
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
'use strict';
|
||||
|
||||
var _ = require('lodash');
|
||||
var log = require('npmlog');
|
||||
log.debug = log.verbose;
|
||||
|
||||
var config = require('../config');
|
||||
var EmailService = require('../lib/emailservice');
|
||||
|
||||
var emailService = new EmailService();
|
||||
emailService.start(config, function(err) {
|
||||
if (err) throw err;
|
||||
|
||||
console.log('Email service started');
|
||||
});
|
|
@ -10,7 +10,6 @@ var BlockchainExplorer = require('./blockchainexplorer');
|
|||
var Storage = require('./storage');
|
||||
var MessageBroker = require('./messagebroker');
|
||||
var Lock = require('./lock');
|
||||
var EmailService = require('./emailservice');
|
||||
|
||||
var Notification = require('./model/notification');
|
||||
|
||||
|
@ -20,8 +19,8 @@ BlockchainMonitor.prototype.start = function(opts, cb) {
|
|||
opts = opts || {};
|
||||
$.checkArgument(opts.blockchainExplorerOpts);
|
||||
$.checkArgument(opts.storageOpts);
|
||||
$.checkArgument(opts.messageBrokerOpts);
|
||||
$.checkArgument(opts.lockOpts);
|
||||
$.checkArgument(opts.emailOpts);
|
||||
|
||||
var self = this;
|
||||
|
||||
|
@ -49,16 +48,8 @@ BlockchainMonitor.prototype.start = function(opts, cb) {
|
|||
], function(err) {
|
||||
if (err) {
|
||||
log.error(err);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
self.emailService = new EmailService({
|
||||
lock: self.lock,
|
||||
storage: self.storage,
|
||||
mailer: opts.mailer,
|
||||
emailOpts: opts.emailOpts,
|
||||
});
|
||||
return cb();
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -136,11 +127,7 @@ BlockchainMonitor.prototype._createNotification = function(walletId, txid, addre
|
|||
});
|
||||
self.storage.storeNotification(walletId, notification, function() {
|
||||
self.messageBroker.send(notification)
|
||||
if (self.emailService) {
|
||||
self.emailService.sendEmail(notification, cb);
|
||||
} else {
|
||||
return cb();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -8,6 +8,11 @@ log.debug = log.verbose;
|
|||
var fs = require('fs');
|
||||
var nodemailer = require('nodemailer');
|
||||
|
||||
var WalletUtils = require('bitcore-wallet-utils');
|
||||
var Storage = require('./storage');
|
||||
var MessageBroker = require('./messagebroker');
|
||||
var Lock = require('./lock');
|
||||
|
||||
var Model = require('./model');
|
||||
|
||||
var EMAIL_TYPES = {
|
||||
|
@ -38,21 +43,52 @@ var EMAIL_TYPES = {
|
|||
};
|
||||
|
||||
|
||||
function EmailService(opts) {
|
||||
$.checkArgument(opts);
|
||||
function EmailService() {};
|
||||
|
||||
opts.emailOpts = opts.emailOpts || {};
|
||||
EmailService.prototype.start = function(opts, cb) {
|
||||
opts = opts || {};
|
||||
|
||||
this.storage = opts.storage;
|
||||
this.lock = opts.lock;
|
||||
this.mailer = opts.mailer || nodemailer.createTransport(opts.emailOpts);
|
||||
this.subjectPrefix = opts.emailOpts.subjectPrefix || '[Wallet service]';
|
||||
this.from = opts.emailOpts.from;
|
||||
var self = this;
|
||||
|
||||
$.checkState(this.mailer);
|
||||
$.checkState(this.from);
|
||||
async.parallel([
|
||||
|
||||
function(done) {
|
||||
if (opts.storage) {
|
||||
self.storage = opts.storage;
|
||||
done();
|
||||
} else {
|
||||
self.storage = new Storage();
|
||||
self.storage.connect(opts.storageOpts, done);
|
||||
}
|
||||
},
|
||||
function(done) {
|
||||
if (opts.messageBroker) {
|
||||
self.messageBroker = opts.messageBroker;
|
||||
} else {
|
||||
self.messageBroker = new MessageBroker(opts.messageBrokerOpts);
|
||||
}
|
||||
self.messageBroker.onMessage(_.bind(self.sendEmail, self));
|
||||
done();
|
||||
},
|
||||
function(done) {
|
||||
self.lock = opts.lock || new Lock(opts.lockOpts);
|
||||
done();
|
||||
},
|
||||
function(done) {
|
||||
self.mailer = opts.mailer || nodemailer.createTransport(opts.emailOpts);
|
||||
self.subjectPrefix = opts.emailOpts.subjectPrefix || '[Wallet service]';
|
||||
self.from = opts.emailOpts.from;
|
||||
done();
|
||||
},
|
||||
], function(err) {
|
||||
if (err) {
|
||||
log.error(err);
|
||||
}
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
// TODO: cache for X minutes
|
||||
EmailService.prototype._readTemplate = function(filename, cb) {
|
||||
fs.readFile(__dirname + '/templates/' + filename + '.plain', 'utf8', function(err, template) {
|
||||
|
@ -105,6 +141,9 @@ EmailService.prototype._getDataForTemplate = function(notification, cb) {
|
|||
|
||||
var data = _.cloneDeep(notification.data);
|
||||
data.subjectPrefix = _.trim(self.subjectPrefix) + ' ';
|
||||
if (data.amount) {
|
||||
data.amount = WalletUtils.formatAmount(+data.amount, 'bit') + ' bits';
|
||||
}
|
||||
self.storage.fetchWallet(notification.walletId, function(err, wallet) {
|
||||
if (err) return cb(err);
|
||||
data.walletId = wallet.id;
|
||||
|
@ -144,6 +183,8 @@ EmailService.prototype._send = function(email, cb) {
|
|||
EmailService.prototype.sendEmail = function(notification, cb) {
|
||||
var self = this;
|
||||
|
||||
cb = cb || function() {};
|
||||
|
||||
var emailType = EMAIL_TYPES[notification.type];
|
||||
if (!emailType) return cb();
|
||||
|
||||
|
@ -197,7 +238,12 @@ EmailService.prototype.sendEmail = function(notification, cb) {
|
|||
return next();
|
||||
});
|
||||
},
|
||||
], cb);
|
||||
], function(err) {
|
||||
if (err) {
|
||||
log.error('An error ocurred generating email notification', err);
|
||||
}
|
||||
return cb(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -19,7 +19,6 @@ var Lock = require('./lock');
|
|||
var Storage = require('./storage');
|
||||
var MessageBroker = require('./messagebroker');
|
||||
var BlockchainExplorer = require('./blockchainexplorer');
|
||||
var EmailService = require('./emailservice');
|
||||
|
||||
var Model = require('./model');
|
||||
var Wallet = Model.Wallet;
|
||||
|
@ -31,7 +30,6 @@ var storage;
|
|||
var blockchainExplorer;
|
||||
var blockchainExplorerOpts;
|
||||
var messageBroker;
|
||||
var emailService;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -48,7 +46,6 @@ function WalletService() {
|
|||
this.blockchainExplorerOpts = blockchainExplorerOpts;
|
||||
this.messageBroker = messageBroker;
|
||||
this.notifyTicker = 0;
|
||||
this.emailService = emailService;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -80,17 +77,6 @@ WalletService.initialize = function(opts, cb) {
|
|||
}
|
||||
};
|
||||
|
||||
function initEmailService(cb) {
|
||||
if (!opts.mailer && !opts.emailOpts) return cb();
|
||||
emailService = new EmailService({
|
||||
lock: lock,
|
||||
storage: storage,
|
||||
mailer: opts.mailer,
|
||||
emailOpts: opts.emailOpts,
|
||||
});
|
||||
return cb();
|
||||
};
|
||||
|
||||
function initMessageBroker(cb) {
|
||||
if (opts.messageBroker) {
|
||||
messageBroker = opts.messageBroker;
|
||||
|
@ -108,9 +94,6 @@ WalletService.initialize = function(opts, cb) {
|
|||
function(next) {
|
||||
initMessageBroker(next);
|
||||
},
|
||||
function(next) {
|
||||
initEmailService(next);
|
||||
},
|
||||
], function(err) {
|
||||
if (err) {
|
||||
log.error('Could not initialize', err);
|
||||
|
@ -356,11 +339,7 @@ WalletService.prototype._notify = function(type, data, opts, cb) {
|
|||
|
||||
this.storage.storeNotification(walletId, notification, function() {
|
||||
self.messageBroker.send(notification);
|
||||
if (self.emailService) {
|
||||
self.emailService.sendEmail(notification, cb);
|
||||
} else {
|
||||
return cb();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -1012,7 +991,8 @@ WalletService.prototype.broadcastTx = function(opts, cb) {
|
|||
|
||||
self._notify('NewOutgoingTx', {
|
||||
txProposalId: opts.txProposalId,
|
||||
txid: txid
|
||||
txid: txid,
|
||||
amount: txp.amount,
|
||||
}, function() {
|
||||
return cb(null, txp);
|
||||
});
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
<%= subjectPrefix %>New payment received
|
||||
A Payment has been received into your wallet <%= walletName %>.
|
||||
A payment of <%= amount %> has been received into your wallet <%= walletName %>.
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
<%= subjectPrefix %>Payment sent
|
||||
A Payment has been sent from your wallet <%= walletName %>.
|
||||
A Payment of <%= amount %> has been sent from your wallet <%= walletName %>.
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
"name": "bitcore-wallet-service",
|
||||
"description": "A service for Mutisig HD Bitcoin Wallets",
|
||||
"author": "BitPay Inc",
|
||||
"version": "0.0.32",
|
||||
"version": "0.0.33",
|
||||
"keywords": [
|
||||
"bitcoin",
|
||||
"copay",
|
||||
|
@ -21,7 +21,7 @@
|
|||
"async": "^0.9.0",
|
||||
"bitcore": "^0.11.6",
|
||||
"bitcore-explorers": "^0.10.3",
|
||||
"bitcore-wallet-utils": "0.0.12",
|
||||
"bitcore-wallet-utils": "0.0.13",
|
||||
"body-parser": "^1.11.0",
|
||||
"coveralls": "^2.11.2",
|
||||
"email-validator": "^1.0.1",
|
||||
|
|
|
@ -23,6 +23,8 @@ var Storage = require('../../lib/storage');
|
|||
var Model = require('../../lib/model');
|
||||
|
||||
var WalletService = require('../../lib/server');
|
||||
var EmailService = require('../../lib/emailservice');
|
||||
|
||||
var TestData = require('../testdata');
|
||||
|
||||
var helpers = {};
|
||||
|
@ -210,7 +212,7 @@ helpers.createAddresses = function(server, wallet, main, change, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
var storage, blockchainExplorer, mailer;
|
||||
var storage, blockchainExplorer;
|
||||
|
||||
var useMongo = false;
|
||||
|
||||
|
@ -250,14 +252,9 @@ describe('Wallet service', function() {
|
|||
beforeEach(function(done) {
|
||||
resetStorage(function() {
|
||||
blockchainExplorer = sinon.stub();
|
||||
mailer = sinon.stub();
|
||||
WalletService.initialize({
|
||||
storage: storage,
|
||||
blockchainExplorer: blockchainExplorer,
|
||||
mailer: mailer,
|
||||
emailOpts: {
|
||||
from: 'bws@dummy.net',
|
||||
}
|
||||
}, done);
|
||||
});
|
||||
});
|
||||
|
@ -265,6 +262,160 @@ describe('Wallet service', function() {
|
|||
WalletService.shutDown(done);
|
||||
});
|
||||
|
||||
describe('Email notifications', function() {
|
||||
var server, wallet, mailerStub, emailService;
|
||||
|
||||
beforeEach(function(done) {
|
||||
helpers.createAndJoinWallet(2, 3, function(s, w) {
|
||||
server = s;
|
||||
wallet = w;
|
||||
|
||||
var i = 0;
|
||||
async.eachSeries(w.copayers, function(copayer, next) {
|
||||
helpers.getAuthServer(copayer.id, function(server) {
|
||||
server.savePreferences({
|
||||
email: 'copayer' + (i++) + '@domain.com',
|
||||
}, next);
|
||||
});
|
||||
}, function(err) {
|
||||
should.not.exist(err);
|
||||
|
||||
mailerStub = sinon.stub();
|
||||
mailerStub.sendMail = sinon.stub();
|
||||
mailerStub.sendMail.yields();
|
||||
|
||||
emailService = new EmailService();
|
||||
emailService.start({
|
||||
lockOpts: {},
|
||||
messageBroker: server.messageBroker,
|
||||
storage: storage,
|
||||
mailer: mailerStub,
|
||||
emailOpts: {
|
||||
from: 'bws@dummy.net',
|
||||
subjectPrefix: '[test wallet]',
|
||||
},
|
||||
}, function(err) {
|
||||
should.not.exist(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should notify copayers a new tx proposal has been created', function(done) {
|
||||
helpers.stubUtxos(server, wallet, [1, 1], function() {
|
||||
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.8, 'some message', TestData.copayers[0].privKey_1H_0);
|
||||
server.createTx(txOpts, function(err, tx) {
|
||||
should.not.exist(err);
|
||||
setTimeout(function() {
|
||||
var calls = mailerStub.sendMail.getCalls();
|
||||
calls.length.should.equal(2);
|
||||
var emails = _.map(calls, function(c) {
|
||||
return c.args[0];
|
||||
});
|
||||
_.difference(['copayer1@domain.com', 'copayer2@domain.com'], _.pluck(emails, 'to')).should.be.empty;
|
||||
var one = emails[0];
|
||||
one.from.should.equal('bws@dummy.net');
|
||||
one.subject.should.contain('New payment proposal');
|
||||
one.text.should.contain(wallet.name);
|
||||
one.text.should.contain(wallet.copayers[0].name);
|
||||
server.storage.fetchUnsentEmails(function(err, unsent) {
|
||||
should.not.exist(err);
|
||||
unsent.should.be.empty;
|
||||
done();
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should notify copayers a new outgoing tx has been created', function(done) {
|
||||
helpers.stubUtxos(server, wallet, [1, 1], function() {
|
||||
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.8, 'some message', TestData.copayers[0].privKey_1H_0);
|
||||
|
||||
var txpId;
|
||||
async.waterfall([
|
||||
|
||||
function(next) {
|
||||
server.createTx(txOpts, next);
|
||||
},
|
||||
function(txp, next) {
|
||||
txpId = txp.id;
|
||||
async.eachSeries(_.range(2), function(i, next) {
|
||||
var copayer = TestData.copayers[i];
|
||||
helpers.getAuthServer(copayer.id, function(server) {
|
||||
var signatures = helpers.clientSign(txp, copayer.xPrivKey);
|
||||
server.signTx({
|
||||
txProposalId: txp.id,
|
||||
signatures: signatures,
|
||||
}, next);
|
||||
});
|
||||
}, next);
|
||||
},
|
||||
function(next) {
|
||||
helpers.stubBroadcast('999');
|
||||
server.broadcastTx({
|
||||
txProposalId: txpId,
|
||||
}, next);
|
||||
},
|
||||
], function(err) {
|
||||
should.not.exist(err);
|
||||
|
||||
setTimeout(function() {
|
||||
var calls = mailerStub.sendMail.getCalls();
|
||||
var emails = _.map(_.takeRight(calls, 3), function(c) {
|
||||
return c.args[0];
|
||||
});
|
||||
_.difference(['copayer0@domain.com', 'copayer1@domain.com', 'copayer2@domain.com'], _.pluck(emails, 'to')).should.be.empty;
|
||||
var one = emails[0];
|
||||
one.from.should.equal('bws@dummy.net');
|
||||
one.subject.should.contain('Payment sent');
|
||||
one.text.should.contain(wallet.name);
|
||||
one.text.should.contain('800,000');
|
||||
server.storage.fetchUnsentEmails(function(err, unsent) {
|
||||
should.not.exist(err);
|
||||
unsent.should.be.empty;
|
||||
done();
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should notify copayers of incoming txs', function(done) {
|
||||
server.createAddress({}, function(err, address) {
|
||||
should.not.exist(err);
|
||||
|
||||
// Simulate incoming tx notification
|
||||
server._notify('NewIncomingTx', {
|
||||
txid: '999',
|
||||
address: address,
|
||||
amount: 12300000,
|
||||
}, function(err) {
|
||||
setTimeout(function() {
|
||||
var calls = mailerStub.sendMail.getCalls();
|
||||
calls.length.should.equal(3);
|
||||
var emails = _.map(calls, function(c) {
|
||||
return c.args[0];
|
||||
});
|
||||
_.difference(['copayer0@domain.com', 'copayer1@domain.com', 'copayer2@domain.com'], _.pluck(emails, 'to')).should.be.empty;
|
||||
var one = emails[0];
|
||||
one.from.should.equal('bws@dummy.net');
|
||||
one.subject.should.contain('New payment received');
|
||||
one.text.should.contain(wallet.name);
|
||||
one.text.should.contain('123,000');
|
||||
server.storage.fetchUnsentEmails(function(err, unsent) {
|
||||
should.not.exist(err);
|
||||
unsent.should.be.empty;
|
||||
done();
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#getInstanceWithAuth', function() {
|
||||
|
||||
it('should get server instance for existing copayer', function(done) {
|
||||
|
@ -3229,51 +3380,4 @@ describe('Wallet service', function() {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Email notifications', function() {
|
||||
var server, wallet, sendMailStub;
|
||||
beforeEach(function(done) {
|
||||
helpers.createAndJoinWallet(2, 3, function(s, w) {
|
||||
server = s;
|
||||
wallet = w;
|
||||
sendMailStub = sinon.stub();
|
||||
sendMailStub.yields();
|
||||
server.emailService.mailer.sendMail = sendMailStub;
|
||||
|
||||
var i = 0;
|
||||
async.eachSeries(w.copayers, function(copayer, next) {
|
||||
helpers.getAuthServer(copayer.id, function(server) {
|
||||
server.savePreferences({
|
||||
email: 'copayer' + (i++) + '@domain.com',
|
||||
}, next);
|
||||
});
|
||||
}, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('should notify copayers a new tx proposal has been created', function(done) {
|
||||
helpers.stubUtxos(server, wallet, [1, 1], function() {
|
||||
var txOpts = helpers.createProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.8, 'some message', TestData.copayers[0].privKey_1H_0);
|
||||
server.createTx(txOpts, function(err, tx) {
|
||||
should.not.exist(err);
|
||||
var calls = sendMailStub.getCalls();
|
||||
calls.length.should.equal(2);
|
||||
var emails = _.map(calls, function(c) {
|
||||
return c.args[0];
|
||||
});
|
||||
_.difference(['copayer1@domain.com', 'copayer2@domain.com'], _.pluck(emails, 'to')).should.be.empty;
|
||||
var one = emails[0];
|
||||
one.from.should.equal('bws@dummy.net');
|
||||
one.subject.should.contain('New payment proposal');
|
||||
one.text.should.contain(wallet.name);
|
||||
one.text.should.contain(wallet.copayers[0].name);
|
||||
server.storage.fetchUnsentEmails(function(err, unsent) {
|
||||
should.not.exist(err);
|
||||
unsent.should.be.empty;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue