Movement/Satoshi Fire Alarm


Create a Service



Please refer to the [service development document].



The Code



Add a new file within the service directory `satoshi-fire-alarm` called `index.js`


var util = require('util');

var EventEmitter = require('events').EventEmitter;

var ltzcore = require('ltzcore-lib');

var spawn = require('child_process').spawn;



function SatoshiFireAlarm(options) {

 EventEmitter.call(this, options);

 this.node = options.node;



 this.alarmActivated = false;

 this.child = false;

 this.interestingAddresses = [

  'L17opZaVcRK4h9MV4KhkCmzUBa56BxSRb8A', //this is the address that the genesis paid its coinbase to.

 ];

 this.node.services.litecoinzd.on('tx', this.transactionHandler.bind(this));

}



/*

* We are going to need litecoinzd because we will be setting event listeners (subscribers)

* on Blocks and such

*/

SatoshiFireAlarm.dependencies = ['litecoinzd'];



/*

* inherits the serivce base class so we get some stuff for free

*/

util.inherits(SatoshiFireAlarm, EventEmitter);



/*

* start: REQUIRED!! Ours just calls the callback

*/

SatoshiFireAlarm.prototype.start = function(callback) {

 callback();

};



/*

* stop: REQUIRED!! Ours just calls the callback

*/

SatoshiFireAlarm.prototype.stop = function(callback) {

 callback();

};



SatoshiFireAlarm.prototype.getAPIMethods = function() {

 return [];

};



SatoshiFireAlarm.prototype.getPublishEvents = function() {

 return [];

};



/*

* transactionHandler: this is the delegate when a transaction is received by your node

*/

SatoshiFireAlarm.prototype.transactionHandler = function(txBuffer) {

 var self = this;



 var tx = ltzcore.Transaction().fromBuffer(txBuffer);



 for (var i = 0; i < tx.inputs.length; i++) {

  self.transactionInputHandler(tx.inputs[i]);

 }



};



/*

* transactionInputHandler: helper for transactionHandler

*/

SatoshiFireAlarm.prototype.transactionInputHandler = function(input) {

 if (!input.script) {

  return;

 }

 var address = input.script.toAddress(this.node.network);

 if (address && this.interestingAddresses.indexOf(address.toString()) != -1) {

  this.soundAlarm();

 }

};



/*

* soundAlarm: will launch a separate alarm program (not provided)

*/

SatoshiFireAlarm.prototype.soundAlarm = function() {

 if (this.alarmActivated) {

  return;

 }



 this.alarmActivated = true;

 this.child = spawn('alarm', []);

};



SatoshiFireAlarm.prototype.resetAlarm = function() {

 if (this.child) {

  this.child.kill();

 }

 this.alarmActivated = false;

};



module.exports = SatoshiFireAlarm;


Create a `package.json` for our service in `satoshi-fire-alarm`:


npm init

npm install ltzcore-lib --save


Start the Node ( .sh )



ltzcore-node start


Wait for LTZtoshi to Spend ;)



Just kidding, this example can be used to inform you about ANY litecoinz address. Try it by inserting your own address and transferring litecoinz in or out.