Skip to content

Commit 493b723

Browse files
committed
Revert "feat: add isMessageWhitelisted() in MessageBuilder"
This reverts commit b5dbd70. TICKET: SC-2419
1 parent b5dbd70 commit 493b723

File tree

7 files changed

+50
-42
lines changed

7 files changed

+50
-42
lines changed

modules/account-lib/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ export { Vet };
206206
import * as CosmosSharedCoin from '@bitgo/sdk-coin-cosmos';
207207
export { CosmosSharedCoin };
208208

209-
import { isMessageWhitelisted, MIDNIGHT_TNC_HASH, MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE } from './utils';
210-
export { isMessageWhitelisted, MIDNIGHT_TNC_HASH, MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE };
209+
import { validateAgainstMessageTemplates, MIDNIGHT_TNC_HASH } from './utils';
210+
export { MIDNIGHT_TNC_HASH };
211211

212212
const coinBuilderMap = {
213213
trx: Trx.WrappedBuilder,
@@ -435,7 +435,7 @@ export async function verifyMessage(
435435
if (!isValidMessageEncoded) {
436436
return false;
437437
}
438-
return messageBuilder.isMessageWhitelisted(messageRaw);
438+
return validateAgainstMessageTemplates(messageRaw);
439439
} catch (e) {
440440
console.error(`Error verifying message for coin ${coinName}:`, e);
441441
return false;

modules/account-lib/src/utils/messages/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ export const MIDNIGHT_TNC_HASH = '31a6bab50a84b8439adcfb786bb2020f6807e6e8fda629
55
* then " to addr" or " to addr_test1", followed by a 50+ character alphanumeric address,
66
* and ends with the midnight TnC hash
77
*/
8-
export const MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE = `STAR \\d+ to addr(?:1|_test1)[a-z0-9]{50,} ${MIDNIGHT_TNC_HASH}`;
8+
const MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE = `STAR \\d+ to addr(?:1|_test1)[a-z0-9]{50,} ${MIDNIGHT_TNC_HASH}`;
9+
10+
/**
11+
* @file Utility functions for validating messages against whitelisted templates.
12+
* This is used to ensure that only specific message formats are accepted.
13+
*/
14+
const whitelistedMessageTemplates = [MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE];
915

1016
/**
1117
* Validates a message against a set of whitelisted templates.
1218
* The templates can contain placeholders like {{variable}} which will be replaced with a wildcard in the regex.
1319
*
14-
* @param whitelistedMessageTemplates - An array of whitelisted message templates.
1520
* @param {string} messageRaw - The raw message to validate.
1621
* @returns {boolean} - Returns true if the message matches any of the whitelisted templates, false otherwise.
1722
*/
18-
export function isMessageWhitelisted(whitelistedMessageTemplates: string[], messageRaw: string): boolean {
19-
if (!whitelistedMessageTemplates || whitelistedMessageTemplates.length === 0) {
20-
return true;
21-
}
23+
export function validateAgainstMessageTemplates(messageRaw: string): boolean {
2224
return whitelistedMessageTemplates.some((template) => {
2325
const regex = new RegExp(`^${template}$`, 's'); // 's' flag to match newlines
2426
return regex.test(messageRaw);

modules/account-lib/test/unit/utils/messages/index.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
import should from 'should';
2-
import {
3-
MIDNIGHT_TNC_HASH,
4-
isMessageWhitelisted,
5-
MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE,
6-
} from '../../../../src/utils';
2+
import { MIDNIGHT_TNC_HASH, validateAgainstMessageTemplates } from '../../../../src/utils';
73

84
describe('Message validation', () => {
9-
describe('isMessageWhitelisted', () => {
10-
const whitelistedMessageTemplates = [MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE];
11-
5+
describe('validateAgainstMessageTemplates', () => {
126
const adaTestnetDestinationAddress = 'addr_test1vz7xs7ceu4xx9n5xn57lfe86vrwddqpp77vjwq5ptlkh49cqy3wur';
137
const adaMainnetDestinationAddress =
148
'addr1q9k6u7lhf467y2f8skr2dafldx2npsd8fymq0mslnj0t44nd4ealwnt4ug5j0pvx5m6n76v4xrq6wjfkqlhpl8y7httq2m9cmu';
159

1610
it('should validate testnet message matching the Midnight glacier drop claim template', () => {
1711
const messageRaw = `STAR 100 to ${adaTestnetDestinationAddress} ${MIDNIGHT_TNC_HASH}`;
1812

19-
const result = isMessageWhitelisted(whitelistedMessageTemplates, messageRaw);
13+
const result = validateAgainstMessageTemplates(messageRaw);
2014

2115
should.equal(result, true);
2216
});
2317

2418
it('should validate mainnet message matching the Midnight glacier drop claim template', () => {
2519
const messageRaw = `STAR 100 to ${adaMainnetDestinationAddress} ${MIDNIGHT_TNC_HASH}`;
2620

27-
const result = isMessageWhitelisted(whitelistedMessageTemplates, messageRaw);
21+
const result = validateAgainstMessageTemplates(messageRaw);
2822

2923
should.equal(result, true);
3024
});
3125

3226
it('should not validate message with incorrect format', () => {
3327
const messageRaw = `INCORRECT 100 to ${adaTestnetDestinationAddress} ${MIDNIGHT_TNC_HASH}`;
3428

35-
const result = isMessageWhitelisted(whitelistedMessageTemplates, messageRaw);
29+
const result = validateAgainstMessageTemplates(messageRaw);
3630

3731
should.equal(result, false);
3832
});
@@ -41,7 +35,7 @@ describe('Message validation', () => {
4135
// Missing "to addr" part
4236
const messageRaw = `STAR 100 ${MIDNIGHT_TNC_HASH}`;
4337

44-
const result = isMessageWhitelisted(whitelistedMessageTemplates, messageRaw);
38+
const result = validateAgainstMessageTemplates(messageRaw);
4539

4640
should.equal(result, false);
4741
});
@@ -52,21 +46,21 @@ describe('Message validation', () => {
5246
'5af1adf825baa496729e2eac1e895ebc77973744bce67f44276bf6006f5c21de863ed121e11828d8fc0241773191e26dc1134803a681a9a98ba0ae812553db24';
5347
const messageRaw = `STAR 100 to ${adaTestnetDestinationAddress} ${incorrectHash}`;
5448

55-
const result = isMessageWhitelisted(whitelistedMessageTemplates, messageRaw);
49+
const result = validateAgainstMessageTemplates(messageRaw);
5650

5751
should.equal(result, false);
5852
});
5953

6054
it('should handle empty message', () => {
61-
const result = isMessageWhitelisted(whitelistedMessageTemplates, '');
55+
const result = validateAgainstMessageTemplates('');
6256

6357
should.equal(result, false);
6458
});
6559

6660
it('should not validate message with special regex characters', () => {
6761
const messageRaw = `STAR shade.with+special*chars to addr.with[special]chars ${MIDNIGHT_TNC_HASH}`;
6862

69-
const result = isMessageWhitelisted(whitelistedMessageTemplates, messageRaw);
63+
const result = validateAgainstMessageTemplates(messageRaw);
7064

7165
should.equal(result, false);
7266
});
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import { Cip8Message } from './cip8Message';
22
import { BaseCoin as CoinConfig } from '@bitgo/statics';
33
import { BaseMessageBuilder, IMessage, MessageOptions, MessageStandardType } from '@bitgo/sdk-core';
4-
import { MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE } from '@bitgo/account-lib';
54

65
/**
76
* Builder for CIP-8 messages
87
*/
98
export class Cip8MessageBuilder extends BaseMessageBuilder {
9+
private static readonly MIDNIGHT_TNC_HASH = '31a6bab50a84b8439adcfb786bb2020f6807e6e8fda629b424110fc7bb1c6b8b';
10+
11+
/*
12+
* matches a message that starts with "STAR ", followed by a number,
13+
* then " to addr" or " to addr_test1", followed by a 50+ character alphanumeric address,
14+
* and ends with the midnight TnC hash
15+
*/
16+
private static readonly MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE = `STAR \\d+ to addr(?:1|_test1)[a-z0-9]{50,} ${Cip8MessageBuilder.MIDNIGHT_TNC_HASH}`;
1017
/**
1118
* Base constructor.
1219
* @param _coinConfig BaseCoin from statics library
1320
*/
1421
public constructor(_coinConfig: Readonly<CoinConfig>) {
1522
super(_coinConfig, MessageStandardType.CIP8);
16-
this.whitelistedMessageTemplates = [
17-
MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE,
18-
// Add more templates as needed
19-
];
2023
}
2124

2225
/**
@@ -27,4 +30,15 @@ export class Cip8MessageBuilder extends BaseMessageBuilder {
2730
async buildMessage(options: MessageOptions): Promise<IMessage> {
2831
return new Cip8Message(options);
2932
}
33+
34+
/**
35+
* Validates the signable payload
36+
* @param message The message to validate
37+
* @returns A boolean indicating whether the signable payload is valid
38+
*/
39+
public validateSignablePayload(message: string | Buffer): boolean {
40+
const messageStr = Buffer.isBuffer(message) ? message.toString('utf8') : message;
41+
const regex = new RegExp(`^${Cip8MessageBuilder.MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE}$`, 's');
42+
return regex.test(messageStr);
43+
}
3044
}

modules/sdk-core/src/account-lib/baseCoin/messages/baseMessageBuilder.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { BroadcastableMessage, MessageOptions, MessagePayload, MessageStandardTy
22
import { BaseCoin as CoinConfig } from '@bitgo/statics';
33
import { IMessage, IMessageBuilder } from './iface';
44
import { deserializeSignatures, Signature } from '../iface';
5-
import { isMessageWhitelisted } from '@bitgo/account-lib';
65

76
/**
87
* Base Message Builder
@@ -13,7 +12,6 @@ export abstract class BaseMessageBuilder implements IMessageBuilder {
1312
protected type: MessageStandardType;
1413
protected signatures: Signature[] = [];
1514
protected signers: string[] = [];
16-
protected whitelistedMessageTemplates: string[] = [];
1715
protected metadata?: Record<string, unknown> = {};
1816
protected digest?: string;
1917

@@ -120,8 +118,13 @@ export abstract class BaseMessageBuilder implements IMessageBuilder {
120118
return this;
121119
}
122120

123-
public isMessageWhitelisted(messageRaw: string): boolean {
124-
return isMessageWhitelisted(this.whitelistedMessageTemplates, messageRaw);
121+
/**
122+
* Validates the signable payload
123+
* @param message The message to validate
124+
* @returns A boolean indicating whether the signable payload is valid
125+
*/
126+
public validateSignablePayload(message: string | Buffer): boolean {
127+
return true;
125128
}
126129

127130
/**

modules/sdk-core/src/account-lib/baseCoin/messages/iface.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ export interface IMessageBuilder {
137137
getPayload(): MessagePayload | undefined;
138138

139139
/**
140-
* Checks if the message string is whitelisted
141-
* @param messageRaw The raw message string to check
142-
* @return True if the message is whitelisted, false otherwise
140+
* Validates the signable payload
141+
* @param message The message to validate
142+
* @returns A boolean indicating whether the signable payload is valid
143143
*/
144-
isMessageWhitelisted(messageRaw: string): boolean;
144+
validateSignablePayload(message: string | Buffer): boolean;
145145

146146
/**
147147
* Gets the current metadata

modules/sdk-core/src/account-lib/baseCoin/messages/simple/simpleMessageBuilder.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { BaseCoin as CoinConfig } from '@bitgo/statics';
33
import { BaseMessageBuilder } from '../baseMessageBuilder';
44
import { MessageOptions, MessageStandardType } from '../../../../bitgo';
55
import { IMessage } from '../iface';
6-
import { MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE } from '@bitgo/account-lib';
76

87
/**
98
* Builder for string messages
@@ -15,10 +14,6 @@ export class SimpleMessageBuilder extends BaseMessageBuilder {
1514
*/
1615
public constructor(_coinConfig: Readonly<CoinConfig>) {
1716
super(_coinConfig, MessageStandardType.SIMPLE);
18-
this.whitelistedMessageTemplates = [
19-
MIDNIGHT_GLACIER_DROP_CLAIM_MESSAGE_TEMPLATE,
20-
// Add more templates as needed
21-
];
2217
}
2318

2419
/**

0 commit comments

Comments
 (0)