Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
Fix contract deployments (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
danjm authored Dec 8, 2021
1 parent 8b6ed88 commit 3259e27
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,11 @@ class TrezorKeyring extends EventEmitter {
} else {
// new-style transaction from @ethereumjs/tx package
// we can just copy tx.toJSON() for everything except chainId, which must be a number
transaction = { ...tx.toJSON(), chainId };
transaction = {
...tx.toJSON(),
chainId,
to: this._normalize(tx.to),
};
}

try {
Expand Down
55 changes: 55 additions & 0 deletions test/test-eth-trezor-keyring.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ const newFakeTx = TransactionFactory.fromTxData(
{ common, freeze: false },
);

const contractDeploymentFakeTx = TransactionFactory.fromTxData(
{
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
},
{ common, freeze: false },
);

const fakeTypeTwoTx = FeeMarketEIP1559Transaction.fromTxData(
{
nonce: '0x00',
Expand Down Expand Up @@ -416,6 +427,50 @@ describe('TrezorKeyring', function () {
assert(TrezorConnect.ethereumSignTransaction.calledOnce);
});

it('should pass serialized contract deployment transaction to trezor and return signed tx', async function () {
sinon.stub(TransactionFactory, 'fromTxData').callsFake(() => {
// without having a private key/public key pair in this test, we have
// mock out this method and return the original tx because we can't
// replicate r and s values without the private key.
return contractDeploymentFakeTx;
});

sinon.stub(TrezorConnect, 'ethereumSignTransaction').callsFake(() => {
return Promise.resolve({
success: true,
payload: { v: '0x25', r: '0x0', s: '0x0' },
});
});

sinon
.stub(contractDeploymentFakeTx, 'getSenderAddress')
.callsFake(() => fakeAccounts[0]);

sinon
.stub(contractDeploymentFakeTx, 'verifySignature')
.callsFake(() => true);

const returnedTx = await keyring.signTransaction(
fakeAccounts[0],
contractDeploymentFakeTx,
);
// ensure we get a new version transaction back
assert.equal(returnedTx.getChainId, undefined);
assert.equal(returnedTx.common.chainIdBN().toString('hex'), '1');
assert(TrezorConnect.ethereumSignTransaction.calledOnce);
assert.deepEqual(
TrezorConnect.ethereumSignTransaction.getCall(0).args[0],
{
path: `m/44'/60'/0'/0/0`,
transaction: {
...contractDeploymentFakeTx.toJSON(),
to: '0x',
chainId: 1,
},
},
);
});

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
Expand Down

0 comments on commit 3259e27

Please sign in to comment.