Skip to content

feat: whitelisted message templates per builder #6669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ export class Eip191MessageBuilder extends BaseMessageBuilder {
async buildMessage(options: MessageOptions): Promise<IMessage> {
return new EIP191Message(options);
}

protected getWhitelistedMessageTemplates(): Record<string, string> {
// EIP-191 does not have whitelisted message templates
// This means all messages are allowed
return {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export abstract class BaseMessageBuilder implements IMessageBuilder {
protected type: MessageStandardType;
protected signatures: Signature[] = [];
protected signers: string[] = [];
protected whitelistedMessageTemplates: Record<string, string> = {};
protected metadata?: Record<string, unknown> = {};
protected digest?: string;

Expand All @@ -26,6 +27,7 @@ export abstract class BaseMessageBuilder implements IMessageBuilder {
) {
this.coinConfig = coinConfig;
this.type = messageType;
this.whitelistedMessageTemplates = this.getWhitelistedMessageTemplates();
}

/**
Expand Down Expand Up @@ -167,4 +169,24 @@ export abstract class BaseMessageBuilder implements IMessageBuilder {
};
return this.build();
}

public isMessageWhitelisted(messageRaw: string): boolean {
if (!this.whitelistedMessageTemplates || Object.keys(this.whitelistedMessageTemplates).length === 0) {
return true;
}
// Check if the message matches any of the whitelisted templates
return Object.values(this.whitelistedMessageTemplates).some((template) => {
const regex = new RegExp(`^${template}$`, 's'); // 's' flag to match newlines
return regex.test(messageRaw);
});
}

protected getWhitelistedMessageTemplates(): Record<string, string> {
const midnightTNCHash = '31a6bab50a84b8439adcfb786bb2020f6807e6e8fda629b424110fc7bb1c6b8b';
const midnightGlacierDropClaimMessageTemplate = `STAR \\d+ to addr(?:1|_test1)[a-z0-9]{50,} ${midnightTNCHash}`;
return {
midnightGDClaimMsgTemplate: midnightGlacierDropClaimMessageTemplate,
// Add more whitelisted templates as needed
};
}
}
9 changes: 9 additions & 0 deletions modules/sdk-core/src/account-lib/baseCoin/messages/iface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ export interface IMessageBuilder {
* @returns The parsed message and signature
*/
fromBroadcastFormat(message: BroadcastableMessage): Promise<IMessage>;

/**
* Checks if the message string is whitelisted.
* Some message standards like EIP-191 allow any message,
*
* @param messageRaw The raw message string to check
* @return True if the message is whitelisted, false otherwise
*/
isMessageWhitelisted(messageRaw: string): boolean;
}

/**
Expand Down
Loading