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

Add validateMessage option to signMessage #148

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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