Skip to content

Commit 576d171

Browse files
committed
Merge pull request #234 from braydonf/sendtxevent
Bindings: Send transaction emit "tx" events.
2 parents a0d1646 + cf1c01a commit 576d171

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

integration/regtest.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,24 @@ describe('Daemon Binding Functionality', function() {
291291
}).should.throw('\x10: mandatory-script-verify-flag-failed (Operation not valid with the current stack size)');
292292
});
293293

294+
it('will emit "tx" events', function(done) {
295+
var tx = bitcore.Transaction();
296+
tx.from(utxos[2]);
297+
tx.change(privateKey.toAddress());
298+
tx.to(destKey.toAddress(), utxos[2].amount * 1e8 - 1000);
299+
tx.sign(bitcore.PrivateKey.fromWIF(utxos[2].privateKeyWIF));
300+
301+
var serialized = tx.serialize();
302+
303+
bitcoind.on('tx', function(result) {
304+
result.buffer.toString('hex').should.equal(serialized);
305+
result.hash.should.equal(tx.hash);
306+
result.mempool.should.equal(true);
307+
done();
308+
});
309+
bitcoind.sendTransaction(serialized);
310+
});
311+
294312
});
295313

296314
describe('fee estimation', function() {

src/libbitcoind.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ init(Handle<Object>);
9696
static std::vector<CDataStream> txmon_messages;
9797
static uv_async_t txmon_async;
9898
static Eternal<Function> txmon_callback;
99+
static bool txmon_callback_available;
99100

100101
static volatile bool shutdown_complete = false;
101102
static char *g_data_dir = NULL;
@@ -219,6 +220,7 @@ NAN_METHOD(StartTxMon) {
219220
Local<Function> callback = Local<Function>::Cast(args[0]);
220221
Eternal<Function> cb(isolate, callback);
221222
txmon_callback = cb;
223+
txmon_callback_available = true;
222224

223225
CNodeSignals& nodeSignals = GetNodeSignals();
224226
nodeSignals.ProcessMessages.connect(&scan_messages, boost::signals2::at_front);
@@ -1499,6 +1501,32 @@ NAN_METHOD(SendTransaction) {
14991501
// Relay the transaction connect peers
15001502
RelayTransaction(tx);
15011503

1504+
// Notify any listeners about the transaction
1505+
if(txmon_callback_available) {
1506+
1507+
Local<Array> results = Array::New(isolate);
1508+
Local<Object> obj = NanNew<Object>();
1509+
1510+
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
1511+
ssTx << tx;
1512+
std::string stx = ssTx.str();
1513+
Local<Value> txBuffer = node::Buffer::New(isolate, stx.c_str(), stx.size());
1514+
1515+
obj->Set(NanNew<String>("buffer"), txBuffer);
1516+
obj->Set(NanNew<String>("hash"), NanNew<String>(hashTx.GetHex()));
1517+
obj->Set(NanNew<String>("mempool"), NanNew<Boolean>(true));
1518+
1519+
results->Set(0, obj);
1520+
1521+
const unsigned argc = 1;
1522+
Local<Value> argv[argc] = {
1523+
Local<Value>::New(isolate, results)
1524+
};
1525+
Local<Function> cb = txmon_callback.Get(isolate);
1526+
1527+
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
1528+
}
1529+
15021530
NanReturnValue(Local<Value>::New(isolate, NanNew<String>(hashTx.GetHex())));
15031531
}
15041532

0 commit comments

Comments
 (0)