Merge pull request #11 from jgarzik/parse-block

Block: add header parsing.  Begin new Deserialize module.
This commit is contained in:
Stephen Pair 2013-07-18 15:43:35 -07:00
commit 4407b08f40
2 changed files with 30 additions and 0 deletions

View File

@ -5,6 +5,7 @@ function spec(b) {
var Debug1 = b.Debug1 || function() {};
var Script = b.Script || require('./Script').class();
var Bignum = b.Bignum || require('bignum');
var Binary = b.Binary || require('binary');
var Put = b.Put || require('bufferput');
var Step = b.Step || require('step');
var Transaction = b.Transaction || require('./Transaction').class();
@ -47,6 +48,27 @@ function spec(b) {
return put.buffer();
};
Block.prototype.parseHeader = function parseHeader(buf) {
if (buf.length != 80)
throw new VerificationError('Block header length invalid');
var vars = Binary.parse(buf)
.word32lu('version')
.buffer('prev_hash', 32)
.buffer('merkle_root', 32)
.word32lu('timestamp')
.word32lu('bits')
.word32lu('nonce')
.vars;
this.version = vars.version;
this.prev_hash = vars.prev_hash;
this.merkle_root = vars.merkle_root;
this.timestamp = vars.timestamp;
this.bits = vars.bits;
this.nonce = vars.nonce;
};
Block.prototype.calcHash = function calcHash() {
var header = this.getHeader();

8
Deserialize.js Normal file
View File

@ -0,0 +1,8 @@
exports.intFromCompact = function(c)
{
var bytes = (c >> 24) & 0xff;
var v = (c & 0xffffff) << (8 * (bytes - 3));
return v;
}