coin method for framer and parser.
This commit is contained in:
parent
5923726e78
commit
b473ca7d18
@ -578,7 +578,7 @@ BlockDB.prototype._getCoinsByAddress = function _getCoinsByAddress(address, call
|
||||
|
||||
if (data) {
|
||||
try {
|
||||
data = self.parser.parseTXOut(data);
|
||||
data = self.parser.parseOutput(data);
|
||||
} catch (e) {
|
||||
return callback(e);
|
||||
}
|
||||
@ -640,7 +640,7 @@ BlockDB.prototype.getCoin = function getCoin(hash, index, callback) {
|
||||
|
||||
if (data) {
|
||||
try {
|
||||
data = self.parser.parseTXOut(data);
|
||||
data = self.parser.parseOutput(data);
|
||||
} catch (e) {
|
||||
return callback(e);
|
||||
}
|
||||
|
||||
@ -170,30 +170,8 @@ Coin.fromFullJSON = function fromFullJSON(json) {
|
||||
});
|
||||
};
|
||||
|
||||
// This is basically BIP64 with some
|
||||
// extra fields tacked on the end.
|
||||
Coin.prototype.toRaw = function toRaw(enc, strict) {
|
||||
var script = bcoin.script.encode(this.script);
|
||||
var intSize = utils.sizeIntv(script.length);
|
||||
var height = this.height;
|
||||
var data = new Buffer(16 + intSize + script.length + (!strict ? 37 : 0));
|
||||
var off = 0;
|
||||
|
||||
if (height === -1)
|
||||
height = 0x7fffffff;
|
||||
|
||||
off += utils.writeU32(data, this.version, off);
|
||||
off += utils.writeU32(data, height, off);
|
||||
off += utils.write64(data, this.value, off);
|
||||
assert(this.value.byteLength() <= 8);
|
||||
off += utils.writeIntv(data, script.length, off);
|
||||
off += utils.copy(script, data, off);
|
||||
|
||||
if (!strict) {
|
||||
off += utils.copy(new Buffer(this.hash, 'hex'), data, off);
|
||||
off += utils.writeU32(data, this.index, off);
|
||||
off += utils.writeU8(data, this.spent ? 1 : 0, off);
|
||||
}
|
||||
Coin.prototype.toRaw = function toRaw(enc) {
|
||||
var data = bcoin.protocol.framer.coin(this, true);
|
||||
|
||||
if (enc === 'hex')
|
||||
data = utils.toHex(data);
|
||||
@ -201,61 +179,13 @@ Coin.prototype.toRaw = function toRaw(enc, strict) {
|
||||
return data;
|
||||
};
|
||||
|
||||
Coin.fromRaw = function fromRaw(data, enc, strict) {
|
||||
var off = 0;
|
||||
var version, height, value, script, hash, index, spent, scriptLen;
|
||||
|
||||
Coin.fromRaw = function fromRaw(data, enc) {
|
||||
if (enc === 'hex')
|
||||
data = new Buffer(data, 'hex');
|
||||
|
||||
if (data.length < 17 + (!strict ? 37 : 0))
|
||||
throw new Error('Invalid utxo size');
|
||||
data = parser.parseCoin(data, true);
|
||||
|
||||
version = utils.readU32(data, off);
|
||||
off += 4;
|
||||
|
||||
height = utils.readU32(data, off);
|
||||
if (height === 0x7fffffff)
|
||||
height = -1;
|
||||
off += 4;
|
||||
|
||||
value = utils.read64(data, off);
|
||||
off += 8;
|
||||
|
||||
scriptLen = utils.readIntv(data, off);
|
||||
off = scriptLen.off;
|
||||
scriptLen = scriptLen.r;
|
||||
|
||||
if (off + scriptLen > data.length - (!strict ? 37 : 0))
|
||||
throw new Error('Invalid utxo script length');
|
||||
|
||||
script = bcoin.script.decode(data.slice(off, off + scriptLen));
|
||||
off += scriptLen;
|
||||
|
||||
if (!strict) {
|
||||
hash = utils.toHex(data.slice(off, off + 32));
|
||||
off += 32;
|
||||
|
||||
index = utils.readU32(data, off);
|
||||
off += 4;
|
||||
|
||||
spent = utils.readU8(data, off) === 1;
|
||||
off += 1;
|
||||
} else {
|
||||
hash = constants.zeroHash.slice();
|
||||
index = 0xffffffff;
|
||||
spent = false;
|
||||
}
|
||||
|
||||
return new Coin({
|
||||
version: version,
|
||||
height: height,
|
||||
value: value,
|
||||
script: script,
|
||||
hash: hash,
|
||||
index: index,
|
||||
spent: spent
|
||||
});
|
||||
return new Coin(data);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -277,7 +277,7 @@ Framer.prototype._getBlocks = function _getBlocks(cmd, hashes, stop) {
|
||||
return this.packet(cmd, p);
|
||||
};
|
||||
|
||||
Framer.txIn = function txIn(input) {
|
||||
Framer.input = function _input(input) {
|
||||
var off = 0;
|
||||
var s, p;
|
||||
|
||||
@ -295,7 +295,7 @@ Framer.txIn = function txIn(input) {
|
||||
return p;
|
||||
};
|
||||
|
||||
Framer.txOut = function txOut(output) {
|
||||
Framer.output = function _output(output) {
|
||||
var off = 0;
|
||||
var s, p;
|
||||
|
||||
@ -311,7 +311,35 @@ Framer.txOut = function txOut(output) {
|
||||
return p;
|
||||
};
|
||||
|
||||
Framer.tx = function tx(tx) {
|
||||
Framer.coin = function _coin(coin, extended) {
|
||||
var script = bcoin.script.encode(coin.script);
|
||||
var intSize = utils.sizeIntv(script.length);
|
||||
var height = coin.height;
|
||||
var off = 0;
|
||||
var data;
|
||||
|
||||
data = new Buffer(16 + intSize + script.length + (extended ? 37 : 0));
|
||||
|
||||
if (height === -1)
|
||||
height = 0x7fffffff;
|
||||
|
||||
off += utils.writeU32(data, coin.version, off);
|
||||
off += utils.writeU32(data, height, off);
|
||||
off += utils.write64(data, coin.value, off);
|
||||
assert(coin.value.byteLength() <= 8);
|
||||
off += utils.writeIntv(data, script.length, off);
|
||||
off += utils.copy(script, data, off);
|
||||
|
||||
if (extended) {
|
||||
off += utils.copy(new Buffer(coin.hash, 'hex'), data, off);
|
||||
off += utils.writeU32(data, coin.index, off);
|
||||
off += utils.writeU8(data, coin.spent ? 1 : 0, off);
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
Framer.tx = function _tx(tx) {
|
||||
var inputs = [];
|
||||
var outputs = [];
|
||||
var inputSize = 0;
|
||||
@ -320,13 +348,13 @@ Framer.tx = function tx(tx) {
|
||||
var p, i, input, output;
|
||||
|
||||
for (i = 0; i < tx.inputs.length; i++) {
|
||||
input = Framer.txIn(tx.inputs[i]);
|
||||
input = Framer.input(tx.inputs[i]);
|
||||
inputs.push(input);
|
||||
inputSize += input.length;
|
||||
}
|
||||
|
||||
for (i = 0; i < tx.outputs.length; i++) {
|
||||
output = Framer.txOut(tx.outputs[i]);
|
||||
output = Framer.output(tx.outputs[i]);
|
||||
outputs.push(output);
|
||||
outputSize += output.length;
|
||||
}
|
||||
@ -363,7 +391,7 @@ Framer.block = function _block(block, type) {
|
||||
var off = 0;
|
||||
var txSize = 0;
|
||||
var txs = [];
|
||||
var i, tx;
|
||||
var i, tx, p;
|
||||
|
||||
if (!type)
|
||||
type = block.subtype;
|
||||
|
||||
@ -358,7 +358,7 @@ Parser.prototype.parseBlock = function parseBlock(p) {
|
||||
};
|
||||
};
|
||||
|
||||
Parser.prototype.parseTXIn = function parseTXIn(p) {
|
||||
Parser.prototype.parseInput = function parseInput(p) {
|
||||
var scriptLen, off;
|
||||
|
||||
if (p.length < 41)
|
||||
@ -382,7 +382,7 @@ Parser.prototype.parseTXIn = function parseTXIn(p) {
|
||||
};
|
||||
};
|
||||
|
||||
Parser.prototype.parseTXOut = function parseTXOut(p) {
|
||||
Parser.prototype.parseOutput = function parseOutput(p) {
|
||||
var scriptLen, off;
|
||||
|
||||
if (p.length < 9)
|
||||
@ -402,6 +402,60 @@ Parser.prototype.parseTXOut = function parseTXOut(p) {
|
||||
};
|
||||
};
|
||||
|
||||
Parser.prototype.parseCoin = function parseCoin(p, extended) {
|
||||
var off = 0;
|
||||
var version, height, value, script, hash, index, spent, scriptLen;
|
||||
|
||||
if (p.length < 17 + (extended ? 37 : 0))
|
||||
throw new Error('Invalid utxo size');
|
||||
|
||||
version = utils.readU32(p, off);
|
||||
off += 4;
|
||||
|
||||
height = utils.readU32(p, off);
|
||||
if (height === 0x7fffffff)
|
||||
height = -1;
|
||||
off += 4;
|
||||
|
||||
value = utils.read64(p, off);
|
||||
off += 8;
|
||||
|
||||
scriptLen = utils.readIntv(p, off);
|
||||
off = scriptLen.off;
|
||||
scriptLen = scriptLen.r;
|
||||
|
||||
if (off + scriptLen > p.length - (extended ? 37 : 0))
|
||||
throw new Error('Invalid utxo script length');
|
||||
|
||||
script = bcoin.script.decode(p.slice(off, off + scriptLen));
|
||||
off += scriptLen;
|
||||
|
||||
if (extended) {
|
||||
hash = p.slice(off, off + 32);
|
||||
off += 32;
|
||||
|
||||
index = utils.readU32(p, off);
|
||||
off += 4;
|
||||
|
||||
spent = utils.readU8(p, off) === 1;
|
||||
off += 1;
|
||||
} else {
|
||||
hash = constants.zeroHash;
|
||||
index = 0xffffffff;
|
||||
spent = false;
|
||||
}
|
||||
|
||||
return {
|
||||
version: version,
|
||||
height: height,
|
||||
value: value,
|
||||
script: script,
|
||||
hash: hash,
|
||||
index: index,
|
||||
spent: spent
|
||||
};
|
||||
};
|
||||
|
||||
Parser.prototype.parseTX = function parseTX(p) {
|
||||
var inCount, off, txIn, tx;
|
||||
var outCount, txOut;
|
||||
@ -422,7 +476,7 @@ Parser.prototype.parseTX = function parseTX(p) {
|
||||
|
||||
txIn = new Array(inCount);
|
||||
for (i = 0; i < inCount; i++) {
|
||||
tx = this.parseTXIn(p.slice(off));
|
||||
tx = this.parseInput(p.slice(off));
|
||||
|
||||
if (!tx)
|
||||
return;
|
||||
@ -445,7 +499,7 @@ Parser.prototype.parseTX = function parseTX(p) {
|
||||
|
||||
txOut = new Array(outCount);
|
||||
for (i = 0; i < outCount; i++) {
|
||||
tx = this.parseTXOut(p.slice(off));
|
||||
tx = this.parseOutput(p.slice(off));
|
||||
|
||||
if (!tx)
|
||||
return;
|
||||
|
||||
@ -2331,13 +2331,12 @@ script.format = function format(s) {
|
||||
}
|
||||
|
||||
return script.concat(scripts).map(function(chunk) {
|
||||
if (Buffer.isBuffer(chunk)) {
|
||||
if (chunk.length === 0)
|
||||
return 0 + '';
|
||||
if (Buffer.isBuffer(chunk))
|
||||
return '[' + utils.toHex(chunk) + ']';
|
||||
}
|
||||
|
||||
if (typeof chunk === 'number')
|
||||
return chunk + '';
|
||||
|
||||
return chunk;
|
||||
}).join(' ');
|
||||
};
|
||||
|
||||
@ -50,7 +50,6 @@ describe('TX', function() {
|
||||
var tx = bcoin.tx(parser.parseTX(new Buffer(raw, 'hex')));
|
||||
var p = bcoin.tx(parser.parseTX(new Buffer(inp, 'hex')));
|
||||
tx.input(p, 1);
|
||||
bcoin.utils.print(tx);
|
||||
|
||||
assert(tx.verify());
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user