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

Commit 3259e27

Browse files
authored
Fix contract deployments (#112)
1 parent 8b6ed88 commit 3259e27

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,11 @@ class TrezorKeyring extends EventEmitter {
295295
} else {
296296
// new-style transaction from @ethereumjs/tx package
297297
// we can just copy tx.toJSON() for everything except chainId, which must be a number
298-
transaction = { ...tx.toJSON(), chainId };
298+
transaction = {
299+
...tx.toJSON(),
300+
chainId,
301+
to: this._normalize(tx.to),
302+
};
299303
}
300304

301305
try {

test/test-eth-trezor-keyring.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ const newFakeTx = TransactionFactory.fromTxData(
7171
{ common, freeze: false },
7272
);
7373

74+
const contractDeploymentFakeTx = TransactionFactory.fromTxData(
75+
{
76+
nonce: '0x00',
77+
gasPrice: '0x09184e72a000',
78+
gasLimit: '0x2710',
79+
value: '0x00',
80+
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
81+
},
82+
{ common, freeze: false },
83+
);
84+
7485
const fakeTypeTwoTx = FeeMarketEIP1559Transaction.fromTxData(
7586
{
7687
nonce: '0x00',
@@ -416,6 +427,50 @@ describe('TrezorKeyring', function () {
416427
assert(TrezorConnect.ethereumSignTransaction.calledOnce);
417428
});
418429

430+
it('should pass serialized contract deployment transaction to trezor and return signed tx', async function () {
431+
sinon.stub(TransactionFactory, 'fromTxData').callsFake(() => {
432+
// without having a private key/public key pair in this test, we have
433+
// mock out this method and return the original tx because we can't
434+
// replicate r and s values without the private key.
435+
return contractDeploymentFakeTx;
436+
});
437+
438+
sinon.stub(TrezorConnect, 'ethereumSignTransaction').callsFake(() => {
439+
return Promise.resolve({
440+
success: true,
441+
payload: { v: '0x25', r: '0x0', s: '0x0' },
442+
});
443+
});
444+
445+
sinon
446+
.stub(contractDeploymentFakeTx, 'getSenderAddress')
447+
.callsFake(() => fakeAccounts[0]);
448+
449+
sinon
450+
.stub(contractDeploymentFakeTx, 'verifySignature')
451+
.callsFake(() => true);
452+
453+
const returnedTx = await keyring.signTransaction(
454+
fakeAccounts[0],
455+
contractDeploymentFakeTx,
456+
);
457+
// ensure we get a new version transaction back
458+
assert.equal(returnedTx.getChainId, undefined);
459+
assert.equal(returnedTx.common.chainIdBN().toString('hex'), '1');
460+
assert(TrezorConnect.ethereumSignTransaction.calledOnce);
461+
assert.deepEqual(
462+
TrezorConnect.ethereumSignTransaction.getCall(0).args[0],
463+
{
464+
path: `m/44'/60'/0'/0/0`,
465+
transaction: {
466+
...contractDeploymentFakeTx.toJSON(),
467+
to: '0x',
468+
chainId: 1,
469+
},
470+
},
471+
);
472+
});
473+
419474
it('should pass correctly encoded EIP1559 transaction to trezor and return signed tx', async function () {
420475
// Copied from @MetaMask/eth-ledger-bridge-keyring
421476
// Generated by signing fakeTypeTwoTx with an unknown private key

0 commit comments

Comments
 (0)