diff --git a/lib/bcoin/fullnode.js b/lib/bcoin/fullnode.js index 7d941ed2..3867bdaa 100644 --- a/lib/bcoin/fullnode.js +++ b/lib/bcoin/fullnode.js @@ -257,7 +257,7 @@ Fullnode.prototype.destroy = function destroy(callback) { Fullnode.prototype.createWallet = function createWallet(options, callback) { var self = this; callback = utils.ensure(callback); - this.walletdb.create(options, function(err, wallet) { + this.walletdb.ensure(options, function(err, wallet) { if (err) return callback(err); diff --git a/lib/bcoin/http/server.js b/lib/bcoin/http/server.js index 49c65bb4..9eecd222 100644 --- a/lib/bcoin/http/server.js +++ b/lib/bcoin/http/server.js @@ -296,7 +296,7 @@ NodeServer.prototype._init = function _init() { // Create/get wallet this.post(['/wallet', '/wallet/:id'], function(req, res, next, send) { - self.walletdb.create(req.options, function(err, wallet) { + self.walletdb.ensure(req.options, function(err, wallet) { var json; if (err) diff --git a/lib/bcoin/spvnode.js b/lib/bcoin/spvnode.js index eb0ab94b..cc1d93d4 100644 --- a/lib/bcoin/spvnode.js +++ b/lib/bcoin/spvnode.js @@ -197,7 +197,7 @@ SPVNode.prototype.destroy = function destroy(callback) { SPVNode.prototype.createWallet = function createWallet(options, callback) { var self = this; callback = utils.ensure(callback); - this.walletdb.create(options, function(err, wallet) { + this.walletdb.ensure(options, function(err, wallet) { if (err) return callback(err); diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index a6738f7f..1ab98972 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -12,6 +12,7 @@ var EventEmitter = require('events').EventEmitter; var utils = require('./utils'); var assert = utils.assert; var DUMMY = new Buffer([0]); +var network = bcoin.protocol.network; /** * WalletDB @@ -336,11 +337,10 @@ WalletDB.prototype.removeJSON = function removeJSON(id, callback) { }; WalletDB.prototype._getDB = function _getDB(id, callback) { - var key; + var key = 'w/w/' + id; callback = utils.ensure(callback); - key = 'w/w/' + id; this.db.get(key, function(err, json) { if (err && err.type === 'NotFoundError') @@ -425,75 +425,117 @@ WalletDB.prototype.get = function get(id, passphrase, callback) { }); }; -WalletDB.prototype.save = function save(options, callback) { +WalletDB.prototype.save = function save(id, options, callback) { + if (id && typeof id === 'object') { + callback = options; + options = id; + id = null; + } + + if (!id) + id = options.id; + else + options.id = id; + callback = utils.ensure(callback); - if (options instanceof bcoin.wallet) - options = options.toJSON(); + assert(options instanceof bcoin.wallet); + options = options.toJSON(); - this.saveJSON(options.id, options, callback); + this.saveJSON(id, options, callback); }; WalletDB.prototype.remove = function remove(id, callback) { - callback = utils.ensure(callback); - - if (id instanceof bcoin.wallet) { + if (id instanceof bcoin.wallet) id.destroy(); + + if (id && id.id) id = id.id; - } + + callback = utils.ensure(callback); return this.removeJSON(id, callback); }; -WalletDB.prototype.create = function create(options, callback) { +WalletDB.prototype.create = function create(id, options, callback) { var self = this; + if (id && typeof id === 'object') { + callback = options; + options = id; + id = null; + } + + if (!id) + id = options.id; + else + options.id = id; + callback = utils.ensure(callback); - function getJSON(id, callback) { - if (!id) - return callback(); - - return self.getJSON(id, function(err, json) { - if (err) - return callback(err); - - return callback(null, json); - }); - } - - return getJSON(options.id, function(err, json) { + function create(err, json) { var wallet; if (err) return callback(err); - if (json) { - try { - options = bcoin.wallet._fromJSON(json, options.passphrase); - options.provider = new Provider(self); - wallet = new bcoin.wallet(options); - } catch (e) { - return callback(e); - } - done(); - } else { - if (bcoin.protocol.network.type === 'segnet3' - || bcoin.protocol.network.type === 'segnet4') { - options.witness = options.witness !== false; - } + if (json) + return callback(new Error('`' + id + '` already exists.'), null, json); - options.provider = new Provider(self); - wallet = new bcoin.wallet(options); - self.saveJSON(wallet.id, wallet.toJSON(), done); - } + if (network.type === 'segnet3' || network.type === 'segnet4') + options.witness = options.witness !== false; - function done(err) { + options.provider = new Provider(self); + wallet = new bcoin.wallet(options); + + self.saveJSON(wallet.id, wallet.toJSON(), function(err) { if (err) return callback(err); return callback(null, wallet); + }); + } + + if (!id) + return create(); + + return this.getJSON(id, create); +}; + +WalletDB.prototype.ensure = function ensure(id, options, callback) { + var self = this; + + if (id && typeof id === 'object') { + callback = options; + options = id; + id = null; + } + + if (!id) + id = options.id; + else + options.id = id; + + callback = utils.ensure(callback); + + return this.create(id, options, function(err, wallet, json) { + if (err && !json) + return callback(err); + + if (wallet) + return callback(null, wallet); + + assert(json); + + try { + options = bcoin.wallet._fromJSON(json, options.passphrase); + options.provider = new Provider(self); + wallet = new bcoin.wallet(options); + } catch (e) { + return callback(e); } + + return callback(null, wallet); }); };