test: more refactoring of various tests and assert module.

This commit is contained in:
Christopher Jeffrey 2017-08-12 12:45:09 -07:00
parent 3a7484d782
commit c522c79c58
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
8 changed files with 309 additions and 222 deletions

View File

@ -128,15 +128,17 @@ function toAddress(hrp, version, program) {
} }
function createProgram(version, program) { function createProgram(version, program) {
const ver = Buffer.from([version ? version + 0x80 : 0, program.length]); const data = Buffer.allocUnsafe(2 + program.length);
return Buffer.concat([ver, program]); data[0] = version ? version + 0x80 : 0;
data[1] = program.length;
program.copy(data, 2);
return data;
} }
describe('Bech32', function() { describe('Bech32', function() {
for (const addr of validChecksums) { for (const addr of validChecksums) {
it(`should have valid checksum for ${addr}`, () => { it(`should have valid checksum for ${addr}`, () => {
const ret = bech32.deserialize(addr); assert(bech32.deserialize(addr));
assert(ret);
}); });
} }
@ -160,42 +162,20 @@ describe('Bech32', function() {
} }
} }
let ok = ret !== null; assert(ret !== null);
if (ok) { const output = createProgram(ret.version, ret.program);
const output = createProgram(ret.version, ret.program); assert.bufferEqual(output, script);
ok = output.equals(script);
}
if (ok) { const recreate = toAddress(hrp, ret.version, ret.program);
const recreate = toAddress(hrp, ret.version, ret.program); assert.strictEqual(recreate, addr.toLowerCase());
ok = recreate === addr.toLowerCase();
}
assert(ok);
}); });
} }
for (const addr of invalidAddresses) { for (const addr of invalidAddresses) {
it(`should have invalid address for ${addr}`, () => { it(`should have invalid address for ${addr}`, () => {
let ok1 = null; assert.throws(() => fromAddress('bc', addr));
assert.throws(() => fromAddress('tb', addr));
try {
ok1 = fromAddress('bc', addr);
} catch (e) {
ok1 = null;
}
let ok2 = null;
try {
ok2 = fromAddress('tb', addr);
} catch (e) {
ok2 = null;
}
const ok = ok1 === null && ok2 === null;
assert(ok);
}); });
} }
@ -217,41 +197,20 @@ describe('Bech32', function() {
} }
} }
let ok = ret !== null; assert(ret !== null);
if (ok) { const output = createProgram(ret.version, ret.hash);
const output = createProgram(ret.version, ret.hash); assert.bufferEqual(output, script);
ok = output.equals(script);
}
if (ok) { const recreate = ret.toBech32();
const recreate = ret.toBech32(); assert.strictEqual(recreate, addr.toLowerCase());
ok = recreate === addr.toLowerCase();
}
assert(ok);
}); });
} }
for (const addr of invalidAddresses) { for (const addr of invalidAddresses) {
it(`should have invalid address for ${addr}`, () => { it(`should have invalid address for ${addr}`, () => {
let ok1 = null; assert.throws(() => Address.fromBech32(addr, 'main'));
assert.throws(() => Address.fromBech32(addr, 'testnet'));
try {
ok1 = Address.fromBech32(addr, 'main');
} catch (e) {
ok1 = null;
}
let ok2 = null;
try {
ok2 = Address.fromBech32(addr, 'testnet');
} catch (e) {
ok2 = null;
}
assert(!ok1 && !ok2);
}); });
} }
}); });

View File

@ -66,49 +66,61 @@ describe('BIP150', function() {
it('should encrypt payload from client to server', () => { it('should encrypt payload from client to server', () => {
const packet = client.packet('fake', payload()); const packet = client.packet('fake', payload());
let emitted = false; let emitted = false;
server.once('packet', (cmd, body) => { server.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
server.feed(packet); server.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from server to client', () => { it('should encrypt payload from server to client', () => {
const packet = server.packet('fake', payload()); const packet = server.packet('fake', payload());
let emitted = false; let emitted = false;
client.once('packet', (cmd, body) => { client.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
client.feed(packet); client.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from client to server (2)', () => { it('should encrypt payload from client to server (2)', () => {
const packet = client.packet('fake', payload()); const packet = client.packet('fake', payload());
let emitted = false; let emitted = false;
server.once('packet', (cmd, body) => { server.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
server.feed(packet); server.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from server to client (2)', () => { it('should encrypt payload from server to client (2)', () => {
const packet = server.packet('fake', payload()); const packet = server.packet('fake', payload());
let emitted = false; let emitted = false;
client.once('packet', (cmd, body) => { client.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
client.feed(packet); client.feed(packet);
assert(emitted); assert(emitted);
}); });
@ -141,69 +153,85 @@ describe('BIP150', function() {
it('should encrypt payload from client to server after rekey', () => { it('should encrypt payload from client to server after rekey', () => {
const packet = client.packet('fake', payload()); const packet = client.packet('fake', payload());
let emitted = false; let emitted = false;
server.once('packet', (cmd, body) => { server.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
server.feed(packet); server.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from server to client after rekey', () => { it('should encrypt payload from server to client after rekey', () => {
const packet = server.packet('fake', payload()); const packet = server.packet('fake', payload());
let emitted = false; let emitted = false;
client.once('packet', (cmd, body) => { client.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
client.feed(packet); client.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from client to server after rekey (2)', () => { it('should encrypt payload from client to server after rekey (2)', () => {
const packet = client.packet('fake', payload()); const packet = client.packet('fake', payload());
let emitted = false; let emitted = false;
server.once('packet', (cmd, body) => { server.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
server.feed(packet); server.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from server to client after rekey (2)', () => { it('should encrypt payload from server to client after rekey (2)', () => {
const packet = server.packet('fake', payload()); const packet = server.packet('fake', payload());
let emitted = false; let emitted = false;
client.once('packet', (cmd, body) => { client.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
client.feed(packet); client.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payloads both ways asynchronously', () => { it('should encrypt payloads both ways asynchronously', () => {
const spacket = server.packet('fake', payload()); const spacket = server.packet('fake', payload());
const cpacket = client.packet('fake', payload()); const cpacket = client.packet('fake', payload());
let cemitted = false; let cemitted = false;
let semitted = false;
client.once('packet', (cmd, body) => { client.once('packet', (cmd, body) => {
cemitted = true; cemitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
let semitted = false;
server.once('packet', (cmd, body) => { server.once('packet', (cmd, body) => {
semitted = true; semitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
client.feed(spacket); client.feed(spacket);
server.feed(cpacket); server.feed(cpacket);
assert(cemitted); assert(cemitted);
assert(semitted); assert(semitted);
}); });

View File

@ -41,49 +41,61 @@ describe('BIP151', function() {
it('should encrypt payload from client to server', () => { it('should encrypt payload from client to server', () => {
const packet = client.packet('fake', payload()); const packet = client.packet('fake', payload());
let emitted = false; let emitted = false;
server.once('packet', (cmd, body) => { server.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
server.feed(packet); server.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from server to client', () => { it('should encrypt payload from server to client', () => {
const packet = server.packet('fake', payload()); const packet = server.packet('fake', payload());
let emitted = false; let emitted = false;
client.once('packet', (cmd, body) => { client.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
client.feed(packet); client.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from client to server (2)', () => { it('should encrypt payload from client to server (2)', () => {
const packet = client.packet('fake', payload()); const packet = client.packet('fake', payload());
let emitted = false; let emitted = false;
server.once('packet', (cmd, body) => { server.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
server.feed(packet); server.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from server to client (2)', () => { it('should encrypt payload from server to client (2)', () => {
const packet = server.packet('fake', payload()); const packet = server.packet('fake', payload());
let emitted = false; let emitted = false;
client.once('packet', (cmd, body) => { client.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
client.feed(packet); client.feed(packet);
assert(emitted); assert(emitted);
}); });
@ -116,69 +128,85 @@ describe('BIP151', function() {
it('should encrypt payload from client to server after rekey', () => { it('should encrypt payload from client to server after rekey', () => {
const packet = client.packet('fake', payload()); const packet = client.packet('fake', payload());
let emitted = false; let emitted = false;
server.once('packet', (cmd, body) => { server.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
server.feed(packet); server.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from server to client after rekey', () => { it('should encrypt payload from server to client after rekey', () => {
const packet = server.packet('fake', payload()); const packet = server.packet('fake', payload());
let emitted = false; let emitted = false;
client.once('packet', (cmd, body) => { client.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
client.feed(packet); client.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from client to server after rekey (2)', () => { it('should encrypt payload from client to server after rekey (2)', () => {
const packet = client.packet('fake', payload()); const packet = client.packet('fake', payload());
let emitted = false; let emitted = false;
server.once('packet', (cmd, body) => { server.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
server.feed(packet); server.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payload from server to client after rekey (2)', () => { it('should encrypt payload from server to client after rekey (2)', () => {
const packet = server.packet('fake', payload()); const packet = server.packet('fake', payload());
let emitted = false; let emitted = false;
client.once('packet', (cmd, body) => { client.once('packet', (cmd, body) => {
emitted = true; emitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
client.feed(packet); client.feed(packet);
assert(emitted); assert(emitted);
}); });
it('should encrypt payloads both ways asynchronously', () => { it('should encrypt payloads both ways asynchronously', () => {
const spacket = server.packet('fake', payload()); const spacket = server.packet('fake', payload());
const cpacket = client.packet('fake', payload()); const cpacket = client.packet('fake', payload());
let cemitted = false; let cemitted = false;
let semitted = false;
client.once('packet', (cmd, body) => { client.once('packet', (cmd, body) => {
cemitted = true; cemitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
let semitted = false;
server.once('packet', (cmd, body) => { server.once('packet', (cmd, body) => {
semitted = true; semitted = true;
assert.strictEqual(cmd, 'fake'); assert.strictEqual(cmd, 'fake');
assert.bufferEqual(body, payload()); assert.bufferEqual(body, payload());
}); });
client.feed(spacket); client.feed(spacket);
server.feed(cpacket); server.feed(cpacket);
assert(cemitted); assert(cemitted);
assert(semitted); assert(semitted);
}); });

View File

@ -39,24 +39,22 @@ const undo928828 = fs.readFileSync(`${__dirname}/data/undo928828.raw`);
const block1087400 = fs.readFileSync(`${__dirname}/data/block1087400.raw`); const block1087400 = fs.readFileSync(`${__dirname}/data/block1087400.raw`);
const undo1087400 = fs.readFileSync(`${__dirname}/data/undo1087400.raw`); const undo1087400 = fs.readFileSync(`${__dirname}/data/undo1087400.raw`);
// Abstract
const mblock = MerkleBlock.fromRaw(merkle300025);
const block = Block.fromRaw(block300025);
describe('Block', function() { describe('Block', function() {
this.timeout(10000); this.timeout(10000);
it('should parse partial merkle tree', () => { it('should parse partial merkle tree', () => {
assert(mblock.verifyPOW()); const block = MerkleBlock.fromRaw(merkle300025);
assert(mblock.verifyBody());
assert(mblock.verify());
const tree = mblock.getTree(); assert(block.verifyPOW());
assert(block.verifyBody());
assert(block.verify());
const tree = block.getTree();
assert.strictEqual(tree.matches.length, 2); assert.strictEqual(tree.matches.length, 2);
assert.strictEqual(mblock.hash('hex'), assert.strictEqual(block.hash('hex'),
'8cc72c02a958de5a8b35a23bb7e3bced8bf840cc0a4e1c820000000000000000'); '8cc72c02a958de5a8b35a23bb7e3bced8bf840cc0a4e1c820000000000000000');
assert.strictEqual(mblock.rhash(), assert.strictEqual(block.rhash(),
'0000000000000000821c4e0acc40f88bedbce3b73ba2358b5ade58a9022cc78c'); '0000000000000000821c4e0acc40f88bedbce3b73ba2358b5ade58a9022cc78c');
assert.strictEqual( assert.strictEqual(
tree.matches[0].toString('hex'), tree.matches[0].toString('hex'),
@ -67,29 +65,30 @@ describe('Block', function() {
}); });
it('should decode/encode merkle block', () => { it('should decode/encode merkle block', () => {
const merkle = MerkleBlock.fromRaw(merkle300025); const block = MerkleBlock.fromRaw(merkle300025);
merkle.refresh(); block.refresh();
assert.bufferEqual(merkle.toRaw(), merkle300025); assert.bufferEqual(block.toRaw(), merkle300025);
assert.bufferEqual(mblock.toRaw(), merkle300025);
}); });
it('should be verify merkle block', () => { it('should verify merkle block', () => {
const merkle = MerkleBlock.fromRaw(merkle300025); const block = MerkleBlock.fromRaw(merkle300025);
assert(merkle.verify()); assert(block.verify());
}); });
it('should be encoded/decoded and still verify', () => { it('should be encoded/decoded and still verify', () => {
const raw = mblock.toRaw(); const block1 = MerkleBlock.fromRaw(merkle300025);
const merkle = MerkleBlock.fromRaw(raw); const raw = block1.toRaw();
assert.bufferEqual(merkle.toRaw(), raw); const block2 = MerkleBlock.fromRaw(raw);
assert(merkle.verify()); assert.bufferEqual(block2.toRaw(), raw);
assert(block2.verify());
}); });
it('should be jsonified/unjsonified and still verify', () => { it('should be jsonified/unjsonified and still verify', () => {
const json = mblock.toJSON(); const block1 = MerkleBlock.fromRaw(merkle300025);
const merkle = MerkleBlock.fromJSON(json); const json = block1.toJSON();
assert.deepStrictEqual(merkle.toJSON(), json); const block2 = MerkleBlock.fromJSON(json);
assert(merkle.verify()); assert.deepStrictEqual(block2.toJSON(), json);
assert(block2.verify());
}); });
it('should calculate reward properly', () => { it('should calculate reward properly', () => {
@ -110,12 +109,13 @@ describe('Block', function() {
}); });
it('should parse JSON', () => { it('should parse JSON', () => {
const block = Block.fromJSON(Block.fromRaw(block300025).toJSON()); const block1 = Block.fromRaw(block300025);
assert.strictEqual(block.hash('hex'), const block2 = Block.fromJSON(block1.toJSON());
assert.strictEqual(block2.hash('hex'),
'8cc72c02a958de5a8b35a23bb7e3bced8bf840cc0a4e1c820000000000000000'); '8cc72c02a958de5a8b35a23bb7e3bced8bf840cc0a4e1c820000000000000000');
assert.strictEqual(block.rhash(), assert.strictEqual(block2.rhash(),
'0000000000000000821c4e0acc40f88bedbce3b73ba2358b5ade58a9022cc78c'); '0000000000000000821c4e0acc40f88bedbce3b73ba2358b5ade58a9022cc78c');
assert.strictEqual(block.merkleRoot, block.createMerkleRoot('hex')); assert.strictEqual(block2.merkleRoot, block2.createMerkleRoot('hex'));
}); });
it('should create a merkle block', () => { it('should create a merkle block', () => {
@ -129,10 +129,11 @@ describe('Block', function() {
filter.add(item1, 'hex'); filter.add(item1, 'hex');
filter.add(item2, 'hex'); filter.add(item2, 'hex');
const merkle = MerkleBlock.fromBlock(block, filter); const block1 = Block.fromRaw(block300025);
const block2 = MerkleBlock.fromBlock(block1, filter);
assert(merkle.verifyBody()); assert(block2.verifyBody());
assert.bufferEqual(merkle.toRaw(), mblock.toRaw()); assert.bufferEqual(block2.toRaw(), merkle300025);
}); });
it('should verify a historical block', () => { it('should verify a historical block', () => {
@ -173,53 +174,56 @@ describe('Block', function() {
}); });
it('should fail with a bad merkle root', () => { it('should fail with a bad merkle root', () => {
const block2 = new Block(block); const block = Block.fromRaw(block300025);
block2.merkleRoot = encoding.NULL_HASH; const merkleRoot = block.merkleRoot;
block2.refresh(); block.merkleRoot = encoding.NULL_HASH;
assert(!block2.verifyPOW()); block.refresh();
const [, reason] = block2.checkBody(); assert(!block.verifyPOW());
const [, reason] = block.checkBody();
assert.strictEqual(reason, 'bad-txnmrklroot'); assert.strictEqual(reason, 'bad-txnmrklroot');
assert(!block2.verify()); assert(!block.verify());
block2.merkleRoot = block.merkleRoot; block.merkleRoot = merkleRoot;
block2.refresh(); block.refresh();
assert(block2.verify()); assert(block.verify());
}); });
it('should fail on merkle block with a bad merkle root', () => { it('should fail on merkle block with a bad merkle root', () => {
const mblock2 = new MerkleBlock(mblock); const block = MerkleBlock.fromRaw(merkle300025);
mblock2.merkleRoot = encoding.NULL_HASH; const merkleRoot = block.merkleRoot;
mblock2.refresh(); block.merkleRoot = encoding.NULL_HASH;
assert(!mblock2.verifyPOW()); block.refresh();
const [, reason] = mblock2.checkBody(); assert(!block.verifyPOW());
const [, reason] = block.checkBody();
assert.strictEqual(reason, 'bad-txnmrklroot'); assert.strictEqual(reason, 'bad-txnmrklroot');
assert(!mblock2.verify()); assert(!block.verify());
mblock2.merkleRoot = mblock.merkleRoot; block.merkleRoot = merkleRoot;
mblock2.refresh(); block.refresh();
assert(mblock2.verify()); assert(block.verify());
}); });
it('should fail with a low target', () => { it('should fail with a low target', () => {
const block2 = new Block(block); const block = Block.fromRaw(block300025);
block2.bits = 403014710; const bits = block.bits;
block2.refresh(); block.bits = 403014710;
assert(!block2.verifyPOW()); block.refresh();
assert(block2.verifyBody()); assert(!block.verifyPOW());
assert(!block2.verify()); assert(block.verifyBody());
block2.bits = block.bits; assert(!block.verify());
block2.refresh(); block.bits = bits;
assert(block2.verify()); block.refresh();
assert(block.verify());
}); });
it('should fail on duplicate txs', () => { it('should fail on duplicate txs', () => {
const block2 = new Block(block); const block = Block.fromRaw(block300025);
block2.txs.push(block2.txs[block2.txs.length - 1]); block.txs.push(block.txs[block.txs.length - 1]);
block2.refresh(); block.refresh();
const [, reason] = block2.checkBody(); const [, reason] = block.checkBody();
assert.strictEqual(reason, 'bad-txns-duplicate'); assert.strictEqual(reason, 'bad-txns-duplicate');
}); });
it('should verify with headers', () => { it('should verify with headers', () => {
const headers = new Headers(block); const headers = Headers.fromHead(block300025);
assert(headers.verifyPOW()); assert(headers.verifyPOW());
assert(headers.verifyBody()); assert(headers.verifyBody());
assert(headers.verify()); assert(headers.verify());

View File

@ -102,7 +102,7 @@ describe('HTTP', function() {
assert.strictEqual(balance.confirmed, 0); assert.strictEqual(balance.confirmed, 0);
assert.strictEqual(balance.unconfirmed, 201840); assert.strictEqual(balance.unconfirmed, 201840);
assert(details); assert(details);
assert.strictEqual(details.hash, tx.rhash()); assert.strictEqual(details.hash, tx.txid());
}); });
it('should get balance', async () => { it('should get balance', async () => {

View File

@ -6,47 +6,47 @@
const assert = require('./util/assert'); const assert = require('./util/assert');
const KeyRing = require('../lib/primitives/keyring'); const KeyRing = require('../lib/primitives/keyring');
const uncompressed = KeyRing.fromSecret(
'5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss');
const compressed = KeyRing.fromSecret(
'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1');
describe('KeyRing', function() { describe('KeyRing', function() {
const ukey = KeyRing.fromSecret( it('should get uncompressed public key', () => {
'5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss');
const ckey = KeyRing.fromSecret(
'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1');
it('check uncompressed public key', () => {
assert.strictEqual( assert.strictEqual(
'04a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd5b' '04a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd5b'
+ '8dec5235a0fa8722476c7709c02559e3aa73aa03918ba2d492eea75abea235', + '8dec5235a0fa8722476c7709c02559e3aa73aa03918ba2d492eea75abea235',
ukey.getPublicKey('hex')); uncompressed.getPublicKey('hex'));
}); });
it('check uncompressed public key to address', () => { it('should get uncompressed public key address', () => {
assert.strictEqual( assert.strictEqual(
'1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN', '1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN',
ukey.getKeyAddress('base58')); uncompressed.getKeyAddress('base58'));
}); });
it('check uncompressed secret', () => { it('should get uncompressed WIF', () => {
assert.strictEqual( assert.strictEqual(
'5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss', '5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss',
ukey.toSecret()); uncompressed.toSecret());
}); });
it('check compressed public key', () => { it('should get compressed public key', () => {
assert.strictEqual( assert.strictEqual(
'03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd', '03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd',
ckey.getPublicKey('hex')); compressed.getPublicKey('hex'));
}); });
it('check compressed public key to address', () => { it('should get compressed public key address', () => {
assert.strictEqual( assert.strictEqual(
'1F3sAm6ZtwLAUnj7d38pGFxtP3RVEvtsbV', '1F3sAm6ZtwLAUnj7d38pGFxtP3RVEvtsbV',
ckey.getKeyAddress('base58')); compressed.getKeyAddress('base58'));
}); });
it('check compressed secret', () => { it('should get compressed WIF', () => {
assert.strictEqual( assert.strictEqual(
'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1', 'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
ckey.toSecret()); compressed.toSecret());
}); });
}); });

View File

@ -135,7 +135,7 @@ describe('Mempool', function() {
await mempool.addTX(t4.toTX()); await mempool.addTX(t4.toTX());
const balance = mempool.getBalance(); const balance = mempool.getBalance();
assert.strictEqual(balance, 70000); // note: funding balance assert.strictEqual(balance, 70000);
} }
{ {

View File

@ -3,84 +3,87 @@
const _assert = require('assert'); const _assert = require('assert');
const util = require('util'); const util = require('util');
const assert = function assert(ok, message) { const assert = function assert(value, message) {
return _assert(ok, message); if (!value) {
throw new assert.AssertionError({
message,
actual: value,
expected: true,
operator: '==',
stackStartFunction: assert
});
}
}; };
Object.setPrototypeOf(assert, _assert); Object.setPrototypeOf(assert, _assert);
assert.typeOf = function typeOf(value, expected, message) { assert.typeOf = function typeOf(value, expected, message) {
const type = _typeOf(value); _isString(expected, '`expected` must be a string.', typeOf);
if (type !== expected) {
const actual = _typeOf(value);
if (actual !== expected) {
throw new assert.AssertionError({ throw new assert.AssertionError({
mesage: message, message,
actual: type, actual,
expected: expected, expected,
operator: '===', operator: 'typeof ==',
stackStartFunction: typeOf stackStartFunction: typeOf
}); });
} }
}; };
assert.notTypeOf = function notTypeOf(value, expected, message) { assert.notTypeOf = function notTypeOf(value, expected, message) {
const type = _typeOf(value); _isString(expected, '`expected` must be a string.', notTypeOf);
if (type === expected) {
const actual = _typeOf(value);
if (actual === expected) {
throw new assert.AssertionError({ throw new assert.AssertionError({
mesage: message, message,
actual: type, actual,
expected: expected, expected,
operator: '!==', operator: 'typeof !=',
stackStartFunction: notTypeOf stackStartFunction: notTypeOf
}); });
} }
}; };
assert.instanceOf = function instanceOf(object, parent, message) { assert.instanceOf = function instanceOf(object, parent, message) {
_isFunction(parent, '`parent` must be a constructor.', instanceOf);
if (!(object instanceof parent)) { if (!(object instanceof parent)) {
throw new assert.AssertionError({ throw new assert.AssertionError({
mesage: message, message,
actual: _getConstructor(object), actual: _getConstructorName(object),
expected: parent.name, expected: _getFunctionName(parent),
operator: '===', operator: 'instanceof',
stackStartFunction: instanceOf stackStartFunction: instanceOf
}); });
} }
}; };
assert.notInstanceOf = function notInstanceOf(object, parent, message) { assert.notInstanceOf = function notInstanceOf(object, parent, message) {
_isFunction(parent, '`parent` must be a constructor.', notInstanceOf);
if (object instanceof parent) { if (object instanceof parent) {
throw new assert.AssertionError({ throw new assert.AssertionError({
mesage: message, message,
actual: _getConstructor(object), actual: _getConstructorName(object),
expected: parent.name, expected: _getFunctionName(parent),
operator: '!==', operator: 'not instanceof',
stackStartFunction: notInstanceOf stackStartFunction: notInstanceOf
}); });
} }
}; };
assert.isBuffer = function isBuffer(value, message) {
if (!Buffer.isBuffer(value)) {
throw new assert.AssertionError({
mesage: message,
actual: _typeOf(value),
expected: 'buffer',
operator: '===',
stackStartFunction: isBuffer
});
}
};
assert.bufferEqual = function bufferEqual(actual, expected, message) { assert.bufferEqual = function bufferEqual(actual, expected, message) {
assert.isBuffer(actual, message); _isBuffer(actual, '`actual` must be a buffer.', bufferEqual);
assert.isBuffer(expected, message); _isBuffer(expected, '`expected` must be a buffer.', bufferEqual);
if (actual === expected) if (actual !== expected && !actual.equals(expected)) {
return;
if (!actual.equals(expected)) {
throw new assert.AssertionError({ throw new assert.AssertionError({
mesage: message, message,
actual: actual.toString('hex'), actual: actual.toString('hex'),
expected: expected.toString('hex'), expected: expected.toString('hex'),
operator: '===', operator: '===',
@ -90,12 +93,12 @@ assert.bufferEqual = function bufferEqual(actual, expected, message) {
}; };
assert.notBufferEqual = function notBufferEqual(actual, expected, message) { assert.notBufferEqual = function notBufferEqual(actual, expected, message) {
assert.isBuffer(actual, message); _isBuffer(actual, '`actual` must be a buffer.', notBufferEqual);
assert.isBuffer(expected, message); _isBuffer(expected, '`expected` must be a buffer.', notBufferEqual);
if (actual.equals(expected)) { if (actual === expected || actual.equals(expected)) {
throw new assert.AssertionError({ throw new assert.AssertionError({
mesage: message, message,
actual: actual.toString('hex'), actual: actual.toString('hex'),
expected: expected.toString('hex'), expected: expected.toString('hex'),
operator: '!==', operator: '!==',
@ -104,44 +107,109 @@ assert.notBufferEqual = function notBufferEqual(actual, expected, message) {
} }
}; };
function _typeOf(value) { function _isString(value, message, stackStartFunction) {
if (value === null) if (typeof value !== 'string') {
return 'null'; throw new assert.AssertionError({
message,
if (util.isDate(value)) actual: _typeOf(value),
return 'date'; expected: 'string',
operator: 'typeof ==',
if (util.isRegExp(value)) stackStartFunction
return 'regexp'; });
}
if (util.isError(value))
return 'error';
if (Array.isArray(value))
return 'array';
if (Buffer.isBuffer(value))
return 'buffer';
return typeof value;
} }
function _getConstructor(value) { function _isFunction(value, message, stackStartFunction) {
if (value === undefined) if (typeof value !== 'function') {
return 'Undefined'; throw new assert.AssertionError({
message,
actual: _typeOf(value),
expected: 'function',
operator: 'typeof ==',
stackStartFunction
});
}
}
if (value === null) function _isBuffer(value, message, stackStartFunction) {
if (!Buffer.isBuffer(value)) {
throw new assert.AssertionError({
message,
actual: _typeOf(value),
expected: 'buffer',
operator: 'typeof ==',
stackStartFunction
});
}
}
function _typeOf(value) {
const type = typeof value;
switch (type) {
case 'object':
if (value === null)
return 'null';
if (Array.isArray(value))
return 'array';
if (Buffer.isBuffer(value))
return 'buffer';
if (ArrayBuffer.isView(value))
return 'arraybuffer';
if (util.isError(value))
return 'error';
if (util.isDate(value))
return 'date';
if (util.isRegExp(value))
return 'regexp';
break;
case 'number':
if (!isFinite(value))
return 'nan';
break;
}
return type;
}
function _getConstructorName(object) {
if (object === undefined)
return 'undefined';
if (object === null)
return 'null';
const proto = Object.getPrototypeOf(object);
// Should never happen.
if (proto === undefined)
throw new Error('Bad prototype.');
// Inherited from `null`.
if (proto === null)
return 'Null'; return 'Null';
const proto = Object.getPrototypeOf(value); // Someone overwrote their
// constructor property?
if (!proto.constructor)
return 'Object';
if (proto == null) // Non-named constructor function.
return 'Null'; if (!proto.constructor.name)
return 'Unknown';
const ctor = proto.constructor; return proto.constructor.name;
const name = ctor ? ctor.name : null; }
return name || 'Unknown'; function _getFunctionName(func) {
return func.name || 'Unknown';
} }
module.exports = assert; module.exports = assert;