Updates for bitcore v4

This commit is contained in:
Braydon Fuller 2016-05-19 18:29:06 -04:00
parent d8e66d23e0
commit 3339e292b4
4 changed files with 69 additions and 32 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

View File

@ -1,14 +1,16 @@
var index = require('../../node_modules/bitcore-node'); 'use strict';
var log = index.log;
var util = require('util'); var util = require('util');
var Service = require('../../node_modules/bitcore-node/lib/service'); var EventEmitter = require('events').EventEmitter;
var Transaction = require('../../node_modules/bitcore-node/lib/transaction'); var bitcore = require('bitcore-lib');
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;
function SatoshiCoins(options) { function SatoshiCoins(options) {
Service.call(this, options); EventEmitter.call(this, options);
this.node = options.node;
this.alarmActivated = false; this.alarmActivated = false;
this.child; this.child = false;
this.interestingAddresses = [ 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. '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 '12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX', //Block 1
@ -21,68 +23,80 @@ function SatoshiCoins(options) {
* We are going to need bitcoind because we will be setting event listeners (subscribers) * We are going to need bitcoind because we will be setting event listeners (subscribers)
* on Blocks and such * on Blocks and such
*/ */
SatoshiCoins.dependencies = ['bitcoind', 'db', 'address']; SatoshiCoins.dependencies = ['bitcoind'];
/* /*
* inherits the serivce base class so we get some stuff for free * inherits the serivce base class so we get some stuff for free
*/ */
util.inherits(SatoshiCoins, Service); util.inherits(SatoshiCoins, EventEmitter);
/* /*
* start: REQUIRED!! Ours just calls the callback * start: REQUIRED!! Ours just calls the callback
*/ */
SatoshiCoins.prototype.start = function(callback) { SatoshiCoins.prototype.start = function(callback) {
callback(); callback();
} };
/* /*
* stop: REQUIRED!! Ours just calls the callback * stop: REQUIRED!! Ours just calls the callback
*/ */
SatoshiCoins.prototype.stop = function(callback) { SatoshiCoins.prototype.stop = function(callback) {
callback(); callback();
} };
SatoshiCoins.prototype.getAPIMethods = function() {
return [];
};
SatoshiCoins.prototype.getPublishEvents = function() {
return [];
};
/* /*
* transactionHandler: this is the delegate when a transaction is received by your node * transactionHandler: this is the delegate when a transaction is received by your node
*/ */
SatoshiCoins.prototype.transactionHandler = function(txinfo) { SatoshiCoins.prototype.transactionHandler = function(txBuffer) {
var tx = bitcore.Transaction().fromBuffer(txInfo.buffer); var self = this;
var messages = {}; var tx = bitcore.Transaction().fromBuffer(txBuffer);
var inputsLength = tx.inputs.length; for (var i = 0; i < tx.inputs.length; i++) {
for (var i = 0; i < inputsLength; i++) { self.transactionInputHandler(tx.inputs[i]);
this.transactionInputHandler(tx, i);
} }
} };
/* /*
* transactionInputHandler: helper for transactionHandler * transactionInputHandler: helper for transactionHandler
*/ */
SatoshiCoins.prototype.transactionInputHandler = function(tx, i) { SatoshiCoins.prototype.transactionInputHandler = function(input) {
var address = tx.inputs[i].script.toAddress(); if (!input.script) {
return;
if (typeof address !== 'undefined' && }
this.interestingAddresses.indexOf(address) != -1) { var address = input.script.toAddress(this.node.network);
if (address && this.interestingAddresses.indexOf(address.toString()) != -1) {
this.soundAlarm(); this.soundAlarm();
} }
} };
/* /*
* soundAlarm: will launch a separate alarm program (not provided) * soundAlarm: will launch a separate alarm program (not provided)
*/ */
SatoshiCoins.prototype.soundAlarm = function() { SatoshiCoins.prototype.soundAlarm = function() {
if (this.alarmActivated) return; if (this.alarmActivated) {
return;
}
this.alarmActivated = true; this.alarmActivated = true;
var child = spawn('alarm', []); this.child = spawn('alarm', []);
} };
SatoshiCoins.prototype.resetAlarm = function() { SatoshiCoins.prototype.resetAlarm = function() {
child.kill(); if (this.child) {
this.child.kill();
}
this.alarmActivated = false; this.alarmActivated = false;
} };
module.exports = SatoshiCoins; module.exports = SatoshiCoins;

26
package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "satoshi-fire-alarm",
"version": "1.0.0",
"description": "Tutorial for Bitcore",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/bitpay/satoshi-fire-alarm.git"
},
"keywords": [
"bitcore"
],
"author": "BitPay",
"license": "MIT",
"bugs": {
"url": "https://github.com/bitpay/satoshi-fire-alarm/issues"
},
"homepage": "https://github.com/bitpay/satoshi-fire-alarm#readme",
"private": true,
"dependencies": {
"bitcore-lib": "^0.13.15"
}
}

View File

@ -1,4 +0,0 @@
{
"dependencies": {
}
}