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

TokenInteroperableMethod.recover validates storeValue #9029

Merged
merged 3 commits into from
Sep 25, 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: 2 additions & 0 deletions framework/src/modules/token/cc_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Removal or modification of this copyright notice is prohibited.
*/

import { validator } from '@liskhq/lisk-validator';
import { codec } from '@liskhq/lisk-codec';
import { BaseCCMethod } from '../interoperability/base_cc_method';
import {
Expand Down Expand Up @@ -200,6 +201,7 @@ export class TokenInteroperableMethod extends BaseCCMethod {

try {
account = codec.decode<UserStoreData>(userStoreSchema, ctx.storeValue);
validator.validate(userStoreSchema, account);
} catch (error) {
this.events
.get(RecoverEvent)
Expand Down
48 changes: 48 additions & 0 deletions framework/test/unit/modules/token/cc_method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
CCM_STATUS_OK,
CHAIN_ID_LENGTH,
CROSS_CHAIN_COMMAND_NAME_TRANSFER,
MIN_MODULE_NAME_LENGTH,
MAX_MODULE_NAME_LENGTH,
TokenEventResult,
} from '../../../../src/modules/token/constants';
import { TokenInteroperableMethod } from '../../../../src/modules/token/cc_method';
Expand Down Expand Up @@ -700,6 +702,52 @@ describe('TokenInteroperableMethod', () => {
);
});

it('should reject if module name length in lockedBalances is not valid', async () => {
await expect(
tokenInteropMethod.recover({
...createRecoverContext(stateStore),
storeKey: Buffer.concat([defaultAddress, defaultTokenID]),
substorePrefix: userStore.subStorePrefix,
storeValue: codec.encode(userStoreSchema, {
availableBalance: defaultAccount.availableBalance,
lockedBalances: [
{ module: 'token'.repeat(MIN_MODULE_NAME_LENGTH - 1), amount: BigInt(10) },
],
}),
terminatedChainID: sendingChainID,
}),
).rejects.toThrow('Invalid arguments.');

checkEventResult(
methodContext.eventQueue,
RecoverEvent,
TokenEventResult.RECOVER_FAIL_INVALID_INPUTS,
);

await expect(
tokenInteropMethod.recover({
...createRecoverContext(stateStore),
storeKey: Buffer.concat([defaultAddress, defaultTokenID]),
substorePrefix: userStore.subStorePrefix,
storeValue: codec.encode(userStoreSchema, {
availableBalance: defaultAccount.availableBalance,
lockedBalances: [
{ module: '1'.repeat(MAX_MODULE_NAME_LENGTH + 1), amount: BigInt(10) },
],
}),
terminatedChainID: sendingChainID,
}),
).rejects.toThrow('Invalid arguments.');

checkEventResult(
methodContext.eventQueue,
RecoverEvent,
TokenEventResult.RECOVER_FAIL_INVALID_INPUTS,
2,
1,
);
});

it('should reject if token is not native', async () => {
jest
.spyOn(tokenInteropMethod['_interopMethod'], 'getMessageFeeTokenIDFromCCM')
Expand Down