Add `change` when serializing transaction
This commit is contained in:
parent
4920932db8
commit
fe86abc093
|
@ -112,6 +112,8 @@ Address.prototype._classifyArguments = function(data, network, type) {
|
||||||
return Address._transformScript(data, network);
|
return Address._transformScript(data, network);
|
||||||
} else if (typeof(data) === 'string') {
|
} else if (typeof(data) === 'string') {
|
||||||
return Address._transformString(data, network, type);
|
return Address._transformString(data, network, type);
|
||||||
|
} else if (_.isObject(data)) {
|
||||||
|
return Address._transformObject(data);
|
||||||
} else {
|
} else {
|
||||||
throw new TypeError('First argument is an unrecognized data format.');
|
throw new TypeError('First argument is an unrecognized data format.');
|
||||||
}
|
}
|
||||||
|
@ -139,6 +141,23 @@ Address._transformHash = function(hash){
|
||||||
return info;
|
return info;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserializes an address serialized through `Address#toObject()`
|
||||||
|
* @param {Object} data
|
||||||
|
* @param {string} data.hash - the hash that this address encodes
|
||||||
|
* @param {string} data.type - either 'pubkeyhash' or 'scripthash'
|
||||||
|
* @param {Network=} data.network - the name of the network associated
|
||||||
|
* @return {Address}
|
||||||
|
*/
|
||||||
|
Address._transformObject = function(data) {
|
||||||
|
$.checkArgument(data.hash, 'Must provide a `hash` property');
|
||||||
|
$.checkArgument(data.type, 'Must provide a `type` property');
|
||||||
|
data.hashBuffer = new Buffer(data.hash, 'hex');
|
||||||
|
data.network = Networks.get(data.network) || Networks.defaultNetwork;
|
||||||
|
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal function to discover the network and type based on the first data byte
|
* Internal function to discover the network and type based on the first data byte
|
||||||
*
|
*
|
||||||
|
|
|
@ -202,6 +202,9 @@ Transaction.prototype.fromJSON = function(json) {
|
||||||
outputs.forEach(function(output) {
|
outputs.forEach(function(output) {
|
||||||
self.outputs.push(Output.fromJSON(output));
|
self.outputs.push(Output.fromJSON(output));
|
||||||
});
|
});
|
||||||
|
if (json.change) {
|
||||||
|
this.change(json.change);
|
||||||
|
}
|
||||||
this.version = json.version;
|
this.version = json.version;
|
||||||
this.nLockTime = json.nLockTime;
|
this.nLockTime = json.nLockTime;
|
||||||
return this;
|
return this;
|
||||||
|
@ -217,6 +220,7 @@ Transaction.prototype.toObject = function toObject() {
|
||||||
outputs.push(output.toObject());
|
outputs.push(output.toObject());
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
|
change: this._change ? this._change.toObject() : undefined,
|
||||||
version: this.version,
|
version: this.version,
|
||||||
inputs: inputs,
|
inputs: inputs,
|
||||||
outputs: outputs,
|
outputs: outputs,
|
||||||
|
@ -232,6 +236,9 @@ Transaction.prototype.fromObject = function(transaction) {
|
||||||
_.each(transaction.outputs, function(output) {
|
_.each(transaction.outputs, function(output) {
|
||||||
self.addOutput(new Output(output));
|
self.addOutput(new Output(output));
|
||||||
});
|
});
|
||||||
|
if (transaction.change) {
|
||||||
|
this.change(transaction.change);
|
||||||
|
}
|
||||||
this.nLockTime = transaction.nLockTime;
|
this.nLockTime = transaction.nLockTime;
|
||||||
this.version = transaction.version;
|
this.version = transaction.version;
|
||||||
};
|
};
|
||||||
|
|
|
@ -233,7 +233,7 @@ describe('Address', function() {
|
||||||
it('should error because of unrecognized data format', function() {
|
it('should error because of unrecognized data format', function() {
|
||||||
(function() {
|
(function() {
|
||||||
return new Address(new Error());
|
return new Address(new Error());
|
||||||
}).should.throw('First argument is an unrecognized data format.');
|
}).should.throw(bitcore.errors.InvalidArgument);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error because of incorrect format for pubkey hash', function() {
|
it('should error because of incorrect format for pubkey hash', function() {
|
||||||
|
|
|
@ -213,6 +213,16 @@ describe('Transaction', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('serialization', function() {
|
||||||
|
it('stores the change address correctly', function() {
|
||||||
|
var serialized = new Transaction()
|
||||||
|
.change(changeAddress)
|
||||||
|
.toObject();
|
||||||
|
var deserialized = new Transaction(serialized);
|
||||||
|
expect(deserialized._change.toString()).to.equal(changeAddress);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('checked serialize', function() {
|
describe('checked serialize', function() {
|
||||||
it('fails if no change address was set', function() {
|
it('fails if no change address was set', function() {
|
||||||
var transaction = new Transaction()
|
var transaction = new Transaction()
|
||||||
|
|
Loading…
Reference in New Issue