add utils.hash test.
This commit is contained in:
parent
10fce032b7
commit
6da3fbac3a
@ -832,8 +832,8 @@ utils.now = function now() {
|
||||
};
|
||||
|
||||
utils.hash = function hash(obj, enc) {
|
||||
if (!obj)
|
||||
return obj;
|
||||
if (obj == null)
|
||||
throw new Error('Cannot get hash of null');
|
||||
|
||||
if (typeof obj === 'string')
|
||||
return enc === 'hex' ? obj : utils.toArray(obj, 'hex');
|
||||
@ -850,5 +850,5 @@ utils.hash = function hash(obj, enc) {
|
||||
if (obj._hash)
|
||||
return hash(obj._hash, enc);
|
||||
|
||||
return obj;
|
||||
throw new Error('Cannot get hash of object');
|
||||
};
|
||||
|
||||
@ -1,44 +1,66 @@
|
||||
var assert = require('assert');
|
||||
var bn = require('bn.js');
|
||||
var bcoin = require('../');
|
||||
var utils = bcoin.utils;
|
||||
|
||||
describe('Utils', function() {
|
||||
it('should encode/decode base58', function() {
|
||||
var arr = [ 0, 0, 0, 0xde, 0xad, 0xbe, 0xef ];
|
||||
var b = bcoin.utils.toBase58(arr);
|
||||
var b = utils.toBase58(arr);
|
||||
assert.equal(b, '1116h8cQN');
|
||||
assert.deepEqual(bcoin.utils.fromBase58(b), arr);
|
||||
assert.deepEqual(utils.fromBase58(b), arr);
|
||||
});
|
||||
|
||||
it('should translate bits to target', function() {
|
||||
var bits = 0x1900896c;
|
||||
var hash = bcoin.utils.toArray(
|
||||
var hash = utils.toArray(
|
||||
'672b3f1bb11a994267ea4171069ba0aa4448a840f38e8f340000000000000000',
|
||||
'hex'
|
||||
);
|
||||
var target = bcoin.utils.bitsToTarget(bits);
|
||||
assert(bcoin.utils.testTarget(target, hash));
|
||||
var target = utils.fromCompact(bits);
|
||||
assert(utils.testTarget(target, hash));
|
||||
});
|
||||
|
||||
it('should convert satoshi to btc', function() {
|
||||
var btc = bcoin.utils.toBTC(new bn(5460));
|
||||
var btc = utils.toBTC(new bn(5460));
|
||||
assert.equal(btc, '0.0000546');
|
||||
btc = bcoin.utils.toBTC(new bn(54678).mul(new bn(1000000)));
|
||||
btc = utils.toBTC(new bn(54678).mul(new bn(1000000)));
|
||||
assert.equal(btc, '546.78');
|
||||
btc = bcoin.utils.toBTC(new bn(5460).mul(new bn(10000000)));
|
||||
btc = utils.toBTC(new bn(5460).mul(new bn(10000000)));
|
||||
assert.equal(btc, '546.0');
|
||||
btc = bcoin.utils.toBTC(new bn(5460).mul(new bn(10000000)).toArray());
|
||||
btc = utils.toBTC(new bn(5460).mul(new bn(10000000)).toArray());
|
||||
assert.equal(btc, '546.0');
|
||||
btc = bcoin.utils.toBTC(new bn(5460).mul(new bn(10000000)).toString('hex'));
|
||||
btc = utils.toBTC(new bn(5460).mul(new bn(10000000)).toString('hex'));
|
||||
assert.equal(btc, '546.0');
|
||||
});
|
||||
|
||||
it('should convert btc to satoshi', function() {
|
||||
var btc = bcoin.utils.fromBTC('0.0000546');
|
||||
var btc = utils.fromBTC('0.0000546');
|
||||
assert(btc.cmp(new bn(5460)) === 0);
|
||||
btc = bcoin.utils.fromBTC('546.78');
|
||||
btc = utils.fromBTC('546.78');
|
||||
assert(btc.cmp(new bn(54678).mul(new bn(1000000))) === 0);
|
||||
btc = bcoin.utils.fromBTC('546.0');
|
||||
btc = utils.fromBTC('546.0');
|
||||
assert(btc.cmp(new bn(5460).mul(new bn(10000000))) === 0);
|
||||
});
|
||||
|
||||
it('should convert objects to hashes', function() {
|
||||
var b1 = '00';
|
||||
var b2 = [0];
|
||||
var b3 = { hash: function(enc) { return enc === 'hex' ? '00' : [0]; } };
|
||||
var b4 = { hash: '00' };
|
||||
var b5 = { _hash: '00' };
|
||||
var b6 = { hash: [0] };
|
||||
var b7 = { _hash: [0] };
|
||||
[b1, b2, b3, b4, b5, b6, b7].forEach(function(b, i) {
|
||||
utils.assert.equal(utils.hash(b, 'hex'), '00');
|
||||
utils.assert(utils.isEqual(utils.hash(b), [0]));
|
||||
});
|
||||
var thrown = true;
|
||||
try {
|
||||
utils.hash(1, 'hex');
|
||||
} catch (e) {
|
||||
thrown = true;
|
||||
}
|
||||
assert.equal(thrown, true);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user