Skip to content

Commit

Permalink
Merge pull request #471 from GridPlus/dl/fix-valid
Browse files Browse the repository at this point in the history
  • Loading branch information
netbonus committed Sep 23, 2022
2 parents f649f77 + 2c173df commit dd57740
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gridplus-sdk",
"version": "2.2.8",
"version": "2.2.9",
"description": "SDK to interact with GridPlus Lattice1 device",
"scripts": {
"build": "NODE_ENV=production tsc -p tsconfig.json",
Expand Down
48 changes: 48 additions & 0 deletions src/__test__/unit/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
validateRemoveKvRequest,
validateSignRequest,
} from '../../functions';
import {
isValidBlockExplorerResponse,
isValid4ByteResponse,
} from '../../shared/validators';
import {
buildGetAddressesObject,
buildValidateConnectObject,
Expand Down Expand Up @@ -125,4 +129,48 @@ describe('validators', () => {
).toThrowError();
});
});

describe('abi data responses', () => {
describe('block explorers', () => {
test('should successfully validate etherscan data', () => {
const response: any = {
result:
'[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"stateMutability":"payable","type":"fallback"},{"inputs":[],"name":"implementation","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]',
};
expect(isValidBlockExplorerResponse(response)).toBe(true);
});

test('should validate as false bad data', () => {
const response: any = {
result:
'Max rate limit reached, please use API Key for higher rate limit',
};
expect(isValidBlockExplorerResponse(response)).toBe(false);
});
});

describe('4byte', () => {
test('should successfully validate etherscan data', () => {
const response: any = {
results: [
{
id: 447919,
created_at: '2021-12-25T13:54:33.120581Z',
text_signature: 'multicall(uint256,bytes[])',
hex_signature: '0x5ae401dc',
bytes_signature: 'test',
},
],
};
expect(isValid4ByteResponse(response)).toBe(true);
});

test('should validate as false bad data', () => {
const response: any = {
results: [],
};
expect(isValid4ByteResponse(response)).toBe(false);
});
});
});
});
18 changes: 14 additions & 4 deletions src/shared/validators.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UInt4 } from 'bitwise/types';
import { MAX_ADDR, encReqCodes, EMPTY_WALLET_UID, ASCII_REGEX } from '../constants';
import { isUInt4, checksum } from '../util';
import isEmpty from 'lodash/isEmpty'

export const validateValueExists = (arg: { [key: string]: any }) => {
const [key, [, value]] = Object.entries(arg);
Expand Down Expand Up @@ -206,9 +207,18 @@ export const validateRequestLength = (req: any, fwConstants: FirmwareConstants)
}

export const isValidBlockExplorerResponse = (data: any) => {
return data.result?.length > 0;
}
try {
const result = JSON.parse(data.result);
return !isEmpty(result);
} catch (err) {
return false;
}
};

export const isValid4ByteResponse = (data: any) => {
return data.results?.length > 0;
}
try {
return !isEmpty(data.results);
} catch (err) {
return false;
}
};

0 comments on commit dd57740

Please sign in to comment.