diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 4df61556..e347a98d 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -85,6 +85,8 @@ function Wallet(options) { : this.master.deriveAccount44(this.accountIndex); } + this.id = this.getID(); + this.addKey(this.accountKey); (options.keys || []).forEach(function(key) { @@ -100,13 +102,8 @@ Wallet.prototype._init = function _init() { var addr, i; assert(!this._initialized); - this._initialized = true; - 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); @@ -129,12 +126,6 @@ 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; @@ -234,13 +225,21 @@ Wallet.prototype._finalizeKeys = function _finalizeKeys(key) { }; // Wallet ID: -// bip45: Purpose key address -// bip44: Account key address +// bip45: Purpose key "address" (prefix: WLT) +// bip44: Account key "address" (prefix: WLT) Wallet.prototype.getID = function getID() { + var publicKey = this.accountKey.publicKey; + var id; + if (this.options.id) return this.options.id; - return bcoin.address.compileData(this.accountKey.publicKey); + id = new Buffer(27); + utils.copy(new Buffer([0x03, 0xbe, 0x04]), id, 0); + utils.copy(utils.ripesha(publicKey), id, 3); + utils.copy(utils.checksum(id.slice(0, 23)), id, 23); + + return utils.toBase58(id); }; Wallet.prototype.createReceive = function createReceive() { @@ -426,7 +425,7 @@ Wallet.prototype.ownInput = function ownInput(tx, index) { }; Wallet.prototype.ownOutput = function ownOutput(tx, index) { - if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin)) + if (tx instanceof bcoin.output) return tx.test(this.addressMap); return tx.testOutputs(this.addressMap, index); diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 9ec12eff..c9b03272 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -291,15 +291,8 @@ WalletDB.prototype.save = function save(options, callback) { options.on('add address', self._onAddress(options, options.id)); options.provider = self; } - 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); + if (options instanceof bcoin.wallet) + options = options.toJSON(); } this.saveJSON(options.id, options, callback); @@ -353,13 +346,7 @@ WalletDB.prototype.create = function create(options, callback) { } else { options.provider = self; wallet = new bcoin.wallet(options); - wallet.onInit(function() { - self.saveJSON(wallet.id, wallet.toJSON(), function(err) { - if (err) - self.emit('error', err); - }); - }); - return done(); + self.saveJSON(wallet.id, wallet.toJSON(), done); } function done(err) { @@ -415,6 +402,7 @@ WalletDB.prototype.getAll = function getAll(id, callback) { return this.getAddresses(id, function(err, addresses) { if (err) return callback(err); + return self.tx.getAllByAddress(addresses, callback); }); }; @@ -425,36 +413,7 @@ WalletDB.prototype.getUnspent = function getUnspent(id, callback) { if (err) return callback(err); - return self.tx.getUnspentByAddress(addresses, function(err, unspent) { - if (err) - return callback(err); - - if (unspent.length === 0) - return callback(null, unspent); - - // Warning: won't work with encrypted wallets - self.get(id, null, function(err, wallet) { - var res = false; - - if (!wallet) - return callback(null, unspent); - - unspent.forEach(function(coin) { - if (wallet.syncOutputDepth(coin)) - res = true; - }); - - if (!res) - return callback(null, wallet); - - self.save(wallet, function(err) { - if (err) - return callback(err); - - return callback(null, unspent); - }); - }); - }); + return self.tx.getUnspentByAddress(addresses, callback); }); }; @@ -463,6 +422,7 @@ WalletDB.prototype.getPending = function getPending(id, callback) { return this.getAddresses(id, function(err, addresses) { if (err) return callback(err); + return self.tx.getPendingByAddress(addresses, callback); }); }; @@ -472,6 +432,7 @@ WalletDB.prototype.getBalance = function getBalance(id, callback) { return this.getAddresses(id, function(err, addresses) { if (err) return callback(err); + return self.tx.getBalanceByAddress(addresses, callback); }); }; @@ -481,6 +442,7 @@ WalletDB.prototype.getLast = function getLast(id, callback) { return this.getAddresses(id, function(err, addresses) { if (err) return callback(err); + return self.tx.getLast(addresses, callback); }); };