From ae86571758bfb9c529b7ed77f84ea28a2934667b Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Fri, 1 Sep 2023 21:23:24 +0000 Subject: [PATCH] Add validateMessage option to signMessage --- src/simple-keyring.test.ts | 2 +- src/simple-keyring.ts | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/simple-keyring.test.ts b/src/simple-keyring.test.ts index 900448b..8ac0261 100644 --- a/src/simple-keyring.test.ts +++ b/src/simple-keyring.test.ts @@ -192,7 +192,7 @@ describe('simple-keyring', function () { it('throw error for invalid message', async function () { await keyring.deserialize([privateKey]); await expect(keyring.signMessage(address, '')).rejects.toThrow( - 'Cannot convert 0x to a BigInt', + 'Cannot sign invalid message', ); }); diff --git a/src/simple-keyring.ts b/src/simple-keyring.ts index b535495..26a6284 100644 --- a/src/simple-keyring.ts +++ b/src/simple-keyring.ts @@ -94,9 +94,15 @@ export default class SimpleKeyring implements Keyring { async signMessage( address: Hex, data: string, - opts = { withAppKeyOrigin: '' }, + opts = { withAppKeyOrigin: '', validateMessage: true }, ) { const message = stripHexPrefix(data); + if ( + opts.validateMessage && + (message.length === 0 || !message.match(/^[a-fA-F0-9]*$/u)) + ) { + throw new Error('Cannot sign invalid message'); + } const privKey = this.#getPrivateKeyFor(address, opts); const msgSig = ecsign(Buffer.from(message, 'hex'), privKey); const rawMsgSig = concatSig(toBuffer(msgSig.v), msgSig.r, msgSig.s);