bitcore/lib/transaction.js

158 lines
4.1 KiB
JavaScript
Raw Normal View History

2014-11-20 07:56:35 -08:00
'use strict';
2014-11-21 07:54:56 -08:00
var BufferWriter = require('./encoding/bufferwriter');
var BufferReader = require('./encoding/bufferreader');
var Varint = require('./encoding/varint');
2014-11-20 13:08:31 -08:00
var Hash = require('./crypto/hash');
2014-09-16 14:35:26 -07:00
var Txin = require('./txin');
var Txout = require('./txout');
2014-09-17 16:48:23 -07:00
var Transaction = function Transaction(version, txinsvi, txins, txoutsvi, txouts, nlocktime) {
2014-09-16 14:35:26 -07:00
if (!(this instanceof Transaction))
2014-09-17 16:48:23 -07:00
return new Transaction(version, txinsvi, txins, txoutsvi, txouts, nlocktime);
2014-09-16 14:35:26 -07:00
if (typeof version === 'number') {
this.initialize();
2014-09-16 14:35:26 -07:00
this.set({
version: version,
2014-09-17 16:48:23 -07:00
txinsvi: txinsvi,
2014-09-16 14:35:26 -07:00
txins: txins,
2014-09-17 16:48:23 -07:00
txoutsvi: txoutsvi,
2014-09-16 14:35:26 -07:00
txouts: txouts,
nlocktime: nlocktime
});
} else if (Buffer.isBuffer(version)) {
//not necessary to initialize, since everything should be overwritten
var txbuf = version;
this.fromBuffer(txbuf);
2014-09-16 14:35:26 -07:00
} else if (version) {
this.initialize();
2014-09-16 14:35:26 -07:00
var obj = version;
this.set(obj);
} else {
this.initialize();
2014-09-16 14:35:26 -07:00
}
};
Transaction.prototype.initialize = function() {
this.version = 1;
this.txinsvi = Varint(0);
this.txins = [];
this.txoutsvi = Varint(0);
this.txouts = [];
this.nlocktime = 0xffffffff;
return this;
};
2014-09-16 14:35:26 -07:00
Transaction.prototype.set = function(obj) {
this.version = typeof obj.version !== 'undefined' ? obj.version : this.version;
2014-09-17 16:48:23 -07:00
this.txinsvi = obj.txinsvi || this.txinsvi;
2014-09-16 14:35:26 -07:00
this.txins = obj.txins || this.txins;
2014-09-17 16:48:23 -07:00
this.txoutsvi = obj.txoutsvi || this.txoutsvi;
2014-09-16 14:35:26 -07:00
this.txouts = obj.txouts || this.txouts;
this.nlocktime = typeof obj.nlocktime !== 'undefined' ? obj.nlocktime : this.nlocktime;
return this;
};
Transaction.prototype.fromJSON = function(json) {
var txins = [];
json.txins.forEach(function(txin) {
txins.push(Txin().fromJSON(txin));
});
var txouts = [];
json.txouts.forEach(function(txout) {
txouts.push(Txout().fromJSON(txout));
});
this.set({
version: json.version,
txinsvi: Varint().fromJSON(json.txinsvi),
txins: txins,
txoutsvi: Varint().fromJSON(json.txoutsvi),
txouts: txouts,
nlocktime: json.nlocktime
});
return this;
};
Transaction.prototype.toJSON = function() {
var txins = [];
this.txins.forEach(function(txin) {
txins.push(txin.toJSON());
});
var txouts = [];
this.txouts.forEach(function(txout) {
txouts.push(txout.toJSON());
});
return {
version: this.version,
txinsvi: this.txinsvi.toJSON(),
txins: txins,
txoutsvi: this.txoutsvi.toJSON(),
txouts: txouts,
nlocktime: this.nlocktime
};
};
2014-09-16 14:35:26 -07:00
Transaction.prototype.fromBuffer = function(buf) {
return this.fromBufferReader(BufferReader(buf));
};
Transaction.prototype.fromBufferReader = function(br) {
this.version = br.readUInt32LE();
2014-09-17 16:48:23 -07:00
this.txinsvi = Varint(br.readVarintBuf());
var txinsnum = this.txinsvi.toNumber();
2014-09-16 14:35:26 -07:00
this.txins = [];
for (var i = 0; i < txinsnum; i++) {
this.txins.push(Txin().fromBufferReader(br));
}
2014-09-17 16:48:23 -07:00
this.txoutsvi = Varint(br.readVarintBuf());
var txoutsnum = this.txoutsvi.toNumber();
2014-09-16 14:35:26 -07:00
this.txouts = [];
for (var i = 0; i < txoutsnum; i++) {
this.txouts.push(Txout().fromBufferReader(br));
}
this.nlocktime = br.readUInt32LE();
return this;
};
Transaction.prototype.toBuffer = function() {
return this.toBufferWriter().concat();
};
Transaction.prototype.toBufferWriter = function(bw) {
if (!bw)
bw = new BufferWriter();
bw.writeUInt32LE(this.version);
2014-09-17 16:48:23 -07:00
bw.write(this.txinsvi.buf);
2014-09-16 14:35:26 -07:00
for (var i = 0; i < this.txins.length; i++) {
this.txins[i].toBufferWriter(bw);
}
2014-09-17 16:48:23 -07:00
bw.write(this.txoutsvi.buf)
2014-09-16 14:35:26 -07:00
for (var i = 0; i < this.txouts.length; i++) {
this.txouts[i].toBufferWriter(bw);
}
bw.writeUInt32LE(this.nlocktime);
return bw;
};
Transaction.prototype.hash = function() {
return Hash.sha256sha256(this.toBuffer());
};
Transaction.prototype.id = function() {
return BufferReader(this.hash()).reverse().read();
};
Transaction.prototype.pushin = function(txin) {
this.txins.push(txin);
this.txinsvi = Varint(this.txinsvi.toNumber() + 1);
return this;
};
Transaction.prototype.pushout = function(txout) {
this.txouts.push(txout);
this.txoutsvi = Varint(this.txoutsvi.toNumber() + 1);
return this;
};
2014-09-16 14:35:26 -07:00
module.exports = Transaction;