Rename UTXO to Transaction.UnspentOutput

This commit is contained in:
Esteban Ordano 2014-12-31 01:29:36 -03:00
parent 1cf108ab47
commit 11975bc0df
6 changed files with 35 additions and 35 deletions

View File

@ -45,7 +45,6 @@ bitcore.Script = require('./lib/script');
bitcore.Transaction = require('./lib/transaction'); bitcore.Transaction = require('./lib/transaction');
bitcore.URI = require('./lib/uri'); bitcore.URI = require('./lib/uri');
bitcore.Unit = require('./lib/unit'); bitcore.Unit = require('./lib/unit');
bitcore.UTXO = require('./lib/utxo');
// dependencies, subject to change // dependencies, subject to change
bitcore.deps = {}; bitcore.deps = {};

View File

@ -6,7 +6,7 @@ var $ = require('../util/preconditions');
var _ = require('lodash'); var _ = require('lodash');
var Address = require('../address'); var Address = require('../address');
var Transaction = require('../transaction'); var Transaction = require('../transaction');
var UTXO = require('../utxo'); var UnspentOutput = Transaction.UnspentOutput;
var request = require('request'); var request = require('request');
@ -44,7 +44,7 @@ Insight.prototype.getUnspentUtxos = function(addresses, callback) {
if (err || res.statusCode !== 200) { if (err || res.statusCode !== 200) {
return callback(err || res); return callback(err || res);
} }
unspent = _.map(unspent, UTXO); unspent = _.map(unspent, UnspentOutput);
return callback(null, unspent); return callback(null, unspent);
}); });

View File

@ -2,3 +2,4 @@ module.exports = require('./transaction');
module.exports.Input = require('./input'); module.exports.Input = require('./input');
module.exports.Output = require('./output'); module.exports.Output = require('./output');
module.exports.UnspentOutput = require('./unspentoutput');

View File

@ -15,7 +15,7 @@ var Signature = require('../crypto/signature');
var Sighash = require('./sighash'); var Sighash = require('./sighash');
var Address = require('../address'); var Address = require('../address');
var UTXO = require('../utxo'); var UnspentOutput = require('./unspentoutput');
var Input = require('./input'); var Input = require('./input');
var PublicKeyHashInput = Input.PublicKeyHash; var PublicKeyHashInput = Input.PublicKeyHash;
var MultiSigScriptHashInput = Input.MultiSigScriptHash; var MultiSigScriptHashInput = Input.MultiSigScriptHash;
@ -309,7 +309,7 @@ Transaction.prototype.from = function(utxo, pubkeys, threshold) {
}; };
Transaction.prototype._fromNonP2SH = function(utxo) { Transaction.prototype._fromNonP2SH = function(utxo) {
utxo = new UTXO(utxo); utxo = new UnspentOutput(utxo);
this.inputs.push(new PublicKeyHashInput({ this.inputs.push(new PublicKeyHashInput({
output: new Output({ output: new Output({
script: utxo.script, script: utxo.script,
@ -324,7 +324,7 @@ Transaction.prototype._fromNonP2SH = function(utxo) {
}; };
Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold) { Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold) {
utxo = new UTXO(utxo); utxo = new UnspentOutput(utxo);
this.addInput(new MultiSigScriptHashInput({ this.addInput(new MultiSigScriptHashInput({
output: new Output({ output: new Output({
script: utxo.script, script: utxo.script,

View File

@ -1,18 +1,18 @@
'use strict'; 'use strict';
var _ = require('lodash'); var _ = require('lodash');
var $ = require('./util/preconditions'); var $ = require('../util/preconditions');
var JSUtil = require('./util/js'); var JSUtil = require('../util/js');
var Script = require('./script'); var Script = require('../script');
var Address = require('./address'); var Address = require('../address');
var Unit = require('./unit'); var Unit = require('../unit');
function UTXO(data) { function UnspentOutput(data) {
/* jshint maxcomplexity: 20 */ /* jshint maxcomplexity: 20 */
/* jshint maxstatements: 20 */ /* jshint maxstatements: 20 */
if (!(this instanceof UTXO)) { if (!(this instanceof UnspentOutput)) {
return new UTXO(data); return new UnspentOutput(data);
} }
$.checkArgument(_.isObject(data), 'Must provide an object from where to extract data'); $.checkArgument(_.isObject(data), 'Must provide an object from where to extract data');
var address = data.address ? new Address(data.address) : undefined; var address = data.address ? new Address(data.address) : undefined;
@ -39,27 +39,27 @@ function UTXO(data) {
}); });
} }
UTXO.prototype.inspect = function() { UnspentOutput.prototype.inspect = function() {
return '<UTXO: ' + this.txId + ':' + this.outputIndex + return '<UnspentOutput: ' + this.txId + ':' + this.outputIndex +
', satoshis: ' + this.satoshis + ', address: ' + this.address + '>'; ', satoshis: ' + this.satoshis + ', address: ' + this.address + '>';
}; };
UTXO.prototype.toString = function() { UnspentOutput.prototype.toString = function() {
return this.txId + ':' + this.outputIndex; return this.txId + ':' + this.outputIndex;
}; };
UTXO.fromJSON = UTXO.fromObject = function(data) { UnspentOutput.fromJSON = UnspentOutput.fromObject = function(data) {
if (JSUtil.isValidJSON(data)) { if (JSUtil.isValidJSON(data)) {
data = JSON.parse(data); data = JSON.parse(data);
} }
return new UTXO(data); return new UnspentOutput(data);
}; };
UTXO.prototype.toJSON = function() { UnspentOutput.prototype.toJSON = function() {
return JSON.stringify(this.toObject()); return JSON.stringify(this.toObject());
}; };
UTXO.prototype.toObject = function() { UnspentOutput.prototype.toObject = function() {
return { return {
address: this.address.toString(), address: this.address.toString(),
txid: this.txId, txid: this.txId,
@ -69,4 +69,4 @@ UTXO.prototype.toObject = function() {
}; };
}; };
module.exports = UTXO; module.exports = UnspentOutput;

View File

@ -6,9 +6,9 @@ var should = chai.should();
var expect = chai.expect; var expect = chai.expect;
var bitcore = require('..'); var bitcore = require('..');
var UTXO = bitcore.UTXO; var UnspentOutput = bitcore.Transaction.UnspentOutput;
describe('UTXO', function() { describe('UnspentOutput', function() {
var sampleData1 = { var sampleData1 = {
'address': 'mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1', 'address': 'mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1',
@ -26,16 +26,16 @@ describe('UTXO', function() {
}; };
it('roundtrip from raw data', function() { it('roundtrip from raw data', function() {
expect(UTXO(sampleData2).toObject()).to.deep.equal(sampleData2); expect(UnspentOutput(sampleData2).toObject()).to.deep.equal(sampleData2);
}); });
it('can be created without "new" operand', function() { it('can be created without "new" operand', function() {
expect(UTXO(sampleData1) instanceof UTXO).to.equal(true); expect(UnspentOutput(sampleData1) instanceof UnspentOutput).to.equal(true);
}); });
it('fails if no tx id is provided', function() { it('fails if no tx id is provided', function() {
expect(function() { expect(function() {
return new UTXO({}); return new UnspentOutput({});
}).to.throw(); }).to.throw();
}); });
@ -43,28 +43,28 @@ describe('UTXO', function() {
var sample = _.cloneDeep(sampleData2); var sample = _.cloneDeep(sampleData2);
sample.vout = '1'; sample.vout = '1';
expect(function() { expect(function() {
return new UTXO(sample); return new UnspentOutput(sample);
}).to.throw(); }).to.throw();
}); });
it('displays nicely on the console', function() { it('displays nicely on the console', function() {
var expected = '<UTXO: a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0' + var expected = '<UnspentOutput: a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0' +
', satoshis: 1020000, address: mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1>'; ', satoshis: 1020000, address: mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1>';
expect(new UTXO(sampleData1).inspect()).to.equal(expected); expect(new UnspentOutput(sampleData1).inspect()).to.equal(expected);
}); });
it('toString returns txid:vout', function() { it('toString returns txid:vout', function() {
var expected = 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0'; var expected = 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0';
expect(new UTXO(sampleData1).toString()).to.equal(expected); expect(new UnspentOutput(sampleData1).toString()).to.equal(expected);
}); });
it('to/from JSON roundtrip', function() { it('to/from JSON roundtrip', function() {
var utxo = new UTXO(sampleData2); var utxo = new UnspentOutput(sampleData2);
expect( expect(
JSON.parse( JSON.parse(
UTXO.fromJSON( UnspentOutput.fromJSON(
UTXO.fromObject( UnspentOutput.fromObject(
UTXO.fromJSON( UnspentOutput.fromJSON(
utxo.toJSON() utxo.toJSON()
).toObject() ).toObject()
).toJSON() ).toJSON()