From d86031c0739c24639d419170c6191dafe766ba51 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 2 Mar 2016 02:59:08 -0800 Subject: [PATCH] more refactoring. --- lib/bcoin/address.js | 79 ++++---------------------- lib/bcoin/txdb.js | 2 + lib/bcoin/wallet.js | 128 +++++++++++++++++++++--------------------- lib/bcoin/walletdb.js | 8 ++- 4 files changed, 83 insertions(+), 134 deletions(-) diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js index 9cb90260..655e78e3 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/address.js @@ -65,30 +65,6 @@ Address.prototype.getID = function getID() { return this.getKeyAddress(); }; -Address.prototype.getAll = function getAll() { - return this._wallet.getAll(this); -}; - -Address.prototype.getUnspent = function getUnspent() { - return this._wallet.getUnspent(this); -}; - -Address.prototype.getPending = function getPending() { - return this._wallet.getPending(this); -}; - -Address.prototype.getSent = function getSent() { - return this._wallet.getSent(this); -}; - -Address.prototype.getReceived = function getReceived() { - return this._wallet.getReceived(this); -}; - -Address.prototype.getBalance = function getBalance() { - return this._wallet.getBalance(this); -}; - Address.prototype.addKey = function addKey(key) { key = utils.ensureBuffer(key); @@ -125,6 +101,10 @@ Address.prototype.getPrivateKey = function getPrivateKey(enc) { return this.key.getPrivateKey(enc); }; +Address.prototype.getPublicKey = function getPublicKey(enc) { + return this.key.getPublicKey(enc); +}; + Address.prototype.getScript = function getScript() { var redeem; @@ -243,10 +223,6 @@ Address.prototype.getScriptAddress = function getScriptAddress() { return this._scriptAddress; }; -Address.prototype.getPublicKey = function getPublicKey(enc) { - return this.key.getPublicKey(enc); -}; - Address.prototype.getKeyHash = function getKeyHash() { if (this._hash) return this._hash; @@ -328,9 +304,6 @@ Address.prototype.ownInput = function ownInput(tx, index) { tx = null; } - if (tx) - this._wallet.fillPrevout(tx); - inputs = inputs.filter(function(input, i) { if (index != null && index !== i) return false; @@ -430,6 +403,14 @@ Address.prototype.sign = function sign(tx, type, index) { }, 0); }; +Address.prototype.__defineGetter__('privateKey', function() { + return this.getPrivateKey(); +}); + +Address.prototype.__defineGetter__('publicKey', function() { + return this.getPublicKey(); +}); + Address.prototype.__defineGetter__('script', function() { return this.getScript(); }); @@ -462,14 +443,6 @@ Address.prototype.__defineGetter__('programAddress', function() { return this.getProgramAddress(); }); -Address.prototype.__defineGetter__('privateKey', function() { - return this.getPrivateKey(); -}); - -Address.prototype.__defineGetter__('publicKey', function() { - return this.getPublicKey(); -}); - Address.prototype.__defineGetter__('keyHash', function() { return this.getKeyHash(); }); @@ -486,17 +459,6 @@ Address.prototype.__defineGetter__('address', function() { return this.getAddress(); }); -Address.prototype.toExplore = function toExplore() { - return { - address: this.getAddress(), - hash160: utils.toHex(this.getHash()), - received: this.getReceived(), - sent: this.getSent(), - balance: this.getBalance(), - txs: this.getAll() - }; -}; - Address.hash160 = function hash160(key) { key = utils.ensureBuffer(key); return utils.ripesha(key); @@ -622,23 +584,6 @@ Address.validate = function validate(addr, prefix) { return true; }; -Address.getType = function getType(addr) { - var prefix; - - if (!addr) - return 'unknown'; - - if (!Buffer.isBuffer(addr)) - addr = utils.fromBase58(addr); - - prefix = network.address.prefixes[addr[0]]; - - if (!Address.validate(addr, prefix)) - return 'unknown'; - - return prefix; -}; - Address.prototype.toJSON = function toJSON(passphrase) { var key = this.key; diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 525d0a61..66e0ffaa 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -237,6 +237,8 @@ TXPool.prototype._add = function add(tx, callback, force) { return next(); } + // TODO: Check for double-spend by doing self.getTX(input.prevout.hash) + // Only add orphans if this input is ours. self._hasAddress(input.getAddress(), function(err, result) { if (err) diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index a0f4cf5a..53bf4760 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -410,62 +410,6 @@ Wallet.prototype.setChangeDepth = function setChangeDepth(depth) { return true; }; -Wallet.prototype.getPrivateKey = function getPrivateKey(enc) { - return this.receiveAddress.getPrivateKey(enc); -}; - -Wallet.prototype.getScript = function getScript() { - return this.receiveAddress.getScript(); -}; - -Wallet.prototype.getScriptHash = function getScriptHash() { - return this.receiveAddress.getScriptHash(); -}; - -Wallet.prototype.getScriptHash160 = function getScriptHash160() { - return this.receiveAddress.getScriptHash160(); -}; - -Wallet.prototype.getScriptHash256 = function getScriptHash256() { - return this.receiveAddress.getScriptHash160(); -}; - -Wallet.prototype.getScriptAddress = function getScriptAddress() { - return this.receiveAddress.getScriptAddress(); -}; - -Wallet.prototype.getProgram = function getProgram() { - return this.receiveAddress.getProgram(); -}; - -Wallet.prototype.getProgramHash = function getProgramHash() { - return this.receiveAddress.getProgramHash(); -}; - -Wallet.prototype.getProgramAddress = function getProgramAddress() { - return this.receiveAddress.getProgramAddress(); -}; - -Wallet.prototype.getPublicKey = function getPublicKey(enc) { - return this.receiveAddress.getPublicKey(enc); -}; - -Wallet.prototype.getKeyHash = function getKeyHash() { - return this.receiveAddress.getKeyHash(); -}; - -Wallet.prototype.getKeyAddress = function getKeyAddress() { - return this.receiveAddress.getKeyAddress(); -}; - -Wallet.prototype.getHash = function getHash() { - return this.receiveAddress.getHash(); -}; - -Wallet.prototype.getAddress = function getAddress() { - return this.receiveAddress.getAddress(); -}; - Wallet.prototype.ownInput = function ownInput(tx, index) { if (tx instanceof bcoin.input) return tx.test(this.addressMap); @@ -857,6 +801,70 @@ Wallet.prototype.getLast = function getLast(callback) { return this.db.getLast(this, callback); }; +Wallet.prototype.getPrivateKey = function getPrivateKey(enc) { + return this.receiveAddress.getPrivateKey(enc); +}; + +Wallet.prototype.getPublicKey = function getPublicKey(enc) { + return this.receiveAddress.getPublicKey(enc); +}; + +Wallet.prototype.getScript = function getScript() { + return this.receiveAddress.getScript(); +}; + +Wallet.prototype.getScriptHash = function getScriptHash() { + return this.receiveAddress.getScriptHash(); +}; + +Wallet.prototype.getScriptHash160 = function getScriptHash160() { + return this.receiveAddress.getScriptHash160(); +}; + +Wallet.prototype.getScriptHash256 = function getScriptHash256() { + return this.receiveAddress.getScriptHash160(); +}; + +Wallet.prototype.getScriptAddress = function getScriptAddress() { + return this.receiveAddress.getScriptAddress(); +}; + +Wallet.prototype.getProgram = function getProgram() { + return this.receiveAddress.getProgram(); +}; + +Wallet.prototype.getProgramHash = function getProgramHash() { + return this.receiveAddress.getProgramHash(); +}; + +Wallet.prototype.getProgramAddress = function getProgramAddress() { + return this.receiveAddress.getProgramAddress(); +}; + +Wallet.prototype.getKeyHash = function getKeyHash() { + return this.receiveAddress.getKeyHash(); +}; + +Wallet.prototype.getKeyAddress = function getKeyAddress() { + return this.receiveAddress.getKeyAddress(); +}; + +Wallet.prototype.getHash = function getHash() { + return this.receiveAddress.getHash(); +}; + +Wallet.prototype.getAddress = function getAddress() { + return this.receiveAddress.getAddress(); +}; + +Wallet.prototype.__defineGetter__('privateKey', function() { + return this.getPrivateKey(); +}); + +Wallet.prototype.__defineGetter__('publicKey', function() { + return this.getPublicKey(); +}); + Wallet.prototype.__defineGetter__('script', function() { return this.getScript(); }); @@ -889,14 +897,6 @@ Wallet.prototype.__defineGetter__('programAddress', function() { return this.getProgramAddress(); }); -Wallet.prototype.__defineGetter__('privateKey', function() { - return this.getPrivateKey(); -}); - -Wallet.prototype.__defineGetter__('publicKey', function() { - return this.getPublicKey(); -}); - Wallet.prototype.__defineGetter__('keyHash', function() { return this.getKeyHash(); }); diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 21b98cc3..6777c1e7 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -344,7 +344,7 @@ WalletDB.prototype.create = function create(options, callback) { options.store = true; options.db = self; wallet = new bcoin.wallet(options); - self._saveDB(wallet.id, wallet.toJSON(), done); + self.saveJSON(wallet.id, wallet.toJSON(), done); } function done(err) { @@ -382,7 +382,8 @@ WalletDB.prototype._onAddress = function _onAddress(wallet, id) { batch.write(function(err) { if (err) self.emit('error', err); - self.saveJSON(wallet.id, wallet.toJSON(), function(err) { + + self._saveDB(wallet.id, wallet.toJSON(), function(err) { if (err) self.emit('error', err); }); @@ -416,7 +417,8 @@ WalletDB.prototype.getUnspent = function getUnspent(id, callback) { if (unspent.length === 0) return callback(null, unspent); - self.get(id, function(err, wallet) { + // Warning: won't work with encrypted wallets + self.get(id, null, function(err, wallet) { var res = false; if (!wallet)