Skip to content

Commit

Permalink
Add change when serializing transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
eordano committed Jan 5, 2015
1 parent 4920932 commit e53b0c7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/address.js
Expand Up @@ -112,6 +112,8 @@ Address.prototype._classifyArguments = function(data, network, type) {
return Address._transformScript(data, network);
} else if (typeof(data) === 'string') {
return Address._transformString(data, network, type);
} else if (_.isObject(data) && data.network && data.hash && data.type) {
return Address._transformObject(data);
} else {
throw new TypeError('First argument is an unrecognized data format.');
}
Expand Down Expand Up @@ -139,6 +141,20 @@ Address._transformHash = function(hash){
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) {
data.hashBuffer = new Buffer(data.hash, 'hex');
data.network = Networks.get(data.network);
return data;
};

/**
* Internal function to discover the network and type based on the first data byte
*
Expand Down
7 changes: 7 additions & 0 deletions lib/transaction/transaction.js
Expand Up @@ -202,6 +202,9 @@ Transaction.prototype.fromJSON = function(json) {
outputs.forEach(function(output) {
self.outputs.push(Output.fromJSON(output));
});
if (json.change) {
this.change(json.change);
}
this.version = json.version;
this.nLockTime = json.nLockTime;
return this;
Expand All @@ -217,6 +220,7 @@ Transaction.prototype.toObject = function toObject() {
outputs.push(output.toObject());
});
return {
change: this._change.toObject(),
version: this.version,
inputs: inputs,
outputs: outputs,
Expand All @@ -232,6 +236,9 @@ Transaction.prototype.fromObject = function(transaction) {
_.each(transaction.outputs, function(output) {
self.addOutput(new Output(output));
});
if (transaction.change) {
this.change(transaction.change);
}
this.nLockTime = transaction.nLockTime;
this.version = transaction.version;
};
Expand Down
10 changes: 10 additions & 0 deletions test/transaction/transaction.js
Expand Up @@ -213,6 +213,16 @@ describe('Transaction', function() {
});
});

describe.only('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() {
it('fails if no change address was set', function() {
var transaction = new Transaction()
Expand Down

0 comments on commit e53b0c7

Please sign in to comment.