Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
Refactor hex checks
Browse files Browse the repository at this point in the history
Closes #209
  • Loading branch information
willclarktech committed Jul 25, 2017
1 parent 60228f5 commit 3bbfa38
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 72 deletions.
10 changes: 10 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ var cryptoLib = require('crypto-browserify');
var should = require('should');
var sinon = require('sinon');

should.use(function (should, Assertion) {
Assertion.add('hexString', function () {
this.params = {
operator: 'to be hex string',
};
(Buffer.from(this.obj, 'hex').toString('hex'))
.should.equal(this.obj);
});
});

// See https://github.com/shouldjs/should.js/issues/41
Object.defineProperty(global, 'should', { value: should });
global.sinon = sinon;
Expand Down
10 changes: 2 additions & 8 deletions test/transactions/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,8 @@ describe('crypto.js', function () {

(keys).should.be.ok();
(keys).should.be.type('object');
(keys).should.have.property('publicKey').and.be.type('string');
(keys).should.have.property('privateKey').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(keys.publicKey, 'hex');
});
should.doesNotThrow(function () {
Buffer.from(keys.privateKey, 'hex');
});
(keys).should.have.property('publicKey').and.be.type('string').and.be.hexString();
(keys).should.have.property('privateKey').and.be.type('string').and.be.hexString();
});
});

Expand Down
15 changes: 3 additions & 12 deletions test/transactions/dapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ describe('dapp.js', function () {
});

it('should have senderPublicKey as hex string', function () {
(trs).should.have.property('senderPublicKey').and.be.type('string');
return should.doesNotThrow(function () {
Buffer.from(trs.senderPublicKey, 'hex');
});
(trs).should.have.property('senderPublicKey').and.be.type('string').and.be.hexString();
});

it('should have timestamp as number', function () {
Expand Down Expand Up @@ -159,17 +156,11 @@ describe('dapp.js', function () {
});

it('should have signature as hex string', function () {
(trs).should.have.property('signature').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(trs.signature, 'hex');
});
(trs).should.have.property('signature').and.be.type('string').and.be.hexString();
});

it('should have second signature in hex', function () {
(trs).should.have.property('signSignature').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(trs.signSignature, 'hex');
});
(trs).should.have.property('signSignature').and.be.type('string').and.be.hexString();
});

it('should be signed correctly', function () {
Expand Down
15 changes: 3 additions & 12 deletions test/transactions/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,15 @@ describe('delegate.js', function () {
});

it('should have senderPublicKey in hex', function () {
(trs).should.have.property('senderPublicKey').and.type('string').and.equal(keys.publicKey);
should.doesNotThrow(function () {
Buffer.from(trs.senderPublicKey, 'hex');
});
(trs).should.have.property('senderPublicKey').and.type('string').and.equal(keys.publicKey).and.be.hexString();
});

it('should have signature in hex', function () {
(trs).should.have.property('signature').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(trs.signature, 'hex');
});
(trs).should.have.property('signature').and.be.type('string').and.be.hexString();
});

it('should have second signature in hex', function () {
(trs).should.have.property('signSignature').and.type('string');
should.doesNotThrow(function () {
Buffer.from(trs.signSignature, 'hex');
});
(trs).should.have.property('signSignature').and.type('string').and.be.hexString();
});

it('should have delegate asset', function () {
Expand Down
5 changes: 1 addition & 4 deletions test/transactions/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ describe('signature.js', function () {
});

it('should have publicKey in hex', function () {
(sgn.asset.signature).should.have.property('publicKey').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(sgn.asset.signature.publicKey, 'hex');
});
(sgn.asset.signature).should.have.property('publicKey').and.be.type('string').and.be.hexString();
});

it('should have publicKey in 32 bytes', function () {
Expand Down
25 changes: 5 additions & 20 deletions test/transactions/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ describe('transaction.js', function () {
});

it('should have senderPublicKey as hex string', function () {
(trs).should.have.property('senderPublicKey').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(trs.senderPublicKey, 'hex');
});
(trs).should.have.property('senderPublicKey').and.be.type('string').and.be.hexString();
});

it('should have recipientId as string and to be equal 58191285901858109L', function () {
Expand All @@ -104,10 +101,7 @@ describe('transaction.js', function () {
});

it('should have signature as hex string', function () {
(trs).should.have.property('signature').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(trs.signature, 'hex');
});
(trs).should.have.property('signature').and.be.type('string').and.be.hexString();
});

it('should be signed correctly', function () {
Expand Down Expand Up @@ -160,10 +154,7 @@ describe('transaction.js', function () {
});

it('should have senderPublicKey as hex string', function () {
(trs).should.have.property('senderPublicKey').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(trs.senderPublicKey, 'hex');
});
(trs).should.have.property('senderPublicKey').and.be.type('string').and.be.hexString();
});

it('should have recipientId as string and to be equal 58191285901858109L', function () {
Expand All @@ -183,17 +174,11 @@ describe('transaction.js', function () {
});

it('should have signature as hex string', function () {
(trs).should.have.property('signature').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(trs.signature, 'hex');
});
(trs).should.have.property('signature').and.be.type('string').and.be.hexString();
});

it('should have signSignature as hex string', function () {
(trs).should.have.property('signSignature').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(trs.signSignature, 'hex');
});
(trs).should.have.property('signSignature').and.be.type('string').and.be.hexString();
});

it('should be signed correctly', function () {
Expand Down
21 changes: 5 additions & 16 deletions test/transactions/vote.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,15 @@ describe('vote.js', function () {
});

it('should have senderPublicKey hex string equal to sender public key', function () {
(vt).should.have.property('senderPublicKey').and.be.type('string').and.equal(publicKey);
should.doesNotThrow(function () {
Buffer.from(vt.senderPublicKey, 'hex');
});
(vt).should.have.property('senderPublicKey').and.be.type('string').and.equal(publicKey).and.be.hexString();
});

it('should have signature hex string', function () {
(vt).should.have.property('signature').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(vt.signature, 'hex');
});
(vt).should.have.property('signature').and.be.type('string').and.be.hexString();
});

it('should have second signature hex string', function () {
(vt).should.have.property('signSignature').and.be.type('string');
should.doesNotThrow(function () {
Buffer.from(vt.signSignature, 'hex');
});
(vt).should.have.property('signSignature').and.be.type('string').and.be.hexString();
});

it('should be signed correctly', function () {
Expand Down Expand Up @@ -167,10 +158,8 @@ describe('vote.js', function () {

it('should have public keys in hex', function () {
vt.asset.votes.forEach(function (v) {
(v).should.be.type('string').startWith('+');
should.doesNotThrow(function () {
Buffer.from(v.substring(1, v.length), 'hex');
});
(v).should.be.type('string').and.startWith('+');
(v.slice(1)).should.be.hexString();
});
});

Expand Down

0 comments on commit 3bbfa38

Please sign in to comment.