refactor json and cloning.
This commit is contained in:
parent
5b1ee4cccb
commit
3740281232
@ -278,7 +278,9 @@ Block.prototype.inspect = function inspect() {
|
||||
version: this.version,
|
||||
prevBlock: utils.revHex(this.prevBlock),
|
||||
merkleRoot: utils.revHex(this.merkleRoot),
|
||||
commitmentHash: this.commitmentHash ? utils.revHex(this.commitmentHash) : null,
|
||||
commitmentHash: this.commitmentHash
|
||||
? utils.revHex(this.commitmentHash)
|
||||
: null,
|
||||
ts: this.ts,
|
||||
bits: this.bits,
|
||||
nonce: this.nonce,
|
||||
@ -289,13 +291,12 @@ Block.prototype.inspect = function inspect() {
|
||||
|
||||
Block.prototype.toJSON = function toJSON() {
|
||||
return {
|
||||
type: this.type,
|
||||
type: 'block',
|
||||
height: this.height,
|
||||
hash: utils.revHex(this.hash('hex')),
|
||||
version: this.version,
|
||||
prevBlock: utils.revHex(this.prevBlock),
|
||||
merkleRoot: utils.revHex(this.merkleRoot),
|
||||
commitmentHash: this.commitmentHash ? utils.revHex(this.commitmentHash) : null,
|
||||
ts: this.ts,
|
||||
bits: this.bits,
|
||||
nonce: this.nonce,
|
||||
@ -307,6 +308,7 @@ Block.prototype.toJSON = function toJSON() {
|
||||
};
|
||||
|
||||
Block._fromJSON = function _fromJSON(json) {
|
||||
assert.equal(json.type, 'block');
|
||||
json.prevBlock = utils.revHex(json.prevBlock);
|
||||
json.merkleRoot = utils.revHex(json.merkleRoot);
|
||||
json.txs = json.txs.map(function(tx) {
|
||||
@ -321,7 +323,7 @@ Block.fromJSON = function fromJSON(json) {
|
||||
|
||||
Block.prototype.toCompact = function toCompact() {
|
||||
return {
|
||||
type: this.type,
|
||||
type: 'block',
|
||||
hash: this.hash('hex'),
|
||||
prevBlock: this.prevBlock,
|
||||
ts: this.ts,
|
||||
|
||||
@ -62,20 +62,7 @@ function MTX(options) {
|
||||
utils.inherits(MTX, bcoin.tx);
|
||||
|
||||
MTX.prototype.clone = function clone() {
|
||||
var tx = new MTX(this);
|
||||
|
||||
tx.inputs = tx.inputs.map(function(input) {
|
||||
input.script = input.script.slice();
|
||||
input.witness = input.witness.slice();
|
||||
return input;
|
||||
});
|
||||
|
||||
tx.outputs = tx.outputs.map(function(output) {
|
||||
output.script = output.script.slice();
|
||||
return output;
|
||||
});
|
||||
|
||||
return tx;
|
||||
return new MTX(this);
|
||||
};
|
||||
|
||||
MTX.prototype.hash = function hash(enc) {
|
||||
@ -152,6 +139,12 @@ MTX.prototype.addInput = function addInput(options, index) {
|
||||
|
||||
input = bcoin.input(options, this);
|
||||
|
||||
if (options.script)
|
||||
input.script = options.script.slice();
|
||||
|
||||
if (options.witness)
|
||||
input.witness = options.witness.slice();
|
||||
|
||||
this.inputs.push(input);
|
||||
|
||||
return this;
|
||||
@ -579,6 +572,9 @@ MTX.prototype.addOutput = function addOutput(obj, value) {
|
||||
|
||||
this.outputs.push(output);
|
||||
|
||||
if (options.script)
|
||||
output.script = options.script.slice();
|
||||
|
||||
this.scriptOutput(this.outputs.length - 1, options);
|
||||
|
||||
return this;
|
||||
@ -1078,7 +1074,7 @@ MTX.prototype.increaseFee = function increaseFee(unspent, address, fee) {
|
||||
|
||||
MTX.prototype.toCompact = function toCompact(coins) {
|
||||
return {
|
||||
type: 'tx',
|
||||
type: 'mtx',
|
||||
block: this.block,
|
||||
height: this.height,
|
||||
ts: this.ts,
|
||||
@ -1094,7 +1090,7 @@ MTX.prototype.toCompact = function toCompact(coins) {
|
||||
MTX._fromCompact = function _fromCompact(json) {
|
||||
var raw, data, tx;
|
||||
|
||||
assert.equal(json.type, 'tx');
|
||||
assert.equal(json.type, 'mtx');
|
||||
|
||||
raw = new Buffer(json.tx, 'hex');
|
||||
data = bcoin.protocol.parser.parseTX(raw);
|
||||
@ -1143,6 +1139,7 @@ MTX.prototype.toJSON = function toJSON() {
|
||||
};
|
||||
|
||||
MTX._fromJSON = function fromJSON(json) {
|
||||
assert.equal(json.type, 'mtx');
|
||||
return {
|
||||
block: json.block ? utils.revHex(json.block) : null,
|
||||
height: json.height,
|
||||
@ -1185,43 +1182,11 @@ MTX.fromRaw = function fromRaw(data, enc) {
|
||||
};
|
||||
|
||||
MTX.fromTX = function fromTX(tx) {
|
||||
var mtx = new bcoin.tx({
|
||||
ts: tx.ts,
|
||||
ps: tx.ps,
|
||||
block: tx.block,
|
||||
height: tx.height,
|
||||
version: tx.version,
|
||||
inputs: tx.inputs.map(function(input) {
|
||||
input.script = input.script.slice();
|
||||
return input;
|
||||
}),
|
||||
outputs: tx.outputs.map(function(output) {
|
||||
output.script = output.script.slice();
|
||||
return output;
|
||||
}),
|
||||
locktime: tx.locktime
|
||||
});
|
||||
return mtx;
|
||||
return new MTX(tx);
|
||||
};
|
||||
|
||||
MTX.prototype.toTX = function toTX() {
|
||||
var tx = new bcoin.tx({
|
||||
ts: this.ts,
|
||||
ps: this.ps,
|
||||
block: this.block,
|
||||
height: this.height,
|
||||
version: this.version,
|
||||
inputs: this.inputs.map(function(input) {
|
||||
input.script = input.script.slice();
|
||||
return input;
|
||||
}),
|
||||
outputs: this.outputs.map(function(output) {
|
||||
output.script = output.script.slice();
|
||||
return output;
|
||||
}),
|
||||
locktime: this.locktime
|
||||
});
|
||||
return tx;
|
||||
return new bcoin.tx(this);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -71,17 +71,6 @@ TX.prototype.setBlock = function setBlock(block, index) {
|
||||
this.index = index;
|
||||
};
|
||||
|
||||
TX.prototype.clone = function clone() {
|
||||
var tx = new TX(this);
|
||||
delete tx._raw;
|
||||
delete tx._size;
|
||||
delete tx._offset;
|
||||
delete tx._hash;
|
||||
delete tx._whash;
|
||||
delete tx._witnessSize;
|
||||
return tx;
|
||||
};
|
||||
|
||||
TX.prototype.hash = function hash(enc) {
|
||||
if (!this._hash)
|
||||
this._hash = utils.dsha256(this.renderNormal());
|
||||
@ -955,6 +944,7 @@ TX.prototype.inspect = function inspect() {
|
||||
date: new Date(this.ts * 1000).toISOString(),
|
||||
block: this.block ? utils.revHex(this.block) : null,
|
||||
ts: this.ts,
|
||||
index: this.index,
|
||||
version: this.version,
|
||||
inputs: this.inputs,
|
||||
outputs: this.outputs,
|
||||
@ -1004,12 +994,13 @@ TX.fromCompact = function fromCompact(json) {
|
||||
|
||||
TX.prototype.toJSON = function toJSON() {
|
||||
return {
|
||||
type: this.type,
|
||||
type: 'tx',
|
||||
hash: utils.revHex(this.hash('hex')),
|
||||
witnessHash: utils.revHex(this.witnessHash('hex')),
|
||||
height: this.height,
|
||||
block: this.block ? utils.revHex(this.block) : null,
|
||||
ts: this.ts,
|
||||
index: this.index,
|
||||
version: this.version,
|
||||
inputs: this.inputs.map(function(input) {
|
||||
return input.toJSON();
|
||||
@ -1022,10 +1013,12 @@ TX.prototype.toJSON = function toJSON() {
|
||||
};
|
||||
|
||||
TX._fromJSON = function fromJSON(json) {
|
||||
assert.equal(json.type, 'tx');
|
||||
return {
|
||||
block: json.block ? utils.revHex(json.block) : null,
|
||||
height: json.height,
|
||||
ts: json.ts,
|
||||
index: json.index,
|
||||
version: json.version,
|
||||
inputs: json.inputs.map(function(input) {
|
||||
return bcoin.input._fromJSON(input);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user