diff --git a/lib/bcoin/fullnode.js b/lib/bcoin/fullnode.js index 2286124e..51adf150 100644 --- a/lib/bcoin/fullnode.js +++ b/lib/bcoin/fullnode.js @@ -98,6 +98,7 @@ Fullnode.prototype._init = function _init() { }); this.on('tx', function(tx) { + return; self.walletdb.addTX(tx, function(err) { if (err) self.emit('error', err); diff --git a/lib/bcoin/http/client.js b/lib/bcoin/http/client.js index c29ebbbe..710153ce 100644 --- a/lib/bcoin/http/client.js +++ b/lib/bcoin/http/client.js @@ -13,14 +13,13 @@ var request = require('./request'); * Client */ -function Client(uri, passphrase) { +function Client(uri) { if (!(this instanceof Client)) return new Client(uri); EventEmitter.call(this); this.uri = uri; - this.passphrase = passphrase; this.loaded = false; this.id = null; this._init(); @@ -142,9 +141,6 @@ Client.prototype._request = function _request(method, endpoint, json, callback) json = null; } - if (json) - json.passphrase = this.passphrase; - if (json && method === 'get') { json = null; query = json; @@ -193,6 +189,50 @@ Client.prototype._del = function _del(endpoint, json, callback) { return this._request('delete', endpoint, json, callback); }; +Client.prototype._createWallet = function createWallet(options, callback) { + return this._post('/wallet', options, callback); +}; + +Client.prototype._getWallet = function getWallet(id, callback) { + return this._get('/wallet/' + id, callback); +}; + +Client.prototype.getWallet = function getWallet(id, passphrase, callback) { + var provider; + + return this._getWallet(id, function(err, json) { + if (err) + return callback(err); + + try { + json = bcoin.wallet._fromJSON(json, passphrase); + } catch (e) { + return callback(e); + } + + json.provider = new bcoin.http.provider(self.url); + + return callback(null, new bcoin.wallet(json)); + }); +}; + +Client.prototype.createWallet = function createWallet(options, callback) { + return this._createWallet(options, function(err) { + if (err) + return callback(err); + + try { + json = bcoin.wallet._fromJSON(json, options.passphrase); + } catch (e) { + return callback(e); + } + + json.provider = new bcoin.http.provider(self.url); + + return callback(null, new bcoin.wallet(json)); + }); +}; + Client.prototype.getWalletAll = function getWalletAll(id, callback) { return this._get('/wallet/' + id + '/tx/all', function(err, body) { if (err) @@ -317,7 +357,7 @@ Client.prototype.getWalletTX = function getTX(id, hash, callback) { return callback(err); if (!body) - return callback(null, []); + return callback(); try { body = bcoin.tx.fromJSON(body); @@ -340,7 +380,7 @@ Client.prototype.getWalletCoin = function getCoin(id, hash, index, callback) { return callback(err); if (!body) - return callback(null, []); + return callback(); try { body = bcoin.coin.fromJSON(body); diff --git a/lib/bcoin/http/server.js b/lib/bcoin/http/server.js index 5a484774..f121e9c1 100644 --- a/lib/bcoin/http/server.js +++ b/lib/bcoin/http/server.js @@ -42,8 +42,10 @@ NodeServer.prototype._init = function _init() { 'Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE'); - if (req.method === 'OPTIONS') - return send(200); + if (req.method === 'OPTIONS') { + res.statusCode = 200; + return res.end(); + } next(); }); @@ -100,9 +102,6 @@ NodeServer.prototype._init = function _init() { options.addresses = params.addresses; } - if (params.passphrase) - options.passphrase = params.passphrase; - if (params.tx) { try { options.tx = bcoin.tx.fromRaw(params.tx, 'hex'); @@ -111,6 +110,15 @@ NodeServer.prototype._init = function _init() { } } + if (params.now) + options.now = params.now >>> 0; + + if (params.age) + options.age = params.age >>> 0; + + if (params.passphrase) + options.passphrase = params.passphrase; + if (params.bin) options.bin = true; @@ -121,7 +129,7 @@ NodeServer.prototype._init = function _init() { this.get('/', function(req, res, next, send) { send(200, { - version: require('../../../package.json').version, + version: constants.userAgent, network: self.node.network.type }); }); @@ -135,7 +143,9 @@ NodeServer.prototype._init = function _init() { if (!coins.length) return send(404); - send(200, coins.map(function(coin) { return coin.toJSON(); })); + send(200, coins.map(function(coin) { + return coin.toJSON(); + })); }); }); @@ -161,7 +171,9 @@ NodeServer.prototype._init = function _init() { if (!coins.length) return send(404); - send(200, coins.map(function(coin) { return coin.toJSON(); })); + send(200, coins.map(function(coin) { + return coin.toJSON(); + })); }); }); @@ -187,7 +199,9 @@ NodeServer.prototype._init = function _init() { if (!txs.length) return send(404); - send(200, txs.map(function(tx) { return tx.toJSON(); })); + send(200, txs.map(function(tx) { + return tx.toJSON(); + })); }); }); @@ -200,7 +214,9 @@ NodeServer.prototype._init = function _init() { if (!txs.length) return send(404); - send(200, txs.map(function(tx) { return tx.toJSON(); })); + send(200, txs.map(function(tx) { + return tx.toJSON(); + })); }); }); @@ -249,8 +265,11 @@ NodeServer.prototype._init = function _init() { }); }); + // Send TX this.post('/wallet/:id/send', function(req, res, next, send) { - self.walletdb.get(req.options.id, req.options.passphrase, function(err, wallet) { + var id = req.options.id; + var passphrase = req.options.passphrase; + self.walletdb.get(id, passphrase, function(err, wallet) { if (err) return next(err); @@ -276,6 +295,7 @@ NodeServer.prototype._init = function _init() { }); }); + // Zap Wallet TXs this.post('/wallet/:id/zap', function(req, res, next, send) { var id = req.options.id; var now = req.options.now; @@ -285,7 +305,9 @@ NodeServer.prototype._init = function _init() { if (err) return next(err); - send(200, { success: true }); + send(200, { + success: true + }); }); }); @@ -299,7 +321,9 @@ NodeServer.prototype._init = function _init() { if (err) return next(err); - send(200, { success: true }); + send(200, { + success: true + }); }); }); @@ -328,13 +352,17 @@ NodeServer.prototype._init = function _init() { if (!coins.length) return send(404); - send(200, coins.map(function(coin) { return coin.toJSON(); })); + send(200, coins.map(function(coin) { + return coin.toJSON(); + })); }); }); // Wallet TX this.get('/wallet/:id/coin/:hash/:index', function(req, res, next, send) { - self.walletdb.getCoin(req.options.hash, req.options.index, function(err, coin) { + var hash = req.options.hash; + var index = req.options.index; + self.walletdb.getCoin(hash, index, function(err, coin) { if (err) return next(err); @@ -354,7 +382,9 @@ NodeServer.prototype._init = function _init() { if (!txs.length) return send(404); - send(200, txs.map(function(tx) { return tx.toJSON(); })); + send(200, txs.map(function(tx) { + return tx.toJSON(); + })); }); }); @@ -367,7 +397,9 @@ NodeServer.prototype._init = function _init() { if (!txs.length) return send(404); - send(200, txs.map(function(tx) { return tx.toJSON(); })); + send(200, txs.map(function(tx) { + return tx.toJSON(); + })); }); }); @@ -380,20 +412,26 @@ NodeServer.prototype._init = function _init() { if (!txs.length) return send(404); - send(200, txs.map(function(tx) { return tx.toJSON(); })); + send(200, txs.map(function(tx) { + return tx.toJSON(); + })); }); }); // Wallet TXs within time range this.get('/wallet/:id/tx/last', function(req, res, next, send) { - self.walletdb.getRange(req.options.id, req.options.limit, function(err, txs) { + var id = req.options.id; + var limit = req.options.limit; + self.walletdb.getRange(id, limit, function(err, txs) { if (err) return next(err); if (!txs.length) return send(404); - send(200, txs.map(function(tx) { return tx.toJSON(); })); + send(200, txs.map(function(tx) { + return tx.toJSON(); + })); }); }); @@ -416,7 +454,9 @@ NodeServer.prototype._init = function _init() { if (err) return callback(err); - send(200, { success: true }); + send(200, { + success: true + }); }); });