dont save wallet until it has an id.

This commit is contained in:
Christopher Jeffrey 2016-03-02 04:10:07 -08:00
parent c8f5913477
commit 8d8952add7
2 changed files with 25 additions and 4 deletions

View File

@ -105,6 +105,8 @@ Wallet.prototype._init = function _init() {
this.id = this.getID();
this.emit('init');
if (Object.keys(this.addressMap).length === 0) {
for (i = 0; i < this.receiveDepth - 1; i++)
this.deriveReceive(i);
@ -127,6 +129,12 @@ Wallet.prototype._init = function _init() {
assert(this.changeAddress.change);
};
Wallet.prototype.onInit = function onInit(callback) {
if (this._initialized)
return callback();
this.once('init', callback);
};
Wallet.prototype.addKey = function addKey(key) {
var has, i;

View File

@ -291,11 +291,18 @@ WalletDB.prototype.save = function save(options, callback) {
options.on('add address', self._onAddress(options, options.id));
options.provider = self;
}
if (options instanceof bcoin.wallet)
options = options.toJSON();
options.onInit(function() {
if (options instanceof bcoin.wallet)
options = options.toJSON();
self.saveJSON(options.id, options, function(err) {
if (err)
self.emit('error', err);
});
});
return callback(null, options);
}
return this.saveJSON(options.id, options, callback);
this.saveJSON(options.id, options, callback);
};
WalletDB.prototype.remove = function remove(id, callback) {
@ -346,7 +353,13 @@ WalletDB.prototype.create = function create(options, callback) {
} else {
options.provider = self;
wallet = new bcoin.wallet(options);
self.saveJSON(wallet.id, wallet.toJSON(), done);
wallet.onInit(function() {
self.saveJSON(wallet.id, wallet.toJSON(), function(err) {
if (err)
self.emit('error', err);
});
});
return done();
}
function done(err) {