account: path names.

This commit is contained in:
Christopher Jeffrey 2016-09-30 17:33:26 -07:00
parent c0d47baa08
commit 79e70d7bbb
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 61 additions and 45 deletions

View File

@ -449,7 +449,7 @@ Account.prototype.deriveChange = function deriveChange(index, master) {
*/
Account.prototype.derivePath = function derivePath(path, master) {
var ring, script, raw;
var ring, raw;
// Imported key.
if (path.index === -1) {
@ -470,11 +470,7 @@ Account.prototype.derivePath = function derivePath(path, master) {
return ring;
}
// Custom redeem script.
if (path.script)
script = bcoin.script.fromRaw(path.script);
ring = this.deriveAddress(path.change, path.index, master, script);
ring = this.deriveAddress(path.change, path.index, master);
return ring;
};
@ -486,7 +482,7 @@ Account.prototype.derivePath = function derivePath(path, master) {
* @returns {KeyRing}
*/
Account.prototype.deriveAddress = function deriveAddress(change, index, master, script) {
Account.prototype.deriveAddress = function deriveAddress(change, index, master) {
var keys = [];
var i, key, shared, ring;
@ -502,27 +498,21 @@ Account.prototype.deriveAddress = function deriveAddress(change, index, master,
ring = bcoin.keyring.fromPublic(key.publicKey, this.network);
ring.witness = this.witness;
if (script) {
// Custom redeem script.
assert(this.type === Account.types.PUBKEYHASH);
ring.script = script;
} else {
switch (this.type) {
case Account.types.PUBKEYHASH:
break;
case Account.types.MULTISIG:
keys.push(key.publicKey);
switch (this.type) {
case Account.types.PUBKEYHASH:
break;
case Account.types.MULTISIG:
keys.push(key.publicKey);
for (i = 0; i < this.keys.length; i++) {
shared = this.keys[i];
shared = shared.derive(change).derive(index);
keys.push(shared.publicKey);
}
for (i = 0; i < this.keys.length; i++) {
shared = this.keys[i];
shared = shared.derive(change).derive(index);
keys.push(shared.publicKey);
}
ring.script = bcoin.script.fromMultisig(this.m, this.n, keys);
ring.script = bcoin.script.fromMultisig(this.m, this.n, keys);
break;
}
break;
}
if (key.privateKey)

View File

@ -30,14 +30,13 @@ function Path() {
return new Path();
this.wid = null;
this.name = null;
this.name = null; // Passed in by caller.
this.account = 0;
this.change = -1;
this.index = -1;
this.encrypted = false;
this.imported = null;
this.script = null;
// Currently unused.
this.type = bcoin.script.types.PUBKEYHASH;
@ -64,7 +63,6 @@ Path.prototype.clone = function clone() {
path.encrypted = this.encrypted;
path.imported = this.imported;
path.script = this.script;
path.type = this.type;
path.version = this.version;
@ -85,15 +83,12 @@ Path.prototype.fromRaw = function fromRaw(data) {
var p = new BufferReader(data);
this.wid = p.readU32();
this.name = p.readVarString('utf8');
this.account = p.readU32();
switch (p.readU8()) {
case 0:
this.change = p.readU32();
this.index = p.readU32();
if (p.readU8() === 1)
this.script = p.readVarBytes();
break;
case 1:
this.encrypted = p.readU8() === 1;
@ -131,7 +126,6 @@ Path.prototype.toRaw = function toRaw(writer) {
var p = new BufferWriter(writer);
p.writeU32(this.wid);
p.writeVarString(this.name, 'utf8');
p.writeU32(this.account);
if (this.index !== -1) {
@ -139,12 +133,6 @@ Path.prototype.toRaw = function toRaw(writer) {
p.writeU8(0);
p.writeU32(this.change);
p.writeU32(this.index);
if (this.script) {
p.writeU8(1);
p.writeVarBytes(this.script);
} else {
p.writeU8(0);
}
} else {
assert(this.imported);
p.writeU8(1);

View File

@ -932,13 +932,13 @@ Wallet.prototype._fund = co(function* fund(tx, options) {
coins = yield this.getCoins(options.account);
rate = options.rate;
rate = this.network.feeRate;
if (rate == null) {
if (options.rate != null) {
rate = options.rate;
} else {
if (this.db.fees)
rate = this.db.fees.estimateFee();
else
rate = this.network.feeRate;
}
// Don't use any locked coins.

View File

@ -707,6 +707,22 @@ WalletDB.prototype.getAccountIndex = co(function* getAccountIndex(wid, name) {
return index.readUInt32LE(0, true);
});
/**
* Lookup the corresponding account index's name.
* @param {WalletID} wid
* @param {Number} index - Account index.
* @returns {Promise} - Returns String.
*/
WalletDB.prototype.getAccountName = co(function* getAccountName(wid, index) {
var account = yield this._getAccount(wid, index);
if (!account)
return null;
return account.name;
});
/**
* Save an account to the database.
* @param {Account} account
@ -862,11 +878,31 @@ WalletDB.prototype.getAddressPaths = co(function* getAddressPaths(hash) {
paths = parsePaths(data, hash);
yield this.fillPathNames(paths);
this.pathCache.set(hash, paths);
return paths;
});
/**
* Assign account names to an array of paths.
* @param {Path[]} paths
* @returns {Promise}
*/
WalletDB.prototype.fillPathNames = co(function* fillPathNames(paths) {
var i, path;
for (i = 0; i < paths.length; i++) {
path = paths[i];
if (path.name)
continue;
// These should be mostly cached.
path.name = yield this.db.getAccountName(path.wid, path.account);
}
});
/**
* Test whether an address hash exists in the
* path map and is relevant to the wallet id.
@ -912,8 +948,8 @@ WalletDB.prototype.getAddressHashes = function getAddressHashes(wid) {
* @returns {Promise}
*/
WalletDB.prototype.getWalletPaths = function getWalletPaths(wid) {
return this.db.iterate({
WalletDB.prototype.getWalletPaths = co(function* getWalletPaths(wid) {
var paths = yield this.db.iterate({
gte: layout.p(constants.NULL_HASH),
lte: layout.p(constants.HIGH_HASH),
values: true,
@ -928,7 +964,9 @@ WalletDB.prototype.getWalletPaths = function getWalletPaths(wid) {
return path;
}
});
};
yield this.fillPathNames(paths);
return paths;
});
/**
* Get all wallet ids.