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

Commit

Permalink
Add validateMessage option to signMessage (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat authored Sep 12, 2023
1 parent a5d8b8b commit c71c969
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/simple-keyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
});

Expand Down
8 changes: 7 additions & 1 deletion src/simple-keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ export default class SimpleKeyring implements Keyring<string[]> {
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);
Expand Down

0 comments on commit c71c969

Please sign in to comment.