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

Commit

Permalink
🌱 Update hexToBuffer behavior to throw error when invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Feb 28, 2018
1 parent 17d5d93 commit 6a41d85
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 33 deletions.
10 changes: 6 additions & 4 deletions src/cryptography/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ export const hexToBuffer = hex => {
throw new TypeError('Argument must be a string.');
}
const matchedHex = (hex.match(hexRegex) || [])[0];
if (!matchedHex) {
return Buffer.alloc(0);
if (!matchedHex || matchedHex.length !== hex.length) {
throw new TypeError('Argument must be a valid hex string.');
}
const evenLength = Math.floor(matchedHex.length / 2) * 2;
return Buffer.from(matchedHex.slice(0, evenLength), 'hex');
if (matchedHex.length % 2 !== 0) {
throw new TypeError('Argument must have a valid length of hex string.');
}
return Buffer.from(matchedHex, 'hex');
};

export const getFirstEightBytesReversed = publicKeyBytes =>
Expand Down
3 changes: 0 additions & 3 deletions src/cryptography/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ const encryptAES256GCMWithPassword = (plainText, password) => {

const getTagBuffer = tag => {
const tagBuffer = hexToBuffer(tag);
if (bufferToHex(tagBuffer) !== tag) {
throw new Error('Tag must be a hex string.');
}
if (tagBuffer.length !== 16) {
throw new Error('Tag must be 16 bytes.');
}
Expand Down
6 changes: 1 addition & 5 deletions src/transactions/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@
*
*/
import bignum from 'browserify-bignum';
import { bufferToHex, hexToBuffer } from 'cryptography/convert';
import { hexToBuffer } from 'cryptography/convert';
import { MAX_ADDRESS_NUMBER } from 'constants';

export const validatePublicKey = publicKey => {
const publicKeyBuffer = hexToBuffer(publicKey);
if (bufferToHex(publicKeyBuffer) !== publicKey) {
throw new Error('Public key must be a valid hex string.');
}
if (publicKeyBuffer.length !== 32) {
throw new Error(
`Public key ${publicKey} length differs from the expected 32 bytes for a public key.`,
);
}

return true;
};

Expand Down
35 changes: 20 additions & 15 deletions test/cryptography/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,34 @@ describe('convert', () => {
.should.throw(TypeError, { message: 'Argument must be a string.' });
});

it('should create empty Buffer from non hex string', () => {
const buffer = hexToBuffer('yKJh');
return buffer.should.be.eql(Buffer.alloc(0));
it('should throw TypeError with non hex string', () => {
return hexToBuffer.bind(null, 'yKJj').should.throw(TypeError, {
message: 'Argument must be a valid hex string.',
});
});

it('should create partial Buffer from partially non hex string', () => {
const buffer = hexToBuffer('Abxzzzz');
return buffer.should.be.eql(Buffer.from('Ab', 'hex'));
it('should throw TypeError with partially correct hex string', () => {
return hexToBuffer.bind(null, 'Abxzzzz').should.throw(TypeError, {
message: 'Argument must be a valid hex string.',
});
});

it('should create partial Buffer with only first valid hex string', () => {
const buffer = hexToBuffer('Abxzzab');
return buffer.should.be.eql(Buffer.from('Ab', 'hex'));
it('should throw TypeError with odd number of string with partially correct hex string', () => {
return hexToBuffer.bind(null, 'Abxzzab').should.throw(TypeError, {
message: 'Argument must be a valid hex string.',
});
});

it('should create even numbered Buffer from odd number hex string with invalid hex', () => {
const buffer = hexToBuffer('123xxxx');
return buffer.should.be.eql(Buffer.from('12', 'hex'));
it('should throw TypeError with odd number hex string with invalid hex', () => {
return hexToBuffer.bind(null, '123xxxx').should.throw(TypeError, {
message: 'Argument must be a valid hex string.',
});
});

it('should create even numbered Buffer from odd number hex string', () => {
const buffer = hexToBuffer('c3a5c3a4c3b6a');
return buffer.should.be.eql(Buffer.from('c3a5c3a4c3b6', 'hex'));
it('should throw TypeError with odd number of hex string', () => {
return hexToBuffer.bind(null, 'c3a5c3a4c3b6a').should.throw(TypeError, {
message: 'Argument must have a valid length of hex string.',
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/cryptography/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ describe('encrypt', () => {
)}gg`;
return decryptPassphraseWithPassword
.bind(null, cipherIvSaltTagAndVersion, defaultPassword)
.should.throw('Tag must be a hex string.');
.should.throw('Argument must be a valid hex string.');
});

it('should inform the user if the tag has been altered', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/transactions/3_castVotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe('#castVotes transaction', () => {
unvotes: unvotePublicKeys,
votes: [plusPrependedPublicKey],
})
.should.throw('Public key must be a valid hex string.');
.should.throw('Argument must be a valid hex string.');
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/transactions/4_registerMultisignatureAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ describe('#registerMultisignatureAccount transaction', () => {
lifetime,
minimum,
})
.should.throw('Public key must be a valid hex string.');
.should.throw('Argument must be a valid hex string.');
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/transactions/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('public key validation', () => {
it('should throw an error', () => {
return validatePublicKey
.bind(null, invalidHexPublicKey)
.should.throw('Public key must be a valid hex string.');
.should.throw('Argument must have a valid length of hex string.');
});
});

Expand All @@ -39,7 +39,7 @@ describe('public key validation', () => {
it('should throw an error', () => {
return validatePublicKey
.bind(null, invalidHexPublicKey)
.should.throw('Public key must be a valid hex string.');
.should.throw('Argument must be a valid hex string.');
});
});

Expand Down Expand Up @@ -95,7 +95,7 @@ describe('public key validation', () => {
it('should throw an error', () => {
return validatePublicKeys
.bind(null, publicKeys)
.should.throw('Public key must be a valid hex string.');
.should.throw('Argument must have a valid length of hex string.');
});
});

Expand Down

0 comments on commit 6a41d85

Please sign in to comment.