46 lines
1.5 KiB
JavaScript
46 lines
1.5 KiB
JavaScript
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 = new Buffer([ 0, 0, 0, 0xde, 0xad, 0xbe, 0xef ]);
|
|
var b = utils.toBase58(arr);
|
|
assert.equal(b, '1116h8cQN');
|
|
assert.deepEqual(utils.fromBase58(b), arr);
|
|
});
|
|
|
|
it('should translate bits to target', function() {
|
|
var bits = 0x1900896c;
|
|
var hash = new Buffer(
|
|
'672b3f1bb11a994267ea4171069ba0aa4448a840f38e8f340000000000000000',
|
|
'hex'
|
|
);
|
|
var target = utils.fromCompact(bits);
|
|
assert(utils.testTarget(target, hash));
|
|
});
|
|
|
|
it('should convert satoshi to btc', function() {
|
|
var btc = utils.toBTC(new bn(5460));
|
|
assert.equal(btc, '0.0000546');
|
|
btc = utils.toBTC(new bn(54678).mul(new bn(1000000)));
|
|
assert.equal(btc, '546.78');
|
|
btc = utils.toBTC(new bn(5460).mul(new bn(10000000)));
|
|
assert.equal(btc, '546.0');
|
|
btc = utils.toBTC(new Buffer(new bn(5460).mul(new bn(10000000)).toArray()));
|
|
assert.equal(btc, '546.0');
|
|
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 = utils.fromBTC('0.0000546');
|
|
assert(btc.cmp(new bn(5460)) === 0);
|
|
btc = utils.fromBTC('546.78');
|
|
assert(btc.cmp(new bn(54678).mul(new bn(1000000))) === 0);
|
|
btc = utils.fromBTC('546.0');
|
|
assert(btc.cmp(new bn(5460).mul(new bn(10000000))) === 0);
|
|
});
|
|
});
|