tx extended.

This commit is contained in:
Christopher Jeffrey 2016-03-01 15:12:56 -08:00
parent 45292df44d
commit 66c881bfb9
2 changed files with 99 additions and 9 deletions

View File

@ -1009,6 +1009,7 @@ TX.prototype.toCompact = function toCompact(coins) {
block: this.block, block: this.block,
height: this.height, height: this.height,
ts: this.ts, ts: this.ts,
ps: this.ps,
coins: coins ? this.inputs.map(function(input) { coins: coins ? this.inputs.map(function(input) {
return input.output ? input.output.toRaw('hex') : null; return input.output ? input.output.toRaw('hex') : null;
}) : null, }) : null,
@ -1025,6 +1026,7 @@ TX._fromCompact = function _fromCompact(json) {
data.height = json.height; data.height = json.height;
data.block = json.block; data.block = json.block;
data.ts = json.ts; data.ts = json.ts;
data.ps = json.ps;
if (json.coins) { if (json.coins) {
json.coins.forEach(function(output, i) { json.coins.forEach(function(output, i) {
@ -1051,6 +1053,7 @@ TX.prototype.toJSON = function toJSON() {
height: this.height, height: this.height,
block: this.block ? utils.revHex(this.block) : null, block: this.block ? utils.revHex(this.block) : null,
ts: this.ts, ts: this.ts,
ps: this.ps,
index: this.index, index: this.index,
version: this.version, version: this.version,
inputs: this.inputs.map(function(input) { inputs: this.inputs.map(function(input) {
@ -1069,6 +1072,7 @@ TX._fromJSON = function fromJSON(json) {
block: json.block ? utils.revHex(json.block) : null, block: json.block ? utils.revHex(json.block) : null,
height: json.height, height: json.height,
ts: json.ts, ts: json.ts,
ps: json.ps,
index: json.index, index: json.index,
version: json.version, version: json.version,
inputs: json.inputs.map(function(input) { inputs: json.inputs.map(function(input) {
@ -1106,6 +1110,92 @@ TX.fromRaw = function fromRaw(data, enc) {
return new bcoin.tx(TX._fromRaw(data, enc)); return new bcoin.tx(TX._fromRaw(data, enc));
}; };
TX.prototype.toExtended = function toExtended(coins) {
var tx = this.render();
var buf = new Buffer(tx.length + 4 + 32 + 4 + 4 + 4);
var block = this.block ? new Buffer(this.block, 'hex') : constants.zeroHash;
var height = this.height;
var off = 0;
if (height === -1)
height = 0x7fffffff;
off += utils.copy(tx, buf, off);
off += utils.writeU32(buf, height, off);
off += utils.copy(block, buf, off);
off += utils.writeU32(buf, this.index, off);
off += utils.writeU32(buf, this.ts, off);
off += utils.writeU32(buf, this.ps, off);
if (coins) {
off += utils.writeIntv(buf, this.inputs.length, off);
this.inputs.forEach(function(input) {
var coin;
if (!input.output) {
off += utils.writeIntv(buf, 0, off);
return;
}
coin = bcoin.protocol.framer.coin(input.output, true);
off += utils.writeIntv(buf, coin.length, off);
off += utils.copy(coin, buf, off);
});
}
buf._witnessSize = tx._witnessSize;
buf._size = tx._size;
return buf;
};
TX._fromExtended = function _fromExtended(buf) {
var tx, coinCount, chunkSize, coin, i;
var off = 0;
tx = bcoin.protocol.parser.parseTX(buf);
off = tx._size;
if (buf.length === tx._size)
throw new Error('Not extended.');
tx.height = utils.readU32(buf, off);
off += 4;
tx.block = buf.slice(off, off + 32);
off += 32;
tx.index = utils.readU32(buf, off);
off += 4;
tx.ts = utils.readU32(buf, off);
off += 4;
tx.ps = utils.readU32(buf, off);
off += 4;
if (tx.height === 0x7fffffff)
tx.height = -1;
if (buf.length > off) {
coinCount = utils.readIntv(buf, off);
off = cointCount.off;
coinCount = coinCount.r;
for (i = 0; i < coinCount; i++) {
chunkSize = utils.readIntv(buf, off);
off = chunkSize.off;
chunkSize = chunkSize.r;
coin = buf.slice(off, off + chunkSize);
off += chunkSize;
if (coin.length === 0)
continue;
tx.inputs[i].output = bcoin.protocol.parser.parseCoin(coin, true);
}
}
return tx;
};
TX.fromExtended = function fromExtended(buf) {
return new TX(TX._fromExtended(buf));
};
/** /**
* Expose * Expose
*/ */

View File

@ -152,7 +152,7 @@ WalletDB.prototype.saveJSON = function saveJSON(id, json, callback) {
if (json && self.type === 'leveldb') { if (json && self.type === 'leveldb') {
batch = self.db.batch(); batch = self.db.batch();
Object.keys(json.addressMap).forEach(function(address) { Object.keys(json.addressMap).forEach(function(address) {
batch.put('a/' + address + '/' + json.id, ''); batch.put('w/a/' + address + '/' + json.id, '');
}); });
return batch.write(function(err) { return batch.write(function(err) {
if (err) if (err)
@ -190,7 +190,7 @@ WalletDB.prototype.removeJSON = function removeJSON(id, callback) {
if (json && self.type === 'leveldb') { if (json && self.type === 'leveldb') {
batch = self.db.batch(); batch = self.db.batch();
Object.keys(json.addressMap).forEach(function(address) { Object.keys(json.addressMap).forEach(function(address) {
batch.del('a/' + address + '/' + json.id); batch.del('w/a/' + address + '/' + json.id);
}); });
return batch.write(function(err) { return batch.write(function(err) {
if (err) if (err)
@ -256,7 +256,7 @@ WalletDB.prototype._getDB = function _getDB(id, callback) {
callback = utils.ensure(callback); callback = utils.ensure(callback);
key = 'w/' + id; key = 'w/w/' + id;
this.db.get(key, function(err, json) { this.db.get(key, function(err, json) {
if (err && err.type === 'NotFoundError') if (err && err.type === 'NotFoundError')
@ -276,7 +276,7 @@ WalletDB.prototype._getDB = function _getDB(id, callback) {
}; };
WalletDB.prototype._saveDB = function _saveDB(id, json, callback) { WalletDB.prototype._saveDB = function _saveDB(id, json, callback) {
var key = 'w/' + id; var key = 'w/w/' + id;
var data; var data;
callback = utils.ensure(callback); callback = utils.ensure(callback);
@ -314,7 +314,7 @@ WalletDB.prototype._saveFile = function _saveFile(id, json, callback) {
WalletDB.prototype._removeDB = function _removeDB(id, callback) { WalletDB.prototype._removeDB = function _removeDB(id, callback) {
var self = this; var self = this;
var key = 'w/' + id; var key = 'w/w/' + id;
callback = utils.ensure(callback); callback = utils.ensure(callback);
@ -426,14 +426,14 @@ WalletDB.prototype.saveAddress = function saveAddress(id, address, callback) {
callback = utils.ensure(callback); callback = utils.ensure(callback);
if (this.type !== 'leveldb') if (this.type !== 'leveldb')
return utils.nextTick(callback); return utils.nextTick(callback);
this.db.put('a/' + address + '/' + id, '', callback); this.db.put('w/a/' + address + '/' + id, '', callback);
}; };
WalletDB.prototype.removeAddress = function removeAddress(id, address, callback) { WalletDB.prototype.removeAddress = function removeAddress(id, address, callback) {
callback = utils.ensure(callback); callback = utils.ensure(callback);
if (this.type !== 'leveldb') if (this.type !== 'leveldb')
return utils.nextTick(callback); return utils.nextTick(callback);
this.db.del('a/' + address + '/' + id, callback); this.db.del('w/a/' + address + '/' + id, callback);
}; };
WalletDB.prototype._getIDs = function _getIDs(address, callback) { WalletDB.prototype._getIDs = function _getIDs(address, callback) {
@ -441,8 +441,8 @@ WalletDB.prototype._getIDs = function _getIDs(address, callback) {
var ids = []; var ids = [];
var iter = this.db.db.iterator({ var iter = this.db.db.iterator({
gte: 'a/' + address, gte: 'w/a/' + address,
lte: 'a/' + address + '~', lte: 'w/a/' + address + '~',
keys: true, keys: true,
values: false, values: false,
fillCache: false, fillCache: false,