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

Commit

Permalink
Merge pull request #122 from LiskHQ/121_error-thorw-return
Browse files Browse the repository at this point in the history
verifyMessageWithPublicKey should rather “throw” errors then “return”…
  • Loading branch information
karmacoma committed May 10, 2017
2 parents bebb4f5 + 674c8a2 commit 4af32fe
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
6 changes: 4 additions & 2 deletions dist/lisk-js.js
Expand Up @@ -2040,7 +2040,9 @@ function verifyMessageWithPublicKey (signedMessage, publicKey) {
var signedMessageBytes = convert.hexToBuffer(signedMessage);
var publicKeyBytes = convert.hexToBuffer(publicKey);

if (publicKeyBytes.length !== 32) return { message: 'Invalid publicKey, expected 32-byte publicKey' };
if (publicKeyBytes.length !== 32) {
throw new Error('Invalid publicKey, expected 32-byte publicKey');
}

// Give appropriate error messages from crypto_sign_open
var openSignature = naclInstance.crypto_sign_open(signedMessageBytes, publicKeyBytes);
Expand All @@ -2049,7 +2051,7 @@ function verifyMessageWithPublicKey (signedMessage, publicKey) {
// Returns original message
return naclInstance.decode_utf8(openSignature);
} else {
return { message: 'Invalid signature publicKey combination, cannot verify message' };
throw new Error('Invalid signature publicKey combination, cannot verify message');
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/lisk-js.min.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions lib/transactions/crypto/sign.js
Expand Up @@ -65,7 +65,9 @@ function verifyMessageWithPublicKey (signedMessage, publicKey) {
var signedMessageBytes = convert.hexToBuffer(signedMessage);
var publicKeyBytes = convert.hexToBuffer(publicKey);

if (publicKeyBytes.length !== 32) return { message: 'Invalid publicKey, expected 32-byte publicKey' };
if (publicKeyBytes.length !== 32) {
throw new Error('Invalid publicKey, expected 32-byte publicKey');
}

// Give appropriate error messages from crypto_sign_open
var openSignature = naclInstance.crypto_sign_open(signedMessageBytes, publicKeyBytes);
Expand All @@ -74,7 +76,7 @@ function verifyMessageWithPublicKey (signedMessage, publicKey) {
// Returns original message
return naclInstance.decode_utf8(openSignature);
} else {
return { message: 'Invalid signature publicKey combination, cannot verify message' };
throw new Error('Invalid signature publicKey combination, cannot verify message');
}
}

Expand Down
12 changes: 6 additions & 6 deletions test/transactions/crypto/index.js
Expand Up @@ -141,16 +141,16 @@ describe('crypto/index.js', function () {

it('should detect invalid publicKeys', function () {
var invalidPublicKey = keypair.publicKey + 'ERROR';
var invalidVerifyMessage = newcrypto.verifyMessageWithPublicKey(signedMessage, invalidPublicKey);

(invalidVerifyMessage.message).should.be.equal('Invalid publicKey, expected 32-byte publicKey');
expect(function () {
newcrypto.verifyMessageWithPublicKey(signedMessage, invalidPublicKey);
}).to.throw(Error, 'Invalid publicKey, expected 32-byte publicKey');
});

it('should detect not verifiable signature', function () {
var signedMessage = newcrypto.signMessageWithSecret(message, secret) + 'ERROR';
var invalidVerifyMessage = newcrypto.verifyMessageWithPublicKey(signedMessage, publicKey);

(invalidVerifyMessage.message).should.be.equal('Invalid signature publicKey combination, cannot verify message');
expect(function () {
newcrypto.verifyMessageWithPublicKey(signedMessage, publicKey);
}).to.throw(Error, 'Invalid signature publicKey combination, cannot verify message');
});
});

Expand Down

0 comments on commit 4af32fe

Please sign in to comment.