From b6e9019d56eb8a07c651e9f9cfa8fe3b65024777 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 15 Aug 2016 04:14:13 -0700 Subject: [PATCH] wallet: rename label to id and id to wid. --- lib/bcoin/keyring.js | 32 ++--- lib/bcoin/txdb.js | 56 ++++---- lib/bcoin/wallet.js | 101 +++++++------- lib/bcoin/walletdb.js | 304 +++++++++++++++++++++--------------------- test/http-test.js | 3 +- 5 files changed, 249 insertions(+), 247 deletions(-) diff --git a/lib/bcoin/keyring.js b/lib/bcoin/keyring.js index 12fa178d..135f987e 100644 --- a/lib/bcoin/keyring.js +++ b/lib/bcoin/keyring.js @@ -41,8 +41,8 @@ function KeyRing(options) { this.m = 1; this.n = 1; this.witness = false; - this.id = 0; - this.label = null; + this.wid = 0; + this.id = null; this.name = null; this.account = 0; this.change = 0; @@ -99,14 +99,14 @@ KeyRing.prototype.fromOptions = function fromOptions(options) { this.witness = options.witness; } - if (options.id) { - assert(utils.isNumber(options.id)); - this.id = options.id; + if (options.wid) { + assert(utils.isNumber(options.wid)); + this.wid = options.wid; } - if (options.label) { - assert(utils.isAlpha(options.label)); - this.label = options.label; + if (options.id) { + assert(utils.isAlpha(options.id)); + this.id = options.id; } if (options.name) { @@ -661,8 +661,8 @@ KeyRing.prototype.toJSON = function toJSON() { m: this.m, n: this.n, witness: this.witness, + wid: this.wid, id: this.id, - label: this.label, name: this.name, account: this.account, change: this.change, @@ -690,8 +690,8 @@ KeyRing.prototype.fromJSON = function fromJSON(json) { assert(utils.isNumber(json.m)); assert(utils.isNumber(json.n)); assert(typeof json.witness === 'boolean'); - assert(!json.id || utils.isNumber(json.id)); - assert(!json.label || utils.isAlpha(json.label)); + assert(!json.wid || utils.isNumber(json.wid)); + assert(!json.id || utils.isAlpha(json.id)); assert(!json.name || utils.isAlpha(json.name)); assert(utils.isNumber(json.account)); assert(utils.isNumber(json.change)); @@ -704,7 +704,7 @@ KeyRing.prototype.fromJSON = function fromJSON(json) { this.m = json.m; this.n = json.n; this.witness = json.witness; - this.id = json.id; + this.wid = json.wid; this.name = json.name; this.account = json.account; this.change = json.change; @@ -741,8 +741,8 @@ KeyRing.prototype.toRaw = function toRaw(writer) { p.writeU8(this.m); p.writeU8(this.n); p.writeU8(this.witness ? 1 : 0); - p.writeU32(this.id); - p.writeVarString(this.label, 'utf8'); + p.writeU32(this.wid); + p.writeVarString(this.id, 'utf8'); p.writeVarString(this.name, 'utf8'); p.writeU32(this.account); p.writeU32(this.change); @@ -774,8 +774,8 @@ KeyRing.prototype.fromRaw = function fromRaw(data) { this.m = p.readU8(); this.n = p.readU8(); this.witness = p.readU8() === 1; - this.id = p.readU32(); - this.label = p.readVarString('utf8'); + this.wid = p.readU32(); + this.id = p.readVarString('utf8'); this.name = p.readVarString('utf8'); this.account = p.readU32(); this.change = p.readU32(); diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 16029329..dbd32512 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -89,8 +89,8 @@ TXDB.prototype.open = function open(callback) { */ TXDB.prototype.prefix = function prefix(key) { - assert(this.wallet.id); - return 't/' + pad32(this.wallet.id) + '/' + key; + assert(this.wallet.wid); + return 't/' + pad32(this.wallet.wid) + '/' + key; }; /** @@ -476,7 +476,7 @@ TXDB.prototype._resolveOrphans = function _resolveOrphans(tx, index, callback) { TXDB.prototype.add = function add(tx, info, callback) { var self = this; var unlock = this._lock(add, [tx, info, callback]); - var hash, i, path, id; + var hash, i, path, account; if (!unlock) return; @@ -515,13 +515,13 @@ TXDB.prototype.add = function add(tx, info, callback) { self.put('m/' + pad32(tx.ps) + '/' + hash, DUMMY); for (i = 0; i < info.accounts.length; i++) { - id = pad32(info.accounts[i]); - self.put('T/' + id + '/' + hash, DUMMY); + account = pad32(info.accounts[i]); + self.put('T/' + account + '/' + hash, DUMMY); if (tx.ts === 0) - self.put('P/' + id + '/' + hash, DUMMY); + self.put('P/' + account + '/' + hash, DUMMY); else - self.put('H/' + id + '/' + pad32(tx.height) + '/' + hash, DUMMY); - self.put('M/' + id + '/' + pad32(tx.ps) + '/' + hash, DUMMY); + self.put('H/' + account + '/' + pad32(tx.height) + '/' + hash, DUMMY); + self.put('M/' + account + '/' + pad32(tx.ps) + '/' + hash, DUMMY); } // Consume unspent money or add orphans @@ -772,7 +772,7 @@ TXDB.prototype.isSpent = function isSpent(hash, index, callback) { TXDB.prototype._confirm = function _confirm(tx, info, callback) { var self = this; var hash = tx.hash('hex'); - var i, id; + var i, account; this.getTX(hash, function(err, existing) { if (err) @@ -806,9 +806,9 @@ TXDB.prototype._confirm = function _confirm(tx, info, callback) { self.put('h/' + pad32(tx.height) + '/' + hash, DUMMY); for (i = 0; i < info.accounts.length; i++) { - id = pad32(info.accounts[i]); - self.del('P/' + id + '/' + hash); - self.put('H/' + id + '/' + pad32(tx.height) + '/' + hash, DUMMY); + account = pad32(info.accounts[i]); + self.del('P/' + account + '/' + hash); + self.put('H/' + account + '/' + pad32(tx.height) + '/' + hash, DUMMY); } utils.forEachSerial(tx.outputs, function(output, next, i) { @@ -912,7 +912,7 @@ TXDB.prototype._lazyRemove = function lazyRemove(tx, callback) { TXDB.prototype._remove = function remove(tx, info, callback) { var self = this; var hash = tx.hash('hex'); - var i, path, id, key, address, input, output, coin; + var i, path, account, key, address, input, output, coin; this.del('t/' + hash); @@ -924,13 +924,13 @@ TXDB.prototype._remove = function remove(tx, info, callback) { this.del('m/' + pad32(tx.ps) + '/' + hash); for (i = 0; i < info.accounts.length; i++) { - id = pad32(info.accounts[i]); - this.del('T/' + id + '/' + hash); + account = pad32(info.accounts[i]); + this.del('T/' + account + '/' + hash); if (tx.ts === 0) - this.del('P/' + id + '/' + hash); + this.del('P/' + account + '/' + hash); else - this.del('H/' + id + '/' + pad32(tx.height) + '/' + hash); - this.del('M/' + id + '/' + pad32(tx.ps) + '/' + hash); + this.del('H/' + account + '/' + pad32(tx.height) + '/' + hash); + this.del('M/' + account + '/' + pad32(tx.ps) + '/' + hash); } this.fillHistory(tx, function(err) { @@ -1050,7 +1050,7 @@ TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) { var self = this; var hash = tx.hash('hex'); var height = tx.height; - var i, id; + var i, account; if (height !== -1) return callback(null, false, info); @@ -1066,9 +1066,9 @@ TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) { this.del('h/' + pad32(height) + '/' + hash); for (i = 0; i < info.accounts.length; i++) { - id = pad32(info.accounts[i]); - this.put('P/' + id + '/' + hash, DUMMY); - this.del('H/' + id + '/' + pad32(height) + '/' + hash); + account = pad32(info.accounts[i]); + this.put('P/' + account + '/' + hash, DUMMY); + this.del('H/' + account + '/' + pad32(height) + '/' + hash); } utils.forEachSerial(tx.outputs, function(output, next, i) { @@ -1950,8 +1950,8 @@ TXDB.prototype.abandon = function abandon(hash, callback, force) { function Details(info) { this.db = info.db; this.network = info.db.network; + this.wid = info.wid; this.id = info.id; - this.label = info.label; this.hash = info.tx.hash('hex'); this.height = info.tx.height; this.block = info.tx.block; @@ -1993,8 +1993,8 @@ Details.prototype._insert = function _insert(vector, target, table) { for (j = 0; j < paths.length; j++) { path = paths[j]; - if (path.id === this.id) { - path.label = this.label; + if (path.wid === this.wid) { + path.id = this.id; member.path = path; break; } @@ -2008,8 +2008,8 @@ Details.prototype._insert = function _insert(vector, target, table) { Details.prototype.toJSON = function toJSON() { var self = this; return { + wid: this.wid, id: this.id, - label: this.label, hash: utils.revHex(this.hash), height: this.height, block: this.block ? utils.revHex(this.block) : null, @@ -2055,8 +2055,8 @@ DetailsMember.prototype.toJSON = function toJSON(network) { */ function Balance(wallet) { + this.wid = wallet.wid; this.id = wallet.id; - this.label = wallet.label; this.unconfirmed = 0; this.confirmed = 0; this.total = 0; @@ -2090,8 +2090,8 @@ Balance.prototype.unconfirm = function unconfirm(value) { Balance.prototype.toJSON = function toJSON() { return { + wid: this.wid, id: this.id, - label: this.label, unconfirmed: utils.btc(this.unconfirmed), confirmed: utils.btc(this.confirmed), total: utils.btc(this.total) diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 8a01d9d4..85478598 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -56,8 +56,8 @@ function Wallet(db, options) { this.writeLock = new bcoin.locker(this); this.fundLock = new bcoin.locker(this); - this.id = 0; - this.label = null; + this.wid = 0; + this.id = null; this.master = null; this.initialized = false; this.accountDepth = 0; @@ -81,7 +81,7 @@ utils.inherits(Wallet, EventEmitter); Wallet.prototype.fromOptions = function fromOptions(options) { var master = options.master; - var label, token; + var id, token; if (!master) master = bcoin.hd.fromMnemonic(null, this.network); @@ -106,18 +106,18 @@ Wallet.prototype.fromOptions = function fromOptions(options) { this.accountDepth = options.accountDepth; } - if (options.id != null) { - assert(utils.isNumber(options.id)); - this.id = options.id; + if (options.wid != null) { + assert(utils.isNumber(options.wid)); + this.wid = options.wid; } - if (options.label) { - assert(utils.isAlpha(options.label), 'Wallet ID must be alphanumeric.'); - label = options.label; + if (options.id) { + assert(utils.isAlpha(options.id), 'Wallet ID must be alphanumeric.'); + id = options.id; } - if (!label) - label = this.getLabel(); + if (!id) + id = this.getLabel(); if (options.token) { assert(Buffer.isBuffer(options.token)); @@ -133,7 +133,7 @@ Wallet.prototype.fromOptions = function fromOptions(options) { if (!token) token = this.getToken(this.master.key, this.tokenDepth); - this.label = label; + this.id = id; this.token = token; return this; @@ -497,8 +497,8 @@ Wallet.prototype.createAccount = function createAccount(options, callback, force options = { network: self.network, + wid: self.wid, id: self.id, - label: self.label, name: self.accountDepth === 0 ? 'default' : options.name, witness: options.witness, accountKey: key.hdPublicKey, @@ -558,7 +558,7 @@ Wallet.prototype.ensureAccount = function ensureAccount(options, callback) { */ Wallet.prototype.getAccounts = function getAccounts(callback) { - this.db.getAccounts(this.id, callback); + this.db.getAccounts(this.wid, callback); }; /** @@ -567,7 +567,7 @@ Wallet.prototype.getAccounts = function getAccounts(callback) { */ Wallet.prototype.getAddresses = function getAddresses(callback) { - this.db.getAddresses(this.id, callback); + this.db.getAddresses(this.wid, callback); }; /** @@ -584,15 +584,15 @@ Wallet.prototype.getAccount = function getAccount(account, callback) { return callback(null, this.account); } - this.db.getAccount(this.id, account, function(err, account) { + this.db.getAccount(this.wid, account, function(err, account) { if (err) return callback(err); if (!account) return callback(); + account.wid = self.wid; account.id = self.id; - account.label = self.label; return callback(null, account); }); @@ -605,7 +605,7 @@ Wallet.prototype.getAccount = function getAccount(account, callback) { */ Wallet.prototype.hasAccount = function hasAccount(account, callback) { - this.db.hasAccount(this.id, account, callback); + this.db.hasAccount(this.wid, account, callback); }; /** @@ -699,7 +699,7 @@ Wallet.prototype.save = function save() { */ Wallet.prototype.start = function start() { - return this.db.start(this.id); + return this.db.start(this.wid); }; /** @@ -708,7 +708,7 @@ Wallet.prototype.start = function start() { */ Wallet.prototype.drop = function drop() { - return this.db.drop(this.id); + return this.db.drop(this.wid); }; /** @@ -717,7 +717,7 @@ Wallet.prototype.drop = function drop() { */ Wallet.prototype.commit = function commit(callback) { - return this.db.commit(this.id, callback); + return this.db.commit(this.wid, callback); }; /** @@ -727,7 +727,7 @@ Wallet.prototype.commit = function commit(callback) { */ Wallet.prototype.hasAddress = function hasAddress(address, callback) { - return this.db.hasAddress(this.id, address, callback); + return this.db.hasAddress(this.wid, address, callback); }; /** @@ -737,7 +737,7 @@ Wallet.prototype.hasAddress = function hasAddress(address, callback) { */ Wallet.prototype.getPath = function getPath(address, callback) { - return this.db.getPath(this.id, address, callback); + return this.db.getPath(this.wid, address, callback); }; /** @@ -1640,7 +1640,7 @@ Wallet.prototype._getKey = function _getKey(account, errback, callback) { if (account == null) return callback.call(this, null, errback); - this.db.getAccountIndex(this.id, account, function(err, index) { + this.db.getAccountIndex(this.wid, account, function(err, index) { if (err) return errback(err); @@ -1896,8 +1896,8 @@ Wallet.prototype.__defineGetter__('changeAddress', function() { Wallet.prototype.inspect = function inspect() { return { + wid: this.wid, id: this.id, - label: this.label, network: this.network.type, initialized: this.initialized, accountDepth: this.accountDepth, @@ -1918,8 +1918,8 @@ Wallet.prototype.inspect = function inspect() { Wallet.prototype.toJSON = function toJSON() { return { network: this.network.type, + wid: this.wid, id: this.id, - label: this.label, initialized: this.initialized, accountDepth: this.accountDepth, token: this.token.toString('hex'), @@ -1936,17 +1936,17 @@ Wallet.prototype.toJSON = function toJSON() { */ Wallet.prototype.fromJSON = function fromJSON(json) { - assert(utils.isNumber(json.id)); + assert(utils.isNumber(json.wid)); assert(typeof json.initialized === 'boolean'); - assert(utils.isAlpha(json.label), 'Wallet ID must be alphanumeric.'); + assert(utils.isAlpha(json.id), 'Wallet ID must be alphanumeric.'); assert(utils.isNumber(json.accountDepth)); assert(typeof json.token === 'string'); assert(json.token.length === 64); assert(utils.isNumber(json.tokenDepth)); this.network = bcoin.network.get(json.network); + this.wid = json.wid; this.id = json.id; - this.label = json.label; this.initialized = json.initialized; this.accountDepth = json.accountDepth; this.token = new Buffer(json.token, 'hex'); @@ -1964,8 +1964,8 @@ Wallet.prototype.toRaw = function toRaw(writer) { var p = new BufferWriter(writer); p.writeU32(this.network.magic); - p.writeU32(this.id); - p.writeVarString(this.label, 'utf8'); + p.writeU32(this.wid); + p.writeVarString(this.id, 'utf8'); p.writeU8(this.initialized ? 1 : 0); p.writeU32(this.accountDepth); p.writeBytes(this.token); @@ -1987,8 +1987,8 @@ Wallet.prototype.toRaw = function toRaw(writer) { Wallet.prototype.fromRaw = function fromRaw(data) { var p = new BufferReader(data); this.network = bcoin.network.fromMagic(p.readU32()); - this.id = p.readU32(); - this.label = p.readVarString('utf8'); + this.wid = p.readU32(); + this.id = p.readVarString('utf8'); this.initialized = p.readU8() === 1; this.accountDepth = p.readU32(); this.token = p.readBytes(32); @@ -2050,7 +2050,7 @@ Wallet.isWallet = function isWallet(obj) { * (default=pubkeyhash). * @param {Number?} options.m - `m` value for multisig. * @param {Number?} options.n - `n` value for multisig. - * @param {String?} options.id - Wallet ID + * @param {String?} options.wid - Wallet ID * @param {String?} options.name - Account name */ @@ -2069,8 +2069,8 @@ function Account(db, options) { this.receiveAddress = null; this.changeAddress = null; - this.id = 0; - this.label = null; + this.wid = 0; + this.id = null; this.name = null; this.witness = this.db.options.witness; this.accountKey = null; @@ -2099,13 +2099,13 @@ Account.prototype.fromOptions = function fromOptions(options) { var i; assert(options, 'Options are required.'); - assert(utils.isNumber(options.id)); - assert(utils.isAlpha(options.label), 'Wallet ID must be alphanumeric.'); + assert(utils.isNumber(options.wid)); + assert(utils.isAlpha(options.id), 'Wallet ID must be alphanumeric.'); assert(bcoin.hd.isHD(options.accountKey), 'Account key is required.'); assert(utils.isNumber(options.accountIndex), 'Account index is required.'); + this.wid = options.wid; this.id = options.id; - this.label = options.label; if (options.name != null) { assert(utils.isAlpha(options.name), 'Account name must be alphanumeric.'); @@ -2385,7 +2385,7 @@ Account.prototype._checkKeys = function _checkKeys(callback) { if (!paths) return callback(null, false); - callback(null, paths[self.id] != null); + callback(null, paths[self.wid] != null); }); }; @@ -2509,8 +2509,8 @@ Account.prototype.deriveAddress = function deriveAddress(change, index) { return new bcoin.keyring({ network: this.network, key: key.publicKey, + wid: this.wid, id: this.id, - label: this.label, name: this.name, account: this.accountIndex, change: change, @@ -2540,7 +2540,7 @@ Account.prototype.save = function save() { */ Account.prototype.saveAddress = function saveAddress(address, callback) { - return this.db.saveAddress(this.id, address, callback); + return this.db.saveAddress(this.wid, address, callback); }; /** @@ -2602,7 +2602,7 @@ Account.prototype.setDepth = function setDepth(receiveDepth, changeDepth, callba Account.prototype.inspect = function inspect() { return { - id: this.id, + wid: this.wid, name: this.name, network: this.network, initialized: this.initialized, @@ -2638,7 +2638,7 @@ Account.prototype.inspect = function inspect() { Account.prototype.toJSON = function toJSON() { return { network: this.network.type, - id: this.id, + wid: this.wid, name: this.name, initialized: this.initialized, type: this.type, @@ -2674,7 +2674,8 @@ Account.prototype.fromJSON = function fromJSON(json) { var i; assert.equal(json.network, this.network.type); - assert(utils.isAlpha(json.id), 'Wallet ID must be alphanumeric.'); + assert(utils.isNumber(json.wid)); + assert(utils.isAlpha(json.id), 'Account name must be alphanumeric.'); assert(utils.isAlpha(json.name), 'Account name must be alphanumeric.'); assert(typeof json.initialized === 'boolean'); assert(json.type === 'pubkeyhash' || json.type === 'multisig'); @@ -2686,7 +2687,7 @@ Account.prototype.fromJSON = function fromJSON(json) { assert(utils.isNumber(json.changeDepth)); assert(Array.isArray(json.keys)); - this.id = json.id; + this.wid = json.wid; this.name = json.name; this.initialized = json.initialized; this.type = json.type; @@ -2715,8 +2716,8 @@ Account.prototype.toRaw = function toRaw(writer) { p.writeU32(this.network.magic); // NOTE: Passed in by caller. - // p.writeU32(this.id); - // p.writeVarString(this.label, 'utf8'); + // p.writeU32(this.wid); + // p.writeVarString(this.id, 'utf8'); p.writeVarString(this.name, 'utf8'); p.writeU8(this.initialized ? 1 : 0); p.writeU8(this.type === 'pubkeyhash' ? 0 : 1); @@ -2751,8 +2752,8 @@ Account.prototype.fromRaw = function fromRaw(data) { this.network = bcoin.network.fromMagic(p.readU32()); // NOTE: Passed in by caller. - // this.id = p.readU32(); - // this.label = p.readVarString('utf8'); + // this.wid = p.readU32(); + // this.id = p.readVarString('utf8'); this.name = p.readVarString('utf8'); this.initialized = p.readU8() === 1; this.type = p.readU8() === 0 ? 'pubkeyhash' : 'multisig'; diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index f790ef39..b39a66a6 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -10,14 +10,14 @@ /* * Database Layout: * (inherits all from txdb) - * p/[address] -> id & path data - * w/[id] -> wallet - * l/[label] -> wallet id - * a/[id]/[index] -> account - * i/[id]/[name] -> account index + * p/[address] -> wid & path data + * w/[wid] -> wallet + * l/[label] -> wallet wid + * a/[wid]/[index] -> account + * i/[wid]/[name] -> account index * R -> tip * b/[hash] -> wallet block - * t/[hash] -> tx->wallet-id map + * t/[hash] -> tx->wallet-wid map */ var bcoin = require('./env'); @@ -170,7 +170,7 @@ WalletDB.prototype._close = function close(callback) { }; /** - * Get current wallet ID depth. + * Get current wallet wid depth. * @private * @param {Function} callback */ @@ -186,7 +186,7 @@ WalletDB.prototype.getDepth = function getDepth(callback) { // a "scoped" state. So, we avoid all the // nonsense of adding a global lock to // walletdb.create by simply seeking to the - // highest wallet id. + // highest wallet wid. iter = this.db.iterator({ gte: 'w/' + pad32(0), lte: 'w/' + pad32(0xffffffff), @@ -223,38 +223,38 @@ WalletDB.prototype.getDepth = function getDepth(callback) { /** * Start batch. * @private - * @param {WalletID} id + * @param {WalletID} wid */ -WalletDB.prototype.start = function start(id) { - assert(utils.isNumber(id), 'Bad ID for batch.'); - assert(!this.batches[id], 'Batch already started.'); - this.batches[id] = this.db.batch(); +WalletDB.prototype.start = function start(wid) { + assert(utils.isNumber(wid), 'Bad ID for batch.'); + assert(!this.batches[wid], 'Batch already started.'); + this.batches[wid] = this.db.batch(); }; /** * Drop batch. * @private - * @param {WalletID} id + * @param {WalletID} wid */ -WalletDB.prototype.drop = function drop(id) { - var batch = this.batch(id); +WalletDB.prototype.drop = function drop(wid) { + var batch = this.batch(wid); batch.clear(); - delete this.batches[id]; + delete this.batches[wid]; }; /** * Get batch. * @private - * @param {WalletID} id + * @param {WalletID} wid * @returns {Leveldown.Batch} */ -WalletDB.prototype.batch = function batch(id) { +WalletDB.prototype.batch = function batch(wid) { var batch; - assert(utils.isNumber(id), 'Bad ID for batch.'); - batch = this.batches[id]; + assert(utils.isNumber(wid), 'Bad ID for batch.'); + batch = this.batches[wid]; assert(batch, 'Batch does not exist.'); return batch; }; @@ -262,13 +262,13 @@ WalletDB.prototype.batch = function batch(id) { /** * Save batch. * @private - * @param {WalletID} id + * @param {WalletID} wid * @param {Function} callback */ -WalletDB.prototype.commit = function commit(id, callback) { - var batch = this.batch(id); - delete this.batches[id]; +WalletDB.prototype.commit = function commit(wid, callback) { + var batch = this.batch(wid); + delete this.batches[wid]; batch.write(callback); }; @@ -343,8 +343,8 @@ WalletDB.prototype.dump = function dump(callback) { */ WalletDB.prototype.register = function register(wallet) { - assert(!this.wallets[wallet.id]); - this.wallets[wallet.id] = wallet; + assert(!this.wallets[wallet.wid]); + this.wallets[wallet.wid] = wallet; }; /** @@ -354,8 +354,8 @@ WalletDB.prototype.register = function register(wallet) { */ WalletDB.prototype.unregister = function unregister(wallet) { - assert(this.wallets[wallet.id]); - delete this.wallets[wallet.id]; + assert(this.wallets[wallet.wid]); + delete this.wallets[wallet.wid]; }; /** @@ -364,44 +364,44 @@ WalletDB.prototype.unregister = function unregister(wallet) { * @param {Function} callback */ -WalletDB.prototype.getWalletID = function getWalletID(label, callback) { - var id; +WalletDB.prototype.getWalletID = function getWalletID(id, callback) { + var wid; - if (!label) + if (!id) return callback(); - if (typeof label === 'number') - return callback(null, label); - - id = this.walletCache.get(label); - - if (id) + if (typeof id === 'number') return callback(null, id); - this.db.fetch('l/' + label, function(data) { - id = data.readUInt32LE(0, true); - self.walletCache.set(label, id); - return id; + wid = this.walletCache.get(id); + + if (wid) + return callback(null, wid); + + this.db.fetch('l/' + id, function(data) { + wid = data.readUInt32LE(0, true); + self.walletCache.set(id, wid); + return wid; }, callback); }; /** * Get a wallet from the database, setup watcher. - * @param {WalletID} id + * @param {WalletID} wid * @param {Function} callback - Returns [Error, {@link Wallet}]. */ -WalletDB.prototype.get = function get(id, callback) { +WalletDB.prototype.get = function get(wid, callback) { var self = this; - this.getWalletID(id, function(err, id) { + this.getWalletID(wid, function(err, wid) { if (err) return callback(err); - if (!id) + if (!wid) return callback(); - self._get(id, function(err, wallet, watched) { + self._get(wid, function(err, wallet, watched) { if (err) return callback(err); @@ -430,27 +430,27 @@ WalletDB.prototype.get = function get(id, callback) { /** * Get a wallet from the database, do not setup watcher. * @private - * @param {WalletID} id + * @param {WalletID} wid * @param {Function} callback - Returns [Error, {@link Wallet}]. */ -WalletDB.prototype._get = function get(id, callback) { +WalletDB.prototype._get = function get(wid, callback) { var self = this; var unlock, wallet; - unlock = this.readLock.lock(id, get, [id, callback]); + unlock = this.readLock.lock(wid, get, [wid, callback]); if (!unlock) return; callback = utils.wrap(callback, unlock); - wallet = this.wallets[id]; + wallet = this.wallets[wid]; if (wallet) return callback(null, wallet, true); - this.db.fetch('w/' + pad32(id), function(data) { + this.db.fetch('w/' + pad32(wid), function(data) { return bcoin.wallet.fromRaw(self, data); }, callback); }; @@ -462,23 +462,23 @@ WalletDB.prototype._get = function get(id, callback) { */ WalletDB.prototype.save = function save(wallet) { - var batch = this.batch(wallet.id); - var id = new Buffer(4); - this.walletCache.set(wallet.label, wallet.id); - batch.put('w/' + pad32(wallet.id), wallet.toRaw()); - id.writeUInt32LE(wallet.id, 0, true); - batch.put('l/' + wallet.label, id); + var batch = this.batch(wallet.wid); + var wid = new Buffer(4); + this.walletCache.set(wallet.id, wallet.wid); + batch.put('w/' + pad32(wallet.wid), wallet.toRaw()); + wid.writeUInt32LE(wallet.wid, 0, true); + batch.put('l/' + wallet.id, wid); }; /** * Test an api key against a wallet's api key. - * @param {WalletID} id + * @param {WalletID} wid * @param {String} token * @param {Function} callback */ -WalletDB.prototype.auth = function auth(id, token, callback) { - this.get(id, function(err, wallet) { +WalletDB.prototype.auth = function auth(wid, token, callback) { + this.get(wid, function(err, wallet) { if (err) return callback(err); @@ -514,14 +514,14 @@ WalletDB.prototype.create = function create(options, callback) { options = {}; } - unlock = this.writeLock.lock(options.label, create, [options, callback]); + unlock = this.writeLock.lock(options.id, create, [options, callback]); if (!unlock) return; callback = utils.wrap(callback, unlock); - this.has(options.label, function(err, exists) { + this.has(options.id, function(err, exists) { if (err) return callback(err); @@ -533,7 +533,7 @@ WalletDB.prototype.create = function create(options, callback) { try { wallet = bcoin.wallet.fromOptions(self, options); - wallet.id = self.depth++; + wallet.wid = self.depth++; } catch (e) { return callback(e); } @@ -548,7 +548,7 @@ WalletDB.prototype.create = function create(options, callback) { if (err) return callback(err); - self.logger.info('Created wallet %s.', wallet.label); + self.logger.info('Created wallet %s.', wallet.id); return callback(null, wallet); }); @@ -557,21 +557,21 @@ WalletDB.prototype.create = function create(options, callback) { /** * Test for the existence of a wallet. - * @param {WalletID?} id + * @param {WalletID?} wid * @param {Function} callback */ -WalletDB.prototype.has = function has(id, callback) { - this.getWalletID(id, function(err, id) { +WalletDB.prototype.has = function has(wid, callback) { + this.getWalletID(wid, function(err, wid) { if (err) return callback(err); - return callback(null, id != null); + return callback(null, wid != null); }); }; /** * Attempt to create wallet, return wallet if already exists. - * @param {WalletID?} id + * @param {WalletID?} wid * @param {Object} options - See {@link Wallet}. * @param {Function} callback */ @@ -579,7 +579,7 @@ WalletDB.prototype.has = function has(id, callback) { WalletDB.prototype.ensure = function ensure(options, callback) { var self = this; - this.get(options.label, function(err, wallet) { + this.get(options.id, function(err, wallet) { if (err) return callback(err); @@ -592,22 +592,22 @@ WalletDB.prototype.ensure = function ensure(options, callback) { /** * Get an account from the database. - * @param {WalletID} id + * @param {WalletID} wid * @param {String|Number} name - Account name/index. * @param {Function} callback - Returns [Error, {@link Wallet}]. */ -WalletDB.prototype.getAccount = function getAccount(id, name, callback) { +WalletDB.prototype.getAccount = function getAccount(wid, name, callback) { var self = this; - this.getAccountIndex(id, name, function(err, index) { + this.getAccountIndex(wid, name, function(err, index) { if (err) return callback(err); if (index === -1) return callback(); - self._getAccount(id, index, function(err, account) { + self._getAccount(wid, index, function(err, account) { if (err) return callback(err); @@ -627,14 +627,14 @@ WalletDB.prototype.getAccount = function getAccount(id, name, callback) { /** * Get an account from the database. Do not setup watcher. * @private - * @param {WalletID} id + * @param {WalletID} wid * @param {Number} index - Account index. * @param {Function} callback - Returns [Error, {@link Wallet}]. */ -WalletDB.prototype._getAccount = function getAccount(id, index, callback) { +WalletDB.prototype._getAccount = function getAccount(wid, index, callback) { var self = this; - var key = pad32(id) + '/' + pad32(index); + var key = pad32(wid) + '/' + pad32(index); var account = this.accountCache.get(key); if (account) @@ -649,20 +649,20 @@ WalletDB.prototype._getAccount = function getAccount(id, index, callback) { /** * List account names and indexes from the db. - * @param {WalletID} id + * @param {WalletID} wid * @param {Function} callback - Returns [Error, Array]. */ -WalletDB.prototype.getAccounts = function getAccounts(id, callback) { +WalletDB.prototype.getAccounts = function getAccounts(wid, callback) { var map = []; var i, accounts; - if (!utils.isNumber(id)) + if (!utils.isNumber(wid)) return callback(new Error('Wallet IDs must be alphanumeric.')); this.db.iterate({ - gte: 'i/' + pad32(id) + '/', - lte: 'i/' + pad32(id) + '/~', + gte: 'i/' + pad32(wid) + '/', + lte: 'i/' + pad32(wid) + '/~', values: true, parse: function(value, key) { var name = key.split('/')[2]; @@ -687,13 +687,13 @@ WalletDB.prototype.getAccounts = function getAccounts(id, callback) { /** * Lookup the corresponding account name's index. - * @param {WalletID} id + * @param {WalletID} wid * @param {String|Number} name - Account name/index. * @param {Function} callback - Returns [Error, Number]. */ -WalletDB.prototype.getAccountIndex = function getAccountIndex(id, name, callback) { - if (!id) +WalletDB.prototype.getAccountIndex = function getAccountIndex(wid, name, callback) { + if (!wid) return callback(null, -1); if (name == null) @@ -702,7 +702,7 @@ WalletDB.prototype.getAccountIndex = function getAccountIndex(id, name, callback if (typeof name === 'number') return callback(null, name); - this.db.get('i/' + pad32(id) + '/' + name, function(err, index) { + this.db.get('i/' + pad32(wid) + '/' + name, function(err, index) { if (err) return callback(err); @@ -720,14 +720,14 @@ WalletDB.prototype.getAccountIndex = function getAccountIndex(id, name, callback */ WalletDB.prototype.saveAccount = function saveAccount(account) { - var batch = this.batch(account.id); + var batch = this.batch(account.wid); var index = new Buffer(4); - var key = pad32(account.id) + '/' + pad32(account.accountIndex); + var key = pad32(account.wid) + '/' + pad32(account.accountIndex); index.writeUInt32LE(account.accountIndex, 0, true); batch.put('a/' + key, account.toRaw()); - batch.put('i/' + pad32(account.id) + '/' + account.name, index); + batch.put('i/' + pad32(account.wid) + '/' + account.name, index); this.accountCache.set(key, account); }; @@ -742,7 +742,7 @@ WalletDB.prototype.createAccount = function createAccount(options, callback) { var self = this; var account; - this.hasAccount(options.id, options.accountIndex, function(err, exists) { + this.hasAccount(options.wid, options.accountIndex, function(err, exists) { if (err) return callback(err); @@ -774,26 +774,26 @@ WalletDB.prototype.createAccount = function createAccount(options, callback) { /** * Test for the existence of an account. - * @param {WalletID} id + * @param {WalletID} wid * @param {String|Number} account * @param {Function} callback - Returns [Error, Boolean]. */ -WalletDB.prototype.hasAccount = function hasAccount(id, account, callback) { +WalletDB.prototype.hasAccount = function hasAccount(wid, account, callback) { var self = this; var key; - if (!id) + if (!wid) return callback(null, false); - this.getAccountIndex(id, account, function(err, index) { + this.getAccountIndex(wid, account, function(err, index) { if (err) return callback(err); if (index === -1) return callback(null, false); - key = pad32(id) + '/' + pad32(index); + key = pad32(wid) + '/' + pad32(index); if (self.accountCache.has(key)) return callback(null, true); @@ -806,15 +806,15 @@ WalletDB.prototype.hasAccount = function hasAccount(id, account, callback) { * Save an address to the path map. * The path map exists in the form of: * `p/[address-hash] -> {walletid1=path1, walletid2=path2, ...}` - * @param {WalletID} id + * @param {WalletID} wid * @param {KeyRing[]} addresses * @param {Function} callback */ -WalletDB.prototype.saveAddress = function saveAddress(id, addresses, callback) { +WalletDB.prototype.saveAddress = function saveAddress(wid, addresses, callback) { var self = this; var items = []; - var batch = this.batch(id); + var batch = this.batch(wid); var i, address, path; if (!Array.isArray(addresses)) @@ -850,10 +850,10 @@ WalletDB.prototype.saveAddress = function saveAddress(id, addresses, callback) { if (!paths) paths = {}; - if (paths[id]) + if (paths[wid]) return next(); - paths[id] = path; + paths[wid] = path; self.pathCache.set(hash, paths); @@ -898,17 +898,17 @@ WalletDB.prototype._getPaths = function _getPaths(hash, callback) { /** * Test whether an address hash exists in the * path map and is relevant to the wallet id. - * @param {WalletID} id + * @param {WalletID} wid * @param {Hash} address * @param {Function} callback */ -WalletDB.prototype.hasAddress = function hasAddress(id, address, callback) { +WalletDB.prototype.hasAddress = function hasAddress(wid, address, callback) { this.getAddress(address, function(err, paths) { if (err) return callback(err); - if (!paths || !paths[id]) + if (!paths || !paths[wid]) return callback(null, false); return callback(null, true); @@ -927,14 +927,14 @@ WalletDB.prototype.getAddress = function getAddress(address, callback) { /** * Get all address hashes. - * @param {WalletID} id + * @param {WalletID} wid * @param {Function} callback */ -WalletDB.prototype.getAddresses = function getAddresses(id, callback) { +WalletDB.prototype.getAddresses = function getAddresses(wid, callback) { if (!callback) { - callback = id; - id = null; + callback = wid; + wid = null; } this.db.iterate({ @@ -944,7 +944,7 @@ WalletDB.prototype.getAddresses = function getAddresses(id, callback) { parse: function(value, key) { var paths = parsePaths(value); - if (id && !paths[id]) + if (wid && !paths[wid]) return; return key.split('/')[1]; @@ -991,13 +991,13 @@ WalletDB.prototype.rescan = function rescan(chaindb, callback) { /** * Helper function to get a wallet. * @private - * @param {WalletID} id + * @param {WalletID} wid * @param {Function} callback * @param {Function} handler */ -WalletDB.prototype.fetchWallet = function fetchWallet(id, callback, handler) { - this.get(id, function(err, wallet) { +WalletDB.prototype.fetchWallet = function fetchWallet(wid, callback, handler) { + this.get(wid, function(err, wallet) { if (err) return callback(err); @@ -1058,8 +1058,8 @@ WalletDB.prototype.getPathInfo = function getPathInfo(wallet, tx, callback) { if (!table) return callback(); - info = new PathInfo(self, wallet.id, tx, table); - info.label = wallet.label; + info = new PathInfo(self, wallet.wid, tx, table); + info.id = wallet.id; return callback(null, info); }); @@ -1341,8 +1341,8 @@ WalletDB.prototype.removeBlock = function removeBlock(entry, callback, force) { if (!wallets) return next(); - utils.forEachSerial(wallets, function(id, next) { - self.get(id, function(err, wallet) { + utils.forEachSerial(wallets, function(wid, next) { + self.get(wid, function(err, wallet) { if (err) return next(err); @@ -1385,16 +1385,16 @@ WalletDB.prototype.addTX = function addTX(tx, callback, force) { wallets.length, tx.rhash); utils.forEachSerial(wallets, function(info, next) { - self.get(info.id, function(err, wallet) { + self.get(info.wid, function(err, wallet) { if (err) return next(err); if (!wallet) return next(); - self.logger.debug('Adding tx to wallet: %s', info.id); + self.logger.debug('Adding tx to wallet: %s', info.wid); - info.label = wallet.label; + info.id = wallet.id; wallet.tx.add(tx, info, function(err) { if (err) @@ -1413,20 +1413,20 @@ WalletDB.prototype.addTX = function addTX(tx, callback, force) { /** * Get the corresponding path for an address hash. - * @param {WalletID} id + * @param {WalletID} wid * @param {Hash} address * @param {Function} callback */ -WalletDB.prototype.getPath = function getPath(id, address, callback) { +WalletDB.prototype.getPath = function getPath(wid, address, callback) { this.getAddress(address, function(err, paths) { if (err) return callback(err); - if (!paths || !paths[id]) + if (!paths || !paths[wid]) return callback(); - return callback(null, paths[id]); + return callback(null, paths[wid]); }); }; @@ -1434,7 +1434,7 @@ WalletDB.prototype.getPath = function getPath(id, address, callback) { * Path * @constructor * @private - * @property {WalletID} id + * @property {WalletID} wid * @property {String} name - Account name. * @property {Number} account - Account index. * @property {Number} change - Change index. @@ -1446,14 +1446,14 @@ function Path() { if (!(this instanceof Path)) return new Path(); - this.id = null; + this.wid = null; this.name = null; this.account = 0; this.change = 0; this.index = 0; // NOTE: Passed in by caller. - this.label = null; + this.id = null; } /** @@ -1464,7 +1464,7 @@ function Path() { Path.prototype.fromRaw = function fromRaw(data) { var p = new BufferReader(data); - this.id = p.readU32(); + this.wid = p.readU32(); this.name = p.readVarString('utf8'); this.account = p.readU32(); this.change = p.readU32(); @@ -1490,7 +1490,7 @@ Path.fromRaw = function fromRaw(data) { Path.prototype.toRaw = function toRaw(writer) { var p = new BufferWriter(writer); - p.writeU32(this.id); + p.writeU32(this.wid); p.writeVarString(this.name, 'utf8'); p.writeU32(this.account); p.writeU32(this.change); @@ -1505,13 +1505,13 @@ Path.prototype.toRaw = function toRaw(writer) { /** * Inject properties from keyring. * @private - * @param {WalletID} id + * @param {WalletID} wid * @param {KeyRing} address */ Path.prototype.fromKeyRing = function fromKeyRing(address) { + this.wid = address.wid; this.id = address.id; - this.label = address.label; this.name = address.name; this.account = address.account; this.change = address.change; @@ -1521,7 +1521,7 @@ Path.prototype.fromKeyRing = function fromKeyRing(address) { /** * Instantiate path from keyring. - * @param {WalletID} id + * @param {WalletID} wid * @param {KeyRing} address * @returns {Path} */ @@ -1548,8 +1548,8 @@ Path.prototype.toPath = function() { Path.prototype.toJSON = function toJSON() { return { + wid: this.wid, id: this.id, - label: this.label, name: this.name, change: this.change === 1, path: this.toPath() @@ -1569,8 +1569,8 @@ Path.prototype.fromJSON = function fromJSON(json) { assert(indexes[0] >= 0); indexes[0] -= constants.hd.HARDENED; + this.wid = json.wid; this.id = json.id; - this.label = json.label; this.name = json.name; this.account = indexes[0]; this.change = indexes[1]; @@ -1595,8 +1595,8 @@ Path.fromJSON = function fromJSON(json) { */ Path.prototype.inspect = function() { - return ''; @@ -1606,9 +1606,9 @@ Path.prototype.inspect = function() { * Path Info */ -function PathInfo(db, id, tx, table) { +function PathInfo(db, wid, tx, table) { if (!(this instanceof PathInfo)) - return new PathInfo(db, id, tx, table); + return new PathInfo(db, wid, tx, table); // Reference to the walletdb. this.db = db; @@ -1621,10 +1621,10 @@ function PathInfo(db, id, tx, table) { this.paths = []; // Wallet ID - this.id = id; + this.wid = wid; // Wallet Label (passed in by caller). - this.label = null; + this.id = null; // Map of address hashes->paths (for everything). this.table = null; @@ -1647,14 +1647,14 @@ PathInfo.map = function map(db, tx, table) { var hashes = Object.keys(table); var wallets = {}; var info = []; - var i, j, hash, paths, path, id; + var i, j, hash, paths, path, wid; for (i = 0; i < hashes.length; i++) { hash = hashes[i]; paths = table[hash]; for (j = 0; j < paths.length; j++) { path = paths[j]; - wallets[path.id] = true; + wallets[path.wid] = true; } } @@ -1664,8 +1664,8 @@ PathInfo.map = function map(db, tx, table) { return; for (i = 0; i < wallets.length; i++) { - id = +wallets[i]; - info.push(new PathInfo(db, id, tx, table)); + wid = +wallets[i]; + info.push(new PathInfo(db, wid, tx, table)); } return info; @@ -1685,7 +1685,7 @@ PathInfo.prototype.fromTX = function fromTX(tx, table) { paths = table[hash]; for (j = 0; j < paths.length; j++) { path = paths[j]; - if (path.id !== this.id) + if (path.wid !== this.wid) continue; this.pathMap[hash] = path; if (!uniq[path.account]) { @@ -1702,7 +1702,7 @@ PathInfo.prototype.fromTX = function fromTX(tx, table) { paths = table[hash]; for (j = 0; j < paths.length; j++) { path = paths[j]; - if (path.id !== this.id) + if (path.wid !== this.wid) continue; this.paths.push(path); } @@ -1711,8 +1711,8 @@ PathInfo.prototype.fromTX = function fromTX(tx, table) { return this; }; -PathInfo.fromTX = function fromTX(db, id, tx, table) { - return new PathInfo(db, id).fromTX(tx, table); +PathInfo.fromTX = function fromTX(db, wid, tx, table) { + return new PathInfo(db, wid).fromTX(tx, table); }; /** @@ -1775,7 +1775,7 @@ function parsePaths(data) { while (p.left()) { path = Path.fromRaw(p); - out[path.id] = path; + out[path.wid] = path; } return out; @@ -1784,11 +1784,11 @@ function parsePaths(data) { function serializePaths(out) { var p = new BufferWriter(); var keys = Object.keys(out); - var i, id, path; + var i, wid, path; for (i = 0; i < keys.length; i++) { - id = keys[i]; - path = out[id]; + wid = keys[i]; + path = out[wid]; path.toRaw(p); } @@ -1801,7 +1801,7 @@ function serializeWallets(wallets) { for (i = 0; i < wallets.length; i++) { info = wallets[i]; - p.writeU32(info.id); + p.writeU32(info.wid); } return p.render(); diff --git a/test/http-test.js b/test/http-test.js index 0d5251d5..09e4d3ae 100644 --- a/test/http-test.js +++ b/test/http-test.js @@ -36,7 +36,8 @@ describe('HTTP', function() { var node = new bcoin.fullnode({ network: 'regtest', apiKey: 'foo', - walletAuth: true + walletAuth: true, + db: 'memory' }); var wallet = new bcoin.http.wallet({