refactor mtx.
This commit is contained in:
parent
8069360c7b
commit
59cd4a29fe
@ -205,6 +205,9 @@ function Input(options) {
|
||||
this.coin = null;
|
||||
this.mutable = false;
|
||||
|
||||
this._type = null;
|
||||
this._address = null;
|
||||
|
||||
if (options)
|
||||
this.fromOptions(options);
|
||||
}
|
||||
@ -217,9 +220,9 @@ function Input(options) {
|
||||
|
||||
Input.prototype.fromOptions = function fromOptions(options) {
|
||||
assert(options, 'Input data is required.');
|
||||
assert(options.prevout);
|
||||
|
||||
if (options.prevout)
|
||||
this.prevout.fromOptions(options.prevout);
|
||||
this.prevout.fromOptions(options.prevout);
|
||||
|
||||
if (options.script)
|
||||
this.script.fromOptions(options.script);
|
||||
|
||||
109
lib/bcoin/mtx.js
109
lib/bcoin/mtx.js
@ -150,30 +150,63 @@ MTX.prototype.clone = function clone() {
|
||||
*/
|
||||
|
||||
MTX.prototype.addInput = function addInput(options, index) {
|
||||
var input;
|
||||
|
||||
if (options instanceof TX)
|
||||
options = bcoin.coin.fromTX(options, index);
|
||||
|
||||
if (options instanceof bcoin.coin) {
|
||||
assert(typeof options.hash === 'string');
|
||||
assert(typeof options.index === 'number');
|
||||
options = {
|
||||
prevout: { hash: options.hash, index: options.index },
|
||||
coin: options
|
||||
};
|
||||
}
|
||||
|
||||
assert(options.prevout);
|
||||
|
||||
input = new bcoin.input(options);
|
||||
var input = new bcoin.input();
|
||||
input.mutable = true;
|
||||
|
||||
if (options instanceof TX)
|
||||
input.fromTX(options, index);
|
||||
else if (options instanceof bcoin.coin)
|
||||
input.fromCoin(options);
|
||||
else
|
||||
input.fromOptions(options);
|
||||
|
||||
this.inputs.push(input);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Add an output.
|
||||
* @example
|
||||
* tx.addOutput({ address: ..., value: 100000 });
|
||||
* tx.addOutput({ address: ..., value: utils.satoshi('0.1') });
|
||||
* tx.addOutput(receivingWallet, utils.satoshi('0.1'));
|
||||
* @param {Wallet|KeyRing|Object} obj - Wallet, Address,
|
||||
* or options (see {@link Script.createOutputScript} for options).
|
||||
* @param {Amount?} value - Only needs to be present for non-options.
|
||||
*/
|
||||
|
||||
MTX.prototype.addOutput = function addOutput(options, value) {
|
||||
var options, output;
|
||||
|
||||
if ((options instanceof bcoin.wallet)
|
||||
|| (options instanceof bcoin.keyring)) {
|
||||
options = options.getAddress();
|
||||
}
|
||||
|
||||
if (typeof options === 'string')
|
||||
options = bcoin.address.fromBase58(options);
|
||||
|
||||
if (options instanceof bcoin.address)
|
||||
options = Script.fromAddress(options);
|
||||
|
||||
output = new bcoin.output();
|
||||
output.mutable = true;
|
||||
|
||||
if (options instanceof Script) {
|
||||
assert(utils.isNumber(value));
|
||||
assert(value >= 0);
|
||||
output.script.fromOptions(options);
|
||||
output.value = value;
|
||||
} else {
|
||||
output.fromOptions(options);
|
||||
}
|
||||
|
||||
this.outputs.push(output);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Build input script (or witness) templates (with
|
||||
* OP_0 in place of signatures).
|
||||
@ -698,48 +731,6 @@ MTX.prototype.sign = function sign(index, addr, key, type) {
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Add an output.
|
||||
* @example
|
||||
* tx.addOutput({ address: ..., value: new bn(100000) });
|
||||
* tx.addOutput({ address: ..., value: utils.satoshi('0.1') });
|
||||
* tx.addOutput(receivingWallet, utils.satoshi('0.1'));
|
||||
* @param {Wallet|KeyRing|Object} obj - Wallet, Address,
|
||||
* or options (see {@link Script.createOutputScript} for options).
|
||||
* @param {Amount?} value - Only needs to be present for non-options.
|
||||
*/
|
||||
|
||||
MTX.prototype.addOutput = function addOutput(address, value) {
|
||||
var options, output;
|
||||
|
||||
if ((address instanceof bcoin.wallet)
|
||||
|| (address instanceof bcoin.keyring)) {
|
||||
address = address.getAddress();
|
||||
}
|
||||
|
||||
if (typeof address === 'string')
|
||||
address = bcoin.address.fromBase58(address);
|
||||
|
||||
if (address instanceof bcoin.address) {
|
||||
options = {
|
||||
address: address,
|
||||
value: value
|
||||
};
|
||||
} else {
|
||||
options = address;
|
||||
}
|
||||
|
||||
output = new bcoin.output(options);
|
||||
output.mutable = true;
|
||||
|
||||
if (options.address)
|
||||
output.script = Script.fromAddress(options.address);
|
||||
|
||||
this.outputs.push(output);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether the transaction at least
|
||||
* has all script templates built.
|
||||
|
||||
@ -32,6 +32,9 @@ function Output(options) {
|
||||
this.script = new bcoin.script();
|
||||
this.mutable = false;
|
||||
|
||||
this._type = null;
|
||||
this._address = null;
|
||||
|
||||
if (options)
|
||||
this.fromOptions(options);
|
||||
}
|
||||
@ -44,14 +47,19 @@ function Output(options) {
|
||||
|
||||
Output.prototype.fromOptions = function fromOptions(options) {
|
||||
assert(options, 'Output data is required.');
|
||||
assert(!options.value || utils.isNumber(options.value));
|
||||
|
||||
if (options.value)
|
||||
if (options.value) {
|
||||
assert(utils.isNumber(options.value));
|
||||
assert(options.value >= 0);
|
||||
this.value = options.value;
|
||||
}
|
||||
|
||||
if (options.script)
|
||||
this.script.fromOptions(options.script);
|
||||
|
||||
if (options.address)
|
||||
this.script = bcoin.script.fromAddress(options.address);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user