Added test to check that Transaction fromJSON could initialize from toJSON, and fixed a bug.
This commit is contained in:
parent
f43c2f9aa3
commit
b544b0594d
|
@ -42,7 +42,7 @@ 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._scriptBuffer.toString('hex')
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,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)
|
||||||
|
@ -57,6 +62,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:"473044022013fa3089327b50263029265572ae1b022a91d10ac80eb4f32f291c914533670b02200d8a5ed5f62634a7e1a0dc9188a3cc460a986267ae4d58faf50c79105431327501210223078d2942df62c45621d209fab84ea9a7a23346201b7727b9b45a29c4e76f5e"}],
|
||||||
|
outputs:[{
|
||||||
|
satoshis:1010000,
|
||||||
|
script:"76a9147821c0a3768aa9d1a37e16cf76002aef5373f1a888ac"
|
||||||
|
}],
|
||||||
|
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