diff --git a/app/controllers/transactions.js b/app/controllers/transactions.js index e7b09ad..4b1ac98 100644 --- a/app/controllers/transactions.js +++ b/app/controllers/transactions.js @@ -8,11 +8,19 @@ var async = require('async'); var common = require('./common'); var TransactionDb = require('../../lib/TransactionDb').class(); -var BlockDb = require('../../lib/BlockDb').class(); +var BlockDb = require('../../lib/BlockDb').class(); +var Rpc = require('../../lib/Rpc').class(); var tDb = new TransactionDb(); var bdb = new BlockDb(); +exports.send = function(req, res) { + Rpc.sendRawTransaction(req.body.rawtx, function(err, txid) { + if (err) return common.handleErrors(err, res); + res.json({'txid' : txid}); + }); +}; + /** * Find transaction by hash ... diff --git a/config/routes.js b/config/routes.js index 8d35cd4..4ccb56e 100644 --- a/config/routes.js +++ b/config/routes.js @@ -25,6 +25,7 @@ module.exports = function(app) { app.get(apiPrefix + '/tx/:txid', transactions.show); app.param('txid', transactions.transaction); app.get(apiPrefix + '/txs', transactions.list); + app.post(apiPrefix + '/tx/send', transactions.send); // Address routes var addresses = require('../app/controllers/addresses'); diff --git a/lib/Rpc.js b/lib/Rpc.js index 9a46593..466690b 100644 --- a/lib/Rpc.js +++ b/lib/Rpc.js @@ -96,6 +96,17 @@ function spec(b) { return cb(err,info.result); }); }; + + Rpc.sendRawTransaction = function(rawtx, cb) { + var self = this; + bitcoreRpc.sendRawTransaction(rawtx, function(err, txid) { + if (err && err.code === -5) return cb(err); // transaction already in block chain + if (err) return cb(self.errMsg(err)); + + return cb(err, txid.result); + }); + }; + return Rpc; } module.defineClass(spec);