Added test to check that Transaction fromJSON could initialize from toJSON, and fixed a bug.

This commit is contained in:
Braydon Fuller 2014-12-13 14:10:03 -05:00
parent f43c2f9aa3
commit b544b0594d
3 changed files with 23 additions and 7 deletions

View File

@ -42,7 +42,7 @@ Input.prototype._fromObject = function(params) {
Input.prototype.toObject = function toObject() {
return {
prevTxId: this.prevTxId,
prevTxId: this.prevTxId.toString('hex'),
outputIndex: this.outputIndex,
sequenceNumber: this.sequenceNumber,
script: this._scriptBuffer.toString('hex')
@ -59,7 +59,7 @@ Input.fromJSON = function(json) {
}
return new Input({
prevTxId: json.prevTxId || json.txidbuf,
outputIndex: json.outputIndex || json.txoutnum,
outputIndex: _.isUndefined(json.outputIndex) ? json.txoutnum : json.outputIndex,
sequenceNumber: json.sequenceNumber || json.seqnum,
scriptBuffer: new Script(json.script, 'hex')
});

View File

@ -32,15 +32,12 @@ Object.defineProperty(Output.prototype, 'satoshis', {
configurable: false,
writeable: true,
get: function() {
if (this._satoshis.lt(1e52)) {
return this._satoshis.toNumber();
}
return this._satoshis;
},
set: function(num) {
if (num instanceof BN) {
this._satoshis = num;
} else if (_.isNumber(num)) {
} else {
this._satoshis = BN().fromNumber(num);
}
}

View File

@ -31,6 +31,11 @@ describe('Transaction', function() {
transaction.serialize().should.equal(tx_1_hex);
});
it('should input/output json', function() {
var transaction = JSON.parse(Transaction().fromJSON(tx_1_json).toJSON());
transaction.should.deep.equal(JSON.parse(tx_1_json));
});
it('should create a sample transaction from an utxo', function() {
var transaction = new Transaction()
.from(utxo_1a)
@ -57,6 +62,20 @@ var tx_1_hex = '01000000015884e5db9de218238671572340b207ee85b628074e7e467096c267
var tx_1_id = '779a3e5b3c2c452c85333d8521f804c1a52800e60f4b7c3bbe36f4bab350b72c';
var tx_2_hex = '';
var tx_1_json = JSON.stringify({
version:1,
inputs:[{
prevTxId:"a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458",
outputIndex:0,
sequenceNumber:4294967295,
script:"473044022013fa3089327b50263029265572ae1b022a91d10ac80eb4f32f291c914533670b02200d8a5ed5f62634a7e1a0dc9188a3cc460a986267ae4d58faf50c79105431327501210223078d2942df62c45621d209fab84ea9a7a23346201b7727b9b45a29c4e76f5e"}],
outputs:[{
satoshis:1010000,
script:"76a9147821c0a3768aa9d1a37e16cf76002aef5373f1a888ac"
}],
nLockTime:0
});
var utxo_1a_address = 'mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1';
var utxo_2a_address = 'mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc';