bitcore/test/test.misc.js

209 lines
6.6 KiB
JavaScript
Raw Normal View History

2014-02-06 07:57:47 -08:00
'use strict';
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');
2014-03-10 09:44:22 -07:00
var buffertools = require('buffertools');
2014-04-10 14:19:13 -07:00
buffertools.extend();
2014-02-06 07:57:47 -08:00
var should = chai.should();
2014-03-10 09:44:22 -07:00
var testdata = testdata || require('./testdata');
2014-02-26 12:56:57 -08:00
2014-02-19 09:25:14 -08:00
var bignum = bitcore.bignum;
var base58 = bitcore.base58;
var base58Check = base58.base58Check;
2014-03-19 12:25:23 -07:00
var Address = bitcore.Address;
var networks = bitcore.networks;
2014-03-19 13:12:24 -07:00
var WalletKey = bitcore.WalletKey;
2014-04-10 14:19:13 -07:00
var Buffers = require('buffers');
2014-04-10 14:52:13 -07:00
var m = bitcore['Buffers.monkey'] || require('../Buffers.monkey');
m.patch(Buffers);
2014-03-19 12:25:23 -07:00
2014-02-06 07:57:47 -08:00
describe('Miscelaneous stuff', function() {
it('should initialze the config object', function() {
should.exist(bitcore.config);
});
it('should initialze the log object', function() {
should.exist(bitcore.log);
});
2014-04-07 14:30:49 -07:00
it('should initialze the network object', function() {
should.exist(networks);
var nets = [networks.livenet, networks.testnet];
2014-04-10 14:19:13 -07:00
for (var i = 0; i < 2; i++) {
2014-04-07 14:30:49 -07:00
var net = nets[i];
should.exist(net.addressVersion);
should.exist(net.privKeyVersion);
should.exist(net.P2SHVersion);
should.exist(net.bip32publicVersion);
should.exist(net.bip32privateVersion);
}
});
2014-02-19 09:27:03 -08:00
it('should initialze the const object', function() {
should.exist(bitcore.const);
2014-02-19 09:30:48 -08:00
});
it('should initialze the Deserialize object', function() {
should.exist(bitcore.Deserialize);
should.exist(bitcore.Deserialize.intFromCompact);
2014-02-19 09:27:03 -08:00
});
2014-04-10 14:19:13 -07:00
it('should initialze the Buffer class', function() {
should.exist(bitcore.Buffer);
});
2014-02-06 10:49:22 -08:00
2014-02-19 09:25:14 -08:00
2014-04-10 14:19:13 -07:00
describe('Buffers monkey patch', function() {
var bufs;
beforeEach(function() {
bufs = new Buffers();
2014-04-10 14:52:13 -07:00
bufs.push(new Buffer('aaaaaa', 'hex'));
bufs.push(new Buffer('bbbb', 'hex'));
bufs.push(new Buffer('cc', 'hex'));
2014-04-10 14:19:13 -07:00
});
it('should monkey patch the Buffers class', function() {
should.exist(bufs.skip);
});
it('should work for 0', function() {
bufs.skip(0);
2014-04-10 14:52:13 -07:00
bufs.toBuffer().toHex().should.equal('aaaaaabbbbcc');
2014-04-10 14:19:13 -07:00
});
it('should work for length', function() {
bufs.skip(bufs.length);
bufs.toBuffer().toHex().should.equal('');
});
it('should work for middle values', function() {
2014-04-10 14:52:13 -07:00
bufs.skip(4);
bufs.toBuffer().toHex().should.equal('bbcc');
bufs.skip(1);
bufs.toBuffer().toHex().should.equal('cc');
bufs.skip(1);
bufs.toBuffer().toHex().should.equal('');
2014-04-10 14:19:13 -07:00
});
});
2014-02-19 09:25:14 -08:00
// bignum
it('should initialze the bignum object', function() {
should.exist(bitcore.bignum);
});
it('should create a bignum from string', function() {
var n = bignum('9832087987979879879879879879879879879879879879');
should.exist(n);
});
it('should perform basic math operations for bignum', function() {
var b = bignum('782910138827292261791972728324982')
2014-03-19 13:35:14 -07:00
.sub('182373273283402171237474774728373')
.div(13);
2014-02-19 09:25:14 -08:00
b.toNumber().should.equal(46195143503376160811884457968969);
});
// base58
it('should initialze the base58 object', function() {
should.exist(bitcore.base58);
});
it('should obtain the same string in base58 roundtrip', function() {
var m = 'mqqa8xSMVDyf9QxihGnPtap6Mh6qemUkcu';
base58.encode(base58.decode(m)).should.equal(m);
});
it('should obtain the same string in base58Check roundtrip', function() {
var m = '1QCJj1gPZKx2EwzGo9Ri8mMBs39STvDYcv';
base58Check.encode(base58Check.decode(m)).should.equal(m);
});
2014-03-10 09:44:22 -07:00
testdata.dataEncodeDecode.forEach(function(datum) {
2014-02-26 12:56:57 -08:00
it('base58 encode/decode checks ' + datum, function() {
// from bitcoin/bitcoin tests:
// Goal: test low-level base58 encoding functionality
2014-02-26 12:56:57 -08:00
base58.encode(new Buffer(datum[0], 'hex')).should.equal(datum[1]);
2014-02-26 13:36:01 -08:00
buffertools.toHex(base58.decode(datum[1])).should.equal(datum[0]);
2014-02-26 12:56:57 -08:00
});
});
2014-03-19 12:25:23 -07:00
testdata.dataBase58KeysValid.forEach(function(datum) {
var b58 = datum[0];
var hexPayload = datum[1];
var meta = datum[2];
2014-03-19 13:35:14 -07:00
var network = meta.isTestnet ? networks.testnet : networks.livenet;
if (meta.isPrivkey) {
describe('base58 private key valid ' + b58, function() {
var k;
2014-03-19 13:52:34 -07:00
var opts = {
network: network
};
2014-03-19 13:35:14 -07:00
before(function() {
2014-03-19 13:52:34 -07:00
k = new WalletKey(opts);
2014-03-19 13:35:14 -07:00
});
2014-03-19 13:52:34 -07:00
it('should generate correctly from WIF', function() {
2014-03-19 13:35:14 -07:00
k.fromObj({
priv: b58
});
should.exist(k.privKey);
});
it('should have compressed state', function() {
k.privKey.compressed.should.equal(meta.isCompressed);
});
it('private key should have correct payload', function() {
buffertools.toHex(k.privKey.private).should.equal(hexPayload);
});
it('should not be an Address', function() {
new Address(b58).isValid().should.equal(false);
});
2014-03-19 13:52:34 -07:00
it('should generate correctly from hex', function() {
var k2 = new WalletKey(opts);
k2.fromObj({
priv: hexPayload,
compressed: meta.isCompressed
});
k2.storeObj().priv.should.equal(b58);
});
2014-03-19 13:35:14 -07:00
});
} else {
describe('base58 address valid ' + b58, function() {
var a;
2014-03-19 13:57:28 -07:00
var shouldBeScript = meta.addrType === 'script';
2014-03-19 13:35:14 -07:00
before(function() {
a = new Address(b58);
});
it('should be valid', function() {
a.isValid().should.equal(true);
});
it('should be of correct type', function() {
2014-03-19 13:57:28 -07:00
a.isScript().should.equal(shouldBeScript);
2014-03-19 13:35:14 -07:00
});
it('should get correct network', function() {
a.network().should.equal(network);
});
2014-03-19 13:57:28 -07:00
it('should generate correctly from hex', function() {
2014-04-07 14:30:49 -07:00
var version = shouldBeScript ? network.P2SHVersion : network.addressVersion;
2014-03-19 13:57:28 -07:00
var b = new Address(version, new Buffer(hexPayload, 'hex'));
b.toString().should.equal(b58);
});
2014-03-19 13:35:14 -07:00
});
}
2014-03-19 12:25:23 -07:00
});
2014-03-19 14:44:24 -07:00
testdata.dataBase58KeysInvalid.forEach(function(datum) {
var b58 = datum[0];
it('shouldnt be able to create Address with ' + b58, function() {
2014-03-19 14:44:24 -07:00
var a = new Address(b58);
var invalidAddress = (!a.isValid());
invalidAddress.should.equal(true);
});
it('shouldnt be able to create WalletKey with ' + b58, function() {
var kl = new WalletKey({
network: networks.livenet
});
var kt = new WalletKey({
network: networks.livenet
});
var createLivenet = function() {
kl.fromObj({
priv: b58
});
};
var createTestnet = function() {
kt.fromObj({
priv: b58
});
};
2014-04-10 14:24:20 -07:00
createLivenet.should.throw();
createTestnet.should.throw();
});
2014-03-19 14:44:24 -07:00
});
2014-02-19 09:27:03 -08:00
2014-02-06 07:57:47 -08:00
});