Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes client dependency from primary functions #521

Merged
merged 2 commits into from
Jan 17, 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
17 changes: 2 additions & 15 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@
"no-console": ["off"],
"quotes": ["error", "single"],
"react/react-in-jsx-scope": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/member-delimiter-style": [
"warn",
{
"multiline": {
"delimiter": "semi",
"requireLast": true
},
"singleline": {
"delimiter": "comma",
"requireLast": false
}
}
]
}
"@typescript-eslint/no-explicit-any": "off"
}
}
11 changes: 11 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@ethereumjs/common": "2.4.0",
"@ethereumjs/tx": "3.3.0",
"@ethersproject/abi": "^5.5.0",
"@types/uuid": "^9.0.0",
"aes-js": "^3.1.1",
"bech32": "^2.0.0",
"bignumber.js": "^9.0.1",
Expand Down Expand Up @@ -107,4 +108,4 @@
"vitest": "^0.15.2"
},
"license": "MIT"
}
}
41 changes: 18 additions & 23 deletions src/__test__/unit/decoders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
} from './__mocks__/decoderData';

describe('decoders', () => {

test('connect', () => {
expect(
decodeConnectResponse(connectDecoderData, clientKeyPair),
Expand All @@ -30,52 +29,48 @@ describe('decoders', () => {

test('getAddresses', () => {
expect(
decodeGetAddressesResponse(
getAddressesDecoderData,
getAddressesFlag
),
decodeGetAddressesResponse(getAddressesDecoderData, getAddressesFlag),
).toMatchSnapshot();
});

test('sign - bitcoin', () => {
const params:DecodeSignResponseParams = {
const params: DecodeSignResponseParams = {
data: signBitcoinDecoderData,
request: signBitcoinRequest,
isGeneric: false,
currency: 'BTC',
}
expect(
decodeSignResponse(params),
).toMatchSnapshot();
};
expect(decodeSignResponse(params)).toMatchSnapshot();
});

test('sign - generic', () => {
const params:DecodeSignResponseParams = {
const params: DecodeSignResponseParams = {
data: signGenericDecoderData,
request: signGenericRequest,
isGeneric: true,
}
expect(
decodeSignResponse(params),
).toMatchSnapshot();
};
expect(decodeSignResponse(params)).toMatchSnapshot();
});

test('getKvRecords', () => {
expect(
decodeGetKvRecordsResponse(getKvRecordsDecoderData, decoderTestsFwConstants),
decodeGetKvRecordsResponse(
getKvRecordsDecoderData,
decoderTestsFwConstants,
),
).toMatchSnapshot();
});

test('fetchEncryptedData', () => {
// This test is different than the others because one part of the data is
// randomly generated (UUID) before the response is returned.
// randomly generated (UUID) before the response is returned.
// We will just zero it out for testing purposes.
const decoded = decodeFetchEncData(fetchEncryptedDataDecoderData, fetchEncryptedDataRequest);
const decodedDerp = JSON.parse(decoded.toString())
const decoded = decodeFetchEncData({
data: fetchEncryptedDataDecoderData,
...fetchEncryptedDataRequest,
});
const decodedDerp = JSON.parse(decoded.toString());
decodedDerp.uuid = '00000000-0000-0000-0000-000000000000';
expect(
Buffer.from(JSON.stringify(decodedDerp))
).toMatchSnapshot();
expect(Buffer.from(JSON.stringify(decodedDerp))).toMatchSnapshot();
});

});
43 changes: 20 additions & 23 deletions src/__test__/unit/encoders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
encodeRemoveKvRecordsRequest,
encodeSignRequest,
} from '../../functions';
import { buildTransaction } from '../../shared/functions'
import { buildTransaction } from '../../shared/functions';
import { getP256KeyPair } from '../../util';
import {
buildFirmwareConstants,
buildGetAddressesObject,
buildMockConnectedClient,
buildSignObject,
buildWallet,
getFwVersionsList,
} from '../utils/builders';

Expand All @@ -30,13 +32,11 @@ describe('encoders', () => {
test('pair encoder', () => {
const privKey = Buffer.alloc(32, '1');
expect(privKey.toString()).toMatchSnapshot();
const client = buildMockConnectedClient({
privKey,
name: 'testtest'
});
const key = getP256KeyPair(privKey);
const payload = encodePairRequest({
client,
pairingSecret: 'testtest'
key,
pairingSecret: 'testtest',
appName: 'testtest',
});
const payloadAsString = payload.toString('hex');
expect(payloadAsString).toMatchSnapshot();
Expand All @@ -45,8 +45,7 @@ describe('encoders', () => {

describe('getAddresses', () => {
test('encodeGetAddressesRequest with default flag', () => {
const mockObject = buildGetAddressesObject({});
const payload = encodeGetAddressesRequest(mockObject);
const payload = encodeGetAddressesRequest(buildGetAddressesObject());
const payloadAsString = payload.toString('hex');
expect(payloadAsString).toMatchSnapshot();
});
Expand Down Expand Up @@ -74,18 +73,14 @@ describe('encoders', () => {
test.each(getFwVersionsList())(
'should test sign encoder with firmware v%d.%d.%d',
(major, minor, patch) => {
const txObj = buildSignObject({
fwVersion: Buffer.from([patch, minor, major]),
});
const fwConstants = txObj.client.getFwConstants();
const tx = buildTransaction({
...txObj,
fwConstants,
});
const fwVersion = Buffer.from([patch, minor, major]);
const txObj = buildSignObject(fwVersion);
const tx = buildTransaction(txObj);
const req = {
...txObj,
request: tx.request,
}
...tx,
wallet: buildWallet(),
};
const { payload } = encodeSignRequest(req);
const payloadAsString = payload.toString('hex');
expect(payloadAsString).toMatchSnapshot();
Expand All @@ -102,23 +97,25 @@ describe('encoders', () => {
});

test('addKvRecords', () => {
const fwConstants = buildFirmwareConstants();
const mockObject = {
client: buildMockConnectedClient(),
type: 0,
records: { key: 'value' },
caseSensitive: false,
fwConstants,
};
const payload = encodeAddKvRecordsRequest(mockObject);
const payloadAsString = payload.toString('hex');
expect(payloadAsString).toMatchSnapshot();
});

test('removeKvRecords', () => {
const fwConstants = buildFirmwareConstants();
const mockObject = {
client: buildMockConnectedClient(),
type: 0,
ids: [0],
ids: ['0'],
douglance marked this conversation as resolved.
Show resolved Hide resolved
caseSensitive: false,
fwConstants,
};
const payload = encodeRemoveKvRecordsRequest(mockObject);
const payloadAsString = payload.toString('hex');
Expand Down
40 changes: 2 additions & 38 deletions src/__test__/unit/validators.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import {
validateAddKvRequest,
validateConnectRequest,
validateFetchActiveWallet,
validateGetAddressesRequest,
validateGetKvRequest,
validateRemoveKvRequest,
validateSignRequest,
} from '../../functions';
import {
isValidBlockExplorerResponse,
isValid4ByteResponse,
} from '../../shared/validators';
import {
buildGetAddressesObject,
buildSignObject,
buildValidateConnectObject,
buildValidateRequestObject,
} from '../utils/builders';
Expand Down Expand Up @@ -45,13 +42,13 @@ describe('validators', () => {
const startPath = [0x80000000 + 44, 0x80000000 + 60, 0, 0, 0, 0, 0];
const fwVersion = Buffer.from([0, 0, 0]);
const testEncodingFunction = () =>
encodeGetAddressesRequest(
validateGetAddressesRequest(
buildGetAddressesObject({ startPath, fwVersion }),
);
expect(testEncodingFunction).toThrowError();
});
});

describe('KvRecords', () => {
describe('addKvRecords', () => {
test('should successfully validate', () => {
Expand Down Expand Up @@ -99,39 +96,6 @@ describe('validators', () => {
});
});

describe('fetchActiveWallet', () => {
alex-miller-0 marked this conversation as resolved.
Show resolved Hide resolved
test('should successfully validate', () => {
const validateFetchActiveWalletBundle: any = buildValidateRequestObject(
{},
);
validateFetchActiveWallet(validateFetchActiveWalletBundle);
});

test('should throw errors on validation failure', () => {
// Will fail because no `client` instance is included
const validateFetchActiveWalletBundle: any = { url: '' };
expect(() =>
validateFetchActiveWallet(validateFetchActiveWalletBundle)
).toThrowError();
});
});

describe('sign', () => {
test('should successfully validate', () => {
const validateSignRequestBundle: any = buildSignObject({});
validateSignRequest(validateSignRequestBundle);
});

test('should throw errors on validation failure', () => {
// No tx data included
const validateSignRequestBundle: any = buildSignObject({});
validateSignRequestBundle.data = undefined;
expect(() =>
validateSignRequest(validateSignRequestBundle),
).toThrowError();
});
});

describe('abi data responses', () => {
describe('block explorers', () => {
test('should successfully validate etherscan data', () => {
Expand Down
Loading