wallet: rename label to id and id to wid.

This commit is contained in:
Christopher Jeffrey 2016-08-15 04:14:13 -07:00
parent 9e2dd9145f
commit b6e9019d56
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
5 changed files with 249 additions and 247 deletions

View File

@ -41,8 +41,8 @@ function KeyRing(options) {
this.m = 1; this.m = 1;
this.n = 1; this.n = 1;
this.witness = false; this.witness = false;
this.id = 0; this.wid = 0;
this.label = null; this.id = null;
this.name = null; this.name = null;
this.account = 0; this.account = 0;
this.change = 0; this.change = 0;
@ -99,14 +99,14 @@ KeyRing.prototype.fromOptions = function fromOptions(options) {
this.witness = options.witness; this.witness = options.witness;
} }
if (options.id) { if (options.wid) {
assert(utils.isNumber(options.id)); assert(utils.isNumber(options.wid));
this.id = options.id; this.wid = options.wid;
} }
if (options.label) { if (options.id) {
assert(utils.isAlpha(options.label)); assert(utils.isAlpha(options.id));
this.label = options.label; this.id = options.id;
} }
if (options.name) { if (options.name) {
@ -661,8 +661,8 @@ KeyRing.prototype.toJSON = function toJSON() {
m: this.m, m: this.m,
n: this.n, n: this.n,
witness: this.witness, witness: this.witness,
wid: this.wid,
id: this.id, id: this.id,
label: this.label,
name: this.name, name: this.name,
account: this.account, account: this.account,
change: this.change, change: this.change,
@ -690,8 +690,8 @@ KeyRing.prototype.fromJSON = function fromJSON(json) {
assert(utils.isNumber(json.m)); assert(utils.isNumber(json.m));
assert(utils.isNumber(json.n)); assert(utils.isNumber(json.n));
assert(typeof json.witness === 'boolean'); assert(typeof json.witness === 'boolean');
assert(!json.id || utils.isNumber(json.id)); assert(!json.wid || utils.isNumber(json.wid));
assert(!json.label || utils.isAlpha(json.label)); assert(!json.id || utils.isAlpha(json.id));
assert(!json.name || utils.isAlpha(json.name)); assert(!json.name || utils.isAlpha(json.name));
assert(utils.isNumber(json.account)); assert(utils.isNumber(json.account));
assert(utils.isNumber(json.change)); assert(utils.isNumber(json.change));
@ -704,7 +704,7 @@ KeyRing.prototype.fromJSON = function fromJSON(json) {
this.m = json.m; this.m = json.m;
this.n = json.n; this.n = json.n;
this.witness = json.witness; this.witness = json.witness;
this.id = json.id; this.wid = json.wid;
this.name = json.name; this.name = json.name;
this.account = json.account; this.account = json.account;
this.change = json.change; this.change = json.change;
@ -741,8 +741,8 @@ KeyRing.prototype.toRaw = function toRaw(writer) {
p.writeU8(this.m); p.writeU8(this.m);
p.writeU8(this.n); p.writeU8(this.n);
p.writeU8(this.witness ? 1 : 0); p.writeU8(this.witness ? 1 : 0);
p.writeU32(this.id); p.writeU32(this.wid);
p.writeVarString(this.label, 'utf8'); p.writeVarString(this.id, 'utf8');
p.writeVarString(this.name, 'utf8'); p.writeVarString(this.name, 'utf8');
p.writeU32(this.account); p.writeU32(this.account);
p.writeU32(this.change); p.writeU32(this.change);
@ -774,8 +774,8 @@ KeyRing.prototype.fromRaw = function fromRaw(data) {
this.m = p.readU8(); this.m = p.readU8();
this.n = p.readU8(); this.n = p.readU8();
this.witness = p.readU8() === 1; this.witness = p.readU8() === 1;
this.id = p.readU32(); this.wid = p.readU32();
this.label = p.readVarString('utf8'); this.id = p.readVarString('utf8');
this.name = p.readVarString('utf8'); this.name = p.readVarString('utf8');
this.account = p.readU32(); this.account = p.readU32();
this.change = p.readU32(); this.change = p.readU32();

View File

@ -89,8 +89,8 @@ TXDB.prototype.open = function open(callback) {
*/ */
TXDB.prototype.prefix = function prefix(key) { TXDB.prototype.prefix = function prefix(key) {
assert(this.wallet.id); assert(this.wallet.wid);
return 't/' + pad32(this.wallet.id) + '/' + key; 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) { TXDB.prototype.add = function add(tx, info, callback) {
var self = this; var self = this;
var unlock = this._lock(add, [tx, info, callback]); var unlock = this._lock(add, [tx, info, callback]);
var hash, i, path, id; var hash, i, path, account;
if (!unlock) if (!unlock)
return; return;
@ -515,13 +515,13 @@ TXDB.prototype.add = function add(tx, info, callback) {
self.put('m/' + pad32(tx.ps) + '/' + hash, DUMMY); self.put('m/' + pad32(tx.ps) + '/' + hash, DUMMY);
for (i = 0; i < info.accounts.length; i++) { for (i = 0; i < info.accounts.length; i++) {
id = pad32(info.accounts[i]); account = pad32(info.accounts[i]);
self.put('T/' + id + '/' + hash, DUMMY); self.put('T/' + account + '/' + hash, DUMMY);
if (tx.ts === 0) if (tx.ts === 0)
self.put('P/' + id + '/' + hash, DUMMY); self.put('P/' + account + '/' + hash, DUMMY);
else else
self.put('H/' + id + '/' + pad32(tx.height) + '/' + hash, DUMMY); self.put('H/' + account + '/' + pad32(tx.height) + '/' + hash, DUMMY);
self.put('M/' + id + '/' + pad32(tx.ps) + '/' + hash, DUMMY); self.put('M/' + account + '/' + pad32(tx.ps) + '/' + hash, DUMMY);
} }
// Consume unspent money or add orphans // 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) { TXDB.prototype._confirm = function _confirm(tx, info, callback) {
var self = this; var self = this;
var hash = tx.hash('hex'); var hash = tx.hash('hex');
var i, id; var i, account;
this.getTX(hash, function(err, existing) { this.getTX(hash, function(err, existing) {
if (err) if (err)
@ -806,9 +806,9 @@ TXDB.prototype._confirm = function _confirm(tx, info, callback) {
self.put('h/' + pad32(tx.height) + '/' + hash, DUMMY); self.put('h/' + pad32(tx.height) + '/' + hash, DUMMY);
for (i = 0; i < info.accounts.length; i++) { for (i = 0; i < info.accounts.length; i++) {
id = pad32(info.accounts[i]); account = pad32(info.accounts[i]);
self.del('P/' + id + '/' + hash); self.del('P/' + account + '/' + hash);
self.put('H/' + id + '/' + pad32(tx.height) + '/' + hash, DUMMY); self.put('H/' + account + '/' + pad32(tx.height) + '/' + hash, DUMMY);
} }
utils.forEachSerial(tx.outputs, function(output, next, i) { 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) { TXDB.prototype._remove = function remove(tx, info, callback) {
var self = this; var self = this;
var hash = tx.hash('hex'); 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); this.del('t/' + hash);
@ -924,13 +924,13 @@ TXDB.prototype._remove = function remove(tx, info, callback) {
this.del('m/' + pad32(tx.ps) + '/' + hash); this.del('m/' + pad32(tx.ps) + '/' + hash);
for (i = 0; i < info.accounts.length; i++) { for (i = 0; i < info.accounts.length; i++) {
id = pad32(info.accounts[i]); account = pad32(info.accounts[i]);
this.del('T/' + id + '/' + hash); this.del('T/' + account + '/' + hash);
if (tx.ts === 0) if (tx.ts === 0)
this.del('P/' + id + '/' + hash); this.del('P/' + account + '/' + hash);
else else
this.del('H/' + id + '/' + pad32(tx.height) + '/' + hash); this.del('H/' + account + '/' + pad32(tx.height) + '/' + hash);
this.del('M/' + id + '/' + pad32(tx.ps) + '/' + hash); this.del('M/' + account + '/' + pad32(tx.ps) + '/' + hash);
} }
this.fillHistory(tx, function(err) { this.fillHistory(tx, function(err) {
@ -1050,7 +1050,7 @@ TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) {
var self = this; var self = this;
var hash = tx.hash('hex'); var hash = tx.hash('hex');
var height = tx.height; var height = tx.height;
var i, id; var i, account;
if (height !== -1) if (height !== -1)
return callback(null, false, info); return callback(null, false, info);
@ -1066,9 +1066,9 @@ TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) {
this.del('h/' + pad32(height) + '/' + hash); this.del('h/' + pad32(height) + '/' + hash);
for (i = 0; i < info.accounts.length; i++) { for (i = 0; i < info.accounts.length; i++) {
id = pad32(info.accounts[i]); account = pad32(info.accounts[i]);
this.put('P/' + id + '/' + hash, DUMMY); this.put('P/' + account + '/' + hash, DUMMY);
this.del('H/' + id + '/' + pad32(height) + '/' + hash); this.del('H/' + account + '/' + pad32(height) + '/' + hash);
} }
utils.forEachSerial(tx.outputs, function(output, next, i) { utils.forEachSerial(tx.outputs, function(output, next, i) {
@ -1950,8 +1950,8 @@ TXDB.prototype.abandon = function abandon(hash, callback, force) {
function Details(info) { function Details(info) {
this.db = info.db; this.db = info.db;
this.network = info.db.network; this.network = info.db.network;
this.wid = info.wid;
this.id = info.id; this.id = info.id;
this.label = info.label;
this.hash = info.tx.hash('hex'); this.hash = info.tx.hash('hex');
this.height = info.tx.height; this.height = info.tx.height;
this.block = info.tx.block; this.block = info.tx.block;
@ -1993,8 +1993,8 @@ Details.prototype._insert = function _insert(vector, target, table) {
for (j = 0; j < paths.length; j++) { for (j = 0; j < paths.length; j++) {
path = paths[j]; path = paths[j];
if (path.id === this.id) { if (path.wid === this.wid) {
path.label = this.label; path.id = this.id;
member.path = path; member.path = path;
break; break;
} }
@ -2008,8 +2008,8 @@ Details.prototype._insert = function _insert(vector, target, table) {
Details.prototype.toJSON = function toJSON() { Details.prototype.toJSON = function toJSON() {
var self = this; var self = this;
return { return {
wid: this.wid,
id: this.id, id: this.id,
label: this.label,
hash: utils.revHex(this.hash), hash: utils.revHex(this.hash),
height: this.height, height: this.height,
block: this.block ? utils.revHex(this.block) : null, block: this.block ? utils.revHex(this.block) : null,
@ -2055,8 +2055,8 @@ DetailsMember.prototype.toJSON = function toJSON(network) {
*/ */
function Balance(wallet) { function Balance(wallet) {
this.wid = wallet.wid;
this.id = wallet.id; this.id = wallet.id;
this.label = wallet.label;
this.unconfirmed = 0; this.unconfirmed = 0;
this.confirmed = 0; this.confirmed = 0;
this.total = 0; this.total = 0;
@ -2090,8 +2090,8 @@ Balance.prototype.unconfirm = function unconfirm(value) {
Balance.prototype.toJSON = function toJSON() { Balance.prototype.toJSON = function toJSON() {
return { return {
wid: this.wid,
id: this.id, id: this.id,
label: this.label,
unconfirmed: utils.btc(this.unconfirmed), unconfirmed: utils.btc(this.unconfirmed),
confirmed: utils.btc(this.confirmed), confirmed: utils.btc(this.confirmed),
total: utils.btc(this.total) total: utils.btc(this.total)

View File

@ -56,8 +56,8 @@ function Wallet(db, options) {
this.writeLock = new bcoin.locker(this); this.writeLock = new bcoin.locker(this);
this.fundLock = new bcoin.locker(this); this.fundLock = new bcoin.locker(this);
this.id = 0; this.wid = 0;
this.label = null; this.id = null;
this.master = null; this.master = null;
this.initialized = false; this.initialized = false;
this.accountDepth = 0; this.accountDepth = 0;
@ -81,7 +81,7 @@ utils.inherits(Wallet, EventEmitter);
Wallet.prototype.fromOptions = function fromOptions(options) { Wallet.prototype.fromOptions = function fromOptions(options) {
var master = options.master; var master = options.master;
var label, token; var id, token;
if (!master) if (!master)
master = bcoin.hd.fromMnemonic(null, this.network); master = bcoin.hd.fromMnemonic(null, this.network);
@ -106,18 +106,18 @@ Wallet.prototype.fromOptions = function fromOptions(options) {
this.accountDepth = options.accountDepth; this.accountDepth = options.accountDepth;
} }
if (options.id != null) { if (options.wid != null) {
assert(utils.isNumber(options.id)); assert(utils.isNumber(options.wid));
this.id = options.id; this.wid = options.wid;
} }
if (options.label) { if (options.id) {
assert(utils.isAlpha(options.label), 'Wallet ID must be alphanumeric.'); assert(utils.isAlpha(options.id), 'Wallet ID must be alphanumeric.');
label = options.label; id = options.id;
} }
if (!label) if (!id)
label = this.getLabel(); id = this.getLabel();
if (options.token) { if (options.token) {
assert(Buffer.isBuffer(options.token)); assert(Buffer.isBuffer(options.token));
@ -133,7 +133,7 @@ Wallet.prototype.fromOptions = function fromOptions(options) {
if (!token) if (!token)
token = this.getToken(this.master.key, this.tokenDepth); token = this.getToken(this.master.key, this.tokenDepth);
this.label = label; this.id = id;
this.token = token; this.token = token;
return this; return this;
@ -497,8 +497,8 @@ Wallet.prototype.createAccount = function createAccount(options, callback, force
options = { options = {
network: self.network, network: self.network,
wid: self.wid,
id: self.id, id: self.id,
label: self.label,
name: self.accountDepth === 0 ? 'default' : options.name, name: self.accountDepth === 0 ? 'default' : options.name,
witness: options.witness, witness: options.witness,
accountKey: key.hdPublicKey, accountKey: key.hdPublicKey,
@ -558,7 +558,7 @@ Wallet.prototype.ensureAccount = function ensureAccount(options, callback) {
*/ */
Wallet.prototype.getAccounts = function getAccounts(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) { 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); 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) if (err)
return callback(err); return callback(err);
if (!account) if (!account)
return callback(); return callback();
account.wid = self.wid;
account.id = self.id; account.id = self.id;
account.label = self.label;
return callback(null, account); return callback(null, account);
}); });
@ -605,7 +605,7 @@ Wallet.prototype.getAccount = function getAccount(account, callback) {
*/ */
Wallet.prototype.hasAccount = function hasAccount(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() { 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() { 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) { 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) { 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) { 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) if (account == null)
return callback.call(this, null, errback); 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) if (err)
return errback(err); return errback(err);
@ -1896,8 +1896,8 @@ Wallet.prototype.__defineGetter__('changeAddress', function() {
Wallet.prototype.inspect = function inspect() { Wallet.prototype.inspect = function inspect() {
return { return {
wid: this.wid,
id: this.id, id: this.id,
label: this.label,
network: this.network.type, network: this.network.type,
initialized: this.initialized, initialized: this.initialized,
accountDepth: this.accountDepth, accountDepth: this.accountDepth,
@ -1918,8 +1918,8 @@ Wallet.prototype.inspect = function inspect() {
Wallet.prototype.toJSON = function toJSON() { Wallet.prototype.toJSON = function toJSON() {
return { return {
network: this.network.type, network: this.network.type,
wid: this.wid,
id: this.id, id: this.id,
label: this.label,
initialized: this.initialized, initialized: this.initialized,
accountDepth: this.accountDepth, accountDepth: this.accountDepth,
token: this.token.toString('hex'), token: this.token.toString('hex'),
@ -1936,17 +1936,17 @@ Wallet.prototype.toJSON = function toJSON() {
*/ */
Wallet.prototype.fromJSON = function fromJSON(json) { Wallet.prototype.fromJSON = function fromJSON(json) {
assert(utils.isNumber(json.id)); assert(utils.isNumber(json.wid));
assert(typeof json.initialized === 'boolean'); 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(utils.isNumber(json.accountDepth));
assert(typeof json.token === 'string'); assert(typeof json.token === 'string');
assert(json.token.length === 64); assert(json.token.length === 64);
assert(utils.isNumber(json.tokenDepth)); assert(utils.isNumber(json.tokenDepth));
this.network = bcoin.network.get(json.network); this.network = bcoin.network.get(json.network);
this.wid = json.wid;
this.id = json.id; this.id = json.id;
this.label = json.label;
this.initialized = json.initialized; this.initialized = json.initialized;
this.accountDepth = json.accountDepth; this.accountDepth = json.accountDepth;
this.token = new Buffer(json.token, 'hex'); this.token = new Buffer(json.token, 'hex');
@ -1964,8 +1964,8 @@ Wallet.prototype.toRaw = function toRaw(writer) {
var p = new BufferWriter(writer); var p = new BufferWriter(writer);
p.writeU32(this.network.magic); p.writeU32(this.network.magic);
p.writeU32(this.id); p.writeU32(this.wid);
p.writeVarString(this.label, 'utf8'); p.writeVarString(this.id, 'utf8');
p.writeU8(this.initialized ? 1 : 0); p.writeU8(this.initialized ? 1 : 0);
p.writeU32(this.accountDepth); p.writeU32(this.accountDepth);
p.writeBytes(this.token); p.writeBytes(this.token);
@ -1987,8 +1987,8 @@ Wallet.prototype.toRaw = function toRaw(writer) {
Wallet.prototype.fromRaw = function fromRaw(data) { Wallet.prototype.fromRaw = function fromRaw(data) {
var p = new BufferReader(data); var p = new BufferReader(data);
this.network = bcoin.network.fromMagic(p.readU32()); this.network = bcoin.network.fromMagic(p.readU32());
this.id = p.readU32(); this.wid = p.readU32();
this.label = p.readVarString('utf8'); this.id = p.readVarString('utf8');
this.initialized = p.readU8() === 1; this.initialized = p.readU8() === 1;
this.accountDepth = p.readU32(); this.accountDepth = p.readU32();
this.token = p.readBytes(32); this.token = p.readBytes(32);
@ -2050,7 +2050,7 @@ Wallet.isWallet = function isWallet(obj) {
* (default=pubkeyhash). * (default=pubkeyhash).
* @param {Number?} options.m - `m` value for multisig. * @param {Number?} options.m - `m` value for multisig.
* @param {Number?} options.n - `n` 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 * @param {String?} options.name - Account name
*/ */
@ -2069,8 +2069,8 @@ function Account(db, options) {
this.receiveAddress = null; this.receiveAddress = null;
this.changeAddress = null; this.changeAddress = null;
this.id = 0; this.wid = 0;
this.label = null; this.id = null;
this.name = null; this.name = null;
this.witness = this.db.options.witness; this.witness = this.db.options.witness;
this.accountKey = null; this.accountKey = null;
@ -2099,13 +2099,13 @@ Account.prototype.fromOptions = function fromOptions(options) {
var i; var i;
assert(options, 'Options are required.'); assert(options, 'Options are required.');
assert(utils.isNumber(options.id)); assert(utils.isNumber(options.wid));
assert(utils.isAlpha(options.label), 'Wallet ID must be alphanumeric.'); assert(utils.isAlpha(options.id), 'Wallet ID must be alphanumeric.');
assert(bcoin.hd.isHD(options.accountKey), 'Account key is required.'); assert(bcoin.hd.isHD(options.accountKey), 'Account key is required.');
assert(utils.isNumber(options.accountIndex), 'Account index is required.'); assert(utils.isNumber(options.accountIndex), 'Account index is required.');
this.wid = options.wid;
this.id = options.id; this.id = options.id;
this.label = options.label;
if (options.name != null) { if (options.name != null) {
assert(utils.isAlpha(options.name), 'Account name must be alphanumeric.'); assert(utils.isAlpha(options.name), 'Account name must be alphanumeric.');
@ -2385,7 +2385,7 @@ Account.prototype._checkKeys = function _checkKeys(callback) {
if (!paths) if (!paths)
return callback(null, false); 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({ return new bcoin.keyring({
network: this.network, network: this.network,
key: key.publicKey, key: key.publicKey,
wid: this.wid,
id: this.id, id: this.id,
label: this.label,
name: this.name, name: this.name,
account: this.accountIndex, account: this.accountIndex,
change: change, change: change,
@ -2540,7 +2540,7 @@ Account.prototype.save = function save() {
*/ */
Account.prototype.saveAddress = function saveAddress(address, callback) { 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() { Account.prototype.inspect = function inspect() {
return { return {
id: this.id, wid: this.wid,
name: this.name, name: this.name,
network: this.network, network: this.network,
initialized: this.initialized, initialized: this.initialized,
@ -2638,7 +2638,7 @@ Account.prototype.inspect = function inspect() {
Account.prototype.toJSON = function toJSON() { Account.prototype.toJSON = function toJSON() {
return { return {
network: this.network.type, network: this.network.type,
id: this.id, wid: this.wid,
name: this.name, name: this.name,
initialized: this.initialized, initialized: this.initialized,
type: this.type, type: this.type,
@ -2674,7 +2674,8 @@ Account.prototype.fromJSON = function fromJSON(json) {
var i; var i;
assert.equal(json.network, this.network.type); 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(utils.isAlpha(json.name), 'Account name must be alphanumeric.');
assert(typeof json.initialized === 'boolean'); assert(typeof json.initialized === 'boolean');
assert(json.type === 'pubkeyhash' || json.type === 'multisig'); assert(json.type === 'pubkeyhash' || json.type === 'multisig');
@ -2686,7 +2687,7 @@ Account.prototype.fromJSON = function fromJSON(json) {
assert(utils.isNumber(json.changeDepth)); assert(utils.isNumber(json.changeDepth));
assert(Array.isArray(json.keys)); assert(Array.isArray(json.keys));
this.id = json.id; this.wid = json.wid;
this.name = json.name; this.name = json.name;
this.initialized = json.initialized; this.initialized = json.initialized;
this.type = json.type; this.type = json.type;
@ -2715,8 +2716,8 @@ Account.prototype.toRaw = function toRaw(writer) {
p.writeU32(this.network.magic); p.writeU32(this.network.magic);
// NOTE: Passed in by caller. // NOTE: Passed in by caller.
// p.writeU32(this.id); // p.writeU32(this.wid);
// p.writeVarString(this.label, 'utf8'); // p.writeVarString(this.id, 'utf8');
p.writeVarString(this.name, 'utf8'); p.writeVarString(this.name, 'utf8');
p.writeU8(this.initialized ? 1 : 0); p.writeU8(this.initialized ? 1 : 0);
p.writeU8(this.type === 'pubkeyhash' ? 0 : 1); p.writeU8(this.type === 'pubkeyhash' ? 0 : 1);
@ -2751,8 +2752,8 @@ Account.prototype.fromRaw = function fromRaw(data) {
this.network = bcoin.network.fromMagic(p.readU32()); this.network = bcoin.network.fromMagic(p.readU32());
// NOTE: Passed in by caller. // NOTE: Passed in by caller.
// this.id = p.readU32(); // this.wid = p.readU32();
// this.label = p.readVarString('utf8'); // this.id = p.readVarString('utf8');
this.name = p.readVarString('utf8'); this.name = p.readVarString('utf8');
this.initialized = p.readU8() === 1; this.initialized = p.readU8() === 1;
this.type = p.readU8() === 0 ? 'pubkeyhash' : 'multisig'; this.type = p.readU8() === 0 ? 'pubkeyhash' : 'multisig';

View File

@ -10,14 +10,14 @@
/* /*
* Database Layout: * Database Layout:
* (inherits all from txdb) * (inherits all from txdb)
* p/[address] -> id & path data * p/[address] -> wid & path data
* w/[id] -> wallet * w/[wid] -> wallet
* l/[label] -> wallet id * l/[label] -> wallet wid
* a/[id]/[index] -> account * a/[wid]/[index] -> account
* i/[id]/[name] -> account index * i/[wid]/[name] -> account index
* R -> tip * R -> tip
* b/[hash] -> wallet block * b/[hash] -> wallet block
* t/[hash] -> tx->wallet-id map * t/[hash] -> tx->wallet-wid map
*/ */
var bcoin = require('./env'); 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 * @private
* @param {Function} callback * @param {Function} callback
*/ */
@ -186,7 +186,7 @@ WalletDB.prototype.getDepth = function getDepth(callback) {
// a "scoped" state. So, we avoid all the // a "scoped" state. So, we avoid all the
// nonsense of adding a global lock to // nonsense of adding a global lock to
// walletdb.create by simply seeking to the // walletdb.create by simply seeking to the
// highest wallet id. // highest wallet wid.
iter = this.db.iterator({ iter = this.db.iterator({
gte: 'w/' + pad32(0), gte: 'w/' + pad32(0),
lte: 'w/' + pad32(0xffffffff), lte: 'w/' + pad32(0xffffffff),
@ -223,38 +223,38 @@ WalletDB.prototype.getDepth = function getDepth(callback) {
/** /**
* Start batch. * Start batch.
* @private * @private
* @param {WalletID} id * @param {WalletID} wid
*/ */
WalletDB.prototype.start = function start(id) { WalletDB.prototype.start = function start(wid) {
assert(utils.isNumber(id), 'Bad ID for batch.'); assert(utils.isNumber(wid), 'Bad ID for batch.');
assert(!this.batches[id], 'Batch already started.'); assert(!this.batches[wid], 'Batch already started.');
this.batches[id] = this.db.batch(); this.batches[wid] = this.db.batch();
}; };
/** /**
* Drop batch. * Drop batch.
* @private * @private
* @param {WalletID} id * @param {WalletID} wid
*/ */
WalletDB.prototype.drop = function drop(id) { WalletDB.prototype.drop = function drop(wid) {
var batch = this.batch(id); var batch = this.batch(wid);
batch.clear(); batch.clear();
delete this.batches[id]; delete this.batches[wid];
}; };
/** /**
* Get batch. * Get batch.
* @private * @private
* @param {WalletID} id * @param {WalletID} wid
* @returns {Leveldown.Batch} * @returns {Leveldown.Batch}
*/ */
WalletDB.prototype.batch = function batch(id) { WalletDB.prototype.batch = function batch(wid) {
var batch; var batch;
assert(utils.isNumber(id), 'Bad ID for batch.'); assert(utils.isNumber(wid), 'Bad ID for batch.');
batch = this.batches[id]; batch = this.batches[wid];
assert(batch, 'Batch does not exist.'); assert(batch, 'Batch does not exist.');
return batch; return batch;
}; };
@ -262,13 +262,13 @@ WalletDB.prototype.batch = function batch(id) {
/** /**
* Save batch. * Save batch.
* @private * @private
* @param {WalletID} id * @param {WalletID} wid
* @param {Function} callback * @param {Function} callback
*/ */
WalletDB.prototype.commit = function commit(id, callback) { WalletDB.prototype.commit = function commit(wid, callback) {
var batch = this.batch(id); var batch = this.batch(wid);
delete this.batches[id]; delete this.batches[wid];
batch.write(callback); batch.write(callback);
}; };
@ -343,8 +343,8 @@ WalletDB.prototype.dump = function dump(callback) {
*/ */
WalletDB.prototype.register = function register(wallet) { WalletDB.prototype.register = function register(wallet) {
assert(!this.wallets[wallet.id]); assert(!this.wallets[wallet.wid]);
this.wallets[wallet.id] = wallet; this.wallets[wallet.wid] = wallet;
}; };
/** /**
@ -354,8 +354,8 @@ WalletDB.prototype.register = function register(wallet) {
*/ */
WalletDB.prototype.unregister = function unregister(wallet) { WalletDB.prototype.unregister = function unregister(wallet) {
assert(this.wallets[wallet.id]); assert(this.wallets[wallet.wid]);
delete this.wallets[wallet.id]; delete this.wallets[wallet.wid];
}; };
/** /**
@ -364,44 +364,44 @@ WalletDB.prototype.unregister = function unregister(wallet) {
* @param {Function} callback * @param {Function} callback
*/ */
WalletDB.prototype.getWalletID = function getWalletID(label, callback) { WalletDB.prototype.getWalletID = function getWalletID(id, callback) {
var id; var wid;
if (!label) if (!id)
return callback(); return callback();
if (typeof label === 'number') if (typeof id === 'number')
return callback(null, label);
id = this.walletCache.get(label);
if (id)
return callback(null, id); return callback(null, id);
this.db.fetch('l/' + label, function(data) { wid = this.walletCache.get(id);
id = data.readUInt32LE(0, true);
self.walletCache.set(label, id); if (wid)
return id; return callback(null, wid);
this.db.fetch('l/' + id, function(data) {
wid = data.readUInt32LE(0, true);
self.walletCache.set(id, wid);
return wid;
}, callback); }, callback);
}; };
/** /**
* Get a wallet from the database, setup watcher. * Get a wallet from the database, setup watcher.
* @param {WalletID} id * @param {WalletID} wid
* @param {Function} callback - Returns [Error, {@link Wallet}]. * @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 self = this;
this.getWalletID(id, function(err, id) { this.getWalletID(wid, function(err, wid) {
if (err) if (err)
return callback(err); return callback(err);
if (!id) if (!wid)
return callback(); return callback();
self._get(id, function(err, wallet, watched) { self._get(wid, function(err, wallet, watched) {
if (err) if (err)
return callback(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. * Get a wallet from the database, do not setup watcher.
* @private * @private
* @param {WalletID} id * @param {WalletID} wid
* @param {Function} callback - Returns [Error, {@link Wallet}]. * @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 self = this;
var unlock, wallet; var unlock, wallet;
unlock = this.readLock.lock(id, get, [id, callback]); unlock = this.readLock.lock(wid, get, [wid, callback]);
if (!unlock) if (!unlock)
return; return;
callback = utils.wrap(callback, unlock); callback = utils.wrap(callback, unlock);
wallet = this.wallets[id]; wallet = this.wallets[wid];
if (wallet) if (wallet)
return callback(null, wallet, true); 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); return bcoin.wallet.fromRaw(self, data);
}, callback); }, callback);
}; };
@ -462,23 +462,23 @@ WalletDB.prototype._get = function get(id, callback) {
*/ */
WalletDB.prototype.save = function save(wallet) { WalletDB.prototype.save = function save(wallet) {
var batch = this.batch(wallet.id); var batch = this.batch(wallet.wid);
var id = new Buffer(4); var wid = new Buffer(4);
this.walletCache.set(wallet.label, wallet.id); this.walletCache.set(wallet.id, wallet.wid);
batch.put('w/' + pad32(wallet.id), wallet.toRaw()); batch.put('w/' + pad32(wallet.wid), wallet.toRaw());
id.writeUInt32LE(wallet.id, 0, true); wid.writeUInt32LE(wallet.wid, 0, true);
batch.put('l/' + wallet.label, id); batch.put('l/' + wallet.id, wid);
}; };
/** /**
* Test an api key against a wallet's api key. * Test an api key against a wallet's api key.
* @param {WalletID} id * @param {WalletID} wid
* @param {String} token * @param {String} token
* @param {Function} callback * @param {Function} callback
*/ */
WalletDB.prototype.auth = function auth(id, token, callback) { WalletDB.prototype.auth = function auth(wid, token, callback) {
this.get(id, function(err, wallet) { this.get(wid, function(err, wallet) {
if (err) if (err)
return callback(err); return callback(err);
@ -514,14 +514,14 @@ WalletDB.prototype.create = function create(options, callback) {
options = {}; options = {};
} }
unlock = this.writeLock.lock(options.label, create, [options, callback]); unlock = this.writeLock.lock(options.id, create, [options, callback]);
if (!unlock) if (!unlock)
return; return;
callback = utils.wrap(callback, unlock); callback = utils.wrap(callback, unlock);
this.has(options.label, function(err, exists) { this.has(options.id, function(err, exists) {
if (err) if (err)
return callback(err); return callback(err);
@ -533,7 +533,7 @@ WalletDB.prototype.create = function create(options, callback) {
try { try {
wallet = bcoin.wallet.fromOptions(self, options); wallet = bcoin.wallet.fromOptions(self, options);
wallet.id = self.depth++; wallet.wid = self.depth++;
} catch (e) { } catch (e) {
return callback(e); return callback(e);
} }
@ -548,7 +548,7 @@ WalletDB.prototype.create = function create(options, callback) {
if (err) if (err)
return callback(err); return callback(err);
self.logger.info('Created wallet %s.', wallet.label); self.logger.info('Created wallet %s.', wallet.id);
return callback(null, wallet); return callback(null, wallet);
}); });
@ -557,21 +557,21 @@ WalletDB.prototype.create = function create(options, callback) {
/** /**
* Test for the existence of a wallet. * Test for the existence of a wallet.
* @param {WalletID?} id * @param {WalletID?} wid
* @param {Function} callback * @param {Function} callback
*/ */
WalletDB.prototype.has = function has(id, callback) { WalletDB.prototype.has = function has(wid, callback) {
this.getWalletID(id, function(err, id) { this.getWalletID(wid, function(err, wid) {
if (err) if (err)
return callback(err); return callback(err);
return callback(null, id != null); return callback(null, wid != null);
}); });
}; };
/** /**
* Attempt to create wallet, return wallet if already exists. * Attempt to create wallet, return wallet if already exists.
* @param {WalletID?} id * @param {WalletID?} wid
* @param {Object} options - See {@link Wallet}. * @param {Object} options - See {@link Wallet}.
* @param {Function} callback * @param {Function} callback
*/ */
@ -579,7 +579,7 @@ WalletDB.prototype.has = function has(id, callback) {
WalletDB.prototype.ensure = function ensure(options, callback) { WalletDB.prototype.ensure = function ensure(options, callback) {
var self = this; var self = this;
this.get(options.label, function(err, wallet) { this.get(options.id, function(err, wallet) {
if (err) if (err)
return callback(err); return callback(err);
@ -592,22 +592,22 @@ WalletDB.prototype.ensure = function ensure(options, callback) {
/** /**
* Get an account from the database. * Get an account from the database.
* @param {WalletID} id * @param {WalletID} wid
* @param {String|Number} name - Account name/index. * @param {String|Number} name - Account name/index.
* @param {Function} callback - Returns [Error, {@link Wallet}]. * @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; var self = this;
this.getAccountIndex(id, name, function(err, index) { this.getAccountIndex(wid, name, function(err, index) {
if (err) if (err)
return callback(err); return callback(err);
if (index === -1) if (index === -1)
return callback(); return callback();
self._getAccount(id, index, function(err, account) { self._getAccount(wid, index, function(err, account) {
if (err) if (err)
return callback(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. * Get an account from the database. Do not setup watcher.
* @private * @private
* @param {WalletID} id * @param {WalletID} wid
* @param {Number} index - Account index. * @param {Number} index - Account index.
* @param {Function} callback - Returns [Error, {@link Wallet}]. * @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 self = this;
var key = pad32(id) + '/' + pad32(index); var key = pad32(wid) + '/' + pad32(index);
var account = this.accountCache.get(key); var account = this.accountCache.get(key);
if (account) if (account)
@ -649,20 +649,20 @@ WalletDB.prototype._getAccount = function getAccount(id, index, callback) {
/** /**
* List account names and indexes from the db. * List account names and indexes from the db.
* @param {WalletID} id * @param {WalletID} wid
* @param {Function} callback - Returns [Error, Array]. * @param {Function} callback - Returns [Error, Array].
*/ */
WalletDB.prototype.getAccounts = function getAccounts(id, callback) { WalletDB.prototype.getAccounts = function getAccounts(wid, callback) {
var map = []; var map = [];
var i, accounts; var i, accounts;
if (!utils.isNumber(id)) if (!utils.isNumber(wid))
return callback(new Error('Wallet IDs must be alphanumeric.')); return callback(new Error('Wallet IDs must be alphanumeric.'));
this.db.iterate({ this.db.iterate({
gte: 'i/' + pad32(id) + '/', gte: 'i/' + pad32(wid) + '/',
lte: 'i/' + pad32(id) + '/~', lte: 'i/' + pad32(wid) + '/~',
values: true, values: true,
parse: function(value, key) { parse: function(value, key) {
var name = key.split('/')[2]; var name = key.split('/')[2];
@ -687,13 +687,13 @@ WalletDB.prototype.getAccounts = function getAccounts(id, callback) {
/** /**
* Lookup the corresponding account name's index. * Lookup the corresponding account name's index.
* @param {WalletID} id * @param {WalletID} wid
* @param {String|Number} name - Account name/index. * @param {String|Number} name - Account name/index.
* @param {Function} callback - Returns [Error, Number]. * @param {Function} callback - Returns [Error, Number].
*/ */
WalletDB.prototype.getAccountIndex = function getAccountIndex(id, name, callback) { WalletDB.prototype.getAccountIndex = function getAccountIndex(wid, name, callback) {
if (!id) if (!wid)
return callback(null, -1); return callback(null, -1);
if (name == null) if (name == null)
@ -702,7 +702,7 @@ WalletDB.prototype.getAccountIndex = function getAccountIndex(id, name, callback
if (typeof name === 'number') if (typeof name === 'number')
return callback(null, name); 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) if (err)
return callback(err); return callback(err);
@ -720,14 +720,14 @@ WalletDB.prototype.getAccountIndex = function getAccountIndex(id, name, callback
*/ */
WalletDB.prototype.saveAccount = function saveAccount(account) { WalletDB.prototype.saveAccount = function saveAccount(account) {
var batch = this.batch(account.id); var batch = this.batch(account.wid);
var index = new Buffer(4); 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); index.writeUInt32LE(account.accountIndex, 0, true);
batch.put('a/' + key, account.toRaw()); 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); this.accountCache.set(key, account);
}; };
@ -742,7 +742,7 @@ WalletDB.prototype.createAccount = function createAccount(options, callback) {
var self = this; var self = this;
var account; var account;
this.hasAccount(options.id, options.accountIndex, function(err, exists) { this.hasAccount(options.wid, options.accountIndex, function(err, exists) {
if (err) if (err)
return callback(err); return callback(err);
@ -774,26 +774,26 @@ WalletDB.prototype.createAccount = function createAccount(options, callback) {
/** /**
* Test for the existence of an account. * Test for the existence of an account.
* @param {WalletID} id * @param {WalletID} wid
* @param {String|Number} account * @param {String|Number} account
* @param {Function} callback - Returns [Error, Boolean]. * @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 self = this;
var key; var key;
if (!id) if (!wid)
return callback(null, false); return callback(null, false);
this.getAccountIndex(id, account, function(err, index) { this.getAccountIndex(wid, account, function(err, index) {
if (err) if (err)
return callback(err); return callback(err);
if (index === -1) if (index === -1)
return callback(null, false); return callback(null, false);
key = pad32(id) + '/' + pad32(index); key = pad32(wid) + '/' + pad32(index);
if (self.accountCache.has(key)) if (self.accountCache.has(key))
return callback(null, true); return callback(null, true);
@ -806,15 +806,15 @@ WalletDB.prototype.hasAccount = function hasAccount(id, account, callback) {
* Save an address to the path map. * Save an address to the path map.
* The path map exists in the form of: * The path map exists in the form of:
* `p/[address-hash] -> {walletid1=path1, walletid2=path2, ...}` * `p/[address-hash] -> {walletid1=path1, walletid2=path2, ...}`
* @param {WalletID} id * @param {WalletID} wid
* @param {KeyRing[]} addresses * @param {KeyRing[]} addresses
* @param {Function} callback * @param {Function} callback
*/ */
WalletDB.prototype.saveAddress = function saveAddress(id, addresses, callback) { WalletDB.prototype.saveAddress = function saveAddress(wid, addresses, callback) {
var self = this; var self = this;
var items = []; var items = [];
var batch = this.batch(id); var batch = this.batch(wid);
var i, address, path; var i, address, path;
if (!Array.isArray(addresses)) if (!Array.isArray(addresses))
@ -850,10 +850,10 @@ WalletDB.prototype.saveAddress = function saveAddress(id, addresses, callback) {
if (!paths) if (!paths)
paths = {}; paths = {};
if (paths[id]) if (paths[wid])
return next(); return next();
paths[id] = path; paths[wid] = path;
self.pathCache.set(hash, paths); self.pathCache.set(hash, paths);
@ -898,17 +898,17 @@ WalletDB.prototype._getPaths = function _getPaths(hash, callback) {
/** /**
* Test whether an address hash exists in the * Test whether an address hash exists in the
* path map and is relevant to the wallet id. * path map and is relevant to the wallet id.
* @param {WalletID} id * @param {WalletID} wid
* @param {Hash} address * @param {Hash} address
* @param {Function} callback * @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) { this.getAddress(address, function(err, paths) {
if (err) if (err)
return callback(err); return callback(err);
if (!paths || !paths[id]) if (!paths || !paths[wid])
return callback(null, false); return callback(null, false);
return callback(null, true); return callback(null, true);
@ -927,14 +927,14 @@ WalletDB.prototype.getAddress = function getAddress(address, callback) {
/** /**
* Get all address hashes. * Get all address hashes.
* @param {WalletID} id * @param {WalletID} wid
* @param {Function} callback * @param {Function} callback
*/ */
WalletDB.prototype.getAddresses = function getAddresses(id, callback) { WalletDB.prototype.getAddresses = function getAddresses(wid, callback) {
if (!callback) { if (!callback) {
callback = id; callback = wid;
id = null; wid = null;
} }
this.db.iterate({ this.db.iterate({
@ -944,7 +944,7 @@ WalletDB.prototype.getAddresses = function getAddresses(id, callback) {
parse: function(value, key) { parse: function(value, key) {
var paths = parsePaths(value); var paths = parsePaths(value);
if (id && !paths[id]) if (wid && !paths[wid])
return; return;
return key.split('/')[1]; return key.split('/')[1];
@ -991,13 +991,13 @@ WalletDB.prototype.rescan = function rescan(chaindb, callback) {
/** /**
* Helper function to get a wallet. * Helper function to get a wallet.
* @private * @private
* @param {WalletID} id * @param {WalletID} wid
* @param {Function} callback * @param {Function} callback
* @param {Function} handler * @param {Function} handler
*/ */
WalletDB.prototype.fetchWallet = function fetchWallet(id, callback, handler) { WalletDB.prototype.fetchWallet = function fetchWallet(wid, callback, handler) {
this.get(id, function(err, wallet) { this.get(wid, function(err, wallet) {
if (err) if (err)
return callback(err); return callback(err);
@ -1058,8 +1058,8 @@ WalletDB.prototype.getPathInfo = function getPathInfo(wallet, tx, callback) {
if (!table) if (!table)
return callback(); return callback();
info = new PathInfo(self, wallet.id, tx, table); info = new PathInfo(self, wallet.wid, tx, table);
info.label = wallet.label; info.id = wallet.id;
return callback(null, info); return callback(null, info);
}); });
@ -1341,8 +1341,8 @@ WalletDB.prototype.removeBlock = function removeBlock(entry, callback, force) {
if (!wallets) if (!wallets)
return next(); return next();
utils.forEachSerial(wallets, function(id, next) { utils.forEachSerial(wallets, function(wid, next) {
self.get(id, function(err, wallet) { self.get(wid, function(err, wallet) {
if (err) if (err)
return next(err); return next(err);
@ -1385,16 +1385,16 @@ WalletDB.prototype.addTX = function addTX(tx, callback, force) {
wallets.length, tx.rhash); wallets.length, tx.rhash);
utils.forEachSerial(wallets, function(info, next) { utils.forEachSerial(wallets, function(info, next) {
self.get(info.id, function(err, wallet) { self.get(info.wid, function(err, wallet) {
if (err) if (err)
return next(err); return next(err);
if (!wallet) if (!wallet)
return next(); 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) { wallet.tx.add(tx, info, function(err) {
if (err) if (err)
@ -1413,20 +1413,20 @@ WalletDB.prototype.addTX = function addTX(tx, callback, force) {
/** /**
* Get the corresponding path for an address hash. * Get the corresponding path for an address hash.
* @param {WalletID} id * @param {WalletID} wid
* @param {Hash} address * @param {Hash} address
* @param {Function} callback * @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) { this.getAddress(address, function(err, paths) {
if (err) if (err)
return callback(err); return callback(err);
if (!paths || !paths[id]) if (!paths || !paths[wid])
return callback(); 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 * Path
* @constructor * @constructor
* @private * @private
* @property {WalletID} id * @property {WalletID} wid
* @property {String} name - Account name. * @property {String} name - Account name.
* @property {Number} account - Account index. * @property {Number} account - Account index.
* @property {Number} change - Change index. * @property {Number} change - Change index.
@ -1446,14 +1446,14 @@ function Path() {
if (!(this instanceof Path)) if (!(this instanceof Path))
return new Path(); return new Path();
this.id = null; this.wid = null;
this.name = null; this.name = null;
this.account = 0; this.account = 0;
this.change = 0; this.change = 0;
this.index = 0; this.index = 0;
// NOTE: Passed in by caller. // NOTE: Passed in by caller.
this.label = null; this.id = null;
} }
/** /**
@ -1464,7 +1464,7 @@ function Path() {
Path.prototype.fromRaw = function fromRaw(data) { Path.prototype.fromRaw = function fromRaw(data) {
var p = new BufferReader(data); var p = new BufferReader(data);
this.id = p.readU32(); this.wid = p.readU32();
this.name = p.readVarString('utf8'); this.name = p.readVarString('utf8');
this.account = p.readU32(); this.account = p.readU32();
this.change = p.readU32(); this.change = p.readU32();
@ -1490,7 +1490,7 @@ Path.fromRaw = function fromRaw(data) {
Path.prototype.toRaw = function toRaw(writer) { Path.prototype.toRaw = function toRaw(writer) {
var p = new BufferWriter(writer); var p = new BufferWriter(writer);
p.writeU32(this.id); p.writeU32(this.wid);
p.writeVarString(this.name, 'utf8'); p.writeVarString(this.name, 'utf8');
p.writeU32(this.account); p.writeU32(this.account);
p.writeU32(this.change); p.writeU32(this.change);
@ -1505,13 +1505,13 @@ Path.prototype.toRaw = function toRaw(writer) {
/** /**
* Inject properties from keyring. * Inject properties from keyring.
* @private * @private
* @param {WalletID} id * @param {WalletID} wid
* @param {KeyRing} address * @param {KeyRing} address
*/ */
Path.prototype.fromKeyRing = function fromKeyRing(address) { Path.prototype.fromKeyRing = function fromKeyRing(address) {
this.wid = address.wid;
this.id = address.id; this.id = address.id;
this.label = address.label;
this.name = address.name; this.name = address.name;
this.account = address.account; this.account = address.account;
this.change = address.change; this.change = address.change;
@ -1521,7 +1521,7 @@ Path.prototype.fromKeyRing = function fromKeyRing(address) {
/** /**
* Instantiate path from keyring. * Instantiate path from keyring.
* @param {WalletID} id * @param {WalletID} wid
* @param {KeyRing} address * @param {KeyRing} address
* @returns {Path} * @returns {Path}
*/ */
@ -1548,8 +1548,8 @@ Path.prototype.toPath = function() {
Path.prototype.toJSON = function toJSON() { Path.prototype.toJSON = function toJSON() {
return { return {
wid: this.wid,
id: this.id, id: this.id,
label: this.label,
name: this.name, name: this.name,
change: this.change === 1, change: this.change === 1,
path: this.toPath() path: this.toPath()
@ -1569,8 +1569,8 @@ Path.prototype.fromJSON = function fromJSON(json) {
assert(indexes[0] >= 0); assert(indexes[0] >= 0);
indexes[0] -= constants.hd.HARDENED; indexes[0] -= constants.hd.HARDENED;
this.wid = json.wid;
this.id = json.id; this.id = json.id;
this.label = json.label;
this.name = json.name; this.name = json.name;
this.account = indexes[0]; this.account = indexes[0];
this.change = indexes[1]; this.change = indexes[1];
@ -1595,8 +1595,8 @@ Path.fromJSON = function fromJSON(json) {
*/ */
Path.prototype.inspect = function() { Path.prototype.inspect = function() {
return '<Path: ' + this.label return '<Path: ' + this.id
+ '(' + this.id + ')' + '(' + this.wid + ')'
+ '/' + this.name + '/' + this.name
+ ': ' + this.toPath() + ': ' + this.toPath()
+ '>'; + '>';
@ -1606,9 +1606,9 @@ Path.prototype.inspect = function() {
* Path Info * Path Info
*/ */
function PathInfo(db, id, tx, table) { function PathInfo(db, wid, tx, table) {
if (!(this instanceof PathInfo)) if (!(this instanceof PathInfo))
return new PathInfo(db, id, tx, table); return new PathInfo(db, wid, tx, table);
// Reference to the walletdb. // Reference to the walletdb.
this.db = db; this.db = db;
@ -1621,10 +1621,10 @@ function PathInfo(db, id, tx, table) {
this.paths = []; this.paths = [];
// Wallet ID // Wallet ID
this.id = id; this.wid = wid;
// Wallet Label (passed in by caller). // Wallet Label (passed in by caller).
this.label = null; this.id = null;
// Map of address hashes->paths (for everything). // Map of address hashes->paths (for everything).
this.table = null; this.table = null;
@ -1647,14 +1647,14 @@ PathInfo.map = function map(db, tx, table) {
var hashes = Object.keys(table); var hashes = Object.keys(table);
var wallets = {}; var wallets = {};
var info = []; var info = [];
var i, j, hash, paths, path, id; var i, j, hash, paths, path, wid;
for (i = 0; i < hashes.length; i++) { for (i = 0; i < hashes.length; i++) {
hash = hashes[i]; hash = hashes[i];
paths = table[hash]; paths = table[hash];
for (j = 0; j < paths.length; j++) { for (j = 0; j < paths.length; j++) {
path = paths[j]; path = paths[j];
wallets[path.id] = true; wallets[path.wid] = true;
} }
} }
@ -1664,8 +1664,8 @@ PathInfo.map = function map(db, tx, table) {
return; return;
for (i = 0; i < wallets.length; i++) { for (i = 0; i < wallets.length; i++) {
id = +wallets[i]; wid = +wallets[i];
info.push(new PathInfo(db, id, tx, table)); info.push(new PathInfo(db, wid, tx, table));
} }
return info; return info;
@ -1685,7 +1685,7 @@ PathInfo.prototype.fromTX = function fromTX(tx, table) {
paths = table[hash]; paths = table[hash];
for (j = 0; j < paths.length; j++) { for (j = 0; j < paths.length; j++) {
path = paths[j]; path = paths[j];
if (path.id !== this.id) if (path.wid !== this.wid)
continue; continue;
this.pathMap[hash] = path; this.pathMap[hash] = path;
if (!uniq[path.account]) { if (!uniq[path.account]) {
@ -1702,7 +1702,7 @@ PathInfo.prototype.fromTX = function fromTX(tx, table) {
paths = table[hash]; paths = table[hash];
for (j = 0; j < paths.length; j++) { for (j = 0; j < paths.length; j++) {
path = paths[j]; path = paths[j];
if (path.id !== this.id) if (path.wid !== this.wid)
continue; continue;
this.paths.push(path); this.paths.push(path);
} }
@ -1711,8 +1711,8 @@ PathInfo.prototype.fromTX = function fromTX(tx, table) {
return this; return this;
}; };
PathInfo.fromTX = function fromTX(db, id, tx, table) { PathInfo.fromTX = function fromTX(db, wid, tx, table) {
return new PathInfo(db, id).fromTX(tx, table); return new PathInfo(db, wid).fromTX(tx, table);
}; };
/** /**
@ -1775,7 +1775,7 @@ function parsePaths(data) {
while (p.left()) { while (p.left()) {
path = Path.fromRaw(p); path = Path.fromRaw(p);
out[path.id] = path; out[path.wid] = path;
} }
return out; return out;
@ -1784,11 +1784,11 @@ function parsePaths(data) {
function serializePaths(out) { function serializePaths(out) {
var p = new BufferWriter(); var p = new BufferWriter();
var keys = Object.keys(out); var keys = Object.keys(out);
var i, id, path; var i, wid, path;
for (i = 0; i < keys.length; i++) { for (i = 0; i < keys.length; i++) {
id = keys[i]; wid = keys[i];
path = out[id]; path = out[wid];
path.toRaw(p); path.toRaw(p);
} }
@ -1801,7 +1801,7 @@ function serializeWallets(wallets) {
for (i = 0; i < wallets.length; i++) { for (i = 0; i < wallets.length; i++) {
info = wallets[i]; info = wallets[i];
p.writeU32(info.id); p.writeU32(info.wid);
} }
return p.render(); return p.render();

View File

@ -36,7 +36,8 @@ describe('HTTP', function() {
var node = new bcoin.fullnode({ var node = new bcoin.fullnode({
network: 'regtest', network: 'regtest',
apiKey: 'foo', apiKey: 'foo',
walletAuth: true walletAuth: true,
db: 'memory'
}); });
var wallet = new bcoin.http.wallet({ var wallet = new bcoin.http.wallet({