This commit is contained in:
Christopher Jeffrey 2016-04-01 02:10:29 -07:00
parent 03589f1df1
commit 9e0adcfec1
3 changed files with 109 additions and 28 deletions

View File

@ -98,6 +98,7 @@ Fullnode.prototype._init = function _init() {
}); });
this.on('tx', function(tx) { this.on('tx', function(tx) {
return;
self.walletdb.addTX(tx, function(err) { self.walletdb.addTX(tx, function(err) {
if (err) if (err)
self.emit('error', err); self.emit('error', err);

View File

@ -13,14 +13,13 @@ var request = require('./request');
* Client * Client
*/ */
function Client(uri, passphrase) { function Client(uri) {
if (!(this instanceof Client)) if (!(this instanceof Client))
return new Client(uri); return new Client(uri);
EventEmitter.call(this); EventEmitter.call(this);
this.uri = uri; this.uri = uri;
this.passphrase = passphrase;
this.loaded = false; this.loaded = false;
this.id = null; this.id = null;
this._init(); this._init();
@ -142,9 +141,6 @@ Client.prototype._request = function _request(method, endpoint, json, callback)
json = null; json = null;
} }
if (json)
json.passphrase = this.passphrase;
if (json && method === 'get') { if (json && method === 'get') {
json = null; json = null;
query = json; query = json;
@ -193,6 +189,50 @@ Client.prototype._del = function _del(endpoint, json, callback) {
return this._request('delete', 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) { Client.prototype.getWalletAll = function getWalletAll(id, callback) {
return this._get('/wallet/' + id + '/tx/all', function(err, body) { return this._get('/wallet/' + id + '/tx/all', function(err, body) {
if (err) if (err)
@ -317,7 +357,7 @@ Client.prototype.getWalletTX = function getTX(id, hash, callback) {
return callback(err); return callback(err);
if (!body) if (!body)
return callback(null, []); return callback();
try { try {
body = bcoin.tx.fromJSON(body); body = bcoin.tx.fromJSON(body);
@ -340,7 +380,7 @@ Client.prototype.getWalletCoin = function getCoin(id, hash, index, callback) {
return callback(err); return callback(err);
if (!body) if (!body)
return callback(null, []); return callback();
try { try {
body = bcoin.coin.fromJSON(body); body = bcoin.coin.fromJSON(body);

View File

@ -42,8 +42,10 @@ NodeServer.prototype._init = function _init() {
'Access-Control-Allow-Methods', 'Access-Control-Allow-Methods',
'GET,HEAD,PUT,PATCH,POST,DELETE'); 'GET,HEAD,PUT,PATCH,POST,DELETE');
if (req.method === 'OPTIONS') if (req.method === 'OPTIONS') {
return send(200); res.statusCode = 200;
return res.end();
}
next(); next();
}); });
@ -100,9 +102,6 @@ NodeServer.prototype._init = function _init() {
options.addresses = params.addresses; options.addresses = params.addresses;
} }
if (params.passphrase)
options.passphrase = params.passphrase;
if (params.tx) { if (params.tx) {
try { try {
options.tx = bcoin.tx.fromRaw(params.tx, 'hex'); 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) if (params.bin)
options.bin = true; options.bin = true;
@ -121,7 +129,7 @@ NodeServer.prototype._init = function _init() {
this.get('/', function(req, res, next, send) { this.get('/', function(req, res, next, send) {
send(200, { send(200, {
version: require('../../../package.json').version, version: constants.userAgent,
network: self.node.network.type network: self.node.network.type
}); });
}); });
@ -135,7 +143,9 @@ NodeServer.prototype._init = function _init() {
if (!coins.length) if (!coins.length)
return send(404); 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) if (!coins.length)
return send(404); 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) if (!txs.length)
return send(404); 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) if (!txs.length)
return send(404); 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) { 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) if (err)
return next(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) { this.post('/wallet/:id/zap', function(req, res, next, send) {
var id = req.options.id; var id = req.options.id;
var now = req.options.now; var now = req.options.now;
@ -285,7 +305,9 @@ NodeServer.prototype._init = function _init() {
if (err) if (err)
return next(err); return next(err);
send(200, { success: true }); send(200, {
success: true
});
}); });
}); });
@ -299,7 +321,9 @@ NodeServer.prototype._init = function _init() {
if (err) if (err)
return next(err); return next(err);
send(200, { success: true }); send(200, {
success: true
});
}); });
}); });
@ -328,13 +352,17 @@ NodeServer.prototype._init = function _init() {
if (!coins.length) if (!coins.length)
return send(404); return send(404);
send(200, coins.map(function(coin) { return coin.toJSON(); })); send(200, coins.map(function(coin) {
return coin.toJSON();
}));
}); });
}); });
// Wallet TX // Wallet TX
this.get('/wallet/:id/coin/:hash/:index', function(req, res, next, send) { 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) if (err)
return next(err); return next(err);
@ -354,7 +382,9 @@ NodeServer.prototype._init = function _init() {
if (!txs.length) if (!txs.length)
return send(404); 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) if (!txs.length)
return send(404); 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) if (!txs.length)
return send(404); 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 // Wallet TXs within time range
this.get('/wallet/:id/tx/last', function(req, res, next, send) { 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) if (err)
return next(err); return next(err);
if (!txs.length) if (!txs.length)
return send(404); 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) if (err)
return callback(err); return callback(err);
send(200, { success: true }); send(200, {
success: true
});
}); });
}); });