diff --git a/test/test-eth-trezor-keyring.js b/test/test-eth-trezor-keyring.js index d6b9aa0..79b1c7a 100644 --- a/test/test-eth-trezor-keyring.js +++ b/test/test-eth-trezor-keyring.js @@ -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('..'); @@ -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', @@ -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 () { @@ -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 () {