Skip to content

Commit

Permalink
implement eip-1559 support for Trezors
Browse files Browse the repository at this point in the history
  • Loading branch information
mholtzman committed Aug 25, 2021
1 parent b25c1bd commit c2844f1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
1 change: 1 addition & 0 deletions main/signers/hot/HotSigner/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class HotSignerWorker {
signTransaction (key, rawTx, pseudoCallback) {
sign(rawTx, tx => {
const signedTx = tx.sign(key)

const serialized = signedTx.serialize().toString('hex')

pseudoCallback(null, addHexPrefix(serialized))
Expand Down
40 changes: 26 additions & 14 deletions main/signers/trezor-connect/Trezor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,27 +208,39 @@ class Trezor extends Signer {
})
}

_normalizeTransaction (rawTx) {
return {
nonce: this.normalize(rawTx.nonce),
gasPrice: this.normalize(rawTx.gasPrice),
gasLimit: this.normalize(rawTx.gasLimit),
to: this.normalize(rawTx.to),
value: this.normalize(rawTx.value),
data: this.normalize(rawTx.data),
chainId: utils.hexToNumber(rawTx.chainId)
_normalizeTransaction (chainId, tx) {
const txJson = tx.toJSON()

const unsignedTx = {
nonce: this.normalize(txJson.nonce),
gasLimit: this.normalize(txJson.gasLimit),
to: this.normalize(txJson.to),
value: this.normalize(txJson.value),
data: this.normalize(txJson.data),
chainId: utils.hexToNumber(chainId)
}

const optionalFields = ['gasPrice', 'maxFeePerGas', 'maxPriorityFeePerGas']

optionalFields.forEach(field => {
if (txJson[field]) {
unsignedTx[field] = this.normalize(txJson[field])
}
})

return unsignedTx
}

signTransaction (index, rawTx, cb) {
// as of 08-05-2021 Trezor doesn't support EIP-1559 transactions
const legacyTx = londonToLegacy(rawTx)
const compatibility = signerCompatibility(rawTx, this.summary())
const compatibleTx = compatibility.compatible ? { ...rawTx } : londonToLegacy(rawTx)

const trezorTx = this._normalizeTransaction(legacyTx)
const path = this.getPath(index)

sign(legacyTx, () => {
sign(compatibleTx, () => {
return new Promise((resolve, reject) => {
const trezorTx = this._normalizeTransaction(tx)
const path = this.getPath(index)

flex.rpc('trezor.ethereumSignTransaction', this.device.path, path, trezorTx, (err, result) => {
return err
? reject(err)
Expand Down
3 changes: 2 additions & 1 deletion main/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const londonHardforkSigners: SignerCompatibilityByVersion = {
seed: () => true,
ring: () => true,
ledger: version => version.major >= 2 || (version.major >= 1 && version.minor >= 9),
trezor: version => version.major >= 3 || (version.major >= 2 && version.minor >= 4 && version.patch >= 2),
lattice: version => version.major >= 1 || version.minor >= 11
}

Expand Down Expand Up @@ -87,7 +88,7 @@ function usesBaseFee (rawTx: RawTransaction) {

function populate (rawTx: RawTransaction, chainConfig: Common, gas: any): TransactionData {
const txData: TransactionData = { ...rawTx }

if (chainConfig.isActivatedEIP(1559)) {
txData.type = '0x2'

Expand Down
30 changes: 28 additions & 2 deletions test/main/transaction/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ describe('#signerCompatibility', () => {
expect(compatibility.compatible).toBe(true)
})

it('is not compatible for eip-1559 transactions on Trezor signers', () => {
const appVersion = { major: 1, minor: 1, patch: 1 }
it('is not compatible for eip-1559 transactions on Trezor signers using firmware prior to 2.4.2', () => {
const appVersion = { major: 2, minor: 3, patch: 1 }
const tx = {
type: '0x2'
}
Expand All @@ -130,6 +130,32 @@ describe('#signerCompatibility', () => {
expect(compatibility.tx).toBe('london')
expect(compatibility.compatible).toBe(false)
})

it('is compatible for eip-1559 transactions on Trezor signers using firmware 2.4.2+', () => {
const appVersion = { major: 2, minor: 4, patch: 3 }
const tx = {
type: '0x2'
}

const compatibility = signerCompatibility(tx, { type: 'trezor', appVersion })

expect(compatibility.signer).toBe('trezor')
expect(compatibility.tx).toBe('london')
expect(compatibility.compatible).toBe(true)
})

it('is compatible for eip-1559 transactions on Trezor signers using firmware 3.x', () => {
const appVersion = { major: 3, minor: 2, patch: 4 }
const tx = {
type: '0x2'
}

const compatibility = signerCompatibility(tx, { type: 'trezor', appVersion })

expect(compatibility.signer).toBe('trezor')
expect(compatibility.tx).toBe('london')
expect(compatibility.compatible).toBe(true)
})
})

describe('#londonToLegacy', () => {
Expand Down

0 comments on commit c2844f1

Please sign in to comment.