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

Fix contract deployments #112

Merged
merged 3 commits into from
Dec 8, 2021
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
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