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

Commit

Permalink
Updates TokenInteroperableMethod.recover to validate storeValue
Browse files Browse the repository at this point in the history
  • Loading branch information
has5aan committed Sep 21, 2023
1 parent 22213a7 commit 54143c1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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
46 changes: 46 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,50 @@ 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: ''.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

0 comments on commit 54143c1

Please sign in to comment.