From 66c881bfb9286d1816e683c15afb36a958bb99dd Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 1 Mar 2016 15:12:56 -0800 Subject: [PATCH] tx extended. --- lib/bcoin/tx.js | 90 +++++++++++++++++++++++++++++++++++++++++++ lib/bcoin/walletdb.js | 18 ++++----- 2 files changed, 99 insertions(+), 9 deletions(-) diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index cce7faae..ad1e9061 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1009,6 +1009,7 @@ TX.prototype.toCompact = function toCompact(coins) { block: this.block, height: this.height, ts: this.ts, + ps: this.ps, coins: coins ? this.inputs.map(function(input) { return input.output ? input.output.toRaw('hex') : null; }) : null, @@ -1025,6 +1026,7 @@ TX._fromCompact = function _fromCompact(json) { data.height = json.height; data.block = json.block; data.ts = json.ts; + data.ps = json.ps; if (json.coins) { json.coins.forEach(function(output, i) { @@ -1051,6 +1053,7 @@ TX.prototype.toJSON = function toJSON() { height: this.height, block: this.block ? utils.revHex(this.block) : null, ts: this.ts, + ps: this.ps, index: this.index, version: this.version, inputs: this.inputs.map(function(input) { @@ -1069,6 +1072,7 @@ TX._fromJSON = function fromJSON(json) { block: json.block ? utils.revHex(json.block) : null, height: json.height, ts: json.ts, + ps: json.ps, index: json.index, version: json.version, inputs: json.inputs.map(function(input) { @@ -1106,6 +1110,92 @@ TX.fromRaw = function 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 */ diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 3bce0657..674f6cff 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -152,7 +152,7 @@ WalletDB.prototype.saveJSON = function saveJSON(id, json, callback) { if (json && self.type === 'leveldb') { batch = self.db.batch(); 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) { if (err) @@ -190,7 +190,7 @@ WalletDB.prototype.removeJSON = function removeJSON(id, callback) { if (json && self.type === 'leveldb') { batch = self.db.batch(); 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) { if (err) @@ -256,7 +256,7 @@ WalletDB.prototype._getDB = function _getDB(id, callback) { callback = utils.ensure(callback); - key = 'w/' + id; + key = 'w/w/' + id; this.db.get(key, function(err, json) { if (err && err.type === 'NotFoundError') @@ -276,7 +276,7 @@ WalletDB.prototype._getDB = function _getDB(id, callback) { }; WalletDB.prototype._saveDB = function _saveDB(id, json, callback) { - var key = 'w/' + id; + var key = 'w/w/' + id; var data; callback = utils.ensure(callback); @@ -314,7 +314,7 @@ WalletDB.prototype._saveFile = function _saveFile(id, json, callback) { WalletDB.prototype._removeDB = function _removeDB(id, callback) { var self = this; - var key = 'w/' + id; + var key = 'w/w/' + id; callback = utils.ensure(callback); @@ -426,14 +426,14 @@ WalletDB.prototype.saveAddress = function saveAddress(id, address, callback) { callback = utils.ensure(callback); if (this.type !== 'leveldb') 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) { callback = utils.ensure(callback); if (this.type !== 'leveldb') 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) { @@ -441,8 +441,8 @@ WalletDB.prototype._getIDs = function _getIDs(address, callback) { var ids = []; var iter = this.db.db.iterator({ - gte: 'a/' + address, - lte: 'a/' + address + '~', + gte: 'w/a/' + address, + lte: 'w/a/' + address + '~', keys: true, values: false, fillCache: false,