address-watch/index.js

103 lines
2.4 KiB
JavaScript
Raw 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;
var bitcore = require('bitcore-lib');
2015-10-14 10:16:39 -07:00
var spawn = require('child_process').spawn;
function SatoshiCoins(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 = [
'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
];
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
*/
2016-05-19 15:29:06 -07:00
SatoshiCoins.dependencies = ['bitcoind'];
2015-10-14 10:16:39 -07:00
/*
* inherits the serivce base class so we get some stuff for free
*/
2016-05-19 15:29:06 -07:00
util.inherits(SatoshiCoins, EventEmitter);
2015-10-14 10:16:39 -07:00
/*
* start: REQUIRED!! Ours just calls the callback
*/
SatoshiCoins.prototype.start = function(callback) {
callback();
2016-05-19 15:29:06 -07:00
};
2015-10-14 10:16:39 -07:00
/*
* stop: REQUIRED!! Ours just calls the callback
*/
SatoshiCoins.prototype.stop = function(callback) {
callback();
2016-05-19 15:29:06 -07:00
};
SatoshiCoins.prototype.getAPIMethods = function() {
return [];
};
SatoshiCoins.prototype.getPublishEvents = function() {
return [];
};
2015-10-14 10:16:39 -07:00
/*
* transactionHandler: this is the delegate when a transaction is received by your node
*/
2016-05-19 15:29:06 -07:00
SatoshiCoins.prototype.transactionHandler = function(txBuffer) {
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
*/
2016-05-19 15:29:06 -07:00
SatoshiCoins.prototype.transactionInputHandler = function(input) {
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)
*/
SatoshiCoins.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;
2016-05-19 15:29:06 -07:00
this.child = spawn('alarm', []);
};
2015-10-14 10:16:39 -07:00
SatoshiCoins.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;
2016-05-19 15:29:06 -07:00
};
2015-10-14 10:16:39 -07:00
module.exports = SatoshiCoins;