Merge pull request #26 from jgarzik/txtool2
Transaction: prefer to directly create buffers. Add buf64() helper.
This commit is contained in:
commit
04e147e490
@ -38,14 +38,11 @@ function spec(b) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TransactionIn.prototype.serialize = function serialize() {
|
TransactionIn.prototype.serialize = function serialize() {
|
||||||
var bytes = Put();
|
var slen = util.varIntBuf(this.s.length);
|
||||||
|
var qbuf = new Buffer(4);
|
||||||
|
qbuf.writeUInt32LE(this.q, 0);
|
||||||
|
|
||||||
bytes.put(this.o);
|
return Buffer.concat([this.o, slen, this.s, qbuf]);
|
||||||
bytes.varint(this.s.length);
|
|
||||||
bytes.put(this.s);
|
|
||||||
bytes.word32le(this.q);
|
|
||||||
|
|
||||||
return bytes.buffer();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
TransactionIn.prototype.getOutpointHash = function getOutpointIndex() {
|
TransactionIn.prototype.getOutpointHash = function getOutpointIndex() {
|
||||||
@ -88,13 +85,8 @@ function spec(b) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TransactionOut.prototype.serialize = function serialize() {
|
TransactionOut.prototype.serialize = function serialize() {
|
||||||
var bytes = Put();
|
var slen = util.varIntBuf(this.s.length);
|
||||||
|
return Buffer.concat([this.v, slen, this.s]);
|
||||||
bytes.put(this.v);
|
|
||||||
bytes.varint(this.s.length);
|
|
||||||
bytes.put(this.s);
|
|
||||||
|
|
||||||
return bytes.buffer();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function Transaction(data) {
|
function Transaction(data) {
|
||||||
@ -143,22 +135,27 @@ function spec(b) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Transaction.prototype.serialize = function serialize() {
|
Transaction.prototype.serialize = function serialize() {
|
||||||
var bytes = Put();
|
var bufs = [];
|
||||||
|
|
||||||
bytes.word32le(this.version);
|
var buf = new Buffer(4);
|
||||||
bytes.varint(this.ins.length);
|
buf.writeUInt32LE(this.version, 0);
|
||||||
|
bufs.push(buf);
|
||||||
|
|
||||||
|
bufs.push(util.varIntBuf(this.ins.length));
|
||||||
this.ins.forEach(function (txin) {
|
this.ins.forEach(function (txin) {
|
||||||
bytes.put(txin.serialize());
|
bufs.push(txin.serialize());
|
||||||
});
|
});
|
||||||
|
|
||||||
bytes.varint(this.outs.length);
|
bufs.push(util.varIntBuf(this.outs.length));
|
||||||
this.outs.forEach(function (txout) {
|
this.outs.forEach(function (txout) {
|
||||||
bytes.put(txout.serialize());
|
bufs.push(txout.serialize());
|
||||||
});
|
});
|
||||||
|
|
||||||
bytes.word32le(this.lock_time);
|
var buf = new Buffer(4);
|
||||||
|
buf.writeUInt32LE(this.lock_time, 0);
|
||||||
|
bufs.push(buf);
|
||||||
|
|
||||||
return this._buffer = bytes.buffer();
|
return this._buffer = Buffer.concat(bufs);
|
||||||
};
|
};
|
||||||
|
|
||||||
Transaction.prototype.getBuffer = function getBuffer() {
|
Transaction.prototype.getBuffer = function getBuffer() {
|
||||||
|
|||||||
@ -323,6 +323,15 @@ var varStrBuf = exports.varStrBuf = function varStrBuf(s) {
|
|||||||
return Buffer.concat(varIntBuf(s.length), s);
|
return Buffer.concat(varIntBuf(s.length), s);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var buf64 = exports.buf64 = function buf64(n) {
|
||||||
|
var lo = n & 0xffffffff;
|
||||||
|
var hi = (n >>> 32);
|
||||||
|
var buf = new Buffer(4 + 4);
|
||||||
|
buf.writeUInt32LE(lo, 0);
|
||||||
|
buf.writeUInt32LE(hi, 4);
|
||||||
|
return buf;
|
||||||
|
};
|
||||||
|
|
||||||
// Initializations
|
// Initializations
|
||||||
exports.NULL_HASH = new Buffer(32).fill(0);
|
exports.NULL_HASH = new Buffer(32).fill(0);
|
||||||
exports.EMPTY_BUFFER = new Buffer(0);
|
exports.EMPTY_BUFFER = new Buffer(0);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user