Merge pull request #737 from braydonf/bug/to-object
Transaction: `toJSON` / `fromJSON` tests
This commit is contained in:
commit
264a239e5a
|
@ -149,7 +149,7 @@ Block.fromRawBlock = function fromRawBlock(data) {
|
||||||
Block.prototype.toObject = function toObject() {
|
Block.prototype.toObject = function toObject() {
|
||||||
var txs = [];
|
var txs = [];
|
||||||
this.txs.forEach(function(tx) {
|
this.txs.forEach(function(tx) {
|
||||||
txs.push(tx.toJSON());
|
txs.push(tx.toObject());
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
magicnum: this.magicnum,
|
magicnum: this.magicnum,
|
||||||
|
|
|
@ -42,10 +42,10 @@ Input.prototype._fromObject = function(params) {
|
||||||
|
|
||||||
Input.prototype.toObject = function toObject() {
|
Input.prototype.toObject = function toObject() {
|
||||||
return {
|
return {
|
||||||
prevTxId: this.prevTxId,
|
prevTxId: this.prevTxId.toString('hex'),
|
||||||
outputIndex: this.outputIndex,
|
outputIndex: this.outputIndex,
|
||||||
sequenceNumber: this.sequenceNumber,
|
sequenceNumber: this.sequenceNumber,
|
||||||
script: this._scriptBuffer.toString('hex')
|
script: this._script.toString()
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ Input.fromJSON = function(json) {
|
||||||
}
|
}
|
||||||
return new Input({
|
return new Input({
|
||||||
prevTxId: json.prevTxId || json.txidbuf,
|
prevTxId: json.prevTxId || json.txidbuf,
|
||||||
outputIndex: json.outputIndex || json.txoutnum,
|
outputIndex: _.isUndefined(json.outputIndex) ? json.txoutnum : json.outputIndex,
|
||||||
sequenceNumber: json.sequenceNumber || json.seqnum,
|
sequenceNumber: json.sequenceNumber || json.seqnum,
|
||||||
scriptBuffer: new Script(json.script, 'hex')
|
scriptBuffer: new Script(json.script, 'hex')
|
||||||
});
|
});
|
||||||
|
|
|
@ -32,15 +32,12 @@ Object.defineProperty(Output.prototype, 'satoshis', {
|
||||||
configurable: false,
|
configurable: false,
|
||||||
writeable: true,
|
writeable: true,
|
||||||
get: function() {
|
get: function() {
|
||||||
if (this._satoshis.lt(1e52)) {
|
|
||||||
return this._satoshis.toNumber();
|
return this._satoshis.toNumber();
|
||||||
}
|
|
||||||
return this._satoshis;
|
|
||||||
},
|
},
|
||||||
set: function(num) {
|
set: function(num) {
|
||||||
if (num instanceof BN) {
|
if (num instanceof BN) {
|
||||||
this._satoshis = num;
|
this._satoshis = num;
|
||||||
} else if (_.isNumber(num)) {
|
} else {
|
||||||
this._satoshis = BN().fromNumber(num);
|
this._satoshis = BN().fromNumber(num);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,7 +54,7 @@ Output.prototype._fromObject = function(param) {
|
||||||
Output.prototype.toObject = function toObject() {
|
Output.prototype.toObject = function toObject() {
|
||||||
return {
|
return {
|
||||||
satoshis: this.satoshis,
|
satoshis: this.satoshis,
|
||||||
script: this._scriptBuffer.toString('hex')
|
script: this._script.toString()
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -148,11 +148,11 @@ Transaction.prototype.fromJSON = function(json) {
|
||||||
Transaction.prototype.toObject = function toObject() {
|
Transaction.prototype.toObject = function toObject() {
|
||||||
var inputs = [];
|
var inputs = [];
|
||||||
this.inputs.forEach(function(input) {
|
this.inputs.forEach(function(input) {
|
||||||
inputs.push(input.toJSON());
|
inputs.push(input.toObject());
|
||||||
});
|
});
|
||||||
var outputs = [];
|
var outputs = [];
|
||||||
this.outputs.forEach(function(output) {
|
this.outputs.forEach(function(output) {
|
||||||
outputs.push(output.toJSON());
|
outputs.push(output.toObject());
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
version: this.version,
|
version: this.version,
|
||||||
|
|
|
@ -56,6 +56,11 @@ describe('Transaction', function() {
|
||||||
transaction.serialize().should.equal(tx_1_hex);
|
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() {
|
it('should create a sample transaction from an utxo', function() {
|
||||||
var transaction = new Transaction()
|
var transaction = new Transaction()
|
||||||
.from(utxo_1a)
|
.from(utxo_1a)
|
||||||
|
@ -82,6 +87,20 @@ var tx_1_hex = '01000000015884e5db9de218238671572340b207ee85b628074e7e467096c267
|
||||||
var tx_1_id = '779a3e5b3c2c452c85333d8521f804c1a52800e60f4b7c3bbe36f4bab350b72c';
|
var tx_1_id = '779a3e5b3c2c452c85333d8521f804c1a52800e60f4b7c3bbe36f4bab350b72c';
|
||||||
var tx_2_hex = '';
|
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_1a_address = 'mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1';
|
||||||
|
|
||||||
var utxo_2a_address = 'mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc';
|
var utxo_2a_address = 'mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc';
|
||||||
|
|
Loading…
Reference in New Issue