added some encoding tests.

This commit is contained in:
Chris Kleeschulte 2017-02-06 15:46:16 -05:00
parent 331b4e3f77
commit 921210a6d0
3 changed files with 103 additions and 0 deletions

View File

@ -3,6 +3,7 @@
var bitcore = require('bitcore-lib');
function Encoding(servicePrefix) {
//if you add any more encoding keys, be sure to add a subkey
this.servicePrefix = servicePrefix;
}

View File

@ -0,0 +1,59 @@
'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() {
var servicePrefix = new Buffer('0000', 'hex');
var encoding = new Encoding(servicePrefix);
var txid = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add';
var address = '1EZBqbJSHFKSkVPNKzc5v26HA6nAHiTXq6';
var height = 1;
var addressSizeBuf = new Buffer(1);
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 outputIndex = 5;
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);
});
it('should decode address key', function() {
var addressIndexKey = encoding.decodeAddressIndexKey(addressIndexKeyBuf);
addressIndexKey.address.should.equal(address);
addressIndexKey.txid.should.equal(txid);
addressIndexKey.height.should.equal(height);
});
it('should encode utxo key', function() {
encoding.encodeUtxoIndexKey(address, txid, outputIndex).should.deep.equal(utxoKeyBuf);
});
it('should decode utxo key', function() {
var utxoKey = encoding.decodeUtxoIndexKey(utxoKeyBuf);
utxoKey.address.should.equal(address);
utxoKey.txid.should.equal(txid);
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);
});
it('should decode utxo value', function() {
});
});

View File

@ -0,0 +1,43 @@
'use strict';
var should = require('chai').should();
var sinon = require('sinon');
var bitcore = require('bitcore-lib');
var Encoding = require('../../../lib/services/transaction/encoding');
describe('Transaction service encoding', function() {
var servicePrefix = new Buffer('0000', 'hex');
var encoding = new Encoding(servicePrefix);
var txid = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add';
var txHex = '0100000001cc3ffe0638792c8b39328bb490caaefe2cf418f2ce0144956e0c22515f29724d010000006a473044022030ce9fa68d1a32abf0cd4adecf90fb998375b64fe887c6987278452b068ae74c022036a7d00d1c8af19e298e04f14294c807ebda51a20389ad751b4ff3c032cf8990012103acfcb348abb526526a9f63214639d79183871311c05b2eebc727adfdd016514fffffffff02f6ae7d04000000001976a9144455183e407ee4d3423858c8a3275918aedcd18e88aca99b9b08010000001976a9140beceae2c29bfde08d2b6d80b33067451c5887be88ac00000000';
var tx = new bitcore.Transaction(txHex);
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 transaction key' , function() {
var txBuf = new Buffer(txid, 'hex');
encoding.encodeTransactionKey(txid).should.deep.equal(Buffer.concat([servicePrefix, txBuf]));
});
it('should decode transaction key', function() {
encoding.decodeTransactionKey(Buffer.concat([servicePrefix, new Buffer(txid, 'hex')]))
.should.equal(txid);
});
it('should encode transaction value', function() {
tx.__height = 2;
tx.__timestamp = 1;
tx.__inputValues = [ 2, 3 ];
encoding.encodeTransactionValue(tx).should.deep.equal(txEncoded);
});
it('should decode transaction value', function() {
var tx = encoding.decodeTransactionValue(txEncoded);
tx.__height.should.equal(2);
tx.__timestamp.should.equal(1);
tx.__inputValues.should.deep.equal([2,3]);
tx.toBuffer().toString('hex').should.equal(txHex);
});
});