Merge branch 'feature/walletIndex' of github.com:kleetus/bitcore-node into feature/walletIndex

This commit is contained in:
Chris Kleeschulte 2017-02-07 16:16:29 -05:00
commit 17011bdf69
8 changed files with 285 additions and 33 deletions

View File

@ -103,10 +103,9 @@ Encoding.prototype.encodeUtxoIndexValue = function(height, satoshis, scriptBuffe
};
Encoding.prototype.decodeUtxoIndexValue = function(buffer) {
var reader = new BufferReader(buffer);
var height = reader.readUInt32BE();
var satoshis = reader.readDoubleBE();
var scriptBuffer = reader.read(buffer.length - 12);
var height = buffer.readUInt32BE();
var satoshis = buffer.readDoubleBE(4);
var scriptBuffer = buffer.slice(12);
return {
height: height,
satoshis: satoshis,

View File

@ -19,7 +19,7 @@ Encoding.prototype.encodeBlockTimestampValue = function(timestamp) {
};
Encoding.prototype.decodeBlockTimestampValue = function(buffer) {
return buffer.readDoubleBE(0);
return buffer.readDoubleBE();
};
Encoding.prototype.encodeTimestampBlockKey = function(timestamp) {

View File

@ -53,12 +53,10 @@ Encoding.prototype.decodeWalletTransactionKey = function(buffer) {
var walletSize = reader.readUInt8();
var walletId = reader.read(walletSize).toString('utf8');
var height = reader.readUInt32BE();
var blockIndex = reader.readUInt32BE();
return {
walletId: walletId,
height: height,
blockIndex: blockIndex
height: height
};
};
@ -110,14 +108,14 @@ Encoding.prototype.encodeWalletUtxoValue = function(height, satoshis, scriptBuff
heightBuffer.writeUInt32BE(height);
var satoshisBuffer = new Buffer(8);
satoshisBuffer.writeDoubleBE(satoshis);
return Buffer.concat([height, satoshisBuffer, scriptBuffer]);
return Buffer.concat([heightBuffer, satoshisBuffer, scriptBuffer]);
};
Encoding.prototype.decodeWalletUtxoValue = function(buffer) {
var reader = new BufferReader(buffer);
var height = reader.readUInt32BE();
var satoshis = buffer.readDoubleBE(4);
var scriptBuffer = reader.read(12, buffer.length - 12);
var scriptBuffer = buffer.slice(12);
return {
height: height,
satoshis: satoshis,
@ -136,7 +134,7 @@ Encoding.prototype.encodeWalletUtxoSatoshisKey = function(walletId, satoshis, tx
buffers.push(walletIdBuffer);
var satoshisBuffer = new Buffer(8);
satoshisBuffer.writeUInt32BE(satoshis || 0);
satoshisBuffer.writeDoubleBE(satoshis || 0);
buffers.push(satoshisBuffer);
var txidBuffer = new Buffer(txid || new Array(33).join('0'), 'hex');
@ -150,14 +148,12 @@ Encoding.prototype.encodeWalletUtxoSatoshisKey = function(walletId, satoshis, tx
};
Encoding.prototype.decodeWalletUtxoSatoshisKey = function(buffer) {
var reader = new BufferReader(buffer);
reader.read(3);
var walletIdSize = reader.readUInt8();
var walletId = reader.read(walletIdSize).toString('utf8');
var walletIdSize = buffer.readUInt8(3);
var walletId = buffer.slice(4, walletIdSize + 4).toString('utf8');
var satoshis = buffer.readDoubleBE(walletIdSize + 4);
var txid = reader.read(walletIdSize + 12, 32).toString('hex');
var outputIndex = reader.readUInt32BE();
var txid = buffer.slice(walletIdSize + 12, walletIdSize + 44).toString('hex');
var outputIndex = buffer.readUInt32BE(walletIdSize + 44);
return {
walletId: walletId,
satoshis: satoshis,
@ -169,7 +165,7 @@ Encoding.prototype.decodeWalletUtxoSatoshisKey = function(buffer) {
Encoding.prototype.encodeWalletUtxoSatoshisValue = function(height, scriptBuffer) {
var heightBuffer = new Buffer(4);
heightBuffer.writeUInt32BE(height);
return Buffer.concat([height, scriptBuffer]);
return Buffer.concat([heightBuffer, scriptBuffer]);
};
Encoding.prototype.decodeWalletUtxoSatoshisValue = function(buffer) {

View File

@ -1,12 +1,10 @@
'use strict';
var should = require('chai').should();
var sinon = require('sinon');
var bitcore = require('bitcore-lib');
var Encoding = require('../../../lib/services/address/encoding');
describe.only('Address service encoding', function() {
describe('Address service encoding', function() {
var servicePrefix = new Buffer('0000', 'hex');
var encoding = new Encoding(servicePrefix);
@ -17,16 +15,27 @@ describe.only('Address service encoding', function() {
var prefix0 = new Buffer('00', 'hex');
var prefix1 = new Buffer('01', 'hex');
addressSizeBuf.writeUInt8(address.length);
var addressIndexKeyBuf = Buffer.concat([servicePrefix, prefix0, addressSizeBuf, new Buffer(address), new Buffer('00000001', 'hex'), new Buffer(txid, 'hex')]);
var addressIndexKeyBuf = Buffer.concat([
servicePrefix,
prefix0,
addressSizeBuf,
new Buffer(address),
new Buffer('00000001', 'hex'),
new Buffer(txid, 'hex')]);
var outputIndex = 5;
var utxoKeyBuf = Buffer.concat([servicePrefix, prefix1, addressSizeBuf, new Buffer(address), new Buffer(txid, 'hex'), new Buffer('00000005', 'hex')]);
var utxoKeyBuf = Buffer.concat([
servicePrefix,
prefix1,
addressSizeBuf,
new Buffer(address),
new Buffer(txid, 'hex'),
new Buffer('00000005', 'hex')]);
var txHex = '0100000001cc3ffe0638792c8b39328bb490caaefe2cf418f2ce0144956e0c22515f29724d010000006a473044022030ce9fa68d1a32abf0cd4adecf90fb998375b64fe887c6987278452b068ae74c022036a7d00d1c8af19e298e04f14294c807ebda51a20389ad751b4ff3c032cf8990012103acfcb348abb526526a9f63214639d79183871311c05b2eebc727adfdd016514fffffffff02f6ae7d04000000001976a9144455183e407ee4d3423858c8a3275918aedcd18e88aca99b9b08010000001976a9140beceae2c29bfde08d2b6d80b33067451c5887be88ac00000000';
var tx = new bitcore.Transaction(txHex);
var sats = tx.outputs[0].satoshis;
var satsBuf = new Buffer(8);
satsBuf.writeDoubleBE(sats);
var utxoValueBuf = Buffer.concat([new Buffer('00000001', 'hex'), satsBuf, tx.outputs[0]._scriptBuffer]);
var txEncoded = Buffer.concat([new Buffer('00000002', 'hex'), new Buffer('3ff0000000000000', 'hex'), new Buffer('0002', 'hex'), new Buffer('40000000000000004008000000000000', 'hex'), tx.toBuffer()]);
it('should encode address key' , function() {
encoding.encodeAddressIndexKey(address, height, txid).should.deep.equal(addressIndexKeyBuf);
@ -50,10 +59,17 @@ describe.only('Address service encoding', function() {
utxoKey.outputIndex.should.equal(outputIndex);
});
it('should encode utxo value', function() {
encoding.encodeUtxoIndexValue(height, tx.outputs[0].satoshis, tx.outputs[0]._scriptBuffer).should.deep.equal(utxoValueBuf);
encoding.encodeUtxoIndexValue(
height,
tx.outputs[0].satoshis,
tx.outputs[0]._scriptBuffer).should.deep.equal(utxoValueBuf);
});
it('should decode utxo value', function() {
it('should decode utxo value', function() {
var utxoValue = encoding.decodeUtxoIndexValue(utxoValueBuf);
utxoValue.height.should.equal(height);
utxoValue.satoshis.should.equal(sats);
utxoValue.script.should.deep.equal(tx.outputs[0]._scriptBuffer);
});
});

View File

@ -13,18 +13,18 @@ var proxyquire = require('proxyquire');
var fs = require('fs');
var sinon = require('sinon');
var index = require('../../lib');
var index = require('../../../lib');
var log = index.log;
var errors = index.errors;
var Transaction = bitcore.Transaction;
var readFileSync = sinon.stub().returns(fs.readFileSync(path.resolve(__dirname, '../data/bitcoin.conf')));
var BitcoinService = proxyquire('../../lib/services/bitcoind', {
var readFileSync = sinon.stub().returns(fs.readFileSync(path.resolve(__dirname, '../../data/bitcoin.conf')));
var BitcoinService = proxyquire('../../../lib/services/bitcoind', {
fs: {
readFileSync: readFileSync
}
});
var defaultBitcoinConf = fs.readFileSync(path.resolve(__dirname, '../data/default.bitcoin.conf'), 'utf8');
var defaultBitcoinConf = fs.readFileSync(path.resolve(__dirname, '../../data/default.bitcoin.conf'), 'utf8');
describe('Bitcoin Service', function() {
var txhex = '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000';

View File

@ -0,0 +1,48 @@
'use strict';
var Encoding = require('../../../lib/services/timestamp/encoding');
describe('Timestamp service encoding', function() {
var servicePrefix = new Buffer('0000', 'hex');
var encoding = new Encoding(servicePrefix);
var blockhash = '00000000000000000115b92b1ff4377441049bff75c6c48b626eb99e8b744297';
var timestamp = 5;
var timestampBuf = new Buffer(8);
timestampBuf.writeDoubleBE(timestamp);
it('should encode block timestamp key' , function() {
encoding.encodeBlockTimestampKey(blockhash).should.deep.equal(Buffer.concat([servicePrefix, new Buffer(blockhash, 'hex')]));
});
it('should decode block timestamp key', function() {
var blockTimestampKey = encoding.decodeBlockTimestampKey(Buffer.concat([servicePrefix, new Buffer(blockhash, 'hex')]));
blockTimestampKey.should.equal(blockhash);
});
it('should encode block timestamp value', function() {
encoding.encodeBlockTimestampValue(timestamp).should.deep.equal(timestampBuf);
});
it('should decode block timestamp value', function() {
encoding.decodeBlockTimestampValue(timestampBuf).should.equal(timestamp);
});
it('should encode timestamp block key', function() {
encoding.encodeTimestampBlockKey(timestamp).should.deep.equal(Buffer.concat([servicePrefix, timestampBuf]));
});
it('should decode timestamp block key', function() {
encoding.decodeTimestampBlockKey(Buffer.concat([servicePrefix, timestampBuf])).should.equal(timestamp);
});
it('should encode timestamp block value', function() {
encoding.encodeTimestampBlockValue(blockhash).should.deep.equal(new Buffer(blockhash, 'hex'));
});
it('should decode timestamp block value', function() {
encoding.decodeTimestampBlockValue(new Buffer(blockhash, 'hex')).should.equal(blockhash);
});
});

View File

@ -0,0 +1,193 @@
'use strict';
var bitcore = require('bitcore-lib');
var Encoding = require('../../../lib/services/wallet-api/encoding');
describe('Wallet-Api service encoding', function() {
var servicePrefix = new Buffer('0000', 'hex');
var encoding = new Encoding(servicePrefix);
var walletId = 'abcdef123456';
var txid = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add';
var address = '1EZBqbJSHFKSkVPNKzc5v26HA6nAHiTXq6';
var height = 1;
var addressSizeBuf = new Buffer(1);
addressSizeBuf.writeUInt8(address.length);
var outputIndex = 5;
var txHex = '0100000001cc3ffe0638792c8b39328bb490caaefe2cf418f2ce0144956e0c22515f29724d010000006a473044022030ce9fa68d1a32abf0cd4adecf90fb998375b64fe887c6987278452b068ae74c022036a7d00d1c8af19e298e04f14294c807ebda51a20389ad751b4ff3c032cf8990012103acfcb348abb526526a9f63214639d79183871311c05b2eebc727adfdd016514fffffffff02f6ae7d04000000001976a9144455183e407ee4d3423858c8a3275918aedcd18e88aca99b9b08010000001976a9140beceae2c29bfde08d2b6d80b33067451c5887be88ac00000000';
var tx = new bitcore.Transaction(txHex);
var sats = tx.outputs[0].satoshis;
var satsBuf = new Buffer(8);
satsBuf.writeDoubleBE(sats);
it('should encode wallet transaction key' , function() {
encoding.encodeWalletTransactionKey(walletId, height).should.deep.equal(Buffer.concat([
servicePrefix,
encoding.subKeyMap.transaction.buffer,
new Buffer('0c', 'hex'),
new Buffer(walletId),
new Buffer('00000001', 'hex')
]));
});
it('should decode wallet transaction key', function() {
var walletTransactionKey = encoding.decodeWalletTransactionKey(Buffer.concat([
servicePrefix,
encoding.subKeyMap.transaction.buffer,
new Buffer('0c', 'hex'),
new Buffer(walletId),
new Buffer('00000001', 'hex')
]));
walletTransactionKey.walletId.should.equal(walletId);
walletTransactionKey.height.should.equal(height);
});
it('should encode wallet transaction value', function() {
encoding.encodeWalletTransactionValue(txid).should.deep.equal(new Buffer(txid, 'hex'));
});
it('should decode wallet transaction value', function() {
encoding.decodeWalletTransactionValue(new Buffer(txid, 'hex')).should.equal(txid);
});
it('should encode wallet utxo key', function() {
encoding.encodeWalletUtxoKey(walletId, txid, outputIndex).should.deep.equal(Buffer.concat([
servicePrefix,
encoding.subKeyMap.utxo.buffer,
new Buffer('0c', 'hex'),
new Buffer(walletId),
new Buffer(txid, 'hex'),
new Buffer('00000005', 'hex')]));
});
it('should decode wallet utxo key', function() {
var walletUtxoKey = encoding.decodeWalletUtxoKey(Buffer.concat([
servicePrefix,
encoding.subKeyMap.utxo.buffer,
new Buffer('0c', 'hex'),
new Buffer(walletId),
new Buffer(txid, 'hex'),
new Buffer('00000005', 'hex')]));
walletUtxoKey.walletId.should.equal(walletId);
walletUtxoKey.txid.should.equal(txid);
walletUtxoKey.outputIndex.should.equal(outputIndex);
});
it('should encode wallet utxo value', function() {
encoding.encodeWalletUtxoValue(height, sats, tx.outputs[0]._scriptBuffer).should.deep.equal(Buffer.concat([
new Buffer('00000001', 'hex'),
satsBuf,
tx.outputs[0]._scriptBuffer]));
});
it('should decode wallet utxo value', function() {
var walletUtxoValue = encoding.decodeWalletUtxoValue(Buffer.concat([
new Buffer('00000001', 'hex'),
satsBuf,
tx.outputs[0]._scriptBuffer]));
walletUtxoValue.height.should.equal(height);
walletUtxoValue.satoshis.should.equal(sats);
walletUtxoValue.script.should.deep.equal(tx.outputs[0]._scriptBuffer);
});
it('should encode wallet utxo satoshis key', function() {
encoding.encodeWalletUtxoSatoshisKey(walletId, sats, txid, outputIndex).should.deep.equal(Buffer.concat([
servicePrefix,
encoding.subKeyMap.utxoSat.buffer,
new Buffer('0c', 'hex'),
new Buffer(walletId),
satsBuf,
new Buffer(txid, 'hex'),
new Buffer('00000005', 'hex')]));
});
it('should decode wallet utxo satoshis key', function() {
var walletUtxoSatoshisKey = encoding.decodeWalletUtxoSatoshisKey(Buffer.concat([
servicePrefix,
encoding.subKeyMap.utxoSat.buffer,
new Buffer('0c', 'hex'),
new Buffer(walletId),
satsBuf,
new Buffer(txid, 'hex'),
new Buffer('00000005', 'hex')]));
walletUtxoSatoshisKey.walletId.should.equal(walletId);
walletUtxoSatoshisKey.satoshis.should.equal(sats);
walletUtxoSatoshisKey.txid.should.equal(txid);
walletUtxoSatoshisKey.outputIndex.should.equal(outputIndex);
});
it('should encode wallet utxo satoshis value', function() {
encoding.encodeWalletUtxoSatoshisValue(height, tx.outputs[0]._scriptBuffer).should.deep.equal(Buffer.concat([
new Buffer('00000001', 'hex'),
tx.outputs[0]._scriptBuffer
]));
});
it('should decode wallet utxo satoshis value', function() {
var walletUtxoSatoshisValue = encoding.decodeWalletUtxoSatoshisValue(Buffer.concat([
new Buffer('00000001', 'hex'),
tx.outputs[0]._scriptBuffer
]));
walletUtxoSatoshisValue.height.should.equal(height);
walletUtxoSatoshisValue.script.should.deep.equal(tx.outputs[0]._scriptBuffer);
});
it('should encode wallet addresses key', function() {
encoding.encodeWalletAddressesKey(walletId).should.deep.equal(Buffer.concat([
servicePrefix,
encoding.subKeyMap.addresses.buffer,
new Buffer('0c', 'hex'),
new Buffer(walletId)
]));
});
it('should decode wallet addresses key', function() {
encoding.decodeWalletAddressesKey(Buffer.concat([
servicePrefix,
encoding.subKeyMap.addresses.buffer,
new Buffer('0c', 'hex'),
new Buffer(walletId)
])).should.equal(walletId);
});
it('should encode wallet addresses value', function() {
encoding.encodeWalletAddressesValue(['a']).should.deep.equal(Buffer.concat([
new Buffer('00000001', 'hex'),
new Buffer('01', 'hex'),
new Buffer('a')]));
});
it('should decode wallet addresses value', function() {
encoding.decodeWalletAddressesValue(Buffer.concat([
new Buffer('00000001', 'hex'),
new Buffer('01', 'hex'),
new Buffer('a')])).should.deep.equal(['a']);
});
it('should encode wallet balance key', function() {
encoding.encodeWalletBalanceKey(walletId).should.deep.equal(Buffer.concat([
servicePrefix,
encoding.subKeyMap.balance.buffer,
new Buffer('0c', 'hex'),
new Buffer(walletId)
]));
});
it('should decode wallet balance key', function() {
encoding.decodeWalletBalanceKey(Buffer.concat([
servicePrefix,
encoding.subKeyMap.balance.buffer,
new Buffer('0c', 'hex'),
new Buffer(walletId)
])).should.equal(walletId);
});
it('should encode wallet balance value', function() {
encoding.encodeWalletBalanceValue(sats).should.deep.equal(satsBuf);
});
it('should decode wallet balance value', function() {
encoding.decodeWalletBalanceValue(satsBuf).should.equal(sats);
});
});

View File

@ -5,7 +5,7 @@ var sinon = require('sinon');
var EventEmitter = require('events').EventEmitter;
var proxyquire = require('proxyquire');
var index = require('../../lib');
var index = require('../../../lib');
var log = index.log;
var httpStub = {
@ -30,7 +30,7 @@ fakeSocket.on('test/event1', function(data) {
fakeSocketListener.emit('connection', fakeSocket);
fakeSocket.emit('subscribe', 'test/event1');
var WebService = proxyquire('../../lib/services/web', {http: httpStub, https: httpsStub, fs: fsStub});
var WebService = proxyquire('../../../lib/services/web', {http: httpStub, https: httpsStub, fs: fsStub});
describe('WebService', function() {
var defaultNode = new EventEmitter();