Block, Transaction: de-serialize via BinaryParser
This commit is contained in:
parent
fdb74c5684
commit
8e1200c728
36
Block.js
36
Block.js
@ -48,25 +48,27 @@ function spec(b) {
|
|||||||
return buf;
|
return buf;
|
||||||
};
|
};
|
||||||
|
|
||||||
Block.prototype.parseHeader = function parseHeader(buf) {
|
Block.prototype.parse = function parse(parser, headerOnly) {
|
||||||
if (buf.length != 80)
|
this.version = parser.word32le();
|
||||||
throw new VerificationError('Block header length invalid');
|
this.prev_hash = parser.buffer(32);
|
||||||
|
this.merkle_root = parser.buffer(32);
|
||||||
|
this.timestamp = parser.word32le();
|
||||||
|
this.bits = parser.word32le();
|
||||||
|
this.nonce = parser.word32le();
|
||||||
|
|
||||||
var vars = Binary.parse(buf)
|
this.txs = [];
|
||||||
.word32lu('version')
|
this.size = 0;
|
||||||
.buffer('prev_hash', 32)
|
|
||||||
.buffer('merkle_root', 32)
|
|
||||||
.word32lu('timestamp')
|
|
||||||
.word32lu('bits')
|
|
||||||
.word32lu('nonce')
|
|
||||||
.vars;
|
|
||||||
|
|
||||||
this.version = vars.version;
|
if (headerOnly)
|
||||||
this.prev_hash = vars.prev_hash;
|
return;
|
||||||
this.merkle_root = vars.merkle_root;
|
|
||||||
this.timestamp = vars.timestamp;
|
var txCount = parser.varInt();
|
||||||
this.bits = vars.bits;
|
|
||||||
this.nonce = vars.nonce;
|
for (i = 0; i < txCount; i++) {
|
||||||
|
var tx = new Transaction();
|
||||||
|
tx.parse(parser);
|
||||||
|
this.txs.push(tx);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Block.prototype.calcHash = function calcHash() {
|
Block.prototype.calcHash = function calcHash() {
|
||||||
|
|||||||
@ -575,6 +575,40 @@ function spec(b) {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Transaction.prototype.parse = function (parser) {
|
||||||
|
if (Buffer.isBuffer(parser)) {
|
||||||
|
parser = new Parser(parser);
|
||||||
|
}
|
||||||
|
|
||||||
|
var i, sLen, startPos = parser.pos;
|
||||||
|
|
||||||
|
this.version = parser.word32le();
|
||||||
|
|
||||||
|
var txinCount = parser.varInt();
|
||||||
|
|
||||||
|
this.ins = [];
|
||||||
|
for (j = 0; j < txinCount; j++) {
|
||||||
|
var txin = new TransactionIn();
|
||||||
|
txin.o = parser.buffer(36); // outpoint
|
||||||
|
sLen = parser.varInt(); // script_len
|
||||||
|
txin.s = parser.buffer(sLen); // script
|
||||||
|
txin.q = parser.word32le(); // sequence
|
||||||
|
this.ins.push(txin);
|
||||||
|
}
|
||||||
|
|
||||||
|
var txoutCount = parser.varInt();
|
||||||
|
|
||||||
|
this.outs = [];
|
||||||
|
for (j = 0; j < txoutCount; j++) {
|
||||||
|
var txout = new TransactionOut();
|
||||||
|
txout.v = parser.buffer(8); // value
|
||||||
|
sLen = parser.varInt(); // script_len
|
||||||
|
txout.s = parser.buffer(sLen); // script
|
||||||
|
this.outs.push(txout);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.lock_time = parser.word32le();
|
||||||
|
};
|
||||||
|
|
||||||
var TransactionInputsCache = exports.TransactionInputsCache =
|
var TransactionInputsCache = exports.TransactionInputsCache =
|
||||||
function TransactionInputsCache(tx)
|
function TransactionInputsCache(tx)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user