serialization formats.

This commit is contained in:
Christopher Jeffrey 2016-02-07 03:36:07 -08:00
parent 69925eb7b4
commit bad5250983
4 changed files with 46 additions and 13 deletions

View File

@ -692,6 +692,31 @@ Block.fromJSON = function fromJSON(json) {
return block;
};
Block.prototype.toRaw = function toRaw(enc) {
var data;
assert(this.subtype === 'block');
if (this.network && this._raw && this._raw.length > 80)
data = this._raw;
else
data = new Buffer(this.render());
if (enc === 'hex')
data = utils.toHex(data);
return data;
};
Block.fromRaw = function fromRaw(data, enc) {
var parser = new bcoin.protocol.parser();
if (enc === 'hex')
data = utils.toArray(data, 'hex');
return new Block(parser.parseBlock(data), 'block');
};
/**
* Expose
*/

View File

@ -139,9 +139,9 @@ Coin.prototype.toRaw = function toRaw(enc) {
height = 0xffffffff;
utils.copy(utils.toArray(this.hash, 'hex'), data, 0);
utils.writeU32BE(data, this.index, 32);
utils.writeU32BE(data, height, 36);
utils.write64BE(data, this.value, 40);
utils.writeU32(data, this.index, 32);
utils.writeU32(data, height, 36);
utils.write64(data, this.value, 40);
utils.copy(script, data, 48);
if (enc === 'hex')
@ -156,16 +156,16 @@ Coin.fromRaw = function fromRaw(data, enc) {
if (enc === 'hex')
data = utils.toArray(data, 'hex');
height = utils.readU32BE(data, 36);
height = utils.readU32(data, 36);
if (height === 0xffffffff)
height = -1;
return new Coin({
hash: utils.toHex(data.slice(0, 32)),
index: utils.readU32BE(data, 32),
index: utils.readU32(data, 32),
height: height,
value: utils.read64BE(data, 40),
value: utils.read64(data, 40),
script: bcoin.script.decode(utils.toArray(data.slice(48)))
});
};

View File

@ -281,7 +281,7 @@ Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) {
totalTX: utils.readU32(p, 80),
hashes: hashes,
flags: flags,
_raw: p.slice(0, 80),
_raw: p,
_size: p.length
};
};

View File

@ -1827,18 +1827,26 @@ TX.fromJSON = function fromJSON(json) {
};
TX.prototype.toRaw = function toRaw(enc) {
var raw = this.render();
var data;
if (this.network && this._raw)
data = this._raw;
else
data = new Buffer(this.render());
if (enc === 'hex')
return utils.toHex(raw);
data = utils.toHex(data);
return raw;
return data;
};
TX.fromRaw = function fromRaw(raw, enc) {
TX.fromRaw = function fromRaw(data, enc) {
var parser = new bcoin.protocol.parser();
if (enc === 'hex')
raw = utils.toArray(raw, 'hex');
return new bcoin.tx(new bcoin.protocol.parser().parseTX(raw));
data = utils.toArray(data, 'hex');
return new bcoin.tx(parser.parseTX(data));
};
/**