wallet: refactor variable names.
This commit is contained in:
parent
d0c4ea008b
commit
2c0aacf426
@ -211,13 +211,13 @@ MTX.prototype.addOutput = function addOutput(options, value) {
|
|||||||
* Build input script (or witness) templates (with
|
* Build input script (or witness) templates (with
|
||||||
* OP_0 in place of signatures).
|
* OP_0 in place of signatures).
|
||||||
* @param {Number} index - Input index.
|
* @param {Number} index - Input index.
|
||||||
* @param {KeyRing} addr - Address used to build. The address
|
* @param {KeyRing} ring - Address used to build. The address
|
||||||
* must be able to redeem the coin.
|
* must be able to redeem the coin.
|
||||||
* @returns {Boolean} Whether the script was able to be built.
|
* @returns {Boolean} Whether the script was able to be built.
|
||||||
* @throws on unavailable coins.
|
* @throws on unavailable coins.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
MTX.prototype.scriptInput = function scriptInput(index, ring) {
|
||||||
var input, prev, n, i, vector, redeemScript, witnessScript;
|
var input, prev, n, i, vector, redeemScript, witnessScript;
|
||||||
|
|
||||||
if (typeof index !== 'number')
|
if (typeof index !== 'number')
|
||||||
@ -240,7 +240,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
|||||||
// address map to avoid unnecessary calculation.
|
// address map to avoid unnecessary calculation.
|
||||||
// A hash table lookup may be faster than all
|
// A hash table lookup may be faster than all
|
||||||
// the nonsense below.
|
// the nonsense below.
|
||||||
if (!addr.ownOutput(input.coin))
|
if (!ring.ownOutput(input.coin))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Get the previous output's script
|
// Get the previous output's script
|
||||||
@ -250,26 +250,26 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
|||||||
// with segwit: figuring out where the redeem script and witness
|
// with segwit: figuring out where the redeem script and witness
|
||||||
// redeem scripts go.
|
// redeem scripts go.
|
||||||
if (prev.isScripthash()) {
|
if (prev.isScripthash()) {
|
||||||
if (addr.program && utils.equal(prev.get(1), addr.programHash)) {
|
if (ring.program && utils.equal(prev.get(1), ring.programHash)) {
|
||||||
// Witness program nested in regular P2SH.
|
// Witness program nested in regular P2SH.
|
||||||
redeemScript = addr.program.toRaw();
|
redeemScript = ring.program.toRaw();
|
||||||
vector = input.witness;
|
vector = input.witness;
|
||||||
if (addr.program.isWitnessScripthash()) {
|
if (ring.program.isWitnessScripthash()) {
|
||||||
// P2WSH nested within pay-to-scripthash
|
// P2WSH nested within pay-to-scripthash
|
||||||
// (it had to be this complicated, didn't it?)
|
// (it had to be this complicated, didn't it?)
|
||||||
witnessScript = addr.script.toRaw();
|
witnessScript = ring.script.toRaw();
|
||||||
prev = addr.script;
|
prev = ring.script;
|
||||||
} else if (addr.program.isWitnessPubkeyhash()) {
|
} else if (ring.program.isWitnessPubkeyhash()) {
|
||||||
// P2WPKH nested within pay-to-scripthash.
|
// P2WPKH nested within pay-to-scripthash.
|
||||||
prev = Script.fromPubkeyhash(addr.keyHash);
|
prev = Script.fromPubkeyhash(ring.keyHash);
|
||||||
} else {
|
} else {
|
||||||
assert(false, 'Unknown program.');
|
assert(false, 'Unknown program.');
|
||||||
}
|
}
|
||||||
} else if (addr.script && utils.equal(prev.get(1), addr.scriptHash160)) {
|
} else if (ring.script && utils.equal(prev.get(1), ring.scriptHash160)) {
|
||||||
// Regular P2SH.
|
// Regular P2SH.
|
||||||
redeemScript = addr.script.toRaw();
|
redeemScript = ring.script.toRaw();
|
||||||
vector = input.script;
|
vector = input.script;
|
||||||
prev = addr.script;
|
prev = ring.script;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -279,14 +279,14 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
|||||||
|
|
||||||
if (prev.isWitnessScripthash()) {
|
if (prev.isWitnessScripthash()) {
|
||||||
// Bare P2WSH.
|
// Bare P2WSH.
|
||||||
if (!addr.script || !utils.equal(prev.get(1), addr.scriptHash256))
|
if (!ring.script || !utils.equal(prev.get(1), ring.scriptHash256))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
witnessScript = addr.script.toRaw();
|
witnessScript = ring.script.toRaw();
|
||||||
prev = addr.script;
|
prev = ring.script;
|
||||||
} else if (prev.isWitnessPubkeyhash()) {
|
} else if (prev.isWitnessPubkeyhash()) {
|
||||||
// Bare P2WPKH.
|
// Bare P2WPKH.
|
||||||
if (!utils.equal(prev.get(1), addr.keyHash))
|
if (!utils.equal(prev.get(1), ring.keyHash))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
prev = Script.fromPubkeyhash(prev.get(1));
|
prev = Script.fromPubkeyhash(prev.get(1));
|
||||||
@ -301,7 +301,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
|||||||
|
|
||||||
if (prev.isPubkey()) {
|
if (prev.isPubkey()) {
|
||||||
// P2PK
|
// P2PK
|
||||||
if (!utils.equal(prev.get(1), addr.publicKey))
|
if (!utils.equal(prev.get(1), ring.publicKey))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Already has a script template (at least)
|
// Already has a script template (at least)
|
||||||
@ -311,7 +311,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
|||||||
vector.set(0, opcodes.OP_0);
|
vector.set(0, opcodes.OP_0);
|
||||||
} else if (prev.isPubkeyhash()) {
|
} else if (prev.isPubkeyhash()) {
|
||||||
// P2PKH
|
// P2PKH
|
||||||
if (!utils.equal(prev.get(2), addr.keyHash))
|
if (!utils.equal(prev.get(2), ring.keyHash))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Already has a script template (at least)
|
// Already has a script template (at least)
|
||||||
@ -319,10 +319,10 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
vector.set(0, opcodes.OP_0);
|
vector.set(0, opcodes.OP_0);
|
||||||
vector.set(1, addr.publicKey);
|
vector.set(1, ring.publicKey);
|
||||||
} else if (prev.isMultisig()) {
|
} else if (prev.isMultisig()) {
|
||||||
// Multisig
|
// Multisig
|
||||||
if (prev.indexOf(addr.publicKey) === -1)
|
if (prev.indexOf(ring.publicKey) === -1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Already has a script template (at least)
|
// Already has a script template (at least)
|
||||||
@ -341,7 +341,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
|||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
vector.set(i + 1, opcodes.OP_0);
|
vector.set(i + 1, opcodes.OP_0);
|
||||||
} else {
|
} else {
|
||||||
if (prev.indexOf(addr.publicKey) === -1)
|
if (prev.indexOf(ring.publicKey) === -1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Already has a script template (at least)
|
// Already has a script template (at least)
|
||||||
@ -411,7 +411,7 @@ MTX.prototype.createSignature = function createSignature(index, prev, key, type,
|
|||||||
/**
|
/**
|
||||||
* Sign an input.
|
* Sign an input.
|
||||||
* @param {Number} index - Index of input being signed.
|
* @param {Number} index - Index of input being signed.
|
||||||
* @param {KeyRing} addr - Address used to sign. The address
|
* @param {KeyRing} ring - Address used to sign. The address
|
||||||
* must be able to redeem the coin.
|
* must be able to redeem the coin.
|
||||||
* @param {HDPrivateKey|KeyPair|Buffer} key - Private key.
|
* @param {HDPrivateKey|KeyPair|Buffer} key - Private key.
|
||||||
* @param {SighashType} type
|
* @param {SighashType} type
|
||||||
@ -419,7 +419,7 @@ MTX.prototype.createSignature = function createSignature(index, prev, key, type,
|
|||||||
* @throws on unavailable coins.
|
* @throws on unavailable coins.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
MTX.prototype.signInput = function signInput(index, addr, key, type) {
|
MTX.prototype.signInput = function signInput(index, ring, key, type) {
|
||||||
var input, prev, signature, keyIndex, signatures, i;
|
var input, prev, signature, keyIndex, signatures, i;
|
||||||
var len, m, n, keys, vector, version;
|
var len, m, n, keys, vector, version;
|
||||||
|
|
||||||
@ -479,7 +479,7 @@ MTX.prototype.signInput = function signInput(index, addr, key, type) {
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Make sure the pubkey is ours.
|
// Make sure the pubkey is ours.
|
||||||
if (!utils.equal(addr.publicKey, prev.get(0)))
|
if (!utils.equal(ring.publicKey, prev.get(0)))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (vector.getSmall(0) !== 0)
|
if (vector.getSmall(0) !== 0)
|
||||||
@ -498,7 +498,7 @@ MTX.prototype.signInput = function signInput(index, addr, key, type) {
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Make sure the pubkey hash is ours.
|
// Make sure the pubkey hash is ours.
|
||||||
if (!utils.equal(addr.keyHash, prev.get(2)))
|
if (!utils.equal(ring.keyHash, prev.get(2)))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!Script.isKey(vector.get(1)))
|
if (!Script.isKey(vector.get(1)))
|
||||||
@ -572,7 +572,7 @@ MTX.prototype.signInput = function signInput(index, addr, key, type) {
|
|||||||
|
|
||||||
// Find the key index so we can place
|
// Find the key index so we can place
|
||||||
// the signature in the same index.
|
// the signature in the same index.
|
||||||
keyIndex = utils.indexOf(keys, addr.publicKey);
|
keyIndex = utils.indexOf(keys, ring.publicKey);
|
||||||
|
|
||||||
// Our public key is not in the prev_out
|
// Our public key is not in the prev_out
|
||||||
// script. We tried to sign a transaction
|
// script. We tried to sign a transaction
|
||||||
@ -703,7 +703,7 @@ MTX.prototype.isSigned = function isSigned() {
|
|||||||
/**
|
/**
|
||||||
* Built input scripts (or witnesses) and sign the inputs.
|
* Built input scripts (or witnesses) and sign the inputs.
|
||||||
* @param {Number} index - Index of input being signed.
|
* @param {Number} index - Index of input being signed.
|
||||||
* @param {KeyRing} addr - Address used to sign. The address
|
* @param {KeyRing} ring - Address used to sign. The address
|
||||||
* must be able to redeem the coin.
|
* must be able to redeem the coin.
|
||||||
* @param {HDPrivateKey|KeyPair|Buffer} key - Private key.
|
* @param {HDPrivateKey|KeyPair|Buffer} key - Private key.
|
||||||
* @param {SighashType} type
|
* @param {SighashType} type
|
||||||
@ -711,7 +711,7 @@ MTX.prototype.isSigned = function isSigned() {
|
|||||||
* @throws on unavailable coins.
|
* @throws on unavailable coins.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
MTX.prototype.sign = function sign(index, addr, key, type) {
|
MTX.prototype.sign = function sign(index, ring, key, type) {
|
||||||
var input;
|
var input;
|
||||||
|
|
||||||
if (index && typeof index === 'object')
|
if (index && typeof index === 'object')
|
||||||
@ -721,11 +721,11 @@ MTX.prototype.sign = function sign(index, addr, key, type) {
|
|||||||
assert(input);
|
assert(input);
|
||||||
|
|
||||||
// Build script for input
|
// Build script for input
|
||||||
if (!this.scriptInput(index, addr))
|
if (!this.scriptInput(index, ring))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Sign input
|
// Sign input
|
||||||
if (!this.signInput(index, addr, key, type))
|
if (!this.signInput(index, ring, key, type))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@ -1030,8 +1030,8 @@ Wallet.prototype.resend = function resend(callback) {
|
|||||||
|
|
||||||
Wallet.prototype.deriveInputs = function deriveInputs(tx, callback) {
|
Wallet.prototype.deriveInputs = function deriveInputs(tx, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var addresses = [];
|
var rings = [];
|
||||||
var address;
|
var ring;
|
||||||
|
|
||||||
this.getInputPaths(tx, function(err, paths) {
|
this.getInputPaths(tx, function(err, paths) {
|
||||||
if (err)
|
if (err)
|
||||||
@ -1045,8 +1045,8 @@ Wallet.prototype.deriveInputs = function deriveInputs(tx, callback) {
|
|||||||
if (!account)
|
if (!account)
|
||||||
return next();
|
return next();
|
||||||
|
|
||||||
address = account.deriveAddress(path.change, path.index);
|
ring = account.deriveAddress(path.change, path.index);
|
||||||
addresses.push(address);
|
rings.push(ring);
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
@ -1054,7 +1054,7 @@ Wallet.prototype.deriveInputs = function deriveInputs(tx, callback) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
return callback(null, addresses);
|
return callback(null, rings);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -1068,7 +1068,7 @@ Wallet.prototype.deriveInputs = function deriveInputs(tx, callback) {
|
|||||||
Wallet.prototype.getKeyring = function getKeyring(address, callback) {
|
Wallet.prototype.getKeyring = function getKeyring(address, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var hash = bcoin.address.getHash(address, 'hex');
|
var hash = bcoin.address.getHash(address, 'hex');
|
||||||
var address;
|
var ring;
|
||||||
|
|
||||||
if (!hash)
|
if (!hash)
|
||||||
return callback();
|
return callback();
|
||||||
@ -1087,9 +1087,9 @@ Wallet.prototype.getKeyring = function getKeyring(address, callback) {
|
|||||||
if (!account)
|
if (!account)
|
||||||
return callback();
|
return callback();
|
||||||
|
|
||||||
address = account.deriveAddress(path.change, path.index);
|
ring = account.deriveAddress(path.change, path.index);
|
||||||
|
|
||||||
return callback(null, address);
|
return callback(null, ring);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -1326,7 +1326,7 @@ Wallet.prototype.handleTX = function handleTX(info, callback) {
|
|||||||
|
|
||||||
Wallet.prototype.getRedeem = function getRedeem(hash, callback) {
|
Wallet.prototype.getRedeem = function getRedeem(hash, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var address;
|
var ring;
|
||||||
|
|
||||||
if (typeof hash === 'string')
|
if (typeof hash === 'string')
|
||||||
hash = new Buffer(hash, 'hex');
|
hash = new Buffer(hash, 'hex');
|
||||||
@ -1345,14 +1345,14 @@ Wallet.prototype.getRedeem = function getRedeem(hash, callback) {
|
|||||||
if (!account)
|
if (!account)
|
||||||
return callback();
|
return callback();
|
||||||
|
|
||||||
address = account.deriveAddress(path.change, path.index);
|
ring = account.deriveAddress(path.change, path.index);
|
||||||
|
|
||||||
if (address.program && hash.length === 20) {
|
if (ring.program && hash.length === 20) {
|
||||||
if (utils.equal(hash, address.programHash))
|
if (utils.equal(hash, ring.programHash))
|
||||||
return callback(null, address.program);
|
return callback(null, ring.program);
|
||||||
}
|
}
|
||||||
|
|
||||||
return callback(null, address.script);
|
return callback(null, ring.script);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -1372,12 +1372,12 @@ Wallet.prototype.scriptInputs = function scriptInputs(tx, callback) {
|
|||||||
var total = 0;
|
var total = 0;
|
||||||
var i;
|
var i;
|
||||||
|
|
||||||
this.deriveInputs(tx, function(err, addresses) {
|
this.deriveInputs(tx, function(err, rings) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
for (i = 0; i < addresses.length; i++)
|
for (i = 0; i < rings.length; i++)
|
||||||
total += addresses[i].scriptInputs(tx);
|
total += rings[i].scriptInputs(tx);
|
||||||
|
|
||||||
return callback(null, total);
|
return callback(null, total);
|
||||||
});
|
});
|
||||||
@ -1406,7 +1406,7 @@ Wallet.prototype.sign = function sign(tx, options, callback) {
|
|||||||
if (typeof options === 'string' || Buffer.isBuffer(options))
|
if (typeof options === 'string' || Buffer.isBuffer(options))
|
||||||
options = { passphrase: options };
|
options = { passphrase: options };
|
||||||
|
|
||||||
this.deriveInputs(tx, function(err, addresses) {
|
this.deriveInputs(tx, function(err, rings) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
@ -1414,29 +1414,29 @@ Wallet.prototype.sign = function sign(tx, options, callback) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
self._sign(addresses, master, tx, options.index, options.type, callback);
|
self._sign(rings, master, tx, options.index, options.type, callback);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sign a transaction.
|
* Sign a transaction.
|
||||||
* @param {KeyRing[]} addresses
|
* @param {KeyRing[]} rings
|
||||||
* @param {HDPrivateKey} master
|
* @param {HDPrivateKey} master
|
||||||
* @param {MTX} tx
|
* @param {MTX} tx
|
||||||
* @param {Number?} index
|
* @param {Number?} index
|
||||||
* @param {SighashType?} type
|
* @param {SighashType?} type
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Wallet.sign = function sign(addresses, master, tx, index, type) {
|
Wallet.sign = function sign(rings, master, tx, index, type) {
|
||||||
var total = 0;
|
var total = 0;
|
||||||
var i, address, key;
|
var i, ring, key;
|
||||||
|
|
||||||
for (i = 0; i < addresses.length; i++) {
|
for (i = 0; i < rings.length; i++) {
|
||||||
address = addresses[i];
|
ring = rings[i];
|
||||||
key = address.derive(master);
|
key = ring.derive(master);
|
||||||
assert(utils.equal(key.getPublicKey(), address.key));
|
assert(utils.equal(key.getPublicKey(), ring.key));
|
||||||
total += address.sign(tx, key, index, type);
|
total += ring.sign(tx, key, index, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
@ -1444,7 +1444,7 @@ Wallet.sign = function sign(addresses, master, tx, index, type) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sign a transaction asynchronously.
|
* Sign a transaction asynchronously.
|
||||||
* @param {KeyRing[]} addresses
|
* @param {KeyRing[]} rings
|
||||||
* @param {HDPrivateKey} master
|
* @param {HDPrivateKey} master
|
||||||
* @param {MTX} tx
|
* @param {MTX} tx
|
||||||
* @param {Number?} index
|
* @param {Number?} index
|
||||||
@ -1453,20 +1453,20 @@ Wallet.sign = function sign(addresses, master, tx, index, type) {
|
|||||||
* of inputs scripts built and signed).
|
* of inputs scripts built and signed).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Wallet.prototype._sign = function _sign(addresses, master, tx, index, type, callback) {
|
Wallet.prototype._sign = function _sign(rings, master, tx, index, type, callback) {
|
||||||
var result;
|
var result;
|
||||||
|
|
||||||
if (!this.workerPool) {
|
if (!this.workerPool) {
|
||||||
callback = utils.asyncify(callback);
|
callback = utils.asyncify(callback);
|
||||||
try {
|
try {
|
||||||
result = Wallet.sign(addresses, master, tx, index, type);
|
result = Wallet.sign(rings, master, tx, index, type);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return callback(e);
|
return callback(e);
|
||||||
}
|
}
|
||||||
return callback(null, result);
|
return callback(null, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.workerPool.sign(addresses, master, tx, index, type, callback);
|
this.workerPool.sign(rings, master, tx, index, type, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2390,11 +2390,11 @@ Account.prototype.addKey = function addKey(key, callback) {
|
|||||||
return callback(e);
|
return callback(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._checkKeys(function(err, has) {
|
this._checkKeys(function(err, exists) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (has) {
|
if (exists) {
|
||||||
self.spliceKey(key);
|
self.spliceKey(key);
|
||||||
return callback(new Error('Cannot add a key from another account.'));
|
return callback(new Error('Cannot add a key from another account.'));
|
||||||
}
|
}
|
||||||
@ -2417,7 +2417,7 @@ Account.prototype.addKey = function addKey(key, callback) {
|
|||||||
|
|
||||||
Account.prototype._checkKeys = function _checkKeys(callback) {
|
Account.prototype._checkKeys = function _checkKeys(callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var address;
|
var ring, hash;
|
||||||
|
|
||||||
if (this.initialized || this.type !== keyTypes.MULTISIG)
|
if (this.initialized || this.type !== keyTypes.MULTISIG)
|
||||||
return callback(null, false);
|
return callback(null, false);
|
||||||
@ -2425,9 +2425,10 @@ Account.prototype._checkKeys = function _checkKeys(callback) {
|
|||||||
if (this.keys.length !== this.n)
|
if (this.keys.length !== this.n)
|
||||||
return callback(null, false);
|
return callback(null, false);
|
||||||
|
|
||||||
address = this.deriveReceive(0).getScriptAddress();
|
ring = this.deriveReceive(0);
|
||||||
|
hash = ring.getScriptHash('hex');
|
||||||
|
|
||||||
this.db.getAddressPaths(address.getHash('hex'), function(err, paths) {
|
this.db.getAddressPaths(hash, function(err, paths) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
@ -2485,32 +2486,32 @@ Account.prototype.createChange = function createChange(callback) {
|
|||||||
|
|
||||||
Account.prototype.createAddress = function createAddress(change, callback) {
|
Account.prototype.createAddress = function createAddress(change, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var address, lookahead;
|
var ring, lookahead;
|
||||||
|
|
||||||
if (typeof change === 'function') {
|
if (typeof change === 'function') {
|
||||||
callback = change;
|
callback = change;
|
||||||
change = null;
|
change = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (change) {
|
if (change) {
|
||||||
address = this.deriveChange(this.changeDepth);
|
ring = this.deriveChange(this.changeDepth);
|
||||||
lookahead = this.deriveChange(this.changeDepth + this.lookahead);
|
lookahead = this.deriveChange(this.changeDepth + this.lookahead);
|
||||||
this.changeDepth++;
|
this.changeDepth++;
|
||||||
this.changeAddress = address;
|
this.changeAddress = ring;
|
||||||
} else {
|
} else {
|
||||||
address = this.deriveReceive(this.receiveDepth);
|
ring = this.deriveReceive(this.receiveDepth);
|
||||||
lookahead = this.deriveReceive(this.receiveDepth + this.lookahead);
|
lookahead = this.deriveReceive(this.receiveDepth + this.lookahead);
|
||||||
this.receiveDepth++;
|
this.receiveDepth++;
|
||||||
this.receiveAddress = address;
|
this.receiveAddress = ring;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.saveAddress([address, lookahead], function(err) {
|
this.saveAddress([ring, lookahead], function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
self.save();
|
self.save();
|
||||||
|
|
||||||
return callback(null, address);
|
return callback(null, ring);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -2570,12 +2571,12 @@ Account.prototype.save = function save() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Save addresses to path map.
|
* Save addresses to path map.
|
||||||
* @param {KeyRing[]} address
|
* @param {KeyRing[]} rings
|
||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Account.prototype.saveAddress = function saveAddress(address, callback) {
|
Account.prototype.saveAddress = function saveAddress(rings, callback) {
|
||||||
return this.db.saveAddress(this.wid, address, callback);
|
return this.db.saveAddress(this.wid, rings, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2588,17 +2589,17 @@ Account.prototype.saveAddress = function saveAddress(address, callback) {
|
|||||||
|
|
||||||
Account.prototype.setDepth = function setDepth(receiveDepth, changeDepth, callback) {
|
Account.prototype.setDepth = function setDepth(receiveDepth, changeDepth, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var addresses = [];
|
var rings = [];
|
||||||
var i, receive, change;
|
var i, receive, change;
|
||||||
|
|
||||||
if (receiveDepth > this.receiveDepth) {
|
if (receiveDepth > this.receiveDepth) {
|
||||||
for (i = this.receiveDepth; i < receiveDepth; i++) {
|
for (i = this.receiveDepth; i < receiveDepth; i++) {
|
||||||
receive = this.deriveReceive(i);
|
receive = this.deriveReceive(i);
|
||||||
addresses.push(receive);
|
rings.push(receive);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = receiveDepth; i < receiveDepth + this.lookahead; i++)
|
for (i = receiveDepth; i < receiveDepth + this.lookahead; i++)
|
||||||
addresses.push(this.deriveReceive(i));
|
rings.push(this.deriveReceive(i));
|
||||||
|
|
||||||
this.receiveAddress = receive;
|
this.receiveAddress = receive;
|
||||||
this.receiveDepth = receiveDepth;
|
this.receiveDepth = receiveDepth;
|
||||||
@ -2607,20 +2608,20 @@ Account.prototype.setDepth = function setDepth(receiveDepth, changeDepth, callba
|
|||||||
if (changeDepth > this.changeDepth) {
|
if (changeDepth > this.changeDepth) {
|
||||||
for (i = this.changeDepth; i < changeDepth; i++) {
|
for (i = this.changeDepth; i < changeDepth; i++) {
|
||||||
change = this.deriveChange(i);
|
change = this.deriveChange(i);
|
||||||
addresses.push(change);
|
rings.push(change);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = changeDepth; i < changeDepth + this.lookahead; i++)
|
for (i = changeDepth; i < changeDepth + this.lookahead; i++)
|
||||||
addresses.push(this.deriveChange(i));
|
rings.push(this.deriveChange(i));
|
||||||
|
|
||||||
this.changeAddress = change;
|
this.changeAddress = change;
|
||||||
this.changeDepth = changeDepth;
|
this.changeDepth = changeDepth;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (addresses.length === 0)
|
if (rings.length === 0)
|
||||||
return callback();
|
return callback();
|
||||||
|
|
||||||
this.saveAddress(addresses, function(err) {
|
this.saveAddress(rings, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
|
|||||||
@ -400,18 +400,18 @@ WalletDB.prototype.loadFilter = function loadFilter(callback) {
|
|||||||
/**
|
/**
|
||||||
* Test the bloom filter against an array of address hashes.
|
* Test the bloom filter against an array of address hashes.
|
||||||
* @private
|
* @private
|
||||||
* @param {Hash[]} addresses
|
* @param {Hash[]} hashes
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
WalletDB.prototype.testFilter = function testFilter(addresses) {
|
WalletDB.prototype.testFilter = function testFilter(hashes) {
|
||||||
var i;
|
var i;
|
||||||
|
|
||||||
if (!this.filter)
|
if (!this.filter)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
for (i = 0; i < addresses.length; i++) {
|
for (i = 0; i < hashes.length; i++) {
|
||||||
if (this.filter.test(addresses[i], 'hex'))
|
if (this.filter.test(hashes[i], 'hex'))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -907,35 +907,35 @@ WalletDB.prototype.hasAccount = function hasAccount(wid, account, callback) {
|
|||||||
* 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} wid
|
* @param {WalletID} wid
|
||||||
* @param {KeyRing[]} addresses
|
* @param {KeyRing[]} rings
|
||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
|
|
||||||
WalletDB.prototype.saveAddress = function saveAddress(wid, addresses, callback) {
|
WalletDB.prototype.saveAddress = function saveAddress(wid, rings, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var items = [];
|
var items = [];
|
||||||
var batch = this.batch(wid);
|
var batch = this.batch(wid);
|
||||||
var i, address, path;
|
var i, ring, path;
|
||||||
|
|
||||||
for (i = 0; i < addresses.length; i++) {
|
for (i = 0; i < rings.length; i++) {
|
||||||
address = addresses[i];
|
ring = rings[i];
|
||||||
path = Path.fromKeyRing(address);
|
path = Path.fromKeyRing(ring);
|
||||||
|
|
||||||
items.push([address.getAddress(), path]);
|
items.push([ring.getAddress(), path]);
|
||||||
|
|
||||||
if (address.witness)
|
if (ring.witness)
|
||||||
items.push([address.getProgramAddress(), path]);
|
items.push([ring.getProgramAddress(), path]);
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.forEachSerial(items, function(item, next) {
|
utils.forEachSerial(items, function(item, next) {
|
||||||
var address = item[0];
|
var ring = item[0];
|
||||||
var path = item[1];
|
var path = item[1];
|
||||||
var hash = address.getHash('hex');
|
var hash = ring.getHash('hex');
|
||||||
|
|
||||||
if (self.filter)
|
if (self.filter)
|
||||||
self.filter.add(hash, 'hex');
|
self.filter.add(hash, 'hex');
|
||||||
|
|
||||||
self.emit('save address', address, path);
|
self.emit('save address', ring, path);
|
||||||
|
|
||||||
self.getAddressPaths(hash, function(err, paths) {
|
self.getAddressPaths(hash, function(err, paths) {
|
||||||
if (err)
|
if (err)
|
||||||
@ -995,12 +995,12 @@ WalletDB.prototype.getAddressPaths = function getAddressPaths(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} wid
|
* @param {WalletID} wid
|
||||||
* @param {Hash} address
|
* @param {Hash} hash
|
||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
|
|
||||||
WalletDB.prototype.hasAddress = function hasAddress(wid, address, callback) {
|
WalletDB.prototype.hasAddress = function hasAddress(wid, hash, callback) {
|
||||||
this.getAddressPaths(address, function(err, paths) {
|
this.getAddressPaths(hash, function(err, paths) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
@ -1133,13 +1133,13 @@ WalletDB.prototype.fetchWallet = function fetchWallet(wid, callback, handler) {
|
|||||||
|
|
||||||
WalletDB.prototype.mapWallets = function mapWallets(tx, callback) {
|
WalletDB.prototype.mapWallets = function mapWallets(tx, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var addresses = tx.getHashes('hex');
|
var hashes = tx.getHashes('hex');
|
||||||
var wallets;
|
var wallets;
|
||||||
|
|
||||||
if (!this.testFilter(addresses))
|
if (!this.testFilter(hashes))
|
||||||
return callback();
|
return callback();
|
||||||
|
|
||||||
this.getTable(addresses, function(err, table) {
|
this.getTable(hashes, function(err, table) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
@ -1160,10 +1160,10 @@ WalletDB.prototype.mapWallets = function mapWallets(tx, callback) {
|
|||||||
|
|
||||||
WalletDB.prototype.getPathInfo = function getPathInfo(wallet, tx, callback) {
|
WalletDB.prototype.getPathInfo = function getPathInfo(wallet, tx, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var addresses = tx.getHashes('hex');
|
var hashes = tx.getHashes('hex');
|
||||||
var info;
|
var info;
|
||||||
|
|
||||||
this.getTable(addresses, function(err, table) {
|
this.getTable(hashes, function(err, table) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
@ -1179,24 +1179,24 @@ WalletDB.prototype.getPathInfo = function getPathInfo(wallet, tx, callback) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Map address hashes to paths.
|
* Map address hashes to paths.
|
||||||
* @param {Hash[]} address - Address hashes.
|
* @param {Hash[]} hashes - Address hashes.
|
||||||
* @param {Function} callback - Returns [Error, {@link AddressTable}].
|
* @param {Function} callback - Returns [Error, {@link AddressTable}].
|
||||||
*/
|
*/
|
||||||
|
|
||||||
WalletDB.prototype.getTable = function getTable(addresses, callback) {
|
WalletDB.prototype.getTable = function getTable(hashes, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var table = {};
|
var table = {};
|
||||||
var count = 0;
|
var count = 0;
|
||||||
var i, keys, values;
|
var i, keys, values;
|
||||||
|
|
||||||
utils.forEachSerial(addresses, function(address, next) {
|
utils.forEachSerial(hashes, function(hash, next) {
|
||||||
self.getAddressPaths(address, function(err, paths) {
|
self.getAddressPaths(hash, function(err, paths) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
|
|
||||||
if (!paths) {
|
if (!paths) {
|
||||||
assert(!table[address]);
|
assert(!table[hash]);
|
||||||
table[address] = [];
|
table[hash] = [];
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1206,8 +1206,8 @@ WalletDB.prototype.getTable = function getTable(addresses, callback) {
|
|||||||
for (i = 0; i < keys.length; i++)
|
for (i = 0; i < keys.length; i++)
|
||||||
values.push(paths[keys[i]]);
|
values.push(paths[keys[i]]);
|
||||||
|
|
||||||
assert(!table[address]);
|
assert(!table[hash]);
|
||||||
table[address] = values;
|
table[hash] = values;
|
||||||
count += values.length;
|
count += values.length;
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
@ -1523,12 +1523,12 @@ 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} wid
|
* @param {WalletID} wid
|
||||||
* @param {Hash} address
|
* @param {Hash} hash
|
||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
|
|
||||||
WalletDB.prototype.getAddressPath = function getAddressPath(wid, address, callback) {
|
WalletDB.prototype.getAddressPath = function getAddressPath(wid, hash, callback) {
|
||||||
this.getAddressPaths(address, function(err, paths) {
|
this.getAddressPaths(hash, function(err, paths) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
@ -1629,21 +1629,21 @@ Path.prototype.toRaw = function toRaw(writer) {
|
|||||||
* Inject properties from keyring.
|
* Inject properties from keyring.
|
||||||
* @private
|
* @private
|
||||||
* @param {WalletID} wid
|
* @param {WalletID} wid
|
||||||
* @param {KeyRing} address
|
* @param {KeyRing} ring
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Path.prototype.fromKeyRing = function fromKeyRing(address) {
|
Path.prototype.fromKeyRing = function fromKeyRing(ring) {
|
||||||
this.wid = address.wid;
|
this.wid = ring.wid;
|
||||||
this.name = address.name;
|
this.name = ring.name;
|
||||||
this.account = address.account;
|
this.account = ring.account;
|
||||||
this.change = address.change;
|
this.change = ring.change;
|
||||||
this.index = address.index;
|
this.index = ring.index;
|
||||||
|
|
||||||
this.version = address.witness ? 0 : -1;
|
this.version = ring.witness ? 0 : -1;
|
||||||
this.type = address.getScriptType();
|
this.type = ring.getScriptType();
|
||||||
|
|
||||||
this.id = address.id;
|
this.id = ring.id;
|
||||||
this.hash = address.getHash('hex');
|
this.hash = ring.getHash('hex');
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
@ -1651,12 +1651,12 @@ Path.prototype.fromKeyRing = function fromKeyRing(address) {
|
|||||||
/**
|
/**
|
||||||
* Instantiate path from keyring.
|
* Instantiate path from keyring.
|
||||||
* @param {WalletID} wid
|
* @param {WalletID} wid
|
||||||
* @param {KeyRing} address
|
* @param {KeyRing} ring
|
||||||
* @returns {Path}
|
* @returns {Path}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Path.fromKeyRing = function fromKeyRing(address) {
|
Path.fromKeyRing = function fromKeyRing(ring) {
|
||||||
return new Path().fromKeyRing(address);
|
return new Path().fromKeyRing(ring);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1856,28 +1856,28 @@ PathInfo.fromTX = function fromTX(db, wid, tx, table) {
|
|||||||
/**
|
/**
|
||||||
* Test whether the map has paths
|
* Test whether the map has paths
|
||||||
* for a given address hash.
|
* for a given address hash.
|
||||||
* @param {Hash} address
|
* @param {Hash} hash
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
PathInfo.prototype.hasPath = function hasPath(address) {
|
PathInfo.prototype.hasPath = function hasPath(hash) {
|
||||||
if (!address)
|
if (!hash)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return this.pathMap[address] != null;
|
return this.pathMap[hash] != null;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get paths for a given address hash.
|
* Get paths for a given address hash.
|
||||||
* @param {Hash} address
|
* @param {Hash} hash
|
||||||
* @returns {Path[]|null}
|
* @returns {Path[]|null}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
PathInfo.prototype.getPath = function getPath(address) {
|
PathInfo.prototype.getPath = function getPath(hash) {
|
||||||
if (!address)
|
if (!hash)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
return this.pathMap[address];
|
return this.pathMap[hash];
|
||||||
};
|
};
|
||||||
|
|
||||||
PathInfo.prototype.toDetails = function toDetails() {
|
PathInfo.prototype.toDetails = function toDetails() {
|
||||||
|
|||||||
@ -242,7 +242,7 @@ Workers.prototype.verify = function verify(tx, flags, callback) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the tx signing job (default timeout).
|
* Execute the tx signing job (default timeout).
|
||||||
* @param {KeyRing[]} addresses
|
* @param {KeyRing[]} rings
|
||||||
* @param {HDPrivateKey} master
|
* @param {HDPrivateKey} master
|
||||||
* @param {MTX} tx
|
* @param {MTX} tx
|
||||||
* @param {Number?} index
|
* @param {Number?} index
|
||||||
@ -250,8 +250,8 @@ Workers.prototype.verify = function verify(tx, flags, callback) {
|
|||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Workers.prototype.sign = function sign(addresses, master, tx, index, type, callback) {
|
Workers.prototype.sign = function sign(rings, master, tx, index, type, callback) {
|
||||||
var args = [addresses, master, tx, index, type];
|
var args = [rings, master, tx, index, type];
|
||||||
var i, input, sig, sigs, total;
|
var i, input, sig, sigs, total;
|
||||||
|
|
||||||
return this.execute('sign', args, -1, function(err, result) {
|
return this.execute('sign', args, -1, function(err, result) {
|
||||||
@ -772,15 +772,15 @@ jobs.verify = function verify(tx, flags) {
|
|||||||
/**
|
/**
|
||||||
* Execute Wallet.sign() on worker.
|
* Execute Wallet.sign() on worker.
|
||||||
* @see Wallet.sign
|
* @see Wallet.sign
|
||||||
* @param {KeyRing[]} addresses
|
* @param {KeyRing[]} rings
|
||||||
* @param {HDPrivateKey} master
|
* @param {HDPrivateKey} master
|
||||||
* @param {MTX} tx
|
* @param {MTX} tx
|
||||||
* @param {Number?} index
|
* @param {Number?} index
|
||||||
* @param {SighashType?} type
|
* @param {SighashType?} type
|
||||||
*/
|
*/
|
||||||
|
|
||||||
jobs.sign = function sign(addresses, master, tx, index, type) {
|
jobs.sign = function sign(rings, master, tx, index, type) {
|
||||||
var total = bcoin.wallet.sign(addresses, master, tx, index, type);
|
var total = bcoin.wallet.sign(rings, master, tx, index, type);
|
||||||
var sigs = [];
|
var sigs = [];
|
||||||
var i, input;
|
var i, input;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user