From dd7ccd40dba9b21629c6ec7b8430a29aba74c807 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 4 Oct 2016 12:30:44 -0700 Subject: [PATCH] wallet: path version and type. --- lib/wallet/account.js | 38 +++++++++++++++++++++++++++++++++ lib/wallet/wallet.js | 49 +++++++++++++++++++++++++++++-------------- 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 30f6c098..4e26680c 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -666,6 +666,44 @@ Account.prototype.setDepth = co(function* setDepth(receiveDepth, changeDepth, ne return receive || nested; }); +/** + * Get witness version for a path. + * @param {Path} + * @returns {Number} + */ + +Account.prototype.getWitnessVersion = function getWitnessVersion(path) { + if (!this.witness) + return -1; + + if (path.branch === 2) + return -1; + + return 0; +}; + +/** + * Get address type for a path. + * @param {Path} path + * @returns {Number} + */ + +Account.prototype.getAddressType = function getAddressType(path) { + if (path.branch === 2) + return Script.types.SCRIPTHASH; + + if (this.witness) { + if (this.type === Acount.types.MULTISIG) + return Script.types.WITNESSSCRIPTHASH; + return Script.types.WITNESSPUBKEYHASH; + } + + if (this.type === Acount.types.MULTISIG) + return Script.types.SCRIPTHASH; + + return Script.types.PUBKEYHASH; +}; + /** * Convert the account to a more inspection-friendly object. * @returns {Object} diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index fb2a75c5..24430957 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -1071,6 +1071,14 @@ Wallet.prototype.getPath = co(function* getPath(address) { path.id = this.id; path.name = yield this.getAccountName(path.account); + assert(path.name); + + // account = yield this.getAccount(path.account); + // assert(account); + // path.name = account.name; + // path.version = account.getWitnessVersion(path); + // path.type = account.getAddressType(path); + this.pathCache.set(hash, path); return path; @@ -1083,23 +1091,32 @@ Wallet.prototype.getPath = co(function* getPath(address) { */ Wallet.prototype.getPaths = co(function* getPaths(acct) { - var out = []; - var i, account, paths, path; - - account = yield this._getIndex(acct); - paths = yield this.db.getWalletPaths(this.wid); + var index = yield this.ensureIndex(acct); + var paths = yield this.db.getWalletPaths(this.wid); + var result = []; + var i, path; for (i = 0; i < paths.length; i++) { path = paths[i]; - if (!account || path.account === account) { + if (index == null || path.account === index) { path.id = this.id; path.name = yield this.getAccountName(path.account); + + assert(path.name); + + // account = yield this.getAccount(path.account); + // assert(account); + // path.name = account.name; + // path.version = account.getWitnessVersion(path); + // path.type = account.getAddressType(path); + this.pathCache.set(path.hash, path); - out.push(path); + + result.push(path); } } - return out; + return result; }); /** @@ -1899,7 +1916,7 @@ Wallet.prototype.zap = co(function* zap(acct, age) { */ Wallet.prototype._zap = co(function* zap(acct, age) { - var account = yield this._getIndex(acct); + var account = yield this.ensureIndex(acct); return yield this.txdb.zap(account, age); }); @@ -1957,7 +1974,7 @@ Wallet.prototype.getPathInfo = co(function* getPathInfo(tx) { */ Wallet.prototype.getHistory = co(function* getHistory(acct) { - var account = yield this._getIndex(acct); + var account = yield this.ensureIndex(acct); return this.txdb.getHistory(account); }); @@ -1968,7 +1985,7 @@ Wallet.prototype.getHistory = co(function* getHistory(acct) { */ Wallet.prototype.getCoins = co(function* getCoins(acct) { - var account = yield this._getIndex(acct); + var account = yield this.ensureIndex(acct); return yield this.txdb.getCoins(account); }); @@ -1979,7 +1996,7 @@ Wallet.prototype.getCoins = co(function* getCoins(acct) { */ Wallet.prototype.getUnconfirmed = co(function* getUnconfirmed(acct) { - var account = yield this._getIndex(acct); + var account = yield this.ensureIndex(acct); return yield this.txdb.getUnconfirmed(account); }); @@ -1990,7 +2007,7 @@ Wallet.prototype.getUnconfirmed = co(function* getUnconfirmed(acct) { */ Wallet.prototype.getBalance = co(function* getBalance(acct) { - var account = yield this._getIndex(acct); + var account = yield this.ensureIndex(acct); return yield this.txdb.getBalance(account); }); @@ -2009,7 +2026,7 @@ Wallet.prototype.getRange = co(function* getRange(acct, options) { options = acct; acct = null; } - account = yield this._getIndex(acct); + account = yield this.ensureIndex(acct); return yield this.txdb.getRange(account, options); }); @@ -2021,7 +2038,7 @@ Wallet.prototype.getRange = co(function* getRange(acct, options) { */ Wallet.prototype.getLast = co(function* getLast(acct, limit) { - var account = yield this._getIndex(acct); + var account = yield this.ensureIndex(acct); return yield this.txdb.getLast(account, limit); }); @@ -2033,7 +2050,7 @@ Wallet.prototype.getLast = co(function* getLast(acct, limit) { * @returns {Promise} */ -Wallet.prototype._getIndex = co(function* _getIndex(acct) { +Wallet.prototype.ensureIndex = co(function* ensureIndex(acct) { var index; if (acct == null)