refactor.
This commit is contained in:
parent
1afb9dbc27
commit
9356a40e62
@ -312,7 +312,7 @@ Address.prototype.scriptInputs = function scriptInputs(tx, index) {
|
||||
return total;
|
||||
};
|
||||
|
||||
Address.prototype.signInputs = function signInputs(tx, type, index) {
|
||||
Address.prototype.signInputs = function signInputs(tx, index, type) {
|
||||
var total = 0;
|
||||
var i, input;
|
||||
|
||||
@ -341,7 +341,7 @@ Address.prototype.signInputs = function signInputs(tx, type, index) {
|
||||
return total;
|
||||
};
|
||||
|
||||
Address.prototype.sign = function sign(tx, type, index) {
|
||||
Address.prototype.sign = function sign(tx, index, type) {
|
||||
var total = 0;
|
||||
var i, input;
|
||||
|
||||
|
||||
@ -62,6 +62,7 @@ ChainDB.prototype._init = function _init() {
|
||||
this.db = bcoin.ldb({
|
||||
name: this.options.name || (this.options.spv ? 'spvchain' : 'chain'),
|
||||
location: this.options.location,
|
||||
db: this.options.db,
|
||||
compression: true,
|
||||
cacheSize: 16 << 20,
|
||||
writeBufferSize: 8 << 20
|
||||
|
||||
@ -25,14 +25,14 @@ function Input(options, tx) {
|
||||
this.witness = bcoin.script.witness(options.witness);
|
||||
this._mutable = !tx || (tx instanceof bcoin.mtx);
|
||||
|
||||
if (options.coin) {
|
||||
if (options.coin)
|
||||
this.coin = bcoin.coin(options.coin);
|
||||
assert(typeof this.coin.hash === 'string');
|
||||
assert(typeof this.coin.index === 'number');
|
||||
}
|
||||
|
||||
if (Buffer.isBuffer(this.prevout.hash))
|
||||
this.prevout.hash = utils.toHex(this.prevout.hash);
|
||||
|
||||
assert(typeof this.prevout.hash === 'string');
|
||||
assert(typeof this.prevout.index === 'number');
|
||||
}
|
||||
|
||||
Input.prototype.__defineGetter__('type', function() {
|
||||
|
||||
@ -87,7 +87,7 @@ Mempool.prototype._init = function _init() {
|
||||
var options = {
|
||||
name: this.options.name || 'mempool',
|
||||
location: this.options.location,
|
||||
db: this.backend
|
||||
db: this.options.db || this.backend
|
||||
};
|
||||
|
||||
assert(unlock);
|
||||
|
||||
@ -164,7 +164,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
||||
assert(input);
|
||||
|
||||
// We should have previous outputs by now.
|
||||
assert(input.coin);
|
||||
assert(input.coin, 'Coins are not available for scripting.');
|
||||
|
||||
// Optimization: Don't bother with any below
|
||||
// calculation if the output is already templated.
|
||||
@ -348,7 +348,7 @@ MTX.prototype.signInput = function signInput(index, addr, type) {
|
||||
assert(input);
|
||||
|
||||
// We should have previous outputs by now.
|
||||
assert(input.coin);
|
||||
assert(input.coin, 'Coins are not available for signing.');
|
||||
|
||||
// Get the previous output's script
|
||||
prev = input.coin.script;
|
||||
|
||||
@ -284,8 +284,6 @@ BufferReader.prototype.seek = function seek(off) {
|
||||
};
|
||||
|
||||
BufferReader.prototype.createChecksum = function createChecksum() {
|
||||
assert(this.offset + 4 >= 0);
|
||||
assert(this.offset + 4 <= this.data.length);
|
||||
var start = this.stack[this.stack.length - 1] || 0;
|
||||
var data = this.data.slice(start, this.offset);
|
||||
return utils.readU32BE(utils.checksum(data), 0);
|
||||
|
||||
@ -1012,7 +1012,7 @@ utils.read64BE = function read64BE(arr, off) {
|
||||
return new bn(num, 'be').notn(64).addn(1).neg();
|
||||
}
|
||||
|
||||
return new bn(num);
|
||||
return new bn(num, 'be');
|
||||
};
|
||||
|
||||
utils.writeU8 = function writeU8(dst, num, off) {
|
||||
@ -1436,10 +1436,10 @@ utils.cmp = function cmp(a, b) {
|
||||
}
|
||||
|
||||
if (a.length < b.length)
|
||||
return -1;
|
||||
return -1;
|
||||
|
||||
if (a.length > b.length)
|
||||
return 1;
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
};
|
||||
@ -1756,7 +1756,7 @@ utils.indexOf = function indexOf(arr, buf) {
|
||||
};
|
||||
|
||||
utils.pad32 = function pad32(num) {
|
||||
assert(num >= 0, num);
|
||||
assert(num >= 0);
|
||||
num = num + '';
|
||||
while (num.length < 10)
|
||||
num = '0' + num;
|
||||
|
||||
@ -10,6 +10,7 @@ var utils = require('./utils');
|
||||
var assert = utils.assert;
|
||||
var constants = bcoin.protocol.constants;
|
||||
var network = bcoin.protocol.network;
|
||||
var BufferWriter = require('./writer');
|
||||
|
||||
/**
|
||||
* Wallet
|
||||
@ -66,7 +67,8 @@ function Wallet(options) {
|
||||
if (this.n > 1)
|
||||
this.type = 'multisig';
|
||||
|
||||
assert(this.type === 'pubkeyhash' || this.type === 'multisig');
|
||||
assert(this.type === 'pubkeyhash' || this.type === 'multisig',
|
||||
'`type` must be multisig or pubkeyhash.');
|
||||
|
||||
if (this.m < 1 || this.m > this.n)
|
||||
throw new Error('m ranges between 1 and n');
|
||||
@ -84,8 +86,7 @@ function Wallet(options) {
|
||||
this.id = this.getID();
|
||||
|
||||
// Non-alphanumeric IDs will break leveldb sorting.
|
||||
if (!/^[a-zA-Z0-9]+$/.test(this.id))
|
||||
throw new Error('Wallet IDs must be alphanumeric.');
|
||||
assert(/^[a-zA-Z0-9]+$/.test(this.id), 'Wallet IDs must be alphanumeric.');
|
||||
|
||||
this.addKey(this.accountKey);
|
||||
|
||||
@ -296,17 +297,19 @@ Wallet.prototype._finalizeKeys = function _finalizeKeys() {
|
||||
// bip44: Account key "address" (prefix: WLT)
|
||||
Wallet.prototype.getID = function getID() {
|
||||
var publicKey = this.accountKey.publicKey;
|
||||
var id;
|
||||
var p;
|
||||
|
||||
if (this.options.id)
|
||||
return this.options.id;
|
||||
|
||||
id = new Buffer(27);
|
||||
utils.copy(new Buffer([0x03, 0xbe, 0x04]), id, 0);
|
||||
utils.copy(utils.ripesha(publicKey), id, 3);
|
||||
utils.copy(utils.checksum(id.slice(0, 23)), id, 23);
|
||||
p = new BufferWriter();
|
||||
p.writeU8(0x03);
|
||||
p.writeU8(0xbe);
|
||||
p.writeU8(0x04);
|
||||
p.writeBytes(utils.ripesha(publicKey));
|
||||
p.writeChecksum();
|
||||
|
||||
return utils.toBase58(id);
|
||||
return utils.toBase58(p.render());
|
||||
};
|
||||
|
||||
Wallet.prototype.createReceive = function createReceive() {
|
||||
@ -534,6 +537,7 @@ Wallet.prototype.fill = function fill(tx, options, callback) {
|
||||
try {
|
||||
tx.fill(coins, {
|
||||
selection: options.selection || 'age',
|
||||
accurate: options.accurate,
|
||||
confirmed: options.confirmed,
|
||||
free: options.free,
|
||||
fee: options.fee,
|
||||
@ -574,7 +578,7 @@ Wallet.prototype.getTX = function getTX(hash, callback) {
|
||||
|
||||
Wallet.prototype.createTX = function createTX(options, outputs, callback) {
|
||||
var self = this;
|
||||
var tx;
|
||||
var tx, i;
|
||||
|
||||
if (typeof outputs === 'function') {
|
||||
callback = outputs;
|
||||
@ -593,9 +597,8 @@ Wallet.prototype.createTX = function createTX(options, outputs, callback) {
|
||||
tx = bcoin.mtx();
|
||||
|
||||
// Add the outputs
|
||||
outputs.forEach(function(output) {
|
||||
tx.addOutput(output);
|
||||
});
|
||||
for (i = 0; i < outputs.length; i++)
|
||||
tx.addOutput(outputs[i]);
|
||||
|
||||
// Fill the inputs with unspents
|
||||
this.fill(tx, options, function(err) {
|
||||
@ -620,12 +623,12 @@ Wallet.prototype.createTX = function createTX(options, outputs, callback) {
|
||||
});
|
||||
};
|
||||
|
||||
Wallet.prototype.deriveInput = function deriveInput(tx, i) {
|
||||
return this.deriveInputs(tx.inputs[i])[0];
|
||||
Wallet.prototype.deriveInput = function deriveInput(tx, index) {
|
||||
return this.deriveInputs(tx, index)[0];
|
||||
};
|
||||
|
||||
Wallet.prototype.deriveInputs = function deriveInputs(tx) {
|
||||
var paths = this.getInputPaths(tx);
|
||||
Wallet.prototype.deriveInputs = function deriveInputs(tx, index) {
|
||||
var paths = this.getInputPaths(tx, index);
|
||||
var addresses = [];
|
||||
var i;
|
||||
|
||||
@ -641,9 +644,9 @@ Wallet.prototype.getPath = function getPath(address) {
|
||||
return this.addressMap[address];
|
||||
};
|
||||
|
||||
Wallet.prototype.getInputPaths = function getInputPaths(tx) {
|
||||
Wallet.prototype.getInputPaths = function getInputPaths(tx, index) {
|
||||
var paths = [];
|
||||
var i, input, output, address, path;
|
||||
var i, input, address, path;
|
||||
|
||||
if (tx instanceof bcoin.input) {
|
||||
path = this.getPath(tx.coin.getAddress());
|
||||
@ -654,10 +657,13 @@ Wallet.prototype.getInputPaths = function getInputPaths(tx) {
|
||||
|
||||
for (i = 0; i < tx.inputs.length; i++) {
|
||||
input = tx.inputs[i];
|
||||
output = input.coin;
|
||||
assert(output);
|
||||
|
||||
address = output.getAddress();
|
||||
if (index != null && i !== index)
|
||||
continue;
|
||||
|
||||
assert(input.coin, 'Not all coins available.');
|
||||
|
||||
address = input.coin.getAddress();
|
||||
path = this.getPath(address);
|
||||
|
||||
if (!path)
|
||||
@ -669,7 +675,7 @@ Wallet.prototype.getInputPaths = function getInputPaths(tx) {
|
||||
return utils.uniqs(paths);
|
||||
};
|
||||
|
||||
Wallet.prototype.getOutputPaths = function getOutputPaths(tx) {
|
||||
Wallet.prototype.getOutputPaths = function getOutputPaths(tx, index) {
|
||||
var paths = [];
|
||||
var i, output, address, path;
|
||||
|
||||
@ -683,6 +689,9 @@ Wallet.prototype.getOutputPaths = function getOutputPaths(tx) {
|
||||
for (i = 0; i < tx.outputs.length; i++) {
|
||||
output = tx.outputs[i];
|
||||
|
||||
if (index != null && i !== index)
|
||||
continue;
|
||||
|
||||
address = output.getAddress();
|
||||
path = this.getPath(address);
|
||||
|
||||
@ -733,6 +742,9 @@ Wallet.prototype.syncOutputDepth = function syncOutputDepth(tx) {
|
||||
Wallet.prototype.getRedeem = function getRedeem(hash, prefix) {
|
||||
var addr, address;
|
||||
|
||||
if (typeof hash === 'string')
|
||||
hash = new Buffer(hash, 'hex');
|
||||
|
||||
if (!prefix) {
|
||||
if (hash.length === 20)
|
||||
prefix = 'scripthash';
|
||||
@ -868,7 +880,7 @@ Wallet.prototype._scan = function _scan(options, txByAddress, callback) {
|
||||
};
|
||||
|
||||
Wallet.prototype.scriptInputs = function scriptInputs(tx, index) {
|
||||
var addresses = this.deriveInputs(tx);
|
||||
var addresses = this.deriveInputs(tx, index);
|
||||
var total = 0;
|
||||
var i;
|
||||
|
||||
@ -878,24 +890,24 @@ Wallet.prototype.scriptInputs = function scriptInputs(tx, index) {
|
||||
return total;
|
||||
};
|
||||
|
||||
Wallet.prototype.signInputs = function signInputs(tx, type, index) {
|
||||
var addresses = this.deriveInputs(tx);
|
||||
Wallet.prototype.signInputs = function signInputs(tx, index, type) {
|
||||
var addresses = this.deriveInputs(tx, index);
|
||||
var total = 0;
|
||||
var i;
|
||||
|
||||
for (i = 0; i < addresses.length; i++)
|
||||
total += addresses[i].signInputs(tx, type, index);
|
||||
total += addresses[i].signInputs(tx, index, type);
|
||||
|
||||
return total;
|
||||
};
|
||||
|
||||
Wallet.prototype.sign = function sign(tx, type, index) {
|
||||
var addresses = this.deriveInputs(tx);
|
||||
Wallet.prototype.sign = function sign(tx, index, type) {
|
||||
var addresses = this.deriveInputs(tx, index);
|
||||
var total = 0;
|
||||
var i;
|
||||
|
||||
for (i = 0; i < addresses.length; i++)
|
||||
total += addresses[i].sign(tx, type, index);
|
||||
total += addresses[i].sign(tx, index, type);
|
||||
|
||||
return total;
|
||||
};
|
||||
|
||||
@ -79,6 +79,7 @@ WalletDB.prototype._init = function _init() {
|
||||
this.db = bcoin.ldb({
|
||||
name: this.options.name || 'wallet',
|
||||
location: this.options.location,
|
||||
db: this.options.db,
|
||||
cacheSize: 8 << 20,
|
||||
writeBufferSize: 4 << 20
|
||||
});
|
||||
|
||||
@ -370,8 +370,8 @@ describe('Wallet', function() {
|
||||
tx.addInput(coins1[1]);
|
||||
tx.addInput(coins1[2]);
|
||||
tx.addInput(coins2[1]);
|
||||
assert.equal(w1.sign(tx, 'all'), 2);
|
||||
assert.equal(w2.sign(tx, 'all'), 1);
|
||||
assert.equal(w1.sign(tx), 2);
|
||||
assert.equal(w2.sign(tx), 1);
|
||||
|
||||
// Verify
|
||||
assert.equal(tx.verify(), true);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user