address-watch/index.js

106 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2016-05-19 15:29:06 -07:00
'use strict';
2015-10-14 10:16:39 -07:00
var util = require('util');
2016-05-19 15:29:06 -07:00
var EventEmitter = require('events').EventEmitter;
2018-03-27 23:55:02 -07:00
var bitcore = require('bitcore-lib-btcp');
2015-10-14 10:16:39 -07:00
var spawn = require('child_process').spawn;
function SatoshiFireAlarm(options) {
2016-05-19 15:29:06 -07:00
EventEmitter.call(this, options);
this.node = options.node;
2015-10-14 10:16:39 -07:00
this.alarmActivated = false;
2016-05-19 15:29:06 -07:00
this.child = false;
2015-10-14 10:16:39 -07:00
this.interestingAddresses = [
2018-03-27 23:55:02 -07:00
//'1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', //this is the address that the genesis paid its coinbase to. Can't be spent due to a bug in the code.
//'12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX', //Block 1
//'1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1' //Block 2
'b1RXUJgnuxtzHyJ5UTWerh4pNBWcVikESZe'
2015-10-14 10:16:39 -07:00
];
this.node.services.bitcoind.on('tx', this.transactionHandler.bind(this));
}
/*
* We are going to need bitcoind because we will be setting event listeners (subscribers)
* on Blocks and such
*/
SatoshiFireAlarm.dependencies = ['bitcoind'];
2015-10-14 10:16:39 -07:00
/*
* inherits the serivce base class so we get some stuff for free
*/
util.inherits(SatoshiFireAlarm, EventEmitter);
2015-10-14 10:16:39 -07:00
/*
* start: REQUIRED!! Ours just calls the callback
*/
SatoshiFireAlarm.prototype.start = function(callback) {
2015-10-14 10:16:39 -07:00
callback();
2016-05-19 15:29:06 -07:00
};
2015-10-14 10:16:39 -07:00
/*
* stop: REQUIRED!! Ours just calls the callback
*/
SatoshiFireAlarm.prototype.stop = function(callback) {
2015-10-14 10:16:39 -07:00
callback();
2016-05-19 15:29:06 -07:00
};
SatoshiFireAlarm.prototype.getAPIMethods = function() {
2016-05-19 15:29:06 -07:00
return [];
};
SatoshiFireAlarm.prototype.getPublishEvents = function() {
2016-05-19 15:29:06 -07:00
return [];
};
2015-10-14 10:16:39 -07:00
/*
* transactionHandler: this is the delegate when a transaction is received by your node
*/
SatoshiFireAlarm.prototype.transactionHandler = function(txBuffer) {
2016-05-19 15:29:06 -07:00
var self = this;
2015-10-14 10:16:39 -07:00
2016-05-19 15:29:06 -07:00
var tx = bitcore.Transaction().fromBuffer(txBuffer);
2015-10-14 10:16:39 -07:00
2016-05-19 15:29:06 -07:00
for (var i = 0; i < tx.inputs.length; i++) {
self.transactionInputHandler(tx.inputs[i]);
2015-10-14 10:16:39 -07:00
}
2016-05-19 15:29:06 -07:00
};
2015-10-14 10:16:39 -07:00
/*
* transactionInputHandler: helper for transactionHandler
*/
SatoshiFireAlarm.prototype.transactionInputHandler = function(input) {
2016-05-19 15:29:06 -07:00
if (!input.script) {
return;
}
var address = input.script.toAddress(this.node.network);
if (address && this.interestingAddresses.indexOf(address.toString()) != -1) {
2015-10-14 10:16:39 -07:00
this.soundAlarm();
}
2016-05-19 15:29:06 -07:00
};
2015-10-14 10:16:39 -07:00
/*
* soundAlarm: will launch a separate alarm program (not provided)
*/
SatoshiFireAlarm.prototype.soundAlarm = function() {
2016-05-19 15:29:06 -07:00
if (this.alarmActivated) {
return;
}
2015-10-14 10:16:39 -07:00
this.alarmActivated = true;
2018-03-27 23:55:02 -07:00
console.log('ALARM TRIGGERED!');
2016-05-19 15:29:06 -07:00
this.child = spawn('alarm', []);
};
2015-10-14 10:16:39 -07:00
SatoshiFireAlarm.prototype.resetAlarm = function() {
2016-05-19 15:29:06 -07:00
if (this.child) {
this.child.kill();
}
2015-10-14 10:16:39 -07:00
this.alarmActivated = false;
2018-03-27 23:55:02 -07:00
console.log('ALARM OFF!');
2016-05-19 15:29:06 -07:00
};
2015-10-14 10:16:39 -07:00
module.exports = SatoshiFireAlarm;
2015-10-14 10:16:39 -07:00