2014-12-30 20:21:59 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
var chai = require('chai');
|
|
|
|
var should = chai.should();
|
|
|
|
var expect = chai.expect;
|
|
|
|
|
2015-01-02 11:50:14 -08:00
|
|
|
var bitcore = require('../..');
|
2014-12-30 20:29:36 -08:00
|
|
|
var UnspentOutput = bitcore.Transaction.UnspentOutput;
|
2014-12-30 20:21:59 -08:00
|
|
|
|
2014-12-30 20:29:36 -08:00
|
|
|
describe('UnspentOutput', function() {
|
2014-12-30 20:21:59 -08:00
|
|
|
|
|
|
|
var sampleData1 = {
|
|
|
|
'address': 'mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1',
|
|
|
|
'txId': 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458',
|
|
|
|
'outputIndex': 0,
|
|
|
|
'script': 'OP_DUP OP_HASH160 20 0x88d9931ea73d60eaf7e5671efc0552b912911f2a OP_EQUALVERIFY OP_CHECKSIG',
|
|
|
|
'satoshis': 1020000
|
|
|
|
};
|
|
|
|
var sampleData2 = {
|
|
|
|
'txid': 'e42447187db5a29d6db161661e4bc66d61c3e499690fe5ea47f87b79ca573986',
|
|
|
|
'vout': 1,
|
|
|
|
'address': 'mgBCJAsvzgT2qNNeXsoECg2uPKrUsZ76up',
|
|
|
|
'scriptPubKey': '76a914073b7eae2823efa349e3b9155b8a735526463a0f88ac',
|
|
|
|
'amount': 0.01080000
|
|
|
|
};
|
|
|
|
|
|
|
|
it('roundtrip from raw data', function() {
|
2014-12-30 20:29:36 -08:00
|
|
|
expect(UnspentOutput(sampleData2).toObject()).to.deep.equal(sampleData2);
|
2014-12-30 20:21:59 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can be created without "new" operand', function() {
|
2014-12-30 20:29:36 -08:00
|
|
|
expect(UnspentOutput(sampleData1) instanceof UnspentOutput).to.equal(true);
|
2014-12-30 20:21:59 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('fails if no tx id is provided', function() {
|
|
|
|
expect(function() {
|
2014-12-30 20:29:36 -08:00
|
|
|
return new UnspentOutput({});
|
2014-12-30 20:21:59 -08:00
|
|
|
}).to.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails if vout is not a number', function() {
|
|
|
|
var sample = _.cloneDeep(sampleData2);
|
|
|
|
sample.vout = '1';
|
|
|
|
expect(function() {
|
2014-12-30 20:29:36 -08:00
|
|
|
return new UnspentOutput(sample);
|
2014-12-30 20:21:59 -08:00
|
|
|
}).to.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays nicely on the console', function() {
|
2014-12-30 20:29:36 -08:00
|
|
|
var expected = '<UnspentOutput: a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0' +
|
2014-12-30 20:21:59 -08:00
|
|
|
', satoshis: 1020000, address: mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1>';
|
2014-12-30 20:29:36 -08:00
|
|
|
expect(new UnspentOutput(sampleData1).inspect()).to.equal(expected);
|
2014-12-30 20:21:59 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('toString returns txid:vout', function() {
|
|
|
|
var expected = 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0';
|
2014-12-30 20:29:36 -08:00
|
|
|
expect(new UnspentOutput(sampleData1).toString()).to.equal(expected);
|
2014-12-30 20:21:59 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('to/from JSON roundtrip', function() {
|
2014-12-30 20:29:36 -08:00
|
|
|
var utxo = new UnspentOutput(sampleData2);
|
2014-12-30 20:21:59 -08:00
|
|
|
expect(
|
|
|
|
JSON.parse(
|
2014-12-30 20:29:36 -08:00
|
|
|
UnspentOutput.fromJSON(
|
|
|
|
UnspentOutput.fromObject(
|
|
|
|
UnspentOutput.fromJSON(
|
2014-12-30 20:21:59 -08:00
|
|
|
utxo.toJSON()
|
|
|
|
).toObject()
|
|
|
|
).toJSON()
|
|
|
|
).toJSON()
|
|
|
|
)
|
|
|
|
).to.deep.equal(sampleData2);
|
|
|
|
});
|
|
|
|
});
|