Skip to content

Commit

Permalink
Merge pull request #531 from GridPlus/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
netbonus committed Mar 8, 2023
2 parents ba75842 + 1c1ba07 commit c68322c
Show file tree
Hide file tree
Showing 9 changed files with 816 additions and 309 deletions.
1,049 changes: 784 additions & 265 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gridplus-sdk",
"version": "2.5.0",
"version": "2.5.1",
"description": "SDK to interact with GridPlus Lattice1 device",
"scripts": {
"build": "NODE_ENV=production tsc -p tsconfig.json",
Expand Down Expand Up @@ -43,8 +43,8 @@
"url": "https://github.com/GridPlus/gridplus-sdk.git"
},
"dependencies": {
"@ethereumjs/common": "2.4.0",
"@ethereumjs/tx": "3.3.0",
"@ethereumjs/common": "3.1.1",
"@ethereumjs/tx": "4.1.1",
"@ethersproject/abi": "^5.5.0",
"@types/uuid": "^9.0.0",
"aes-js": "^3.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/__test__/e2e/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getClient } from './../../api/utilities';
import Common, { Chain, Hardfork } from '@ethereumjs/common';
import { Chain, Common, Hardfork } from '@ethereumjs/common';
import { TransactionFactory } from '@ethereumjs/tx';
import { question } from 'readline-sync';
import { encode } from 'rlp';
Expand Down
2 changes: 1 addition & 1 deletion src/__test__/e2e/non-exportable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* make sure you set `CONFIG:DEBUG>:ENABLE_A90=0` or else you will probably brick
* your A90 chip.
*/
import Common, { Chain, Hardfork } from '@ethereumjs/common';
import { Chain, Common, Hardfork } from '@ethereumjs/common';
import { TransactionFactory as EthTxFactory } from '@ethereumjs/tx';
import { question } from 'readline-sync';
import { ecdsaRecover } from 'secp256k1';
Expand Down
37 changes: 16 additions & 21 deletions src/__test__/e2e/signing/evm-tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Test various EVM transaction types.
You must have `FEATURE_TEST_RUNNER=1` enabled in firmware to run these tests.
*/
import Common, { Chain, Hardfork } from '@ethereumjs/common';
import { Chain, Common, Hardfork } from '@ethereumjs/common';
import { TransactionFactory as EthTxFactory } from '@ethereumjs/tx';
import { BN } from 'bn.js';
import { encode as rlpEncode } from 'rlp';
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('[EVM TX]', () => {
CURRENT_SEED,
bypassSetPayload,
shouldFail,
useLegacySigning
useLegacySigning,
).catch((err) => {
if (err.responseCode === 128) {
err.message =
Expand All @@ -45,9 +45,9 @@ describe('[EVM TX]', () => {
});
};

it('Should get the current wallet seed', async () => {
it('Should get the current wallet seed', async () => {
CURRENT_SEED = await initializeSeed(client);
})
});

describe('[EVM] Test transactions', () => {
describe('EIP1559', () => {
Expand Down Expand Up @@ -190,9 +190,8 @@ describe('[EVM TX]', () => {
),
).rejects.toThrow();
});

it('Should test other chains', async () => {

// Polygon
await runBoundaryTest({ common: Common.custom({ chainId: 137 }) });
// BSC
Expand All @@ -210,7 +209,7 @@ describe('[EVM TX]', () => {
// @ts-expect-error - Common.custom() expects a number
common: Common.custom({ chainId: '18446744073709551615' }),
});

// Unknown chain - bypass set payload
await expect(
runBoundaryTest(
Expand All @@ -220,7 +219,7 @@ describe('[EVM TX]', () => {
),
).rejects.toThrow();
});

it('Should test range of `value`', async () => {
// Should display as 1e-18
await runBoundaryTest({ txData: { value: 1 } });
Expand Down Expand Up @@ -265,13 +264,13 @@ describe('[EVM TX]', () => {
txData: { data: `0x${randomBytes(maxDataSz + 1).toString('hex')}` },
});
});

it('Should test contract deployment', async () => {
await runBoundaryTest({
txData: { to: null, data: `0x${randomBytes(96).toString('hex')}` },
});
});

it('Should test direct RLP-encoded payloads with bad params', async () => {
const req = buildEvmReq();
const tx = EthTxFactory.fromTxData(req.txData, {
Expand All @@ -290,22 +289,22 @@ describe('[EVM TX]', () => {
// ---
// Nonce
await expect(
runBoundaryTest(getParamPayloadReq(0), true, true)
runBoundaryTest(getParamPayloadReq(0), true, true),
).rejects.toThrow();

// Gas
await expect(
runBoundaryTest(getParamPayloadReq(1), true, true)
runBoundaryTest(getParamPayloadReq(1), true, true),
).rejects.toThrow();

// Gas Price
await expect(
runBoundaryTest(getParamPayloadReq(2), true, true)
runBoundaryTest(getParamPayloadReq(2), true, true),
).rejects.toThrow();

// Value
await expect(
runBoundaryTest(getParamPayloadReq(4), true, true)
runBoundaryTest(getParamPayloadReq(4), true, true),
).rejects.toThrow();

// Test wrong sized addresses
Expand All @@ -325,18 +324,15 @@ describe('[EVM TX]', () => {
runBoundaryTest(
getParamPayloadReq(
3,
Buffer.from(
'e242e54155b1abc71fc118065270cecaaf8b770102',
'hex',
),
Buffer.from('e242e54155b1abc71fc118065270cecaaf8b770102', 'hex'),
),
true,
true,
),
).rejects.toThrow();
});
});

describe('Random Transactions', () => {
for (let i = 0; i < getNumIter(); i++) {
it(`Should test random transactions: #${i}`, async () => {
Expand All @@ -357,7 +353,7 @@ describe('[EVM TX]', () => {
type: undefined,
},
common: Common.custom({
chainId: parseInt(randIntStr(4)),
chainId: parseInt(randIntStr(4)) + 1, // Ensure chainID >0
}),
});
});
Expand Down Expand Up @@ -432,4 +428,3 @@ describe('[EVM TX]', () => {
});
});
});

2 changes: 1 addition & 1 deletion src/__test__/utils/builders.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Common, { Chain, Hardfork } from '@ethereumjs/common';
import { Chain, Common, Hardfork } from '@ethereumjs/common';
import {
TransactionFactory as EthTxFactory,
TypedTransaction,
Expand Down
22 changes: 7 additions & 15 deletions src/__test__/utils/runners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Constants } from '../..';
import { TransactionFactory as EthTxFactory } from '@ethereumjs/tx';
import { encode as rlpEncode } from 'rlp';
import { getDeviceId } from './getters';
import { ensureHexBuffer } from '../../util';


export async function runTestCase (
Expand Down Expand Up @@ -98,29 +99,20 @@ export async function runEvm (
true,
'Signature failed to verify',
);
const refR = Buffer.from(signedTx.r?.toBuffer());
const refS = Buffer.from(signedTx.s?.toBuffer());
const refV = signedTx.v;
const refR = ensureHexBuffer(signedTx.r?.toString(16));
const refS = ensureHexBuffer(signedTx.s?.toString(16));
const refV = signedTx.v?.toString();
// Get params from Lattice sig
const latticeR = Buffer.from(sig.r);
const latticeS = Buffer.from(sig.s);
const latticeV = new BN(sig.v);
// Strip off leading zeros to do an exact componenet check.
// We will still validate the original lattice sig in a tx.
const rToCheck =
latticeR.length !== refR.length
? latticeR.slice(latticeR.length - refR.length)
: latticeR;
const sToCheck =
latticeS.length !== refS.length
? latticeS.slice(latticeS.length - refS.length)
: latticeS;

// Validate the signature
expect(rToCheck.equals(refR)).toEqualElseLog(
expect(latticeR.equals(refR)).toEqualElseLog(
true,
'Signature R component does not match reference',
);
expect(sToCheck.equals(refS)).toEqualElseLog(
expect(latticeS.equals(refS)).toEqualElseLog(
true,
'Signature S component does not match reference',
);
Expand Down
3 changes: 2 additions & 1 deletion src/__test__/utils/testRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export const testRequest = async ({
data.writeUInt16BE(payload.length, 4);
payload.copy(data, 6);

return await encryptedSecureRequest({
const { decryptedData } = await encryptedSecureRequest({
data,
requestType: LatticeSecureEncryptedRequestType.test,
sharedSecret,
ephemeralPub,
url,
});
return decryptedData;
};
2 changes: 1 addition & 1 deletion src/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Utils for Ethereum transactions. This is effecitvely a shim of ethereumjs-util, which
// does not have browser (or, by proxy, React-Native) support.
import Common, { Chain, Hardfork } from '@ethereumjs/common';
import { Chain, Common, Hardfork } from '@ethereumjs/common';
import { TransactionFactory } from '@ethereumjs/tx';
import BN from 'bignumber.js';
import cbor from 'borc';
Expand Down

0 comments on commit c68322c

Please sign in to comment.