walletdb args.

This commit is contained in:
Christopher Jeffrey 2016-04-09 04:53:41 -07:00
parent 2d83ce9579
commit d93be7b3d7
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 88 additions and 46 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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);
});
};