walletdb args.
This commit is contained in:
parent
2d83ce9579
commit
d93be7b3d7
@ -257,7 +257,7 @@ Fullnode.prototype.destroy = function destroy(callback) {
|
|||||||
Fullnode.prototype.createWallet = function createWallet(options, callback) {
|
Fullnode.prototype.createWallet = function createWallet(options, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
callback = utils.ensure(callback);
|
callback = utils.ensure(callback);
|
||||||
this.walletdb.create(options, function(err, wallet) {
|
this.walletdb.ensure(options, function(err, wallet) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
|
|||||||
@ -296,7 +296,7 @@ NodeServer.prototype._init = function _init() {
|
|||||||
|
|
||||||
// Create/get wallet
|
// Create/get wallet
|
||||||
this.post(['/wallet', '/wallet/:id'], function(req, res, next, send) {
|
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;
|
var json;
|
||||||
|
|
||||||
if (err)
|
if (err)
|
||||||
|
|||||||
@ -197,7 +197,7 @@ SPVNode.prototype.destroy = function destroy(callback) {
|
|||||||
SPVNode.prototype.createWallet = function createWallet(options, callback) {
|
SPVNode.prototype.createWallet = function createWallet(options, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
callback = utils.ensure(callback);
|
callback = utils.ensure(callback);
|
||||||
this.walletdb.create(options, function(err, wallet) {
|
this.walletdb.ensure(options, function(err, wallet) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
|
|||||||
@ -12,6 +12,7 @@ var EventEmitter = require('events').EventEmitter;
|
|||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
var assert = utils.assert;
|
var assert = utils.assert;
|
||||||
var DUMMY = new Buffer([0]);
|
var DUMMY = new Buffer([0]);
|
||||||
|
var network = bcoin.protocol.network;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WalletDB
|
* WalletDB
|
||||||
@ -336,11 +337,10 @@ WalletDB.prototype.removeJSON = function removeJSON(id, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
WalletDB.prototype._getDB = function _getDB(id, callback) {
|
WalletDB.prototype._getDB = function _getDB(id, callback) {
|
||||||
var key;
|
var key = 'w/w/' + id;
|
||||||
|
|
||||||
callback = utils.ensure(callback);
|
callback = utils.ensure(callback);
|
||||||
|
|
||||||
key = 'w/w/' + id;
|
|
||||||
|
|
||||||
this.db.get(key, function(err, json) {
|
this.db.get(key, function(err, json) {
|
||||||
if (err && err.type === 'NotFoundError')
|
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);
|
callback = utils.ensure(callback);
|
||||||
|
|
||||||
if (options instanceof bcoin.wallet)
|
assert(options instanceof bcoin.wallet);
|
||||||
options = options.toJSON();
|
options = options.toJSON();
|
||||||
|
|
||||||
this.saveJSON(options.id, options, callback);
|
this.saveJSON(id, options, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletDB.prototype.remove = function remove(id, callback) {
|
WalletDB.prototype.remove = function remove(id, callback) {
|
||||||
callback = utils.ensure(callback);
|
if (id instanceof bcoin.wallet)
|
||||||
|
|
||||||
if (id instanceof bcoin.wallet) {
|
|
||||||
id.destroy();
|
id.destroy();
|
||||||
|
|
||||||
|
if (id && id.id)
|
||||||
id = id.id;
|
id = id.id;
|
||||||
}
|
|
||||||
|
callback = utils.ensure(callback);
|
||||||
|
|
||||||
return this.removeJSON(id, callback);
|
return this.removeJSON(id, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletDB.prototype.create = function create(options, callback) {
|
WalletDB.prototype.create = function create(id, options, callback) {
|
||||||
var self = this;
|
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);
|
callback = utils.ensure(callback);
|
||||||
|
|
||||||
function getJSON(id, callback) {
|
function create(err, json) {
|
||||||
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) {
|
|
||||||
var wallet;
|
var wallet;
|
||||||
|
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (json) {
|
if (json)
|
||||||
try {
|
return callback(new Error('`' + id + '` already exists.'), null, json);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
options.provider = new Provider(self);
|
if (network.type === 'segnet3' || network.type === 'segnet4')
|
||||||
wallet = new bcoin.wallet(options);
|
options.witness = options.witness !== false;
|
||||||
self.saveJSON(wallet.id, wallet.toJSON(), done);
|
|
||||||
}
|
|
||||||
|
|
||||||
function done(err) {
|
options.provider = new Provider(self);
|
||||||
|
wallet = new bcoin.wallet(options);
|
||||||
|
|
||||||
|
self.saveJSON(wallet.id, wallet.toJSON(), function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
return callback(null, wallet);
|
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);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user