Merge pull request #737 from braydonf/bug/to-object

Transaction:  `toJSON` / `fromJSON` tests
This commit is contained in:
Manuel Aráoz 2014-12-15 17:06:08 -03:00
commit 264a239e5a
5 changed files with 28 additions and 12 deletions

View File

@ -149,7 +149,7 @@ Block.fromRawBlock = function fromRawBlock(data) {
Block.prototype.toObject = function toObject() {
var txs = [];
this.txs.forEach(function(tx) {
txs.push(tx.toJSON());
txs.push(tx.toObject());
});
return {
magicnum: this.magicnum,

View File

@ -42,10 +42,10 @@ 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')
script: this._script.toString()
};
};
@ -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;
return this._satoshis.toNumber();
},
set: function(num) {
if (num instanceof BN) {
this._satoshis = num;
} else if (_.isNumber(num)) {
} else {
this._satoshis = BN().fromNumber(num);
}
}
@ -57,7 +54,7 @@ Output.prototype._fromObject = function(param) {
Output.prototype.toObject = function toObject() {
return {
satoshis: this.satoshis,
script: this._scriptBuffer.toString('hex')
script: this._script.toString()
};
};

View File

@ -148,11 +148,11 @@ Transaction.prototype.fromJSON = function(json) {
Transaction.prototype.toObject = function toObject() {
var inputs = [];
this.inputs.forEach(function(input) {
inputs.push(input.toJSON());
inputs.push(input.toObject());
});
var outputs = [];
this.outputs.forEach(function(output) {
outputs.push(output.toJSON());
outputs.push(output.toObject());
});
return {
version: this.version,

View File

@ -56,6 +56,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)
@ -82,6 +87,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:'71 0x3044022013fa3089327b50263029265572ae1b022a91d10ac80eb4f32f291c914533670b02200d8a5ed5f62634a7e1a0dc9188a3cc460a986267ae4d58faf50c79105431327501 33 0x0223078d2942df62c45621d209fab84ea9a7a23346201b7727b9b45a29c4e76f5e'}],
outputs:[{
satoshis:1010000,
script:'OP_DUP OP_HASH160 20 0x7821c0a3768aa9d1a37e16cf76002aef5373f1a8 OP_EQUALVERIFY OP_CHECKSIG'
}],
nLockTime:0
});
var utxo_1a_address = 'mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1';
var utxo_2a_address = 'mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc';