more refactoring.
This commit is contained in:
parent
6062f1ce94
commit
849f1d842e
@ -31,6 +31,8 @@ function AbstractBlock(data) {
|
|||||||
|
|
||||||
this.valid = null;
|
this.valid = null;
|
||||||
this._hash = null;
|
this._hash = null;
|
||||||
|
|
||||||
|
this.txs = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
AbstractBlock.prototype.hash = function hash(enc) {
|
AbstractBlock.prototype.hash = function hash(enc) {
|
||||||
@ -82,6 +84,18 @@ AbstractBlock.prototype.isGenesis = function isGenesis() {
|
|||||||
return this.hash('hex') === network.genesis.hash;
|
return this.hash('hex') === network.genesis.hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
AbstractBlock.prototype.setHeight = function setHeight(height) {
|
||||||
|
var i;
|
||||||
|
|
||||||
|
this.height = height;
|
||||||
|
|
||||||
|
if (!this.txs)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (i = 0; i < this.txs.length; i++)
|
||||||
|
this.txs[i].height = height;
|
||||||
|
};
|
||||||
|
|
||||||
AbstractBlock.prototype.__defineGetter__('rhash', function() {
|
AbstractBlock.prototype.__defineGetter__('rhash', function() {
|
||||||
return utils.revHex(this.hash('hex'));
|
return utils.revHex(this.hash('hex'));
|
||||||
});
|
});
|
||||||
|
|||||||
@ -16,6 +16,8 @@ var BufferReader = require('./reader');
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function Address(options) {
|
function Address(options) {
|
||||||
|
var i;
|
||||||
|
|
||||||
if (!(this instanceof Address))
|
if (!(this instanceof Address))
|
||||||
return new Address(options);
|
return new Address(options);
|
||||||
|
|
||||||
@ -51,9 +53,10 @@ function Address(options) {
|
|||||||
|
|
||||||
this.addKey(this.getPublicKey());
|
this.addKey(this.getPublicKey());
|
||||||
|
|
||||||
(options.keys || []).forEach(function(key) {
|
if (options.keys) {
|
||||||
this.addKey(key);
|
for (i = 0; i < options.keys.length; i++)
|
||||||
}, this);
|
this.addKey(options.keys[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Address.isAddress = function isAddress(obj) {
|
Address.isAddress = function isAddress(obj) {
|
||||||
@ -284,30 +287,34 @@ Address.prototype.ownOutput = function ownOutput(tx, index) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Address.prototype.scriptInputs = function scriptInputs(tx, index) {
|
Address.prototype.scriptInputs = function scriptInputs(tx, index) {
|
||||||
var self = this;
|
var total = 0;
|
||||||
|
var i, input;
|
||||||
|
|
||||||
if (index && typeof index === 'object')
|
if (index && typeof index === 'object')
|
||||||
index = tx.inputs.indexOf(index);
|
index = tx.inputs.indexOf(index);
|
||||||
|
|
||||||
return tx.inputs.reduce(function(total, input, i) {
|
for (i = 0; i < tx.inputs.length; i++) {
|
||||||
|
input = tx.inputs[i];
|
||||||
|
|
||||||
if (index != null && index !== i)
|
if (index != null && index !== i)
|
||||||
return total;
|
continue;
|
||||||
|
|
||||||
if (!input.coin)
|
if (!input.coin)
|
||||||
return total;
|
continue;
|
||||||
|
|
||||||
if (!self.ownOutput(input.coin))
|
if (!this.ownOutput(input.coin))
|
||||||
return total;
|
continue;
|
||||||
|
|
||||||
if (tx.scriptInput(i, self))
|
if (tx.scriptInput(i, this))
|
||||||
total++;
|
total++;
|
||||||
|
}
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
}, 0);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Address.prototype.signInputs = function signInputs(tx, type, index) {
|
Address.prototype.signInputs = function signInputs(tx, type, index) {
|
||||||
var self = this;
|
var total = 0;
|
||||||
|
var i, input;
|
||||||
|
|
||||||
if (index && typeof index === 'object')
|
if (index && typeof index === 'object')
|
||||||
index = tx.inputs.indexOf(index);
|
index = tx.inputs.indexOf(index);
|
||||||
@ -315,25 +322,28 @@ Address.prototype.signInputs = function signInputs(tx, type, index) {
|
|||||||
if (!this.key.privateKey)
|
if (!this.key.privateKey)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return tx.inputs.reduce(function(total, input, i) {
|
for (i = 0; i < tx.inputs.length; i++) {
|
||||||
|
input = tx.inputs[i];
|
||||||
|
|
||||||
if (index != null && index !== i)
|
if (index != null && index !== i)
|
||||||
return total;
|
continue;
|
||||||
|
|
||||||
if (!input.coin)
|
if (!input.coin)
|
||||||
return total;
|
continue;
|
||||||
|
|
||||||
if (!self.ownOutput(input.coin))
|
if (!this.ownOutput(input.coin))
|
||||||
return total;
|
continue;
|
||||||
|
|
||||||
if (tx.signInput(i, self, type))
|
if (tx.signInput(i, this, type))
|
||||||
total++;
|
total++;
|
||||||
|
}
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
}, 0);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Address.prototype.sign = function sign(tx, type, index) {
|
Address.prototype.sign = function sign(tx, type, index) {
|
||||||
var self = this;
|
var total = 0;
|
||||||
|
var i, input;
|
||||||
|
|
||||||
if (index && typeof index === 'object')
|
if (index && typeof index === 'object')
|
||||||
index = tx.inputs.indexOf(index);
|
index = tx.inputs.indexOf(index);
|
||||||
@ -341,23 +351,23 @@ Address.prototype.sign = function sign(tx, type, index) {
|
|||||||
if (!this.key.privateKey)
|
if (!this.key.privateKey)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// Add signature script to each input
|
for (i = 0; i < tx.inputs.length; i++) {
|
||||||
return tx.inputs.reduce(function(total, input, i) {
|
input = tx.inputs[i];
|
||||||
|
|
||||||
if (index != null && index !== i)
|
if (index != null && index !== i)
|
||||||
return total;
|
continue;
|
||||||
|
|
||||||
// Filter inputs that this wallet own
|
|
||||||
if (!input.coin)
|
if (!input.coin)
|
||||||
return total;
|
continue;
|
||||||
|
|
||||||
if (!self.ownOutput(input.coin))
|
if (!this.ownOutput(input.coin))
|
||||||
return total;
|
continue;
|
||||||
|
|
||||||
if (tx.sign(i, self, type))
|
if (tx.sign(i, this, type))
|
||||||
total++;
|
total++;
|
||||||
|
}
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
}, 0);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Address.prototype.__defineGetter__('privateKey', function() {
|
Address.prototype.__defineGetter__('privateKey', function() {
|
||||||
|
|||||||
@ -461,6 +461,10 @@ Block.fromCompact = function fromCompact(buf) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Block.prototype.toMerkle = function toMerkle(filter) {
|
||||||
|
return bcoin.merkleblock.fromBlock(this, filter);
|
||||||
|
};
|
||||||
|
|
||||||
Block.isBlock = function isBlock(obj) {
|
Block.isBlock = function isBlock(obj) {
|
||||||
return obj
|
return obj
|
||||||
&& typeof obj.merkleRoot === 'string'
|
&& typeof obj.merkleRoot === 'string'
|
||||||
|
|||||||
@ -1183,10 +1183,7 @@ Chain.prototype.add = function add(block, callback, force) {
|
|||||||
// Update the block height early
|
// Update the block height early
|
||||||
// Some things in verifyContext may
|
// Some things in verifyContext may
|
||||||
// need access to height on txs.
|
// need access to height on txs.
|
||||||
block.height = height;
|
block.setHeight(height);
|
||||||
block.txs.forEach(function(tx) {
|
|
||||||
tx.height = height;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Do "contextual" verification on our block
|
// Do "contextual" verification on our block
|
||||||
// now that we're certain its previous
|
// now that we're certain its previous
|
||||||
@ -1197,10 +1194,7 @@ Chain.prototype.add = function add(block, callback, force) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
// Couldn't verify block.
|
// Couldn't verify block.
|
||||||
// Revert the height.
|
// Revert the height.
|
||||||
block.height = -1;
|
block.setHeight(-1);
|
||||||
block.txs.forEach(function(tx) {
|
|
||||||
tx.height = -1;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (err.type === 'VerifyError') {
|
if (err.type === 'VerifyError') {
|
||||||
self.invalid[hash] = true;
|
self.invalid[hash] = true;
|
||||||
|
|||||||
@ -874,10 +874,18 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) {
|
|||||||
outputValue = tx.getOutputValue();
|
outputValue = tx.getOutputValue();
|
||||||
tryFree = options.free;
|
tryFree = options.free;
|
||||||
|
|
||||||
|
if (options.confirmed) {
|
||||||
|
coins = coins.filter(function(coin) {
|
||||||
|
return coin.height !== -1;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!options.selection || options.selection === 'age') {
|
if (!options.selection || options.selection === 'age') {
|
||||||
// Oldest unspents first
|
// Oldest unspents first
|
||||||
coins = coins.slice().sort(function(a, b) {
|
coins = coins.slice().sort(function(a, b) {
|
||||||
return a.height - b.height;
|
a = a.height === -1 ? 0x7fffffff : a.height;
|
||||||
|
b = b.height === -1 ? 0x7fffffff : b.height;
|
||||||
|
return a - b;
|
||||||
});
|
});
|
||||||
} else if (options.selection === 'random' || options.selection === 'all') {
|
} else if (options.selection === 'random' || options.selection === 'all') {
|
||||||
// Random unspents
|
// Random unspents
|
||||||
|
|||||||
@ -957,7 +957,7 @@ Peer.prototype._handleGetData = function handleGetData(items) {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
block = bcoin.merkleblock.fromBlock(block, self.filter);
|
block = block.toMerkle(self.filter);
|
||||||
|
|
||||||
self._write(self.framer.merkleBlock(block));
|
self._write(self.framer.merkleBlock(block));
|
||||||
|
|
||||||
|
|||||||
@ -31,17 +31,12 @@ function Pool(node, options) {
|
|||||||
this.options = options;
|
this.options = options;
|
||||||
this.network = node.network;
|
this.network = node.network;
|
||||||
|
|
||||||
options.spv = options.spv !== false;
|
if (options.relay == null) {
|
||||||
|
if (options.spv)
|
||||||
if (options.type === 'spv')
|
options.relay = false;
|
||||||
options.spv = true;
|
else
|
||||||
else if (options.type === 'full')
|
options.relay = true;
|
||||||
options.spv = false;
|
}
|
||||||
|
|
||||||
options.headers = options.headers;
|
|
||||||
options.relay = options.relay == null
|
|
||||||
? (!options.spv ? true : false)
|
|
||||||
: options.relay;
|
|
||||||
|
|
||||||
seeds = (options.seeds || network.seeds).slice();
|
seeds = (options.seeds || network.seeds).slice();
|
||||||
|
|
||||||
|
|||||||
@ -16,6 +16,8 @@ var network = bcoin.protocol.network;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function Wallet(options) {
|
function Wallet(options) {
|
||||||
|
var i;
|
||||||
|
|
||||||
if (!(this instanceof Wallet))
|
if (!(this instanceof Wallet))
|
||||||
return new Wallet(options);
|
return new Wallet(options);
|
||||||
|
|
||||||
@ -87,9 +89,10 @@ function Wallet(options) {
|
|||||||
|
|
||||||
this.addKey(this.accountKey);
|
this.addKey(this.accountKey);
|
||||||
|
|
||||||
(options.keys || []).forEach(function(key) {
|
if (options.keys) {
|
||||||
this.addKey(key);
|
for (i = 0; i < options.keys.length; i++)
|
||||||
}, this);
|
this.addKey(options.keys[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.inherits(Wallet, EventEmitter);
|
utils.inherits(Wallet, EventEmitter);
|
||||||
@ -184,7 +187,7 @@ Wallet.prototype.destroy = function destroy(callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.addKey = function addKey(key) {
|
Wallet.prototype.addKey = function addKey(key) {
|
||||||
var has;
|
var index, i;
|
||||||
|
|
||||||
if (key instanceof bcoin.wallet) {
|
if (key instanceof bcoin.wallet) {
|
||||||
assert(key.derivation === this.derivation);
|
assert(key.derivation === this.derivation);
|
||||||
@ -207,11 +210,14 @@ Wallet.prototype.addKey = function addKey(key) {
|
|||||||
throw new Error('Must add HD purpose keys to BIP45 wallet.');
|
throw new Error('Must add HD purpose keys to BIP45 wallet.');
|
||||||
}
|
}
|
||||||
|
|
||||||
has = this.keys.some(function(k) {
|
for (i = 0; i < this.keys.length; i++) {
|
||||||
return k.xpubkey === key.xpubkey;
|
if (this.keys[i].xpubkey === key.xpubkey) {
|
||||||
});
|
index = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (has)
|
if (index != null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
assert(!this._keysFinalized);
|
assert(!this._keysFinalized);
|
||||||
@ -223,7 +229,7 @@ Wallet.prototype.addKey = function addKey(key) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.removeKey = function removeKey(key) {
|
Wallet.prototype.removeKey = function removeKey(key) {
|
||||||
var index;
|
var index, i;
|
||||||
|
|
||||||
assert(!this._keysFinalized);
|
assert(!this._keysFinalized);
|
||||||
|
|
||||||
@ -248,11 +254,12 @@ Wallet.prototype.removeKey = function removeKey(key) {
|
|||||||
throw new Error('Must add HD purpose keys to BIP45 wallet.');
|
throw new Error('Must add HD purpose keys to BIP45 wallet.');
|
||||||
}
|
}
|
||||||
|
|
||||||
index = this.keys.map(function(k, i) {
|
for (i = 0; i < this.keys.length; i++) {
|
||||||
return k.xpubkey === key.xpubkey ? i : null;
|
if (this.keys[i].xpubkey === key.xpubkey) {
|
||||||
}).filter(function(i) {
|
index = i;
|
||||||
return i !== null;
|
break;
|
||||||
})[0];
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (index == null)
|
if (index == null)
|
||||||
return;
|
return;
|
||||||
@ -523,6 +530,8 @@ Wallet.prototype.fill = function fill(tx, options, callback) {
|
|||||||
try {
|
try {
|
||||||
tx.fill(coins, {
|
tx.fill(coins, {
|
||||||
selection: options.selection || 'age',
|
selection: options.selection || 'age',
|
||||||
|
confirmed: options.confirmed,
|
||||||
|
free: options.free,
|
||||||
fee: options.fee,
|
fee: options.fee,
|
||||||
subtractFee: options.subtractFee,
|
subtractFee: options.subtractFee,
|
||||||
changeAddress: self.changeAddress.getAddress(),
|
changeAddress: self.changeAddress.getAddress(),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user