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

Update recover method of NFT module to validate NFT attributes array #9000

Merged
merged 3 commits into from
Sep 20, 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
8 changes: 5 additions & 3 deletions framework/src/modules/nft/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 { BaseMethod } from '../base_method';
import { FeeMethod, InteroperabilityMethod, ModuleConfig, TokenMethod } from './types';
Expand Down Expand Up @@ -895,18 +896,19 @@ export class NFTMethod extends BaseMethod {
): Promise<void> {
const nftStore = this.stores.get(NFTStore);
const nftID = storeKey;
let isDecodable = true;
let isValidInput = true;
let decodedValue: NFTStoreData;
try {
decodedValue = codec.decode<NFTStoreData>(nftStoreSchema, storeValue);
validator.validate(nftStoreSchema, decodedValue);
} catch (error) {
isDecodable = false;
isValidInput = false;
}

if (
!substorePrefix.equals(nftStore.subStorePrefix) ||
storeKey.length !== LENGTH_NFT_ID ||
!isDecodable
!isValidInput
) {
this.events.get(RecoverEvent).error(
methodContext,
Expand Down
25 changes: 25 additions & 0 deletions framework/test/unit/modules/nft/method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,31 @@ describe('NFTMethod', () => {
);
});

it('should throw and emit error recover event if module name length in attributes array is not valid', async () => {
const newStoreValue = codec.encode(nftStoreSchema, {
owner: utils.getRandomBytes(LENGTH_CHAIN_ID),
attributesArray: [
{ module: 'customMod1', attributes: Buffer.alloc(5) },
{ module: '', attributes: Buffer.alloc(2) },
],
});

await expect(
method.recover(methodContext, terminatedChainID, substorePrefix, storeKey, newStoreValue),
).rejects.toThrow('Invalid inputs');
checkEventResult<RecoverEventData>(
methodContext.eventQueue,
1,
RecoverEvent,
0,
{
terminatedChainID,
nftID: storeKey,
},
NftEventResult.RESULT_RECOVER_FAIL_INVALID_INPUTS,
);
});

it('should throw and emit error recover event if nft chain id is not same as own chain id', async () => {
await expect(
method.recover(methodContext, terminatedChainID, substorePrefix, storeKey, storeValue),
Expand Down