stop using paths.
This commit is contained in:
parent
390af13b14
commit
cbd7f06aba
@ -38,7 +38,9 @@ function KeyRing(options) {
|
|||||||
this.m = options.m || 1;
|
this.m = options.m || 1;
|
||||||
this.n = options.n || 1;
|
this.n = options.n || 1;
|
||||||
this.witness = options.witness || false;
|
this.witness = options.witness || false;
|
||||||
this.path = options.path;
|
this.account = options.account;
|
||||||
|
this.change = options.change;
|
||||||
|
this.index = options.index;
|
||||||
this.key = options.key;
|
this.key = options.key;
|
||||||
this.keys = [];
|
this.keys = [];
|
||||||
|
|
||||||
@ -525,7 +527,9 @@ KeyRing.prototype.toJSON = function toJSON() {
|
|||||||
m: this.m,
|
m: this.m,
|
||||||
n: this.n,
|
n: this.n,
|
||||||
witness: this.witness,
|
witness: this.witness,
|
||||||
path: this.path,
|
account: this.account,
|
||||||
|
change: this.change,
|
||||||
|
index: this.index,
|
||||||
key: utils.toBase58(this.key),
|
key: utils.toBase58(this.key),
|
||||||
keys: this.keys.map(utils.toBase58),
|
keys: this.keys.map(utils.toBase58),
|
||||||
address: this.getAddress()
|
address: this.getAddress()
|
||||||
@ -548,7 +552,9 @@ KeyRing.fromJSON = function fromJSON(json) {
|
|||||||
m: json.m,
|
m: json.m,
|
||||||
n: json.n,
|
n: json.n,
|
||||||
witness: json.witness,
|
witness: json.witness,
|
||||||
path: json.path,
|
account: json.account,
|
||||||
|
change: json.change,
|
||||||
|
index: json.index,
|
||||||
key: utils.fromBase58(json.key),
|
key: utils.fromBase58(json.key),
|
||||||
keys: json.keys.map(utils.fromBase58)
|
keys: json.keys.map(utils.fromBase58)
|
||||||
});
|
});
|
||||||
@ -568,7 +574,9 @@ 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.writeVarString(this.path, 'ascii');
|
p.writeU32(this.account);
|
||||||
|
p.writeU32(this.change);
|
||||||
|
p.writeU32(this.index);
|
||||||
p.writeVarBytes(this.key);
|
p.writeVarBytes(this.key);
|
||||||
p.writeU8(this.keys.length);
|
p.writeU8(this.keys.length);
|
||||||
|
|
||||||
@ -593,7 +601,9 @@ KeyRing.fromRaw = function fromRaw(data) {
|
|||||||
var m = p.readU8();
|
var m = p.readU8();
|
||||||
var n = p.readU8();
|
var n = p.readU8();
|
||||||
var witness = p.readU8() === 1;
|
var witness = p.readU8() === 1;
|
||||||
var path = p.readVarString('ascii');
|
var account = p.readU32();
|
||||||
|
var change = p.readU32();
|
||||||
|
var index = p.readU32();
|
||||||
var key = p.readVarBytes();
|
var key = p.readVarBytes();
|
||||||
var keys = new Array(p.readU8());
|
var keys = new Array(p.readU8());
|
||||||
var i;
|
var i;
|
||||||
@ -607,7 +617,9 @@ KeyRing.fromRaw = function fromRaw(data) {
|
|||||||
m: m,
|
m: m,
|
||||||
n: n,
|
n: n,
|
||||||
witness: witness,
|
witness: witness,
|
||||||
path: path,
|
account: account,
|
||||||
|
change: change,
|
||||||
|
index: index,
|
||||||
key: key,
|
key: key,
|
||||||
keys: keys
|
keys: keys
|
||||||
});
|
});
|
||||||
|
|||||||
@ -474,9 +474,6 @@ Wallet.prototype.createAddress = function createAddress(change, callback) {
|
|||||||
change = null;
|
change = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof change === 'string')
|
|
||||||
change = this.parsePath(change).change;
|
|
||||||
|
|
||||||
if (change) {
|
if (change) {
|
||||||
address = this.deriveChange(this.changeDepth);
|
address = this.deriveChange(this.changeDepth);
|
||||||
addresses.push(address);
|
addresses.push(address);
|
||||||
@ -510,9 +507,6 @@ Wallet.prototype.createAddress = function createAddress(change, callback) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Wallet.prototype.deriveReceive = function deriveReceive(index) {
|
Wallet.prototype.deriveReceive = function deriveReceive(index) {
|
||||||
if (typeof index === 'string')
|
|
||||||
index = this.parsePath(index).index;
|
|
||||||
|
|
||||||
return this.deriveAddress(false, index);
|
return this.deriveAddress(false, index);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -523,9 +517,6 @@ Wallet.prototype.deriveReceive = function deriveReceive(index) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Wallet.prototype.deriveChange = function deriveChange(index) {
|
Wallet.prototype.deriveChange = function deriveChange(index) {
|
||||||
if (typeof index === 'string')
|
|
||||||
index = this.parsePath(index).index;
|
|
||||||
|
|
||||||
return this.deriveAddress(true, index);
|
return this.deriveAddress(true, index);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -538,32 +529,20 @@ Wallet.prototype.deriveChange = function deriveChange(index) {
|
|||||||
|
|
||||||
Wallet.prototype.deriveAddress = function deriveAddress(change, index) {
|
Wallet.prototype.deriveAddress = function deriveAddress(change, index) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var i, path, data, key, options, address;
|
var i, key, options;
|
||||||
|
|
||||||
assert(this.initialized);
|
assert(this.initialized);
|
||||||
|
|
||||||
if (typeof change === 'string')
|
change = +change;
|
||||||
path = change;
|
|
||||||
|
|
||||||
if (path) {
|
key = this.accountKey.derive(change).derive(index);
|
||||||
data = this.parsePath(path);
|
|
||||||
} else {
|
|
||||||
data = {
|
|
||||||
path: this.createPath(change, index),
|
|
||||||
change: change,
|
|
||||||
index: index
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.cache.has(data.path))
|
|
||||||
return this.cache.get(data.path);
|
|
||||||
|
|
||||||
key = this.accountKey.derive(data.path);
|
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
network: this.network,
|
network: this.network,
|
||||||
key: key.publicKey,
|
key: key.publicKey,
|
||||||
path: data.path,
|
account: this.accountIndex,
|
||||||
|
change: change,
|
||||||
|
index: index,
|
||||||
type: this.type,
|
type: this.type,
|
||||||
witness: this.witness,
|
witness: this.witness,
|
||||||
m: this.m,
|
m: this.m,
|
||||||
@ -573,16 +552,11 @@ Wallet.prototype.deriveAddress = function deriveAddress(change, index) {
|
|||||||
|
|
||||||
for (i = 0; i < this.keys.length; i++) {
|
for (i = 0; i < this.keys.length; i++) {
|
||||||
key = this.keys[i];
|
key = this.keys[i];
|
||||||
path = this.createPath(data.change, data.index);
|
key = key.derive(change).derive(index);
|
||||||
key = key.derive(path);
|
|
||||||
options.keys.push(key.publicKey);
|
options.keys.push(key.publicKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
address = new bcoin.keyring(options);
|
return new bcoin.keyring(options);
|
||||||
|
|
||||||
this.cache.set(data.path, address);
|
|
||||||
|
|
||||||
return address;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -615,37 +589,6 @@ Wallet.prototype.hasAddress = function hasAddress(address, callback) {
|
|||||||
this.db.hasAddress(this.id, address, callback);
|
this.db.hasAddress(this.id, address, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a path.
|
|
||||||
* @param {Boolean} change - Whether the key is on the change branch.
|
|
||||||
* @param {Number} index - The index to derive to.
|
|
||||||
* @returns {String} path
|
|
||||||
*/
|
|
||||||
|
|
||||||
Wallet.prototype.createPath = function createPath(change, index) {
|
|
||||||
return 'm'
|
|
||||||
+ '/' + (change ? 1 : 0)
|
|
||||||
+ '/' + index;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse a path.
|
|
||||||
* @param {String} path
|
|
||||||
* @returns {Object} Contains `path`, `change`, and `index`.
|
|
||||||
*/
|
|
||||||
|
|
||||||
Wallet.prototype.parsePath = function parsePath(path) {
|
|
||||||
var parts = path.split('/');
|
|
||||||
|
|
||||||
assert(/^m\/\d+\/\d+$/.test(path));
|
|
||||||
|
|
||||||
return {
|
|
||||||
path: path,
|
|
||||||
change: parseInt(parts[parts.length - 2], 10) === 1,
|
|
||||||
index: parseInt(parts[parts.length - 1], 10)
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set receiving depth (depth is the index of the _next_ address).
|
* Set receiving depth (depth is the index of the _next_ address).
|
||||||
* Allocate all addresses up to depth. Note that this also allocates
|
* Allocate all addresses up to depth. Note that this also allocates
|
||||||
@ -909,8 +852,10 @@ Wallet.prototype.deriveInputs = function deriveInputs(tx, callback) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
for (i = 0; i < paths.length; i++)
|
for (i = 0; i < paths.length; i++) {
|
||||||
addresses.push(self.deriveAddress(paths[i]));
|
path = paths[i];
|
||||||
|
addresses.push(self.deriveAddress(path.change, path.index));
|
||||||
|
}
|
||||||
|
|
||||||
return callback(null, addresses);
|
return callback(null, addresses);
|
||||||
});
|
});
|
||||||
@ -1031,7 +976,7 @@ Wallet.prototype.getOutputDepth = function getOutputDepth(tx, callback) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
for (i = 0; i < paths.length; i++) {
|
for (i = 0; i < paths.length; i++) {
|
||||||
path = self.parsePath(paths[i]);
|
path = paths[i];
|
||||||
if (path.change) {
|
if (path.change) {
|
||||||
if (path.index > depth.changeDepth)
|
if (path.index > depth.changeDepth)
|
||||||
depth.changeDepth = path.index;
|
depth.changeDepth = path.index;
|
||||||
@ -1291,7 +1236,7 @@ Wallet.prototype.sign = function sign(tx, options, callback) {
|
|||||||
|
|
||||||
for (i = 0; i < addresses.length; i++) {
|
for (i = 0; i < addresses.length; i++) {
|
||||||
address = addresses[i];
|
address = addresses[i];
|
||||||
key = master.derive(address.path);
|
key = master.derive(address.change).derive(address.index);
|
||||||
assert(utils.equal(key.getPublicKey(), address.key));
|
assert(utils.equal(key.getPublicKey(), address.key));
|
||||||
total += address.sign(tx, key, options.index, options.type);
|
total += address.sign(tx, key, options.index, options.type);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -341,7 +341,7 @@ WalletDB.prototype.get = function get(id, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a wallet to the database (setup ida and encrypt).
|
* Save a wallet to the database.
|
||||||
* @param {Wallet} wallet
|
* @param {Wallet} wallet
|
||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
@ -457,13 +457,13 @@ WalletDB.prototype.saveAddress = function saveAddress(id, addresses, callback) {
|
|||||||
for (i = 0; i < addresses.length; i++) {
|
for (i = 0; i < addresses.length; i++) {
|
||||||
address = addresses[i];
|
address = addresses[i];
|
||||||
|
|
||||||
hashes.push([address.getKeyHash('hex'), address.path]);
|
hashes.push([address.getKeyHash('hex'), address]);
|
||||||
|
|
||||||
if (address.type === 'multisig')
|
if (address.type === 'multisig')
|
||||||
hashes.push([address.getScriptHash('hex'), address.path]);
|
hashes.push([address.getScriptHash('hex'), address]);
|
||||||
|
|
||||||
if (address.witness)
|
if (address.witness)
|
||||||
hashes.push([address.getProgramHash('hex'), address.path]);
|
hashes.push([address.getProgramHash('hex'), address]);
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.forEach(hashes, function(hash, next) {
|
utils.forEach(hashes, function(hash, next) {
|
||||||
@ -809,9 +809,16 @@ WalletDB.prototype.getRedeem = function getRedeem(id, hash, callback) {
|
|||||||
function parsePaths(data) {
|
function parsePaths(data) {
|
||||||
var p = new BufferReader(data);
|
var p = new BufferReader(data);
|
||||||
var out = {};
|
var out = {};
|
||||||
|
var id;
|
||||||
|
|
||||||
while (p.left())
|
while (p.left()) {
|
||||||
out[p.readVarString('utf8')] = p.readVarString('ascii');
|
id = p.readVarString('utf8');
|
||||||
|
out[id] = {
|
||||||
|
account: p.readU32(),
|
||||||
|
change: p.readU32(),
|
||||||
|
index: p.readU32()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
@ -825,7 +832,9 @@ function serializePaths(out) {
|
|||||||
id = keys[i];
|
id = keys[i];
|
||||||
path = out[id];
|
path = out[id];
|
||||||
p.writeVarString(id, 'utf8');
|
p.writeVarString(id, 'utf8');
|
||||||
p.writeVarString(path, 'ascii');
|
p.writeU32(path.account);
|
||||||
|
p.writeU32(path.change);
|
||||||
|
p.writeU32(path.index);
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.render();
|
return p.render();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user