one test passes
This commit is contained in:
parent
f8d7d1da9d
commit
837658034a
|
@ -186,6 +186,21 @@ Address.fromPubkeyHashScriptSig = function(scriptSig, network) {
|
|||
return Address.fromPubKey(scriptSig.chunks[1], network);
|
||||
};
|
||||
|
||||
//extract an address from scriptSig
|
||||
Address.fromScriptSig = function(scriptSig, network) {
|
||||
if (typeof scriptSig === 'string') {
|
||||
scriptSig = new Script(new Buffer(scriptSig, 'hex'));
|
||||
}
|
||||
if (!network)
|
||||
network = 'livenet';
|
||||
|
||||
var payload = scriptSig.chunks;
|
||||
if (scriptSig.chunks.length === 2)
|
||||
return Address.fromPubkeyHashScriptSig(scriptSig, network);
|
||||
// TODO: support other scriptSig types
|
||||
return null;
|
||||
};
|
||||
|
||||
Address.getScriptPubKeyFor = function(s) {
|
||||
return new Address(s).getScriptPubKey();
|
||||
};
|
||||
|
|
|
@ -10,13 +10,13 @@ var preconditions = require('preconditions').singleton();
|
|||
var NetworkMonitor = function(peerman) {
|
||||
preconditions.checkArgument(peerman);
|
||||
this.peerman = peerman;
|
||||
this.networkName = peerman.config.network;
|
||||
this.init();
|
||||
}
|
||||
|
||||
util.inherits(NetworkMonitor, EventEmitter);
|
||||
|
||||
NetworkMonitor.create = function(config) {
|
||||
this.config = config;
|
||||
var peerman = new PeerManager({
|
||||
network: config.networkName
|
||||
});
|
||||
|
@ -39,12 +39,12 @@ NetworkMonitor.prototype.init = function() {
|
|||
var handleTx = function(info) {
|
||||
var tx = info.message.tx;
|
||||
|
||||
var from = tx.getSendingAddresses(self.config.networkName);
|
||||
var from = tx.getSendingAddresses(self.networkName);
|
||||
for (var i = 0; i < from.length; i++) {
|
||||
var addr = from[i];
|
||||
self.emit('out:'+addr, tx);
|
||||
}
|
||||
var to = tx.getReceivingAddresses(self.config.networkName);
|
||||
var to = tx.getReceivingAddresses(self.networkName);
|
||||
for (var i = 0; i < to.length; i++) {
|
||||
var addr = to[i];
|
||||
self.emit('in:'+addr, tx);
|
||||
|
|
|
@ -634,10 +634,13 @@ Transaction.prototype.getSendingAddresses = function(networkName) {
|
|||
var ret = [];
|
||||
if (!networkName) networkName = 'livenet';
|
||||
for (var i = 0; i<this.ins.length; i++) {
|
||||
var o = this.ins[i];
|
||||
var scriptSig = o.getScript();
|
||||
if (scriptSig.getBuffer().length === 0) continue;
|
||||
var addr = Address.fromPubkeyHashScriptSig(o.getScript(), networkName).toString();
|
||||
var input = this.ins[i];
|
||||
var scriptSig = input.getScript();
|
||||
if (scriptSig.getBuffer().length === 0) {
|
||||
ret.push(null);
|
||||
continue;
|
||||
}
|
||||
var addr = Address.fromScriptSig(scriptSig, networkName).toString();
|
||||
ret.push(addr);
|
||||
}
|
||||
return ret;
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
var chai = chai || require('chai');
|
||||
var bitcore = bitcore || require('../bitcore');
|
||||
var Transaction = bitcore.Transaction;
|
||||
var NetworkMonitor = bitcore.NetworkMonitor;
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
|
@ -17,6 +18,9 @@ describe('NetworkMonitor', function() {
|
|||
};
|
||||
var fakePM = {};
|
||||
fakePM.on = nop;
|
||||
fakePM.config = {
|
||||
network: config.networkName
|
||||
};
|
||||
it('should initialze the main object', function() {
|
||||
should.exist(NetworkMonitor);
|
||||
});
|
||||
|
@ -71,27 +75,42 @@ describe('NetworkMonitor', function() {
|
|||
});
|
||||
});
|
||||
});
|
||||
var observedAddress = '2NFYBLfabKgLbgoTALYrtBQhbLjEKUcs9Go';
|
||||
describe('incoming tx event', function() {
|
||||
it('should be called on incoming transactions', function(done) {
|
||||
var nm = createConnectedNM();
|
||||
nm.incoming('n2tTCgsJPJBZZEKLiJx9KoU4idJQB37j9E', function(tx) {
|
||||
nm.incoming(observedAddress, function(tx) {
|
||||
should.exist(tx);
|
||||
done();
|
||||
});
|
||||
var fakeTX = null;
|
||||
var tx = new Transaction();
|
||||
var raw = '01000000012732117ef4663b4a7a455ff37c3af26deca57dc43f5d8e7e5440b22c11cefc8b010000006a47304402201ca8b1b33e9f7a515829b887b264b812ab499a08e0002a0fb32629bdbfbc005e0220567adbec3befee04e810e1d34bf31614e1cd397d7a6e3184f219c89562cac7a3012102f1bc222f40a7dd4348e4c2b1e88812179686305f1b56374aae891aa21929ad14ffffffff02809698000000000017a914f487a0aeae655268e2636207abe75228bfcf5631874f219800000000001976a914361d24071123fb9fd88685c877b014ff8543c24488ac00000000';
|
||||
tx.parse(new Buffer(raw, 'hex'));
|
||||
|
||||
nm.connection.emit('tx', {
|
||||
message: {
|
||||
tx: fakeTX
|
||||
tx: tx
|
||||
}
|
||||
});
|
||||
});
|
||||
it('should not be called on outgoing transactions', function() {
|
||||
it('should not be called on unrelated transactions', function(done) {
|
||||
var nm = createConnectedNM();
|
||||
nm.incoming(observedAddress, function(tx) {
|
||||
should.exist(tx);
|
||||
done();
|
||||
});
|
||||
var raw = '010000000114bae675546f758e0dbab95aa88d4db0c63e26f8fd6cbbce3a4827446d4937cf00000000700048304502201da760691f18a0ab140de1437e4bd29767b74add8cca8e38d46a2f37d9a8188f022100cbf4e121d97b4db846d236957da7fc17fd706ad47b41ae63adf953982e34f70901255121022f58491a833933a9bea80d8e820e66bee91bd8c71bfa972fe70482360b48129951aeffffffff01706f9800000000001976a91400a26ff8123593e10d0a9eba2a74db33cd69299288ac00000000';
|
||||
var tx = new Transaction();
|
||||
tx.parse(new Buffer(raw, 'hex'));
|
||||
nm.connection.emit('tx', {
|
||||
message: {
|
||||
tx: tx
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('outgoing tx event', function() {
|
||||
it('should be called on outgoing transactions', function() {
|
||||
});
|
||||
it('should not be called on incoming transactions', function() {
|
||||
});
|
||||
it('should be called on outgoing transactions', function() {});
|
||||
it('should not be called on incoming transactions', function() {});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue