Skip to content

Commit

Permalink
Let verifySignedMessage() handle exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
platfowner committed Apr 30, 2024
1 parent 6aac1e8 commit 13413c6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
14 changes: 10 additions & 4 deletions p2p/p2p-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,18 @@ class P2pUtil {
}
}

static verifySignedMessage(message, address) {
static verifySignedMessage(message, address, chainId) {
const LOG_HEADER = 'verifySignedMessage';
if (!P2pUtil._isValidMessage(message)) {
return null;
return false;
} else {
const chainId = DB.getBlockchainParam('genesis/chain_id');
return ainUtil.ecVerifySig(JSON.stringify(message.data.body), message.data.signature, address, chainId);
const cId = chainId !== undefined ? chainId : DB.getBlockchainParam('genesis/chain_id');
try {
return ainUtil.ecVerifySig(JSON.stringify(message.data.body), message.data.signature, address, cId);
} catch (err) {
logger.error(`[${LOG_HEADER}] The message is not correctly signed. Discard the message!!`);
return false;
}
}
}

Expand Down
30 changes: 22 additions & 8 deletions test/unit/p2p-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const expect = chai.expect;
const assert = chai.assert;
const { BlockchainConsts, BlockchainParams } = require('../../common/constants');

// NOTE(platfowner): Run this test with AirPlay Receiver off on MacOs to avoid port number (5000) conflicts (see https://developer.apple.com/forums/thread/682332).
describe("P2P Util", () => {
const mockAddress = '0x012345678abcdef';
let webServer;
Expand Down Expand Up @@ -240,7 +241,8 @@ describe("P2P Util", () => {
}
};
const signature = util.signMessage(body, mockPrivateKey);
it("returns null with wrong messages", () => {

it("returns false with wrong messages", () => {
const wrongMessage1 = {
data: {
signature: signature
Expand Down Expand Up @@ -281,13 +283,13 @@ describe("P2P Util", () => {
body: body
}
};
expect(util.verifySignedMessage(wrongMessage1)).to.equal(null);
expect(util.verifySignedMessage(wrongMessage2)).to.equal(null);
expect(util.verifySignedMessage(wrongMessage3)).to.equal(null);
expect(util.verifySignedMessage(wrongMessage4)).to.equal(null);
expect(util.verifySignedMessage(wrongMessage5)).to.equal(null);
expect(util.verifySignedMessage(wrongMessage6)).to.equal(null);
expect(util.verifySignedMessage(wrongMessage7)).to.equal(null);
expect(util.verifySignedMessage(wrongMessage1)).to.equal(false);
expect(util.verifySignedMessage(wrongMessage2)).to.equal(false);
expect(util.verifySignedMessage(wrongMessage3)).to.equal(false);
expect(util.verifySignedMessage(wrongMessage4)).to.equal(false);
expect(util.verifySignedMessage(wrongMessage5)).to.equal(false);
expect(util.verifySignedMessage(wrongMessage6)).to.equal(false);
expect(util.verifySignedMessage(wrongMessage7)).to.equal(false);
});

it("verifies signature correctly", () => {
Expand All @@ -301,6 +303,18 @@ describe("P2P Util", () => {
const address = util.getAddressFromMessage(mockMessage);
expect(util.verifySignedMessage(mockMessage, address)).to.equal(true);
});

it("returns false with wrong chainId", () => {
const mockMessage = {
type: 'test',
data: {
body: body,
signature: signature
}
};
const address = util.getAddressFromMessage(mockMessage);
expect(util.verifySignedMessage(mockMessage, address, 1)).to.equal(false); // with wrong chainId = 1
});
});

describe("toHostname", () => {
Expand Down

0 comments on commit 13413c6

Please sign in to comment.