2019-09-07 06:42:03 +02:00
|
|
|
import * as assert from 'assert';
|
|
|
|
import { describe, it } from 'mocha';
|
|
|
|
import * as types from '../src/types';
|
|
|
|
const typeforce = require('typeforce');
|
2015-08-25 05:52:24 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('types', () => {
|
|
|
|
describe('Buffer Hash160/Hash256', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const buffer20byte = Buffer.alloc(20);
|
|
|
|
const buffer32byte = Buffer.alloc(32);
|
2016-02-02 13:06:16 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('return true for valid size', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert(types.Hash160bit(buffer20byte));
|
|
|
|
assert(types.Hash256bit(buffer32byte));
|
|
|
|
});
|
2016-02-02 13:06:16 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('return true for oneOf', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
typeforce(
|
|
|
|
types.oneOf(types.Hash160bit, types.Hash256bit),
|
|
|
|
buffer32byte,
|
|
|
|
);
|
|
|
|
});
|
2016-09-30 08:25:13 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.doesNotThrow(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
typeforce(
|
|
|
|
types.oneOf(types.Hash256bit, types.Hash160bit),
|
|
|
|
buffer32byte,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2016-02-05 04:15:23 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('throws for invalid size', () => {
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
types.Hash160bit(buffer32byte);
|
|
|
|
}, /Expected Buffer\(Length: 20\), got Buffer\(Length: 32\)/);
|
2016-02-02 13:06:16 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
types.Hash256bit(buffer20byte);
|
|
|
|
}, /Expected Buffer\(Length: 32\), got Buffer\(Length: 20\)/);
|
|
|
|
});
|
|
|
|
});
|
2016-09-27 05:54:03 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('Satoshi', () => {
|
2016-09-27 05:54:03 +02:00
|
|
|
[
|
|
|
|
{ value: -1, result: false },
|
|
|
|
{ value: 0, result: true },
|
|
|
|
{ value: 1, result: true },
|
|
|
|
{ value: 20999999 * 1e8, result: true },
|
|
|
|
{ value: 21000000 * 1e8, result: true },
|
2019-09-07 06:42:03 +02:00
|
|
|
{ value: 21000001 * 1e8, result: false },
|
2019-04-09 08:09:50 +02:00
|
|
|
].forEach(f => {
|
|
|
|
it('returns ' + f.result + ' for valid for ' + f.value, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(types.Satoshi(f.value), f.result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-09-02 09:08:31 +02:00
|
|
|
|
|
|
|
describe('UInt31', () => {
|
|
|
|
const UINT31_MAX = Math.pow(2, 31) - 1;
|
|
|
|
it('return true for valid values', () => {
|
|
|
|
assert.strictEqual(types.UInt31(0), true);
|
|
|
|
assert.strictEqual(types.UInt31(1000), true);
|
|
|
|
assert.strictEqual(types.UInt31(UINT31_MAX), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return false for negative values', () => {
|
|
|
|
assert.strictEqual(types.UInt31(-1), false);
|
|
|
|
assert.strictEqual(types.UInt31(-UINT31_MAX), false);
|
|
|
|
});
|
|
|
|
|
2020-09-02 10:15:11 +02:00
|
|
|
it(`return false for value > ${UINT31_MAX}`, () => {
|
2020-09-02 09:08:31 +02:00
|
|
|
assert.strictEqual(types.UInt31(UINT31_MAX + 1), false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('BIP32Path', () => {
|
|
|
|
it('return true for valid paths', () => {
|
|
|
|
assert.strictEqual(types.BIP32Path("m/0'/0'"), true);
|
|
|
|
assert.strictEqual(types.BIP32Path("m/0'/0"), true);
|
|
|
|
assert.strictEqual(types.BIP32Path("m/0'/1'/2'/3/4'"), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return false for invalid paths', () => {
|
|
|
|
assert.strictEqual(types.BIP32Path('m'), false);
|
|
|
|
assert.strictEqual(types.BIP32Path("n/0'/0'"), false);
|
|
|
|
assert.strictEqual(types.BIP32Path("m/0'/x"), false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return "BIP32 derivation path" for JSON.strigify()', () => {
|
|
|
|
const toJsonValue = JSON.stringify(types.BIP32Path);
|
|
|
|
assert.equal(toJsonValue, '"BIP32 derivation path"');
|
|
|
|
});
|
|
|
|
});
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|