diff --git a/src/cryptography/convert.js b/src/cryptography/convert.js index 7ed878e8a..adf009b4d 100644 --- a/src/cryptography/convert.js +++ b/src/cryptography/convert.js @@ -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 => diff --git a/src/cryptography/encrypt.js b/src/cryptography/encrypt.js index 22c6a371b..854a7d9dc 100644 --- a/src/cryptography/encrypt.js +++ b/src/cryptography/encrypt.js @@ -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.'); } diff --git a/src/transactions/utils/validation.js b/src/transactions/utils/validation.js index ac3badf77..bd761effd 100644 --- a/src/transactions/utils/validation.js +++ b/src/transactions/utils/validation.js @@ -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; }; diff --git a/test/cryptography/convert.js b/test/cryptography/convert.js index 9f6b63d17..bebe573a7 100644 --- a/test/cryptography/convert.js +++ b/test/cryptography/convert.js @@ -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.', + }); }); }); diff --git a/test/cryptography/encrypt.js b/test/cryptography/encrypt.js index 0977824ad..fc4cb414c 100644 --- a/test/cryptography/encrypt.js +++ b/test/cryptography/encrypt.js @@ -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', () => { diff --git a/test/transactions/3_castVotes.js b/test/transactions/3_castVotes.js index 79be5dcc7..24894a6d4 100644 --- a/test/transactions/3_castVotes.js +++ b/test/transactions/3_castVotes.js @@ -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.'); }); }); diff --git a/test/transactions/4_registerMultisignatureAccount.js b/test/transactions/4_registerMultisignatureAccount.js index 5777fe63c..3a3f633e0 100644 --- a/test/transactions/4_registerMultisignatureAccount.js +++ b/test/transactions/4_registerMultisignatureAccount.js @@ -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.'); }); }); diff --git a/test/transactions/utils/validation.js b/test/transactions/utils/validation.js index 242596bb9..af138caa8 100644 --- a/test/transactions/utils/validation.js +++ b/test/transactions/utils/validation.js @@ -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.'); }); }); @@ -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.'); }); }); @@ -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.'); }); });