Skip to content

Commit

Permalink
Test EIP-1559 transaction support
Browse files Browse the repository at this point in the history
Test case copied over from
MetaMask/eth-ledger-bridge-keyring#99
  • Loading branch information
aloisklink committed Oct 17, 2021
1 parent 8866801 commit cfed712
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions test/test-eth-trezor-keyring.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ const sinon = require('sinon');
const EthereumTx = require('ethereumjs-tx');
const HDKey = require('hdkey');
const TrezorConnect = require('trezor-connect').default;
const { TransactionFactory } = require('@ethereumjs/tx');
const Common = require('@ethereumjs/common').default;
const {
TransactionFactory,
FeeMarketEIP1559Transaction,
} = require('@ethereumjs/tx');
const { default: Common, Chain, Hardfork } = require('@ethereumjs/common');

const TrezorKeyring = require('..');

Expand Down Expand Up @@ -52,6 +55,10 @@ const fakeTx = new EthereumTx({
});

const common = new Common({ chain: 'mainnet' });
const commonEIP1559 = new Common({
chain: Chain.Mainnet,
hardfork: Hardfork.London,
});
const newFakeTx = TransactionFactory.fromTxData(
{
nonce: '0x00',
Expand All @@ -64,6 +71,21 @@ const newFakeTx = TransactionFactory.fromTxData(
{ common, freeze: false },
);

const fakeTypeTwoTx = FeeMarketEIP1559Transaction.fromTxData(
{
nonce: '0x00',
maxFeePerGas: '0x19184e72a000',
maxPriorityFeePerGas: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
type: 2,
v: '0x01',
},
{ common: commonEIP1559, freeze: false },
);

chai.use(spies);

describe('TrezorKeyring', function () {
Expand Down Expand Up @@ -393,6 +415,41 @@ describe('TrezorKeyring', function () {
assert.equal(returnedTx.common.chainIdBN().toString('hex'), '1');
assert(TrezorConnect.ethereumSignTransaction.calledOnce);
});

it('should pass correctly encoded EIP1559 transaction to trezor and return signed tx', async function () {
// Copied from @MetaMask/eth-ledger-bridge-keyring
// Generated by signing fakeTypeTwoTx with an unknown private key
const expectedRSV = {
v: '0x0',
r: '0x5ffb3adeaec80e430e7a7b02d95c5108b6f09a0bdf3cf69869dc1b38d0fb8d3a',
s: '0x28b234a5403d31564e18258df84c51a62683e3f54fa2b106fdc1a9058006a112',
};
// Override actual address of 0x391535104b6e0Ea6dDC2AD0158aB3Fbd7F04ed1B
sinon.stub(TransactionFactory, 'fromTxData').callsFake((...args) => {
const tx = TransactionFactory.fromTxData.wrappedMethod(...args);
sinon.stub(tx, 'getSenderAddress').returns(fakeAccounts[0]);
return tx;
});

sinon.stub(TrezorConnect, 'ethereumSignTransaction').callsFake(() => {
return Promise.resolve({
success: true,
payload: expectedRSV,
});
});

const returnedTx = await keyring.signTransaction(
fakeAccounts[0],
fakeTypeTwoTx,
commonEIP1559,
);

assert(TrezorConnect.ethereumSignTransaction.calledOnce);
expect(returnedTx.toJSON()).to.deep.equal({
...fakeTypeTwoTx.toJSON(),
...expectedRSV,
});
});
});

describe('signMessage', function () {
Expand Down

0 comments on commit cfed712

Please sign in to comment.